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