summaryrefslogtreecommitdiff
path: root/libc/stdlib/malloc/malloc.h
diff options
context:
space:
mode:
authorMiles Bader <miles@lsi.nec.co.jp>2002-07-25 01:58:57 +0000
committerMiles Bader <miles@lsi.nec.co.jp>2002-07-25 01:58:57 +0000
commit056f9d98941eb98e453bf4fa308f28b892525baf (patch)
tree9adaefa6923b1949e06c4a81cf889976a7e8bb18 /libc/stdlib/malloc/malloc.h
parent255cd531d67d4c5d110409e2a24e2aa5a6249a7a (diff)
Redo the locking, so that it may actually work. Now locking is done at
the malloc/free level, not within the heap abstraction, and there's a separate lock to control sbrk access. Also, get rid of the separate `unmap_free_area' function in free.c, and just put the code in the `free' function directly, which saves a bunch of space (even compared to using an inline function) for some reason.
Diffstat (limited to 'libc/stdlib/malloc/malloc.h')
-rw-r--r--libc/stdlib/malloc/malloc.h38
1 files changed, 37 insertions, 1 deletions
diff --git a/libc/stdlib/malloc/malloc.h b/libc/stdlib/malloc/malloc.h
index 0cab1dba2..f01484368 100644
--- a/libc/stdlib/malloc/malloc.h
+++ b/libc/stdlib/malloc/malloc.h
@@ -38,10 +38,46 @@
mmap/munmap, and guarantees contiguous allocation, but is also less
flexible, and causes the heap to only be shrinkable from the end. */
#ifdef __UCLIBC_HAS_MMU__
-#define MALLOC_USE_SBRK
+# define MALLOC_USE_SBRK
#endif
+#ifdef __UCLIBC_HAS_THREADS__
+
+# include <pthread.h>
+
+# define MALLOC_USE_LOCKING
+
+typedef pthread_mutex_t malloc_mutex_t;
+# define MALLOC_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
+
+/* The main malloc lock. This must be hold while accessing __malloc_heap,
+ and in order to gain __malloc_sbrk_lock. */
+extern malloc_mutex_t __malloc_lock;
+# define __malloc_lock() pthread_mutex_lock (&__malloc_lock)
+# define __malloc_unlock() pthread_mutex_unlock (&__malloc_lock)
+
+# ifdef MALLOC_USE_SBRK
+/* This lock is used to serialize uses of the `sbrk' function (in both
+ malloc and free, sbrk may be used several times in succession, and
+ things will break if these multiple calls are interleaved with another
+ thread's use of sbrk!). */
+extern malloc_mutex_t __malloc_sbrk_lock;
+# define __malloc_lock_sbrk() pthread_mutex_lock (&__malloc_sbrk_lock)
+# define __malloc_unlock_sbrk() pthread_mutex_unlock (&__malloc_sbrk_lock)
+# endif /* MALLOC_USE_SBRK */
+
+#else /* !__UCLIBC_HAS_THREADS__ */
+
+/* Without threads, mutex operations are a nop. */
+# define __malloc_lock() (void)0
+# define __malloc_unlock() (void)0
+# define __malloc_lock_sbrk() (void)0
+# define __malloc_unlock_sbrk() (void)0
+
+#endif /* __UCLIBC_HAS_THREADS__ */
+
+
/* Change this to `#if 1' to cause malloc to emit debugging info to stderr. */
#if 0
#include <stdio.h>