jemalloc/test/src/mtx.c
Kevin Svetlitski da66aa391f Enable a few additional warnings for CI and fix the issues they uncovered
- `-Wmissing-prototypes` and `-Wmissing-variable-declarations` are
  helpful for finding dead code and/or things that should be `static`
  but aren't marked as such.
- `-Wunused-macros` is of similar utility, but for identifying dead macros.
- `-Wunreachable-code` and `-Wunreachable-code-aggressive` do exactly
  what they say: flag unreachable code.
2023-08-11 13:56:23 -07:00

61 lines
1.2 KiB
C

#include "test/jemalloc_test.h"
#if defined(_WIN32) && !defined(_CRT_SPINCOUNT)
#define _CRT_SPINCOUNT 4000
#endif
bool
mtx_init(mtx_t *mtx) {
#ifdef _WIN32
if (!InitializeCriticalSectionAndSpinCount(&mtx->lock,
_CRT_SPINCOUNT)) {
return true;
}
#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
mtx->lock = OS_UNFAIR_LOCK_INIT;
#else
pthread_mutexattr_t attr;
if (pthread_mutexattr_init(&attr) != 0) {
return true;
}
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
if (pthread_mutex_init(&mtx->lock, &attr) != 0) {
pthread_mutexattr_destroy(&attr);
return true;
}
pthread_mutexattr_destroy(&attr);
#endif
return false;
}
void
mtx_fini(mtx_t *mtx) {
#ifdef _WIN32
#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
#else
pthread_mutex_destroy(&mtx->lock);
#endif
}
void
mtx_lock(mtx_t *mtx) {
#ifdef _WIN32
EnterCriticalSection(&mtx->lock);
#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
os_unfair_lock_lock(&mtx->lock);
#else
pthread_mutex_lock(&mtx->lock);
#endif
}
void
mtx_unlock(mtx_t *mtx) {
#ifdef _WIN32
LeaveCriticalSection(&mtx->lock);
#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))
os_unfair_lock_unlock(&mtx->lock);
#else
pthread_mutex_unlock(&mtx->lock);
#endif
}