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