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