xref: /freebsd/sys/contrib/zstd/lib/common/threading.h (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 /**
2  * Copyright (c) 2016 Tino Reichardt
3  * All rights reserved.
4  *
5  * You can contact the author at:
6  * - zstdmt source repository: https://github.com/mcmilk/zstdmt
7  *
8  * This source code is licensed under both the BSD-style license (found in the
9  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
10  * in the COPYING file in the root directory of this source tree).
11  * You may select, at your option, one of the above-listed licenses.
12  */
13 
14 #ifndef THREADING_H_938743
15 #define THREADING_H_938743
16 
17 #include "debug.h"
18 
19 #if defined (__cplusplus)
20 extern "C" {
21 #endif
22 
23 #if defined(ZSTD_MULTITHREAD) && defined(_WIN32)
24 
25 /**
26  * Windows minimalist Pthread Wrapper, based on :
27  * http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
28  */
29 #ifdef WINVER
30 #  undef WINVER
31 #endif
32 #define WINVER       0x0600
33 
34 #ifdef _WIN32_WINNT
35 #  undef _WIN32_WINNT
36 #endif
37 #define _WIN32_WINNT 0x0600
38 
39 #ifndef WIN32_LEAN_AND_MEAN
40 #  define WIN32_LEAN_AND_MEAN
41 #endif
42 
43 #undef ERROR   /* reported already defined on VS 2015 (Rich Geldreich) */
44 #include <windows.h>
45 #undef ERROR
46 #define ERROR(name) ZSTD_ERROR(name)
47 
48 
49 /* mutex */
50 #define ZSTD_pthread_mutex_t           CRITICAL_SECTION
51 #define ZSTD_pthread_mutex_init(a, b)  ((void)(b), InitializeCriticalSection((a)), 0)
52 #define ZSTD_pthread_mutex_destroy(a)  DeleteCriticalSection((a))
53 #define ZSTD_pthread_mutex_lock(a)     EnterCriticalSection((a))
54 #define ZSTD_pthread_mutex_unlock(a)   LeaveCriticalSection((a))
55 
56 /* condition variable */
57 #define ZSTD_pthread_cond_t             CONDITION_VARIABLE
58 #define ZSTD_pthread_cond_init(a, b)    ((void)(b), InitializeConditionVariable((a)), 0)
59 #define ZSTD_pthread_cond_destroy(a)    ((void)(a))
60 #define ZSTD_pthread_cond_wait(a, b)    SleepConditionVariableCS((a), (b), INFINITE)
61 #define ZSTD_pthread_cond_signal(a)     WakeConditionVariable((a))
62 #define ZSTD_pthread_cond_broadcast(a)  WakeAllConditionVariable((a))
63 
64 /* ZSTD_pthread_create() and ZSTD_pthread_join() */
65 typedef struct {
66     HANDLE handle;
67     void* (*start_routine)(void*);
68     void* arg;
69 } ZSTD_pthread_t;
70 
71 int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
72                    void* (*start_routine) (void*), void* arg);
73 
74 int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr);
75 
76 /**
77  * add here more wrappers as required
78  */
79 
80 
81 #elif defined(ZSTD_MULTITHREAD)    /* posix assumed ; need a better detection method */
82 /* ===   POSIX Systems   === */
83 #  include <pthread.h>
84 
85 #if DEBUGLEVEL < 1
86 
87 #define ZSTD_pthread_mutex_t            pthread_mutex_t
88 #define ZSTD_pthread_mutex_init(a, b)   pthread_mutex_init((a), (b))
89 #define ZSTD_pthread_mutex_destroy(a)   pthread_mutex_destroy((a))
90 #define ZSTD_pthread_mutex_lock(a)      pthread_mutex_lock((a))
91 #define ZSTD_pthread_mutex_unlock(a)    pthread_mutex_unlock((a))
92 
93 #define ZSTD_pthread_cond_t             pthread_cond_t
94 #define ZSTD_pthread_cond_init(a, b)    pthread_cond_init((a), (b))
95 #define ZSTD_pthread_cond_destroy(a)    pthread_cond_destroy((a))
96 #define ZSTD_pthread_cond_wait(a, b)    pthread_cond_wait((a), (b))
97 #define ZSTD_pthread_cond_signal(a)     pthread_cond_signal((a))
98 #define ZSTD_pthread_cond_broadcast(a)  pthread_cond_broadcast((a))
99 
100 #define ZSTD_pthread_t                  pthread_t
101 #define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
102 #define ZSTD_pthread_join(a, b)         pthread_join((a),(b))
103 
104 #else /* DEBUGLEVEL >= 1 */
105 
106 /* Debug implementation of threading.
107  * In this implementation we use pointers for mutexes and condition variables.
108  * This way, if we forget to init/destroy them the program will crash or ASAN
109  * will report leaks.
110  */
111 
112 #define ZSTD_pthread_mutex_t            pthread_mutex_t*
113 int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr);
114 int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex);
115 #define ZSTD_pthread_mutex_lock(a)      pthread_mutex_lock(*(a))
116 #define ZSTD_pthread_mutex_unlock(a)    pthread_mutex_unlock(*(a))
117 
118 #define ZSTD_pthread_cond_t             pthread_cond_t*
119 int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr);
120 int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond);
121 #define ZSTD_pthread_cond_wait(a, b)    pthread_cond_wait(*(a), *(b))
122 #define ZSTD_pthread_cond_signal(a)     pthread_cond_signal(*(a))
123 #define ZSTD_pthread_cond_broadcast(a)  pthread_cond_broadcast(*(a))
124 
125 #define ZSTD_pthread_t                  pthread_t
126 #define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
127 #define ZSTD_pthread_join(a, b)         pthread_join((a),(b))
128 
129 #endif
130 
131 #else  /* ZSTD_MULTITHREAD not defined */
132 /* No multithreading support */
133 
134 typedef int ZSTD_pthread_mutex_t;
135 #define ZSTD_pthread_mutex_init(a, b)   ((void)(a), (void)(b), 0)
136 #define ZSTD_pthread_mutex_destroy(a)   ((void)(a))
137 #define ZSTD_pthread_mutex_lock(a)      ((void)(a))
138 #define ZSTD_pthread_mutex_unlock(a)    ((void)(a))
139 
140 typedef int ZSTD_pthread_cond_t;
141 #define ZSTD_pthread_cond_init(a, b)    ((void)(a), (void)(b), 0)
142 #define ZSTD_pthread_cond_destroy(a)    ((void)(a))
143 #define ZSTD_pthread_cond_wait(a, b)    ((void)(a), (void)(b))
144 #define ZSTD_pthread_cond_signal(a)     ((void)(a))
145 #define ZSTD_pthread_cond_broadcast(a)  ((void)(a))
146 
147 /* do not use ZSTD_pthread_t */
148 
149 #endif /* ZSTD_MULTITHREAD */
150 
151 #if defined (__cplusplus)
152 }
153 #endif
154 
155 #endif /* THREADING_H_938743 */
156