10f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#include "test/jemalloc_test.h"
20f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans
3b666378b6b6aecd713524a04100f33ba8861f4e1Mike Hommey#ifndef _CRT_SPINCOUNT
4b666378b6b6aecd713524a04100f33ba8861f4e1Mike Hommey#define	_CRT_SPINCOUNT 4000
5b666378b6b6aecd713524a04100f33ba8861f4e1Mike Hommey#endif
6b666378b6b6aecd713524a04100f33ba8861f4e1Mike Hommey
70f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evansbool
80f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evansmtx_init(mtx_t *mtx)
90f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans{
100f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans
110f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#ifdef _WIN32
120f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	if (!InitializeCriticalSectionAndSpinCount(&mtx->lock, _CRT_SPINCOUNT))
130f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans		return (true);
140f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#elif (defined(JEMALLOC_OSSPIN))
150f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	mtx->lock = 0;
160f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#else
170f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	pthread_mutexattr_t attr;
180f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans
190f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	if (pthread_mutexattr_init(&attr) != 0)
200f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans		return (true);
21a2be4779b19a491a5686cfda067f98d7d70a9056Jason Evans	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
220f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	if (pthread_mutex_init(&mtx->lock, &attr) != 0) {
230f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans		pthread_mutexattr_destroy(&attr);
240f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans		return (true);
250f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	}
260f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	pthread_mutexattr_destroy(&attr);
270f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#endif
280f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	return (false);
290f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans}
300f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans
310f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evansvoid
320f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evansmtx_fini(mtx_t *mtx)
330f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans{
340f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans
350f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#ifdef _WIN32
360f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#elif (defined(JEMALLOC_OSSPIN))
370f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#else
380f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	pthread_mutex_destroy(&mtx->lock);
390f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#endif
400f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans}
410f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans
420f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evansvoid
430f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evansmtx_lock(mtx_t *mtx)
440f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans{
450f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans
460f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#ifdef _WIN32
470f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	EnterCriticalSection(&mtx->lock);
480f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#elif (defined(JEMALLOC_OSSPIN))
490f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	OSSpinLockLock(&mtx->lock);
500f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#else
510f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	pthread_mutex_lock(&mtx->lock);
520f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#endif
530f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans}
540f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans
550f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evansvoid
560f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evansmtx_unlock(mtx_t *mtx)
570f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans{
580f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans
590f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#ifdef _WIN32
600f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	LeaveCriticalSection(&mtx->lock);
610f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#elif (defined(JEMALLOC_OSSPIN))
620f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	OSSpinLockUnlock(&mtx->lock);
630f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#else
640f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans	pthread_mutex_unlock(&mtx->lock);
650f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans#endif
660f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9Jason Evans}
67