Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmzstd/lib/common/threading.h
5002 views
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(ZSTD_MULTITHREAD) && defined(_WIN32)
20
21
/**
22
* Windows minimalist Pthread Wrapper
23
*/
24
#ifdef WINVER
25
# undef WINVER
26
#endif
27
#define WINVER 0x0600
28
29
#ifdef _WIN32_WINNT
30
# undef _WIN32_WINNT
31
#endif
32
#define _WIN32_WINNT 0x0600
33
34
#ifndef WIN32_LEAN_AND_MEAN
35
# define WIN32_LEAN_AND_MEAN
36
#endif
37
38
#undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */
39
#include <windows.h>
40
#undef ERROR
41
#define ERROR(name) ZSTD_ERROR(name)
42
43
44
/* mutex */
45
#define ZSTD_pthread_mutex_t CRITICAL_SECTION
46
#define ZSTD_pthread_mutex_init(a, b) ((void)(b), InitializeCriticalSection((a)), 0)
47
#define ZSTD_pthread_mutex_destroy(a) DeleteCriticalSection((a))
48
#define ZSTD_pthread_mutex_lock(a) EnterCriticalSection((a))
49
#define ZSTD_pthread_mutex_unlock(a) LeaveCriticalSection((a))
50
51
/* condition variable */
52
#define ZSTD_pthread_cond_t CONDITION_VARIABLE
53
#define ZSTD_pthread_cond_init(a, b) ((void)(b), InitializeConditionVariable((a)), 0)
54
#define ZSTD_pthread_cond_destroy(a) ((void)(a))
55
#define ZSTD_pthread_cond_wait(a, b) SleepConditionVariableCS((a), (b), INFINITE)
56
#define ZSTD_pthread_cond_signal(a) WakeConditionVariable((a))
57
#define ZSTD_pthread_cond_broadcast(a) WakeAllConditionVariable((a))
58
59
/* ZSTD_pthread_create() and ZSTD_pthread_join() */
60
typedef HANDLE ZSTD_pthread_t;
61
62
int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
63
void* (*start_routine) (void*), void* arg);
64
65
int ZSTD_pthread_join(ZSTD_pthread_t thread);
66
67
/**
68
* add here more wrappers as required
69
*/
70
71
#elif defined(ZSTD_MULTITHREAD) /* posix assumed ; need a better detection method */
72
/* === POSIX Systems === */
73
# include <pthread.h>
74
75
#if DEBUGLEVEL < 1
76
77
#define ZSTD_pthread_mutex_t pthread_mutex_t
78
#define ZSTD_pthread_mutex_init(a, b) pthread_mutex_init((a), (b))
79
#define ZSTD_pthread_mutex_destroy(a) pthread_mutex_destroy((a))
80
#define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock((a))
81
#define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock((a))
82
83
#define ZSTD_pthread_cond_t pthread_cond_t
84
#define ZSTD_pthread_cond_init(a, b) pthread_cond_init((a), (b))
85
#define ZSTD_pthread_cond_destroy(a) pthread_cond_destroy((a))
86
#define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait((a), (b))
87
#define ZSTD_pthread_cond_signal(a) pthread_cond_signal((a))
88
#define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast((a))
89
90
#define ZSTD_pthread_t pthread_t
91
#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
92
#define ZSTD_pthread_join(a) pthread_join((a),NULL)
93
94
#else /* DEBUGLEVEL >= 1 */
95
96
/* Debug implementation of threading.
97
* In this implementation we use pointers for mutexes and condition variables.
98
* This way, if we forget to init/destroy them the program will crash or ASAN
99
* will report leaks.
100
*/
101
102
#define ZSTD_pthread_mutex_t pthread_mutex_t*
103
int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr);
104
int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex);
105
#define ZSTD_pthread_mutex_lock(a) pthread_mutex_lock(*(a))
106
#define ZSTD_pthread_mutex_unlock(a) pthread_mutex_unlock(*(a))
107
108
#define ZSTD_pthread_cond_t pthread_cond_t*
109
int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr);
110
int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond);
111
#define ZSTD_pthread_cond_wait(a, b) pthread_cond_wait(*(a), *(b))
112
#define ZSTD_pthread_cond_signal(a) pthread_cond_signal(*(a))
113
#define ZSTD_pthread_cond_broadcast(a) pthread_cond_broadcast(*(a))
114
115
#define ZSTD_pthread_t pthread_t
116
#define ZSTD_pthread_create(a, b, c, d) pthread_create((a), (b), (c), (d))
117
#define ZSTD_pthread_join(a) pthread_join((a),NULL)
118
119
#endif
120
121
#else /* ZSTD_MULTITHREAD not defined */
122
/* No multithreading support */
123
124
typedef int ZSTD_pthread_mutex_t;
125
#define ZSTD_pthread_mutex_init(a, b) ((void)(a), (void)(b), 0)
126
#define ZSTD_pthread_mutex_destroy(a) ((void)(a))
127
#define ZSTD_pthread_mutex_lock(a) ((void)(a))
128
#define ZSTD_pthread_mutex_unlock(a) ((void)(a))
129
130
typedef int ZSTD_pthread_cond_t;
131
#define ZSTD_pthread_cond_init(a, b) ((void)(a), (void)(b), 0)
132
#define ZSTD_pthread_cond_destroy(a) ((void)(a))
133
#define ZSTD_pthread_cond_wait(a, b) ((void)(a), (void)(b))
134
#define ZSTD_pthread_cond_signal(a) ((void)(a))
135
#define ZSTD_pthread_cond_broadcast(a) ((void)(a))
136
137
/* do not use ZSTD_pthread_t */
138
139
#endif /* ZSTD_MULTITHREAD */
140
141
142
#endif /* THREADING_H_938743 */
143
144