[threading] Add debug utilities
This commit is contained in:
+7
-3
@@ -127,9 +127,13 @@ POOL_ctx* POOL_create_advanced(size_t numThreads, size_t queueSize,
|
|||||||
ctx->queueTail = 0;
|
ctx->queueTail = 0;
|
||||||
ctx->numThreadsBusy = 0;
|
ctx->numThreadsBusy = 0;
|
||||||
ctx->queueEmpty = 1;
|
ctx->queueEmpty = 1;
|
||||||
(void)ZSTD_pthread_mutex_init(&ctx->queueMutex, NULL);
|
{
|
||||||
(void)ZSTD_pthread_cond_init(&ctx->queuePushCond, NULL);
|
int error = 0;
|
||||||
(void)ZSTD_pthread_cond_init(&ctx->queuePopCond, NULL);
|
error |= ZSTD_pthread_mutex_init(&ctx->queueMutex, NULL);
|
||||||
|
error |= ZSTD_pthread_cond_init(&ctx->queuePushCond, NULL);
|
||||||
|
error |= ZSTD_pthread_cond_init(&ctx->queuePopCond, NULL);
|
||||||
|
if (error) { POOL_free(ctx); return NULL; }
|
||||||
|
}
|
||||||
ctx->shutdown = 0;
|
ctx->shutdown = 0;
|
||||||
/* Allocate space for the thread handles */
|
/* Allocate space for the thread handles */
|
||||||
ctx->threads = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), customMem);
|
ctx->threads = (ZSTD_pthread_t*)ZSTD_malloc(numThreads * sizeof(ZSTD_pthread_t), customMem);
|
||||||
|
|||||||
+46
-1
@@ -14,6 +14,8 @@
|
|||||||
* This file will hold wrapper for systems, which do not support pthreads
|
* This file will hold wrapper for systems, which do not support pthreads
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "threading.h"
|
||||||
|
|
||||||
/* create fake symbol to avoid empty translation unit warning */
|
/* create fake symbol to avoid empty translation unit warning */
|
||||||
int g_ZSTD_threading_useless_symbol;
|
int g_ZSTD_threading_useless_symbol;
|
||||||
|
|
||||||
@@ -28,7 +30,6 @@ int g_ZSTD_threading_useless_symbol;
|
|||||||
/* === Dependencies === */
|
/* === Dependencies === */
|
||||||
#include <process.h>
|
#include <process.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include "threading.h"
|
|
||||||
|
|
||||||
|
|
||||||
/* === Implementation === */
|
/* === Implementation === */
|
||||||
@@ -73,3 +74,47 @@ int ZSTD_pthread_join(ZSTD_pthread_t thread, void **value_ptr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif /* ZSTD_MULTITHREAD */
|
#endif /* ZSTD_MULTITHREAD */
|
||||||
|
|
||||||
|
#if defined(ZSTD_MULTITHREAD) && DEBUGLEVEL >= 1 && !defined(_WIN32)
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr)
|
||||||
|
{
|
||||||
|
*mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
|
||||||
|
if (!*mutex)
|
||||||
|
return 1;
|
||||||
|
return pthread_mutex_init(*mutex, attr);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex)
|
||||||
|
{
|
||||||
|
if (!*mutex)
|
||||||
|
return 0;
|
||||||
|
{
|
||||||
|
int const ret = pthread_mutex_destroy(*mutex);
|
||||||
|
free(*mutex);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr)
|
||||||
|
{
|
||||||
|
*cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t));
|
||||||
|
if (!*cond)
|
||||||
|
return 1;
|
||||||
|
return pthread_cond_init(*cond, attr);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond)
|
||||||
|
{
|
||||||
|
if (!*cond)
|
||||||
|
return 0;
|
||||||
|
{
|
||||||
|
int const ret = pthread_cond_destroy(*cond);
|
||||||
|
free(*cond);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
+32
-1
@@ -13,6 +13,8 @@
|
|||||||
#ifndef THREADING_H_938743
|
#ifndef THREADING_H_938743
|
||||||
#define THREADING_H_938743
|
#define THREADING_H_938743
|
||||||
|
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
#if defined (__cplusplus)
|
#if defined (__cplusplus)
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
@@ -75,10 +77,12 @@ int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr);
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */
|
#elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */
|
||||||
/* === POSIX Systems === */
|
/* === POSIX Systems === */
|
||||||
# include <pthread.h>
|
# include <pthread.h>
|
||||||
|
|
||||||
|
#if DEBUGLEVEL < 1
|
||||||
|
|
||||||
#define ZSTD_pthread_mutex_t pthread_mutex_t
|
#define ZSTD_pthread_mutex_t pthread_mutex_t
|
||||||
#define ZSTD_pthread_mutex_init(a, b) pthread_mutex_init((a), (b))
|
#define ZSTD_pthread_mutex_init(a, b) pthread_mutex_init((a), (b))
|
||||||
#define ZSTD_pthread_mutex_destroy(a) pthread_mutex_destroy((a))
|
#define ZSTD_pthread_mutex_destroy(a) pthread_mutex_destroy((a))
|
||||||
@@ -96,6 +100,33 @@ int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr);
|
|||||||
#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
|
#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
|
||||||
#define ZSTD_pthread_join(a, b) pthread_join((a),(b))
|
#define ZSTD_pthread_join(a, b) pthread_join((a),(b))
|
||||||
|
|
||||||
|
#else /* DEBUGLEVEL >= 1 */
|
||||||
|
|
||||||
|
/* Debug implementation of threading.
|
||||||
|
* In this implementation we use pointers for mutexes and condition variables.
|
||||||
|
* This way, if we forget to init/destroy them the program will crash or ASAN
|
||||||
|
* will report leaks.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define ZSTD_pthread_mutex_t pthread_mutex_t*
|
||||||
|
int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr);
|
||||||
|
int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex);
|
||||||
|
#define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock(*(a))
|
||||||
|
#define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock(*(a))
|
||||||
|
|
||||||
|
#define ZSTD_pthread_cond_t pthread_cond_t*
|
||||||
|
int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr);
|
||||||
|
int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond);
|
||||||
|
#define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait(*(a), *(b))
|
||||||
|
#define ZSTD_pthread_cond_signal(a) pthread_cond_signal(*(a))
|
||||||
|
#define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast(*(a))
|
||||||
|
|
||||||
|
#define ZSTD_pthread_t pthread_t
|
||||||
|
#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
|
||||||
|
#define ZSTD_pthread_join(a, b) pthread_join((a),(b))
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#else /* ZSTD_MULTITHREAD not defined */
|
#else /* ZSTD_MULTITHREAD not defined */
|
||||||
/* No multithreading support */
|
/* No multithreading support */
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -46,7 +46,7 @@ static int testOrder(size_t numThreads, size_t queueSize)
|
|||||||
POOL_ctx* const ctx = POOL_create(numThreads, queueSize);
|
POOL_ctx* const ctx = POOL_create(numThreads, queueSize);
|
||||||
ASSERT_TRUE(ctx);
|
ASSERT_TRUE(ctx);
|
||||||
data.i = 0;
|
data.i = 0;
|
||||||
(void)ZSTD_pthread_mutex_init(&data.mutex, NULL);
|
ASSERT_FALSE(ZSTD_pthread_mutex_init(&data.mutex, NULL));
|
||||||
{ size_t i;
|
{ size_t i;
|
||||||
for (i = 0; i < 16; ++i) {
|
for (i = 0; i < 16; ++i) {
|
||||||
POOL_add(ctx, &fn, &data);
|
POOL_add(ctx, &fn, &data);
|
||||||
|
|||||||
Reference in New Issue
Block a user