1 #include "test/jemalloc_test.h" 2 3 #ifndef _CRT_SPINCOUNT 4 #define _CRT_SPINCOUNT 4000 5 #endif 6 7 bool mtx_init(mtx_t * mtx)8mtx_init(mtx_t *mtx) 9 { 10 #ifdef _WIN32 11 if (!InitializeCriticalSectionAndSpinCount(&mtx->lock, _CRT_SPINCOUNT)) 12 return (true); 13 #elif (defined(JEMALLOC_OS_UNFAIR_LOCK)) 14 mtx->lock = OS_UNFAIR_LOCK_INIT; 15 #elif (defined(JEMALLOC_OSSPIN)) 16 mtx->lock = 0; 17 #else 18 pthread_mutexattr_t attr; 19 20 if (pthread_mutexattr_init(&attr) != 0) 21 return (true); 22 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT); 23 if (pthread_mutex_init(&mtx->lock, &attr) != 0) { 24 pthread_mutexattr_destroy(&attr); 25 return (true); 26 } 27 pthread_mutexattr_destroy(&attr); 28 #endif 29 return (false); 30 } 31 32 void mtx_fini(mtx_t * mtx)33mtx_fini(mtx_t *mtx) 34 { 35 #ifdef _WIN32 36 #elif (defined(JEMALLOC_OS_UNFAIR_LOCK)) 37 #elif (defined(JEMALLOC_OSSPIN)) 38 #else 39 pthread_mutex_destroy(&mtx->lock); 40 #endif 41 } 42 43 void mtx_lock(mtx_t * mtx)44mtx_lock(mtx_t *mtx) 45 { 46 #ifdef _WIN32 47 EnterCriticalSection(&mtx->lock); 48 #elif (defined(JEMALLOC_OS_UNFAIR_LOCK)) 49 os_unfair_lock_lock(&mtx->lock); 50 #elif (defined(JEMALLOC_OSSPIN)) 51 OSSpinLockLock(&mtx->lock); 52 #else 53 pthread_mutex_lock(&mtx->lock); 54 #endif 55 } 56 57 void mtx_unlock(mtx_t * mtx)58mtx_unlock(mtx_t *mtx) 59 { 60 #ifdef _WIN32 61 LeaveCriticalSection(&mtx->lock); 62 #elif (defined(JEMALLOC_OS_UNFAIR_LOCK)) 63 os_unfair_lock_unlock(&mtx->lock); 64 #elif (defined(JEMALLOC_OSSPIN)) 65 OSSpinLockUnlock(&mtx->lock); 66 #else 67 pthread_mutex_unlock(&mtx->lock); 68 #endif 69 } 70