diff options
author | Marco S Hyman <marc@cvs.openbsd.org> | 2002-11-03 20:36:44 +0000 |
---|---|---|
committer | Marco S Hyman <marc@cvs.openbsd.org> | 2002-11-03 20:36:44 +0000 |
commit | 664e44eac173bd00d9757c4d15084393436fcbdc (patch) | |
tree | d0df53493c5ae4d4dceb1c350e7bfa2b9d4770a8 /lib/libc/stdlib/malloc.c | |
parent | a7bb9a3fe7041afd8e1153c8c5b97c0952ec0e2c (diff) |
libc changes for thread safety. Tested on:
alpha (millert@), i386 (marc@), m68k (millert@ and miod@),
powerpc (drahn@ and dhartmei@), sparc (millert@ and marc@),
sparc64 (marc@), and vax (millert@ and miod@).
Thanks to millert@, miod@, and mickey@ for fixes along the way.
Diffstat (limited to 'lib/libc/stdlib/malloc.c')
-rw-r--r-- | lib/libc/stdlib/malloc.c | 57 |
1 files changed, 13 insertions, 44 deletions
diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c index 4e90ce402e4..3aff1bfb9c5 100644 --- a/lib/libc/stdlib/malloc.c +++ b/lib/libc/stdlib/malloc.c @@ -8,7 +8,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: malloc.c,v 1.48 2002/05/27 03:13:23 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: malloc.c,v 1.49 2002/11/03 20:36:43 marc Exp $"; #endif /* LIBC_SCCS and not lint */ /* @@ -48,6 +48,8 @@ static char rcsid[] = "$OpenBSD: malloc.c,v 1.48 2002/05/27 03:13:23 deraadt Exp #include <fcntl.h> #include <errno.h> +#include "thread_private.h" + /* * The basic parameters you can tweak. * @@ -67,39 +69,6 @@ static char rcsid[] = "$OpenBSD: malloc.c,v 1.48 2002/05/27 03:13:23 deraadt Exp # define malloc_pageshift 13U #endif /* __OpenBSD__ */ -#ifdef _THREAD_SAFE -# include "thread_private.h" -# if 0 - /* kernel threads */ -# include <pthread.h> - 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. * @@ -494,7 +463,7 @@ malloc_init () int i, j; int save_errno = errno; - THREAD_LOCK_INIT(); + _MALLOC_LOCK_INIT(); INIT_MMAP(); @@ -1244,17 +1213,17 @@ malloc(size_t size) register void *r; malloc_func = " in malloc():"; - THREAD_LOCK(); + _MALLOC_LOCK(); if (malloc_active++) { wrtwarning("recursive call.\n"); malloc_active--; - THREAD_UNLOCK(); + _MALLOC_UNLOCK(); return (0); } r = imalloc(size); UTRACE(0, size, r); malloc_active--; - THREAD_UNLOCK(); + _MALLOC_UNLOCK(); if (malloc_xmalloc && !r) wrterror("out of memory.\n"); return (r); @@ -1264,17 +1233,17 @@ void free(void *ptr) { malloc_func = " in free():"; - THREAD_LOCK(); + _MALLOC_LOCK(); if (malloc_active++) { wrtwarning("recursive call.\n"); malloc_active--; - THREAD_UNLOCK(); + _MALLOC_UNLOCK(); return; } ifree(ptr); UTRACE(ptr, 0, 0); malloc_active--; - THREAD_UNLOCK(); + _MALLOC_UNLOCK(); return; } @@ -1284,11 +1253,11 @@ realloc(void *ptr, size_t size) register void *r; malloc_func = " in realloc():"; - THREAD_LOCK(); + _MALLOC_LOCK(); if (malloc_active++) { wrtwarning("recursive call.\n"); malloc_active--; - THREAD_UNLOCK(); + _MALLOC_UNLOCK(); return (0); } if (!ptr) { @@ -1298,7 +1267,7 @@ realloc(void *ptr, size_t size) } UTRACE(ptr, size, r); malloc_active--; - THREAD_UNLOCK(); + _MALLOC_UNLOCK(); if (malloc_xmalloc && !r) wrterror("out of memory.\n"); return (r); |