2013-12-13 06:41:02 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
2014-05-21 15:59:50 +08:00
|
|
|
#ifndef _CRT_SPINCOUNT
|
2017-01-20 13:41:41 +08:00
|
|
|
#define _CRT_SPINCOUNT 4000
|
2014-05-21 15:59:50 +08:00
|
|
|
#endif
|
|
|
|
|
2013-12-13 06:41:02 +08:00
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
mtx_init(mtx_t *mtx) {
|
2013-12-13 06:41:02 +08:00
|
|
|
#ifdef _WIN32
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!InitializeCriticalSectionAndSpinCount(&mtx->lock,
|
|
|
|
_CRT_SPINCOUNT)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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
|
|
|
#else
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (pthread_mutexattr_init(&attr) != 0) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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);
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2013-12-13 06:41:02 +08:00
|
|
|
}
|
|
|
|
pthread_mutexattr_destroy(&attr);
|
|
|
|
#endif
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2013-12-13 06:41:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
mtx_fini(mtx_t *mtx) {
|
2013-12-13 06:41:02 +08:00
|
|
|
#ifdef _WIN32
|
2016-11-03 09:09:45 +08:00
|
|
|
#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
|
2013-12-13 06:41:02 +08:00
|
|
|
#else
|
|
|
|
pthread_mutex_destroy(&mtx->lock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
mtx_lock(mtx_t *mtx) {
|
2013-12-13 06:41:02 +08:00
|
|
|
#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
|
|
|
#else
|
|
|
|
pthread_mutex_lock(&mtx->lock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
mtx_unlock(mtx_t *mtx) {
|
2013-12-13 06:41:02 +08:00
|
|
|
#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
|
|
|
#else
|
|
|
|
pthread_mutex_unlock(&mtx->lock);
|
|
|
|
#endif
|
|
|
|
}
|