starfix: HoR fix!!! wave+team check fix+2 bos jalan...kurang LK nya + PR:lootannya...
[st4rcore.git] / dep / jemalloc / src / mutex.c
blob3ecb18a340e37b0f1300dec0d9988db90af046e2
1 #define JEMALLOC_MUTEX_C_
2 #include "jemalloc/internal/jemalloc_internal.h"
4 /******************************************************************************/
5 /* Data. */
7 #ifdef JEMALLOC_LAZY_LOCK
8 bool isthreaded = false;
9 #endif
11 #ifdef JEMALLOC_LAZY_LOCK
12 static void pthread_create_once(void);
13 #endif
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);
25 static void
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");
33 abort();
36 isthreaded = true;
39 JEMALLOC_ATTR(visibility("default"))
40 int
41 pthread_create(pthread_t *__restrict thread,
42 const pthread_attr_t *__restrict attr, void *(*start_routine)(void *),
43 void *__restrict arg)
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));
51 #endif
53 /******************************************************************************/
55 bool
56 malloc_mutex_init(malloc_mutex_t *mutex)
58 pthread_mutexattr_t attr;
60 if (pthread_mutexattr_init(&attr) != 0)
61 return (true);
62 #ifdef PTHREAD_MUTEX_ADAPTIVE_NP
63 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
64 #else
65 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
66 #endif
67 if (pthread_mutex_init(mutex, &attr) != 0) {
68 pthread_mutexattr_destroy(&attr);
69 return (true);
71 pthread_mutexattr_destroy(&attr);
73 return (false);
76 void
77 malloc_mutex_destroy(malloc_mutex_t *mutex)
80 if (pthread_mutex_destroy(mutex) != 0) {
81 malloc_write("<jemalloc>: Error in pthread_mutex_destroy()\n");
82 abort();