2013-12-13 06:41:02 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
2014-05-21 15:59:50 +08:00
|
|
|
#ifndef _CRT_SPINCOUNT
|
|
|
|
#define _CRT_SPINCOUNT 4000
|
|
|
|
#endif
|
|
|
|
|
2013-12-13 06:41:02 +08:00
|
|
|
bool
|
|
|
|
mtx_init(mtx_t *mtx)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (!InitializeCriticalSectionAndSpinCount(&mtx->lock, _CRT_SPINCOUNT))
|
|
|
|
return (true);
|
2016-11-03 09:09:45 +08:00
|
|
|
#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
|
|
|
|
mtx->lock = OS_UNFAIR_LOCK_INIT;
|
2013-12-13 06:41:02 +08:00
|
|
|
#elif (defined(JEMALLOC_OSSPIN))
|
|
|
|
mtx->lock = 0;
|
|
|
|
#else
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
|
|
|
|
if (pthread_mutexattr_init(&attr) != 0)
|
|
|
|
return (true);
|
2013-12-13 07:14:51 +08:00
|
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
|
2013-12-13 06:41:02 +08:00
|
|
|
if (pthread_mutex_init(&mtx->lock, &attr) != 0) {
|
|
|
|
pthread_mutexattr_destroy(&attr);
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
pthread_mutexattr_destroy(&attr);
|
|
|
|
#endif
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mtx_fini(mtx_t *mtx)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2016-11-03 09:09:45 +08:00
|
|
|
#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
|
2013-12-13 06:41:02 +08:00
|
|
|
#elif (defined(JEMALLOC_OSSPIN))
|
|
|
|
#else
|
|
|
|
pthread_mutex_destroy(&mtx->lock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mtx_lock(mtx_t *mtx)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
EnterCriticalSection(&mtx->lock);
|
2016-11-03 09:09:45 +08:00
|
|
|
#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
|
|
|
|
os_unfair_lock_lock(&mtx->lock);
|
2013-12-13 06:41:02 +08:00
|
|
|
#elif (defined(JEMALLOC_OSSPIN))
|
|
|
|
OSSpinLockLock(&mtx->lock);
|
|
|
|
#else
|
|
|
|
pthread_mutex_lock(&mtx->lock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mtx_unlock(mtx_t *mtx)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
LeaveCriticalSection(&mtx->lock);
|
2016-11-03 09:09:45 +08:00
|
|
|
#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
|
|
|
|
os_unfair_lock_unlock(&mtx->lock);
|
2013-12-13 06:41:02 +08:00
|
|
|
#elif (defined(JEMALLOC_OSSPIN))
|
|
|
|
OSSpinLockUnlock(&mtx->lock);
|
|
|
|
#else
|
|
|
|
pthread_mutex_unlock(&mtx->lock);
|
|
|
|
#endif
|
|
|
|
}
|