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.
This commit is contained in:
62
test/src/mtx.c
Normal file
62
test/src/mtx.c
Normal file
@@ -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
|
||||
}
|
35
test/src/thd.c
Normal file
35
test/src/thd.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
void
|
||||
thd_create(thd_t *thd, void *(*proc)(void *), void *arg)
|
||||
{
|
||||
LPTHREAD_START_ROUTINE routine = (LPTHREAD_START_ROUTINE)proc;
|
||||
*thd = CreateThread(NULL, 0, routine, arg, 0, NULL);
|
||||
if (*thd == NULL)
|
||||
test_fail("Error in CreateThread()\n");
|
||||
}
|
||||
|
||||
void
|
||||
thd_join(thd_t thd, void **ret)
|
||||
{
|
||||
|
||||
WaitForSingleObject(thd, INFINITE);
|
||||
}
|
||||
|
||||
#else
|
||||
void
|
||||
thd_create(thd_t *thd, void *(*proc)(void *), void *arg)
|
||||
{
|
||||
|
||||
if (pthread_create(thd, NULL, proc, arg) != 0)
|
||||
test_fail("Error in pthread_create()\n");
|
||||
}
|
||||
|
||||
void
|
||||
thd_join(thd_t thd, void **ret)
|
||||
{
|
||||
|
||||
pthread_join(thd, ret);
|
||||
}
|
||||
#endif
|
@@ -1,35 +0,0 @@
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
void
|
||||
je_thread_create(je_thread_t *thread, void *(*proc)(void *), void *arg)
|
||||
{
|
||||
LPTHREAD_START_ROUTINE routine = (LPTHREAD_START_ROUTINE)proc;
|
||||
*thread = CreateThread(NULL, 0, routine, arg, 0, NULL);
|
||||
if (*thread == NULL)
|
||||
test_fail("Error in CreateThread()\n");
|
||||
}
|
||||
|
||||
void
|
||||
je_thread_join(je_thread_t thread, void **ret)
|
||||
{
|
||||
|
||||
WaitForSingleObject(thread, INFINITE);
|
||||
}
|
||||
|
||||
#else
|
||||
void
|
||||
je_thread_create(je_thread_t *thread, void *(*proc)(void *), void *arg)
|
||||
{
|
||||
|
||||
if (pthread_create(thread, NULL, proc, arg) != 0)
|
||||
test_fail("Error in pthread_create()\n");
|
||||
}
|
||||
|
||||
void
|
||||
je_thread_join(je_thread_t thread, void **ret)
|
||||
{
|
||||
|
||||
pthread_join(thread, ret);
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user