summaryrefslogtreecommitdiffstats
path: root/test/src/mtx.c
diff options
context:
space:
mode:
authorJason Evans <je@fb.com>2013-12-12 22:41:02 (GMT)
committerJason Evans <je@fb.com>2013-12-12 22:41:02 (GMT)
commit0f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9 (patch)
tree8880cd7c807260d0e585ca1b1c263342758d9574 /test/src/mtx.c
parent19609724f9dce1ac644b6cbf89acb740319eb498 (diff)
downloadjemalloc-0f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9.zip
jemalloc-0f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9.tar.gz
jemalloc-0f4f1efd94d33a4bbf766d3d4e7e349fa7c0d3b9.tar.bz2
Add mq (message queue) to test infrastructure.
Add mtx (mutex) to test infrastructure, in order to avoid bootstrapping complications that would result from directly using malloc_mutex. Rename test infrastructure's thread abstraction from je_thread to thd. Fix some header ordering issues.
Diffstat (limited to 'test/src/mtx.c')
-rw-r--r--test/src/mtx.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/test/src/mtx.c b/test/src/mtx.c
new file mode 100644
index 0000000..8d9e0ca
--- /dev/null
+++ b/test/src/mtx.c
@@ -0,0 +1,62 @@
+#include "test/jemalloc_test.h"
+
+bool
+mtx_init(mtx_t *mtx)
+{
+
+#ifdef _WIN32
+ if (!InitializeCriticalSectionAndSpinCount(&mtx->lock, _CRT_SPINCOUNT))
+ return (true);
+#elif (defined(JEMALLOC_OSSPIN))
+ mtx->lock = 0;
+#else
+ pthread_mutexattr_t attr;
+
+ if (pthread_mutexattr_init(&attr) != 0)
+ return (true);
+ pthread_mutexattr_settype(&attr, MALLOC_MUTEX_TYPE);
+ 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_OSSPIN))
+#else
+ pthread_mutex_destroy(&mtx->lock);
+#endif
+}
+
+void
+mtx_lock(mtx_t *mtx)
+{
+
+#ifdef _WIN32
+ EnterCriticalSection(&mtx->lock);
+#elif (defined(JEMALLOC_OSSPIN))
+ OSSpinLockLock(&mtx->lock);
+#else
+ pthread_mutex_lock(&mtx->lock);
+#endif
+}
+
+void
+mtx_unlock(mtx_t *mtx)
+{
+
+#ifdef _WIN32
+ LeaveCriticalSection(&mtx->lock);
+#elif (defined(JEMALLOC_OSSPIN))
+ OSSpinLockUnlock(&mtx->lock);
+#else
+ pthread_mutex_unlock(&mtx->lock);
+#endif
+}