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