Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1 | #define JEMALLOC_MUTEX_C_ |
Jason Evans | 376b152 | 2010-02-11 14:45:59 -0800 | [diff] [blame] | 2 | #include "jemalloc/internal/jemalloc_internal.h" |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3 | |
Mike Hommey | a19e87f | 2012-04-21 21:27:46 -0700 | [diff] [blame] | 4 | #if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32) |
Jason Evans | 4bb0983 | 2012-02-29 10:37:27 -0800 | [diff] [blame] | 5 | #include <dlfcn.h> |
| 6 | #endif |
| 7 | |
Mike Hommey | a19e87f | 2012-04-21 21:27:46 -0700 | [diff] [blame] | 8 | #ifndef _CRT_SPINCOUNT |
Jason Evans | a4f124f | 2013-12-08 22:28:27 -0800 | [diff] [blame] | 9 | #define _CRT_SPINCOUNT 4000 |
Mike Hommey | a19e87f | 2012-04-21 21:27:46 -0700 | [diff] [blame] | 10 | #endif |
| 11 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 12 | /******************************************************************************/ |
| 13 | /* Data. */ |
| 14 | |
| 15 | #ifdef JEMALLOC_LAZY_LOCK |
| 16 | bool isthreaded = false; |
| 17 | #endif |
Jason Evans | 633aaff | 2012-04-03 08:47:07 -0700 | [diff] [blame] | 18 | #ifdef JEMALLOC_MUTEX_INIT_CB |
| 19 | static bool postpone_init = true; |
| 20 | static malloc_mutex_t *postponed_mutexes = NULL; |
| 21 | #endif |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 22 | |
Mike Hommey | a19e87f | 2012-04-21 21:27:46 -0700 | [diff] [blame] | 23 | #if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 24 | static void pthread_create_once(void); |
| 25 | #endif |
| 26 | |
| 27 | /******************************************************************************/ |
| 28 | /* |
| 29 | * We intercept pthread_create() calls in order to toggle isthreaded if the |
| 30 | * process goes multi-threaded. |
| 31 | */ |
| 32 | |
Mike Hommey | a19e87f | 2012-04-21 21:27:46 -0700 | [diff] [blame] | 33 | #if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 34 | static int (*pthread_create_fptr)(pthread_t *__restrict, const pthread_attr_t *, |
| 35 | void *(*)(void *), void *__restrict); |
| 36 | |
| 37 | static void |
| 38 | pthread_create_once(void) |
| 39 | { |
| 40 | |
| 41 | pthread_create_fptr = dlsym(RTLD_NEXT, "pthread_create"); |
| 42 | if (pthread_create_fptr == NULL) { |
Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 43 | malloc_write("<jemalloc>: Error in dlsym(RTLD_NEXT, " |
| 44 | "\"pthread_create\")\n"); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 45 | abort(); |
| 46 | } |
| 47 | |
| 48 | isthreaded = true; |
| 49 | } |
| 50 | |
Mike Hommey | da99e31 | 2012-04-30 12:38:29 +0200 | [diff] [blame] | 51 | JEMALLOC_EXPORT int |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 52 | pthread_create(pthread_t *__restrict thread, |
| 53 | const pthread_attr_t *__restrict attr, void *(*start_routine)(void *), |
| 54 | void *__restrict arg) |
| 55 | { |
| 56 | static pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 57 | |
| 58 | pthread_once(&once_control, pthread_create_once); |
| 59 | |
| 60 | return (pthread_create_fptr(thread, attr, start_routine, arg)); |
| 61 | } |
| 62 | #endif |
| 63 | |
| 64 | /******************************************************************************/ |
| 65 | |
Jason Evans | 41b6afb | 2012-02-02 22:04:57 -0800 | [diff] [blame] | 66 | #ifdef JEMALLOC_MUTEX_INIT_CB |
Jan Beich | d0ffd8e | 2012-09-18 07:40:31 -0500 | [diff] [blame] | 67 | JEMALLOC_EXPORT int _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex, |
Jason Evans | 41b6afb | 2012-02-02 22:04:57 -0800 | [diff] [blame] | 68 | void *(calloc_cb)(size_t, size_t)); |
| 69 | #endif |
| 70 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 71 | bool |
| 72 | malloc_mutex_init(malloc_mutex_t *mutex) |
| 73 | { |
Mike Hommey | da99e31 | 2012-04-30 12:38:29 +0200 | [diff] [blame] | 74 | |
Mike Hommey | a19e87f | 2012-04-21 21:27:46 -0700 | [diff] [blame] | 75 | #ifdef _WIN32 |
| 76 | if (!InitializeCriticalSectionAndSpinCount(&mutex->lock, |
| 77 | _CRT_SPINCOUNT)) |
| 78 | return (true); |
| 79 | #elif (defined(JEMALLOC_OSSPIN)) |
Jason Evans | 633aaff | 2012-04-03 08:47:07 -0700 | [diff] [blame] | 80 | mutex->lock = 0; |
Jason Evans | 41b6afb | 2012-02-02 22:04:57 -0800 | [diff] [blame] | 81 | #elif (defined(JEMALLOC_MUTEX_INIT_CB)) |
Jason Evans | 633aaff | 2012-04-03 08:47:07 -0700 | [diff] [blame] | 82 | if (postpone_init) { |
| 83 | mutex->postponed_next = postponed_mutexes; |
| 84 | postponed_mutexes = mutex; |
| 85 | } else { |
| 86 | if (_pthread_mutex_init_calloc_cb(&mutex->lock, base_calloc) != |
| 87 | 0) |
| 88 | return (true); |
| 89 | } |
Jason Evans | 893a0ed | 2011-03-18 19:30:18 -0700 | [diff] [blame] | 90 | #else |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 91 | pthread_mutexattr_t attr; |
| 92 | |
| 93 | if (pthread_mutexattr_init(&attr) != 0) |
| 94 | return (true); |
Jason Evans | 41b6afb | 2012-02-02 22:04:57 -0800 | [diff] [blame] | 95 | pthread_mutexattr_settype(&attr, MALLOC_MUTEX_TYPE); |
Jason Evans | 633aaff | 2012-04-03 08:47:07 -0700 | [diff] [blame] | 96 | if (pthread_mutex_init(&mutex->lock, &attr) != 0) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 97 | pthread_mutexattr_destroy(&attr); |
| 98 | return (true); |
| 99 | } |
| 100 | pthread_mutexattr_destroy(&attr); |
Jason Evans | 893a0ed | 2011-03-18 19:30:18 -0700 | [diff] [blame] | 101 | #endif |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 102 | return (false); |
| 103 | } |
Jason Evans | a881cd2 | 2010-10-02 15:18:50 -0700 | [diff] [blame] | 104 | |
| 105 | void |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 106 | malloc_mutex_prefork(malloc_mutex_t *mutex) |
| 107 | { |
| 108 | |
| 109 | malloc_mutex_lock(mutex); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | malloc_mutex_postfork_parent(malloc_mutex_t *mutex) |
| 114 | { |
| 115 | |
| 116 | malloc_mutex_unlock(mutex); |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | malloc_mutex_postfork_child(malloc_mutex_t *mutex) |
| 121 | { |
| 122 | |
Jason Evans | 41b6afb | 2012-02-02 22:04:57 -0800 | [diff] [blame] | 123 | #ifdef JEMALLOC_MUTEX_INIT_CB |
| 124 | malloc_mutex_unlock(mutex); |
| 125 | #else |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 126 | if (malloc_mutex_init(mutex)) { |
| 127 | malloc_printf("<jemalloc>: Error re-initializing mutex in " |
| 128 | "child\n"); |
| 129 | if (opt_abort) |
| 130 | abort(); |
| 131 | } |
Jason Evans | 41b6afb | 2012-02-02 22:04:57 -0800 | [diff] [blame] | 132 | #endif |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 133 | } |
Jason Evans | 633aaff | 2012-04-03 08:47:07 -0700 | [diff] [blame] | 134 | |
| 135 | bool |
| 136 | mutex_boot(void) |
| 137 | { |
| 138 | |
| 139 | #ifdef JEMALLOC_MUTEX_INIT_CB |
| 140 | postpone_init = false; |
| 141 | while (postponed_mutexes != NULL) { |
| 142 | if (_pthread_mutex_init_calloc_cb(&postponed_mutexes->lock, |
| 143 | base_calloc) != 0) |
| 144 | return (true); |
| 145 | postponed_mutexes = postponed_mutexes->postponed_next; |
| 146 | } |
| 147 | #endif |
| 148 | return (false); |
| 149 | } |