1/******************************************************************************/ 2#ifdef JEMALLOC_H_TYPES 3 4typedef struct malloc_mutex_s malloc_mutex_t; 5 6#ifdef _WIN32 7# define MALLOC_MUTEX_INITIALIZER 8#elif (defined(JEMALLOC_OSSPIN)) 9# define MALLOC_MUTEX_INITIALIZER {0, WITNESS_INITIALIZER(WITNESS_RANK_OMIT)} 10#elif (defined(JEMALLOC_MUTEX_INIT_CB)) 11# define MALLOC_MUTEX_INITIALIZER \ 12 {PTHREAD_MUTEX_INITIALIZER, NULL, WITNESS_INITIALIZER(WITNESS_RANK_OMIT)} 13#else 14# if (defined(JEMALLOC_HAVE_PTHREAD_MUTEX_ADAPTIVE_NP) && \ 15 defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)) 16# define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_ADAPTIVE_NP 17# define MALLOC_MUTEX_INITIALIZER \ 18 {PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP, \ 19 WITNESS_INITIALIZER(WITNESS_RANK_OMIT)} 20# else 21# define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_DEFAULT 22# define MALLOC_MUTEX_INITIALIZER \ 23 {PTHREAD_MUTEX_INITIALIZER, WITNESS_INITIALIZER(WITNESS_RANK_OMIT)} 24# endif 25#endif 26 27#endif /* JEMALLOC_H_TYPES */ 28/******************************************************************************/ 29#ifdef JEMALLOC_H_STRUCTS 30 31struct malloc_mutex_s { 32#ifdef _WIN32 33# if _WIN32_WINNT >= 0x0600 34 SRWLOCK lock; 35# else 36 CRITICAL_SECTION lock; 37# endif 38#elif (defined(JEMALLOC_OSSPIN)) 39 OSSpinLock lock; 40#elif (defined(JEMALLOC_MUTEX_INIT_CB)) 41 pthread_mutex_t lock; 42 malloc_mutex_t *postponed_next; 43#else 44 pthread_mutex_t lock; 45#endif 46 witness_t witness; 47}; 48 49#endif /* JEMALLOC_H_STRUCTS */ 50/******************************************************************************/ 51#ifdef JEMALLOC_H_EXTERNS 52 53#ifdef JEMALLOC_LAZY_LOCK 54extern bool isthreaded; 55#endif 56 57bool malloc_mutex_init(malloc_mutex_t *mutex, const char *name, 58 witness_rank_t rank); 59void malloc_mutex_prefork(tsdn_t *tsdn, malloc_mutex_t *mutex); 60void malloc_mutex_postfork_parent(tsdn_t *tsdn, malloc_mutex_t *mutex); 61void malloc_mutex_postfork_child(tsdn_t *tsdn, malloc_mutex_t *mutex); 62bool malloc_mutex_first_thread(void); 63bool malloc_mutex_boot(void); 64 65#endif /* JEMALLOC_H_EXTERNS */ 66/******************************************************************************/ 67#ifdef JEMALLOC_H_INLINES 68 69#ifndef JEMALLOC_ENABLE_INLINE 70void malloc_mutex_lock(tsdn_t *tsdn, malloc_mutex_t *mutex); 71void malloc_mutex_unlock(tsdn_t *tsdn, malloc_mutex_t *mutex); 72void malloc_mutex_assert_owner(tsdn_t *tsdn, malloc_mutex_t *mutex); 73void malloc_mutex_assert_not_owner(tsdn_t *tsdn, malloc_mutex_t *mutex); 74#endif 75 76#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_MUTEX_C_)) 77JEMALLOC_INLINE void 78malloc_mutex_lock(tsdn_t *tsdn, malloc_mutex_t *mutex) 79{ 80 81 if (isthreaded) { 82 witness_assert_not_owner(tsdn, &mutex->witness); 83#ifdef _WIN32 84# if _WIN32_WINNT >= 0x0600 85 AcquireSRWLockExclusive(&mutex->lock); 86# else 87 EnterCriticalSection(&mutex->lock); 88# endif 89#elif (defined(JEMALLOC_OSSPIN)) 90 OSSpinLockLock(&mutex->lock); 91#else 92 pthread_mutex_lock(&mutex->lock); 93#endif 94 witness_lock(tsdn, &mutex->witness); 95 } 96} 97 98JEMALLOC_INLINE void 99malloc_mutex_unlock(tsdn_t *tsdn, malloc_mutex_t *mutex) 100{ 101 102 if (isthreaded) { 103 witness_unlock(tsdn, &mutex->witness); 104#ifdef _WIN32 105# if _WIN32_WINNT >= 0x0600 106 ReleaseSRWLockExclusive(&mutex->lock); 107# else 108 LeaveCriticalSection(&mutex->lock); 109# endif 110#elif (defined(JEMALLOC_OSSPIN)) 111 OSSpinLockUnlock(&mutex->lock); 112#else 113 pthread_mutex_unlock(&mutex->lock); 114#endif 115 } 116} 117 118JEMALLOC_INLINE void 119malloc_mutex_assert_owner(tsdn_t *tsdn, malloc_mutex_t *mutex) 120{ 121 122 if (isthreaded) 123 witness_assert_owner(tsdn, &mutex->witness); 124} 125 126JEMALLOC_INLINE void 127malloc_mutex_assert_not_owner(tsdn_t *tsdn, malloc_mutex_t *mutex) 128{ 129 130 if (isthreaded) 131 witness_assert_not_owner(tsdn, &mutex->witness); 132} 133#endif 134 135#endif /* JEMALLOC_H_INLINES */ 136/******************************************************************************/ 137