Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[threading] Add debug utilities #1

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion lib/common/threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#ifndef THREADING_H_938743
#define THREADING_H_938743

#include "debug.h"

#if defined (__cplusplus)
extern "C" {
#endif
Expand Down Expand Up @@ -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 === */
# include <pthread.h>

#if DEBUGLEVEL < 1

#define ZSTD_pthread_mutex_t pthread_mutex_t
#define ZSTD_pthread_mutex_init(a, b) pthread_mutex_init((a), (b))
#define ZSTD_pthread_mutex_destroy(a) pthread_mutex_destroy((a))
Expand All @@ -96,6 +100,31 @@ 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_join(a, b) pthread_join((a),(b))

#else /* DEBUGLEVEL >= 1 */

#include <stdlib.h>

/* Use pointers for mutex/cond so we are alerted if we leak them. */

#define ZSTD_pthread_mutex_t pthread_mutex_t*
#define ZSTD_pthread_mutex_init(a, b) (*(a) = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)), *(a) ? pthread_mutex_init(*(a), (b)) : 1)
#define ZSTD_pthread_mutex_destroy(a) (pthread_mutex_destroy(*(a)), free(*(a)))
#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*
#define ZSTD_pthread_cond_init(a, b) (*(a) = (pthread_cond_t*)malloc(sizeof(pthread_cond_t)), *(a) ? pthread_cond_init(*(a), (b)) : 1)
#define ZSTD_pthread_cond_destroy(a) (pthread_cond_destroy(*(a)), free(*(a)))
#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 */
/* No multithreading support */

Expand Down