]> git.saurik.com Git - redis.git/blob - deps/jemalloc/src/mutex.c
jemalloc source added
[redis.git] / deps / jemalloc / src / mutex.c
1 #define JEMALLOC_MUTEX_C_
2 #include "jemalloc/internal/jemalloc_internal.h"
3
4 /******************************************************************************/
5 /* Data. */
6
7 #ifdef JEMALLOC_LAZY_LOCK
8 bool isthreaded = false;
9 #endif
10
11 #ifdef JEMALLOC_LAZY_LOCK
12 static void pthread_create_once(void);
13 #endif
14
15 /******************************************************************************/
16 /*
17 * We intercept pthread_create() calls in order to toggle isthreaded if the
18 * process goes multi-threaded.
19 */
20
21 #ifdef JEMALLOC_LAZY_LOCK
22 static int (*pthread_create_fptr)(pthread_t *__restrict, const pthread_attr_t *,
23 void *(*)(void *), void *__restrict);
24
25 static void
26 pthread_create_once(void)
27 {
28
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();
34 }
35
36 isthreaded = true;
37 }
38
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)
44 {
45 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
46
47 pthread_once(&once_control, pthread_create_once);
48
49 return (pthread_create_fptr(thread, attr, start_routine, arg));
50 }
51 #endif
52
53 /******************************************************************************/
54
55 bool
56 malloc_mutex_init(malloc_mutex_t *mutex)
57 {
58 #ifdef JEMALLOC_OSSPIN
59 *mutex = 0;
60 #else
61 pthread_mutexattr_t attr;
62
63 if (pthread_mutexattr_init(&attr) != 0)
64 return (true);
65 #ifdef PTHREAD_MUTEX_ADAPTIVE_NP
66 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
67 #else
68 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
69 #endif
70 if (pthread_mutex_init(mutex, &attr) != 0) {
71 pthread_mutexattr_destroy(&attr);
72 return (true);
73 }
74 pthread_mutexattr_destroy(&attr);
75
76 #endif
77 return (false);
78 }
79
80 void
81 malloc_mutex_destroy(malloc_mutex_t *mutex)
82 {
83
84 #ifndef JEMALLOC_OSSPIN
85 if (pthread_mutex_destroy(mutex) != 0) {
86 malloc_write("<jemalloc>: Error in pthread_mutex_destroy()\n");
87 abort();
88 }
89 #endif
90 }