mtx.c revision 0f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9
1#include "test/jemalloc_test.h"
2
3bool
4mtx_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, MALLOC_MUTEX_TYPE);
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
27void
28mtx_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
38void
39mtx_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
51void
52mtx_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