diff options
author | Jason Evans <je@fb.com> | 2011-03-19 02:30:18 (GMT) |
---|---|---|
committer | Jason Evans <je@fb.com> | 2011-03-19 02:30:18 (GMT) |
commit | 893a0ed7c8c11962524ba6f2adeb304d038be2a9 (patch) | |
tree | f3250f735a97587da8ca61f0208f07bc28cd65fe /jemalloc/include | |
parent | 763baa6cfcc8a9df9d3b7f676b2193ac7cd5ef51 (diff) | |
download | jemalloc-893a0ed7c8c11962524ba6f2adeb304d038be2a9.zip jemalloc-893a0ed7c8c11962524ba6f2adeb304d038be2a9.tar.gz jemalloc-893a0ed7c8c11962524ba6f2adeb304d038be2a9.tar.bz2 |
Use OSSpinLock*() for locking on OS X.
pthread_mutex_lock() can call malloc() on OS X (!!!), which causes
deadlock. Work around this by using spinlocks that are built of more
primitive stuff.
Diffstat (limited to 'jemalloc/include')
-rw-r--r-- | jemalloc/include/jemalloc/internal/atomic.h | 18 | ||||
-rw-r--r-- | jemalloc/include/jemalloc/internal/jemalloc_internal.h.in | 2 | ||||
-rw-r--r-- | jemalloc/include/jemalloc/internal/mutex.h | 26 | ||||
-rw-r--r-- | jemalloc/include/jemalloc/jemalloc_defs.h.in | 6 |
4 files changed, 41 insertions, 11 deletions
diff --git a/jemalloc/include/jemalloc/internal/atomic.h b/jemalloc/include/jemalloc/internal/atomic.h index 089affa..f1f0c2b 100644 --- a/jemalloc/include/jemalloc/internal/atomic.h +++ b/jemalloc/include/jemalloc/internal/atomic.h @@ -13,13 +13,19 @@ #define atomic_read_uint32(p) atomic_add_uint32(p, 0) #if (LG_SIZEOF_PTR == 3) -# define atomic_read_z(p) atomic_add_uint64(p, 0) -# define atomic_add_z(p, x) atomic_add_uint64(p, x) -# define atomic_sub_z(p, x) atomic_sub_uint64(p, x) +# define atomic_read_z(p) \ + (size_t)atomic_add_uint64((uint64_t *)p, (uint64_t)0) +# define atomic_add_z(p, x) \ + (size_t)atomic_add_uint64((uint64_t *)p, (uint64_t)x) +# define atomic_sub_z(p, x) \ + (size_t)atomic_sub_uint64((uint64_t *)p, (uint64_t)x) #elif (LG_SIZEOF_PTR == 2) -# define atomic_read_z(p) atomic_add_uint32(p, 0) -# define atomic_add_z(p, x) atomic_add_uint32(p, x) -# define atomic_sub_z(p, x) atomic_sub_uint32(p, x) +# define atomic_read_z(p) \ + (size_t)atomic_add_uint32((uint32_t *)p, (uint32_t)0) +# define atomic_add_z(p, x) \ + (size_t)atomic_add_uint32((uint32_t *)p, (uint32_t)x) +# define atomic_sub_z(p, x) \ + (size_t)atomic_sub_uint32((uint32_t *)p, (uint32_t)x) #endif #endif /* JEMALLOC_H_EXTERNS */ diff --git a/jemalloc/include/jemalloc/internal/jemalloc_internal.h.in b/jemalloc/include/jemalloc/internal/jemalloc_internal.h.in index f660bc8..fc944a8 100644 --- a/jemalloc/include/jemalloc/internal/jemalloc_internal.h.in +++ b/jemalloc/include/jemalloc/internal/jemalloc_internal.h.in @@ -33,7 +33,7 @@ #define JEMALLOC_MANGLE #include "../jemalloc@install_suffix@.h" -#ifdef JEMALLOC_OSATOMIC +#if (defined(JEMALLOC_OSATOMIC) || defined(JEMALLOC_OSSPIN)) #include <libkern/OSAtomic.h> #endif diff --git a/jemalloc/include/jemalloc/internal/mutex.h b/jemalloc/include/jemalloc/internal/mutex.h index dcca01e..62947ce 100644 --- a/jemalloc/include/jemalloc/internal/mutex.h +++ b/jemalloc/include/jemalloc/internal/mutex.h @@ -1,7 +1,11 @@ /******************************************************************************/ #ifdef JEMALLOC_H_TYPES +#ifdef JEMALLOC_OSSPIN +typedef OSSpinLock malloc_mutex_t; +#else typedef pthread_mutex_t malloc_mutex_t; +#endif #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP # define MALLOC_MUTEX_INITIALIZER PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP @@ -41,17 +45,26 @@ JEMALLOC_INLINE void malloc_mutex_lock(malloc_mutex_t *mutex) { - if (isthreaded) + if (isthreaded) { +#ifdef JEMALLOC_OSSPIN + OSSpinLockLock(mutex); +#else pthread_mutex_lock(mutex); +#endif + } } JEMALLOC_INLINE bool malloc_mutex_trylock(malloc_mutex_t *mutex) { - if (isthreaded) + if (isthreaded) { +#ifdef JEMALLOC_OSSPIN + return (OSSpinLockTry(mutex) == false); +#else return (pthread_mutex_trylock(mutex) != 0); - else +#endif + } else return (false); } @@ -59,8 +72,13 @@ JEMALLOC_INLINE void malloc_mutex_unlock(malloc_mutex_t *mutex) { - if (isthreaded) + if (isthreaded) { +#ifdef JEMALLOC_OSSPIN + OSSpinLockUnlock(mutex); +#else pthread_mutex_unlock(mutex); +#endif + } } #endif diff --git a/jemalloc/include/jemalloc/jemalloc_defs.h.in b/jemalloc/include/jemalloc/jemalloc_defs.h.in index c08c5a2..d8c81d7 100644 --- a/jemalloc/include/jemalloc/jemalloc_defs.h.in +++ b/jemalloc/include/jemalloc/jemalloc_defs.h.in @@ -30,6 +30,12 @@ */ #undef JEMALLOC_OSATOMIC +/* + * Defined if OSSpin*() functions are available, as provided by Darwin, and + * documented in the spinlock(3) manual page. + */ +#undef JEMALLOC_OSSPIN + /* Defined if __attribute__((...)) syntax is supported. */ #undef JEMALLOC_HAVE_ATTR #ifdef JEMALLOC_HAVE_ATTR |