Project Alice
Loading...
Searching...
No Matches
threading.h
Go to the documentation of this file.
1
14#ifndef THREADING_H_938743
15#define THREADING_H_938743
16
17#include "debug.h"
18
19#if defined (__cplusplus)
20extern "C" {
21#endif
22
23#if defined(ZSTD_MULTITHREAD) && defined(_WIN32)
24
28#ifdef WINVER
29# undef WINVER
30#endif
31#define WINVER 0x0600
32
33#ifdef _WIN32_WINNT
34# undef _WIN32_WINNT
35#endif
36#define _WIN32_WINNT 0x0600
37
38#ifndef WIN32_LEAN_AND_MEAN
39# define WIN32_LEAN_AND_MEAN
40#endif
41
42#undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */
43#include <windows.h>
44#undef ERROR
45#define ERROR(name) ZSTD_ERROR(name)
46
47
48/* mutex */
49#define ZSTD_pthread_mutex_t CRITICAL_SECTION
50#define ZSTD_pthread_mutex_init(a, b) ((void)(b), InitializeCriticalSection((a)), 0)
51#define ZSTD_pthread_mutex_destroy(a) DeleteCriticalSection((a))
52#define ZSTD_pthread_mutex_lock(a) EnterCriticalSection((a))
53#define ZSTD_pthread_mutex_unlock(a) LeaveCriticalSection((a))
54
55/* condition variable */
56#define ZSTD_pthread_cond_t CONDITION_VARIABLE
57#define ZSTD_pthread_cond_init(a, b) ((void)(b), InitializeConditionVariable((a)), 0)
58#define ZSTD_pthread_cond_destroy(a) ((void)(a))
59#define ZSTD_pthread_cond_wait(a, b) SleepConditionVariableCS((a), (b), INFINITE)
60#define ZSTD_pthread_cond_signal(a) WakeConditionVariable((a))
61#define ZSTD_pthread_cond_broadcast(a) WakeAllConditionVariable((a))
62
63/* ZSTD_pthread_create() and ZSTD_pthread_join() */
64typedef HANDLE ZSTD_pthread_t;
65
66int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
67 void* (*start_routine) (void*), void* arg);
68
69int ZSTD_pthread_join(ZSTD_pthread_t thread);
70
76#elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */
77/* === POSIX Systems === */
78# include <pthread.h>
79
80#if DEBUGLEVEL < 1
81
82#define ZSTD_pthread_mutex_t pthread_mutex_t
83#define ZSTD_pthread_mutex_init(a, b) pthread_mutex_init((a), (b))
84#define ZSTD_pthread_mutex_destroy(a) pthread_mutex_destroy((a))
85#define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock((a))
86#define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock((a))
87
88#define ZSTD_pthread_cond_t pthread_cond_t
89#define ZSTD_pthread_cond_init(a, b) pthread_cond_init((a), (b))
90#define ZSTD_pthread_cond_destroy(a) pthread_cond_destroy((a))
91#define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait((a), (b))
92#define ZSTD_pthread_cond_signal(a) pthread_cond_signal((a))
93#define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast((a))
94
95#define ZSTD_pthread_t pthread_t
96#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
97#define ZSTD_pthread_join(a) pthread_join((a),NULL)
98
99#else /* DEBUGLEVEL >= 1 */
100
101/* Debug implementation of threading.
102 * In this implementation we use pointers for mutexes and condition variables.
103 * This way, if we forget to init/destroy them the program will crash or ASAN
104 * will report leaks.
105 */
106
107#define ZSTD_pthread_mutex_t pthread_mutex_t*
108int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr);
110#define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock(*(a))
111#define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock(*(a))
112
113#define ZSTD_pthread_cond_t pthread_cond_t*
114int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr);
116#define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait(*(a), *(b))
117#define ZSTD_pthread_cond_signal(a) pthread_cond_signal(*(a))
118#define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast(*(a))
119
120#define ZSTD_pthread_t pthread_t
121#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
122#define ZSTD_pthread_join(a) pthread_join((a),NULL)
123
124#endif
125
126#else /* ZSTD_MULTITHREAD not defined */
127/* No multithreading support */
128
130#define ZSTD_pthread_mutex_init(a, b) ((void)(a), (void)(b), 0)
131#define ZSTD_pthread_mutex_destroy(a) ((void)(a))
132#define ZSTD_pthread_mutex_lock(a) ((void)(a))
133#define ZSTD_pthread_mutex_unlock(a) ((void)(a))
134
136#define ZSTD_pthread_cond_init(a, b) ((void)(a), (void)(b), 0)
137#define ZSTD_pthread_cond_destroy(a) ((void)(a))
138#define ZSTD_pthread_cond_wait(a, b) ((void)(a), (void)(b))
139#define ZSTD_pthread_cond_signal(a) ((void)(a))
140#define ZSTD_pthread_cond_broadcast(a) ((void)(a))
141
142/* do not use ZSTD_pthread_t */
143
144#endif /* ZSTD_MULTITHREAD */
145
146#if defined (__cplusplus)
147}
148#endif
149
150#endif /* THREADING_H_938743 */
#define ZSTD_pthread_mutex_init(a, b)
Definition: threading.h:130
int ZSTD_pthread_mutex_t
Definition: threading.h:129
int ZSTD_pthread_cond_t
Definition: threading.h:135
#define ZSTD_pthread_mutex_destroy(a)
Definition: threading.h:131
#define ZSTD_pthread_cond_init(a, b)
Definition: threading.h:136
#define ZSTD_pthread_cond_destroy(a)
Definition: threading.h:137