From c8b772af00b03eb1dd9425663fd4b7f60c9693bb Mon Sep 17 00:00:00 2001 From: marc <> Date: Sun, 3 Nov 2002 23:58:39 +0000 Subject: back out previous patch.. there are still some vax/m68k issues --- src/lib/libc/include/thread_private.h | 70 ++++++++++++++++++++--------------- src/lib/libc/stdlib/abort.c | 10 ++++- src/lib/libc/stdlib/malloc.c | 57 +++++++++++++++++++++------- 3 files changed, 93 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/lib/libc/include/thread_private.h b/src/lib/libc/include/thread_private.h index 976e330cac..89faade556 100644 --- a/src/lib/libc/include/thread_private.h +++ b/src/lib/libc/include/thread_private.h @@ -1,4 +1,4 @@ -/* $OpenBSD: thread_private.h,v 1.12 2002/11/03 20:36:43 marc Exp $ */ +/* $OpenBSD: thread_private.h,v 1.13 2002/11/03 23:58:39 marc Exp $ */ #ifndef _THREAD_PRIVATE_H_ #define _THREAD_PRIVATE_H_ @@ -18,7 +18,19 @@ extern int __isthreaded; * Use WEAK_NAME(n) to get a libc-private name for n (_weak_n), * WEAK_ALIAS(n) to generate the weak symbol n pointing to _weak_n, * WEAK_PROTOTYPE(n) to generate a prototype for _weak_n (based on n). + * + * If the symbol _NO_WEAK_ALIASES is defined, then symbols will be */ + +#ifdef _NO_WEAK_ALIASES +#ifdef _THREAD_SAFE +#define WEAK_NAME(name) __CONCAT(_weak,name) +#else +#define WEAK_NAME(name) name +#endif +#define WEAK_ALIAS(name) /* unavailable */ +#define WEAK_PROTOTYPE(name) /* unnecessary */ +#else /* !_NO_WEAK_ALIASES */ #define WEAK_NAME(name) __CONCAT(_weak_,name) #define WEAK_ALIAS(name) __weak_alias(name, WEAK_NAME(name)) #ifdef __GNUC__ @@ -26,6 +38,7 @@ extern int __isthreaded; #else #define WEAK_PROTOTYPE(name) /* typeof() only in gcc */ #endif +#endif /* !_NO_WEAK_ALIASES */ /* * These macros help in making persistent storage thread-specific. @@ -49,7 +62,7 @@ struct _thread_private_key_struct { void _libc_private_storage_lock(pthread_mutex_t *); void _libc_private_storage_unlock(pthread_mutex_t *); void * _libc_private_storage(volatile struct _thread_private_key_struct *, - void *, size_t, void *); + void *, size_t, void *); /* Declare a module mutex. */ #define _THREAD_PRIVATE_MUTEX(name) \ @@ -84,7 +97,18 @@ void * _libc_private_storage(volatile struct _thread_private_key_struct *, */ #define _THREAD_PRIVATE(keyname, storage, error) \ _libc_private_storage(&__THREAD_KEY_NAME(keyname), \ - &(storage), sizeof (storage), error) + &(storage), sizeof (storage), error) + +/* + * Macros for locking and unlocking FILEs. These test if the + * process is threaded to avoid locking when not required. + */ +#ifdef _FLOCK_DEBUG +#define FLOCKFILE(fp) _flockfile_debug(fp, __FILE__, __LINE__) +#else +#define FLOCKFILE(fp) flockfile(fp) +#endif +#define FUNLOCKFILE(fp) funlockfile(fp) /* * File descriptor locking definitions. @@ -93,33 +117,19 @@ void * _libc_private_storage(volatile struct _thread_private_key_struct *, #define FD_WRITE 0x2 #define FD_RDWR (FD_READ | FD_WRITE) -#define _FD_LOCK(_fd,_type,_ts) \ - _thread_fd_lock(_fd, _type, _ts, __FILE__, __LINE__) -#define _FD_UNLOCK(_fd,_type) \ - _thread_fd_unlock(_fd, _type, __FILE__, __LINE__) - -int _thread_fd_lock(int, int, struct timespec *, const char *, int); -void _thread_fd_unlock(int, int, const char *, int); +#ifdef _LOCK_DEBUG +#define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock_debug(_fd, _type, \ + _ts, __FILE__, __LINE__) +#define _FD_UNLOCK(_fd,_type) _thread_fd_unlock_debug(_fd, _type, \ + __FILE__, __LINE__) +#else +#define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock(_fd, _type, _ts) +#define _FD_UNLOCK(_fd,_type) _thread_fd_unlock(_fd, _type) +#endif -/* - * malloc lock/unlock definitions - */ -# define _MALLOC_LOCK() do { \ - if (__isthreaded) \ - _thread_malloc_lock(); \ - } while (0) -# define _MALLOC_UNLOCK() do { \ - if (__isthreaded) \ - _thread_malloc_unlock();\ - } while (0) -# define _MALLOC_LOCK_INIT()do { \ - if (__isthreaded) \ - _thread_malloc_init();\ - } while (0) - - -void _thread_malloc_init(void); -void _thread_malloc_lock(void); -void _thread_malloc_unlock(void); +int _thread_fd_lock(int, int, struct timespec *); +int _thread_fd_lock_debug(int, int, struct timespec *, char *, int); +void _thread_fd_unlock(int, int); +void _thread_fd_unlock_debug(int, int, char *, int); #endif /* _THREAD_PRIVATE_H_ */ diff --git a/src/lib/libc/stdlib/abort.c b/src/lib/libc/stdlib/abort.c index 6fffe3df7c..7252cd8a8b 100644 --- a/src/lib/libc/stdlib/abort.c +++ b/src/lib/libc/stdlib/abort.c @@ -32,7 +32,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: abort.c,v 1.9 2002/11/03 20:36:43 marc Exp $"; +static char *rcsid = "$OpenBSD: abort.c,v 1.10 2002/11/03 23:58:39 marc Exp $"; #endif /* LIBC_SCCS and not lint */ #include @@ -55,7 +55,11 @@ abort() * any errors -- X311J doesn't allow abort to return anyway. */ sigdelset(&mask, SIGABRT); +#ifdef _THREAD_SAFE (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); +#else /* _THREAD_SAFE */ + (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); +#endif /* _THREAD_SAFE */ /* * POSIX requires we flush stdio buffers on abort @@ -76,7 +80,11 @@ abort() * it again, only harder. */ (void)signal(SIGABRT, SIG_DFL); +#ifdef _THREAD_SAFE (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); +#else /* _THREAD_SAFE */ + (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); +#endif /* _THREAD_SAFE */ (void)kill(getpid(), SIGABRT); exit(1); } diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index 3aff1bfb9c..2ebc1eff05 100644 --- a/src/lib/libc/stdlib/malloc.c +++ b/src/lib/libc/stdlib/malloc.c @@ -8,7 +8,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: malloc.c,v 1.49 2002/11/03 20:36:43 marc Exp $"; +static char rcsid[] = "$OpenBSD: malloc.c,v 1.50 2002/11/03 23:58:39 marc Exp $"; #endif /* LIBC_SCCS and not lint */ /* @@ -48,8 +48,6 @@ static char rcsid[] = "$OpenBSD: malloc.c,v 1.49 2002/11/03 20:36:43 marc Exp $" #include #include -#include "thread_private.h" - /* * The basic parameters you can tweak. * @@ -69,6 +67,39 @@ static char rcsid[] = "$OpenBSD: malloc.c,v 1.49 2002/11/03 20:36:43 marc Exp $" # define malloc_pageshift 13U #endif /* __OpenBSD__ */ +#ifdef _THREAD_SAFE +# include "thread_private.h" +# if 0 + /* kernel threads */ +# include + static pthread_mutex_t malloc_lock; +# define THREAD_LOCK() pthread_mutex_lock(&malloc_lock) +# define THREAD_UNLOCK() pthread_mutex_unlock(&malloc_lock) +# define THREAD_LOCK_INIT() pthread_mutex_init(&malloc_lock, 0); +# else + /* user threads */ +# include "spinlock.h" + static spinlock_t malloc_lock = _SPINLOCK_INITIALIZER; +# define THREAD_LOCK() if (__isthreaded) _SPINLOCK(&malloc_lock) +# define THREAD_UNLOCK() if (__isthreaded) _SPINUNLOCK(&malloc_lock) +# define THREAD_LOCK_INIT() + /* + * Malloc can't use the wrapped write() if it fails very early, so + * we use the unwrapped syscall _thread_sys_write() + */ +# define write _thread_sys_write + ssize_t write(int, const void *, size_t); +# undef malloc +# undef realloc +# undef free +# endif +#else + /* no threads */ +# define THREAD_LOCK() +# define THREAD_UNLOCK() +# define THREAD_LOCK_INIT() +#endif + /* * No user serviceable parts behind this point. * @@ -463,7 +494,7 @@ malloc_init () int i, j; int save_errno = errno; - _MALLOC_LOCK_INIT(); + THREAD_LOCK_INIT(); INIT_MMAP(); @@ -1213,17 +1244,17 @@ malloc(size_t size) register void *r; malloc_func = " in malloc():"; - _MALLOC_LOCK(); + THREAD_LOCK(); if (malloc_active++) { wrtwarning("recursive call.\n"); malloc_active--; - _MALLOC_UNLOCK(); + THREAD_UNLOCK(); return (0); } r = imalloc(size); UTRACE(0, size, r); malloc_active--; - _MALLOC_UNLOCK(); + THREAD_UNLOCK(); if (malloc_xmalloc && !r) wrterror("out of memory.\n"); return (r); @@ -1233,17 +1264,17 @@ void free(void *ptr) { malloc_func = " in free():"; - _MALLOC_LOCK(); + THREAD_LOCK(); if (malloc_active++) { wrtwarning("recursive call.\n"); malloc_active--; - _MALLOC_UNLOCK(); + THREAD_UNLOCK(); return; } ifree(ptr); UTRACE(ptr, 0, 0); malloc_active--; - _MALLOC_UNLOCK(); + THREAD_UNLOCK(); return; } @@ -1253,11 +1284,11 @@ realloc(void *ptr, size_t size) register void *r; malloc_func = " in realloc():"; - _MALLOC_LOCK(); + THREAD_LOCK(); if (malloc_active++) { wrtwarning("recursive call.\n"); malloc_active--; - _MALLOC_UNLOCK(); + THREAD_UNLOCK(); return (0); } if (!ptr) { @@ -1267,7 +1298,7 @@ realloc(void *ptr, size_t size) } UTRACE(ptr, size, r); malloc_active--; - _MALLOC_UNLOCK(); + THREAD_UNLOCK(); if (malloc_xmalloc && !r) wrterror("out of memory.\n"); return (r); -- cgit v1.2.3-55-g6feb