1 #define JEMALLOC_MUTEX_C_
2 #include "jemalloc/internal/jemalloc_internal.h"
4 /******************************************************************************/
7 #ifdef JEMALLOC_LAZY_LOCK
8 bool isthreaded
= false;
11 #ifdef JEMALLOC_LAZY_LOCK
12 static void pthread_create_once(void);
15 /******************************************************************************/
17 * We intercept pthread_create() calls in order to toggle isthreaded if the
18 * process goes multi-threaded.
21 #ifdef JEMALLOC_LAZY_LOCK
22 static int (*pthread_create_fptr
)(pthread_t
*__restrict
, const pthread_attr_t
*,
23 void *(*)(void *), void *__restrict
);
26 pthread_create_once(void)
29 pthread_create_fptr
= dlsym(RTLD_NEXT
, "pthread_create");
30 if (pthread_create_fptr
== NULL
) {
31 malloc_write("<jemalloc>: Error in dlsym(RTLD_NEXT, "
32 "\"pthread_create\")\n");
39 JEMALLOC_ATTR(visibility("default"))
41 pthread_create(pthread_t
*__restrict thread
,
42 const pthread_attr_t
*__restrict attr
, void *(*start_routine
)(void *),
45 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
47 pthread_once(&once_control
, pthread_create_once
);
49 return (pthread_create_fptr(thread
, attr
, start_routine
, arg
));
53 /******************************************************************************/
56 malloc_mutex_init(malloc_mutex_t
*mutex
)
58 #ifdef JEMALLOC_OSSPIN
61 pthread_mutexattr_t attr
;
63 if (pthread_mutexattr_init(&attr
) != 0)
65 #ifdef PTHREAD_MUTEX_ADAPTIVE_NP
66 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_ADAPTIVE_NP
);
68 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_DEFAULT
);
70 if (pthread_mutex_init(mutex
, &attr
) != 0) {
71 pthread_mutexattr_destroy(&attr
);
74 pthread_mutexattr_destroy(&attr
);
81 malloc_mutex_destroy(malloc_mutex_t
*mutex
)
84 #ifndef JEMALLOC_OSSPIN
85 if (pthread_mutex_destroy(mutex
) != 0) {
86 malloc_write("<jemalloc>: Error in pthread_mutex_destroy()\n");