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