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