diff options
author | click <none@none> | 2010-12-18 02:31:10 +0100 |
---|---|---|
committer | click <none@none> | 2010-12-18 02:31:10 +0100 |
commit | 785d5a94a6b69010f1c442ebac645257d63fa830 (patch) | |
tree | e6be12d202e22160f1e57bfaa87734d65cb87797 /dep/jemalloc/src/mutex.c | |
parent | 0054bd83d8bc5f773f2df3a1985b4651a2097ce8 (diff) |
Core/Dependencies: Update jemalloc library from v1.0.0 to v2.1.0 (latest stable)
--HG--
branch : trunk
Diffstat (limited to 'dep/jemalloc/src/mutex.c')
-rw-r--r-- | dep/jemalloc/src/mutex.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/dep/jemalloc/src/mutex.c b/dep/jemalloc/src/mutex.c new file mode 100644 index 00000000000..3ecb18a340e --- /dev/null +++ b/dep/jemalloc/src/mutex.c @@ -0,0 +1,84 @@ +#define JEMALLOC_MUTEX_C_ +#include "jemalloc/internal/jemalloc_internal.h" + +/******************************************************************************/ +/* Data. */ + +#ifdef JEMALLOC_LAZY_LOCK +bool isthreaded = false; +#endif + +#ifdef JEMALLOC_LAZY_LOCK +static void pthread_create_once(void); +#endif + +/******************************************************************************/ +/* + * We intercept pthread_create() calls in order to toggle isthreaded if the + * process goes multi-threaded. + */ + +#ifdef JEMALLOC_LAZY_LOCK +static int (*pthread_create_fptr)(pthread_t *__restrict, const pthread_attr_t *, + void *(*)(void *), void *__restrict); + +static void +pthread_create_once(void) +{ + + pthread_create_fptr = dlsym(RTLD_NEXT, "pthread_create"); + if (pthread_create_fptr == NULL) { + malloc_write("<jemalloc>: Error in dlsym(RTLD_NEXT, " + "\"pthread_create\")\n"); + abort(); + } + + isthreaded = true; +} + +JEMALLOC_ATTR(visibility("default")) +int +pthread_create(pthread_t *__restrict thread, + const pthread_attr_t *__restrict attr, void *(*start_routine)(void *), + void *__restrict arg) +{ + static pthread_once_t once_control = PTHREAD_ONCE_INIT; + + pthread_once(&once_control, pthread_create_once); + + return (pthread_create_fptr(thread, attr, start_routine, arg)); +} +#endif + +/******************************************************************************/ + +bool +malloc_mutex_init(malloc_mutex_t *mutex) +{ + pthread_mutexattr_t attr; + + if (pthread_mutexattr_init(&attr) != 0) + return (true); +#ifdef PTHREAD_MUTEX_ADAPTIVE_NP + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP); +#else + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT); +#endif + if (pthread_mutex_init(mutex, &attr) != 0) { + pthread_mutexattr_destroy(&attr); + return (true); + } + pthread_mutexattr_destroy(&attr); + + return (false); +} + +void +malloc_mutex_destroy(malloc_mutex_t *mutex) +{ + + if (pthread_mutex_destroy(mutex) != 0) { + malloc_write("<jemalloc>: Error in pthread_mutex_destroy()\n"); + abort(); + } +} |