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 #ifdef _WIN32 10 if (!InitializeCriticalSectionAndSpinCount(&mtx->lock, 11 _CRT_SPINCOUNT)) { 12 return true; 13 } 14 #elif (defined(JEMALLOC_OS_UNFAIR_LOCK)) 15 mtx->lock = OS_UNFAIR_LOCK_INIT; 16 #elif (defined(JEMALLOC_OSSPIN)) 17 mtx->lock = 0; 18 #else 19 pthread_mutexattr_t attr; 20 21 if (pthread_mutexattr_init(&attr) != 0) { 22 return true; 23 } 24 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT); 25 if (pthread_mutex_init(&mtx->lock, &attr) != 0) { 26 pthread_mutexattr_destroy(&attr); 27 return true; 28 } 29 pthread_mutexattr_destroy(&attr); 30 #endif 31 return false; 32 } 33 34 void mtx_fini(mtx_t * mtx)35mtx_fini(mtx_t *mtx) { 36 #ifdef _WIN32 37 #elif (defined(JEMALLOC_OS_UNFAIR_LOCK)) 38 #elif (defined(JEMALLOC_OSSPIN)) 39 #else 40 pthread_mutex_destroy(&mtx->lock); 41 #endif 42 } 43 44 void mtx_lock(mtx_t * mtx)45mtx_lock(mtx_t *mtx) { 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 #ifdef _WIN32 60 LeaveCriticalSection(&mtx->lock); 61 #elif (defined(JEMALLOC_OS_UNFAIR_LOCK)) 62 os_unfair_lock_unlock(&mtx->lock); 63 #elif (defined(JEMALLOC_OSSPIN)) 64 OSSpinLockUnlock(&mtx->lock); 65 #else 66 pthread_mutex_unlock(&mtx->lock); 67 #endif 68 } 69