forked from Maratyszcza/pthreadpool
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy paththreadpool-atomics.h
More file actions
381 lines (331 loc) · 12.7 KB
/
threadpool-atomics.h
File metadata and controls
381 lines (331 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright (c) 2017 Facebook Inc.
// Copyright (c) 2015-2017 Georgia Institute of Technology
// All rights reserved.
//
// Copyright 2019 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
#ifndef __PTHREADPOOL_SRC_THREADPOOL_ATOMICS_H_
#define __PTHREADPOOL_SRC_THREADPOOL_ATOMICS_H_
/* Standard C headers */
#include <stdatomic.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
/* Windows-specific headers */
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#else
#include <sched.h>
#endif
/* SSE-specific headers */
#if defined(__i386__) || defined(__i686__) || defined(__x86_64__) || \
defined(_M_IX86) || defined(_M_X64) && !defined(_M_ARM64EC)
#include <xmmintrin.h>
#endif
/* ARM-specific headers */
#if defined(__ARM_ACLE)
#include <arm_acle.h>
#endif
/* MSVC-specific headers */
#ifdef _MSC_VER
#include <intrin.h>
#endif
/* Configuration header */
#include "threadpool-common.h"
/* POSIX headers */
#if PTHREADPOOL_USE_PTHREADS
#include <pthread.h>
#else
#include <threads.h>
#endif
#if PTHREADPOOL_USE_PTHREADS
typedef pthread_t pthreadpool_thread_t;
typedef pthread_mutex_t pthreadpool_mutex_t;
typedef pthread_cond_t pthreadpool_cond_t;
typedef void* pthreadpool_thread_return_t;
static inline void pthreadpool_mutex_init(pthreadpool_mutex_t* mutex) {
pthread_mutex_init(mutex, NULL);
}
static inline void pthreadpool_mutex_destroy(pthreadpool_mutex_t* mutex) {
pthread_mutex_destroy(mutex);
}
static inline void pthreadpool_mutex_lock(pthreadpool_mutex_t* mutex) {
pthread_mutex_lock(mutex);
}
static inline void pthreadpool_mutex_unlock(pthreadpool_mutex_t* mutex) {
pthread_mutex_unlock(mutex);
}
static inline void pthreadpool_cond_init(pthreadpool_cond_t* cond) {
pthread_cond_init(cond, NULL);
}
static inline void pthreadpool_cond_destroy(pthreadpool_cond_t* cond) {
pthread_cond_destroy(cond);
}
static inline void pthreadpool_cond_wait(pthreadpool_cond_t* cond,
pthreadpool_mutex_t* mutex) {
pthread_cond_wait(cond, mutex);
}
static inline void pthreadpool_cond_signal(pthreadpool_cond_t* cond) {
pthread_cond_signal(cond);
}
static inline void pthreadpool_cond_broadcast(pthreadpool_cond_t* cond) {
pthread_cond_broadcast(cond);
}
static inline void pthreadpool_thread_create(
pthreadpool_thread_t* thread, pthreadpool_thread_return_t(fun)(void*),
void* arg) {
pthread_create(thread, NULL, fun, arg);
}
static inline void pthreadpool_thread_join(
pthreadpool_thread_t thread, pthreadpool_thread_return_t* return_value) {
pthread_join(thread, return_value);
}
#else
typedef thrd_t pthreadpool_thread_t;
typedef mtx_t pthreadpool_mutex_t;
typedef cnd_t pthreadpool_cond_t;
typedef int pthreadpool_thread_return_t;
static inline void pthreadpool_mutex_init(pthreadpool_mutex_t* mutex) {
mtx_init(mutex, mtx_plain);
}
static inline void pthreadpool_mutex_destroy(pthreadpool_mutex_t* mutex) {
mtx_destroy(mutex);
}
static inline void pthreadpool_mutex_lock(pthreadpool_mutex_t* mutex) {
mtx_lock(mutex);
}
static inline void pthreadpool_mutex_unlock(pthreadpool_mutex_t* mutex) {
mtx_unlock(mutex);
}
static inline void pthreadpool_cond_init(pthreadpool_cond_t* cond) {
cnd_init(cond);
}
static inline void pthreadpool_cond_destroy(pthreadpool_cond_t* cond) {
cnd_destroy(cond);
}
static inline void pthreadpool_cond_wait(pthreadpool_cond_t* cond,
pthreadpool_mutex_t* mutex) {
cnd_wait(cond, mutex);
}
static inline void pthreadpool_cond_signal(pthreadpool_cond_t* cond) {
cnd_signal(cond);
}
static inline void pthreadpool_cond_broadcast(pthreadpool_cond_t* cond) {
cnd_broadcast(cond);
}
static inline void pthreadpool_thread_create(
pthreadpool_thread_t* thread, pthreadpool_thread_return_t(fun)(void*),
void* arg) {
thrd_create(thread, fun, arg);
}
static inline void pthreadpool_thread_join(
pthreadpool_thread_t thread, pthreadpool_thread_return_t* return_value) {
thrd_join(thread, return_value);
}
#endif // PTHREADPOOL_USE_PTHREADS
/* Align the atomic values on the size of a cache line to avoid false sharing,
* i.e. two or more atomic variables sharing the same cache line will block
* each other during atomic operations.
*/
typedef atomic_uint_fast32_t PTHREADPOOL_CACHELINE_ALIGNED
pthreadpool_atomic_uint32_t;
typedef atomic_int_least32_t PTHREADPOOL_CACHELINE_ALIGNED
pthreadpool_atomic_int32_t;
typedef atomic_size_t PTHREADPOOL_CACHELINE_ALIGNED pthreadpool_atomic_size_t;
typedef atomic_uintptr_t PTHREADPOOL_CACHELINE_ALIGNED
pthreadpool_atomic_void_p;
static inline int32_t pthreadpool_load_relaxed_int32_t(
pthreadpool_atomic_int32_t* address) {
return atomic_load_explicit(address, memory_order_relaxed);
}
static inline uint32_t pthreadpool_load_relaxed_uint32_t(
pthreadpool_atomic_uint32_t* address) {
return atomic_load_explicit(address, memory_order_relaxed);
}
static inline size_t pthreadpool_load_relaxed_size_t(
pthreadpool_atomic_size_t* address) {
return atomic_load_explicit(address, memory_order_relaxed);
}
static inline void* pthreadpool_load_relaxed_void_p(
pthreadpool_atomic_void_p* address) {
return (void*)atomic_load_explicit(address, memory_order_relaxed);
}
static inline uint32_t pthreadpool_load_acquire_uint32_t(
pthreadpool_atomic_uint32_t* address) {
return atomic_load_explicit(address, memory_order_acquire);
}
static inline uint32_t pthreadpool_load_consume_uint32_t(
pthreadpool_atomic_uint32_t* address) {
return atomic_load_explicit(address, memory_order_consume);
}
static inline uint32_t pthreadpool_load_sequentially_consistent_uint32_t(
pthreadpool_atomic_uint32_t* address) {
return atomic_load_explicit(address, memory_order_seq_cst);
}
static inline int32_t pthreadpool_load_acquire_int32_t(
pthreadpool_atomic_int32_t* address) {
return atomic_load_explicit(address, memory_order_acquire);
}
static inline int32_t pthreadpool_load_consume_int32_t(
pthreadpool_atomic_int32_t* address) {
return atomic_load_explicit(address, memory_order_consume);
}
static inline uint32_t pthreadpool_load_sequentially_consistent_int32_t(
pthreadpool_atomic_int32_t* address) {
return atomic_load_explicit(address, memory_order_seq_cst);
}
static inline size_t pthreadpool_load_acquire_size_t(
pthreadpool_atomic_size_t* address) {
return atomic_load_explicit(address, memory_order_acquire);
}
static inline void pthreadpool_store_relaxed_uint32_t(
pthreadpool_atomic_uint32_t* address, uint32_t value) {
atomic_store_explicit(address, value, memory_order_relaxed);
}
static inline void pthreadpool_store_relaxed_size_t(
pthreadpool_atomic_size_t* address, size_t value) {
atomic_store_explicit(address, value, memory_order_relaxed);
}
static inline void pthreadpool_store_relaxed_void_p(
pthreadpool_atomic_void_p* address, void* value) {
atomic_store_explicit(address, (uintptr_t)value, memory_order_relaxed);
}
static inline void pthreadpool_store_release_uint32_t(
pthreadpool_atomic_uint32_t* address, uint32_t value) {
atomic_store_explicit(address, value, memory_order_release);
}
static inline void pthreadpool_store_sequentially_consistent_uint32_t(
pthreadpool_atomic_uint32_t* address, uint32_t value) {
atomic_store_explicit(address, value, memory_order_seq_cst);
}
static inline void pthreadpool_store_release_int32_t(
pthreadpool_atomic_int32_t* address, int32_t value) {
atomic_store_explicit(address, value, memory_order_release);
}
static inline void pthreadpool_store_sequentially_consistent_int32_t(
pthreadpool_atomic_int32_t* address, int32_t value) {
atomic_store_explicit(address, value, memory_order_seq_cst);
}
static inline void pthreadpool_store_release_size_t(
pthreadpool_atomic_size_t* address, size_t value) {
atomic_store_explicit(address, value, memory_order_release);
}
static inline size_t pthreadpool_decrement_fetch_relaxed_size_t(
pthreadpool_atomic_size_t* address) {
return atomic_fetch_sub_explicit(address, 1, memory_order_relaxed) - 1;
}
static inline size_t pthreadpool_decrement_n_fetch_relaxed_size_t(
pthreadpool_atomic_size_t* address, size_t n) {
return atomic_fetch_sub_explicit(address, n, memory_order_relaxed) - n;
}
static inline size_t pthreadpool_fetch_decrement_n_relaxed_size_t(
pthreadpool_atomic_size_t* address, size_t n) {
return atomic_fetch_sub_explicit(address, n, memory_order_relaxed);
}
static inline size_t pthreadpool_decrement_fetch_release_size_t(
pthreadpool_atomic_size_t* address) {
return atomic_fetch_sub_explicit(address, 1, memory_order_release) - 1;
}
static inline size_t pthreadpool_decrement_fetch_acquire_release_size_t(
pthreadpool_atomic_size_t* address) {
return atomic_fetch_sub_explicit(address, 1, memory_order_acq_rel) - 1;
}
static inline uint32_t pthreadpool_decrement_fetch_acquire_release_uint32_t(
pthreadpool_atomic_uint32_t* address) {
return atomic_fetch_sub_explicit(address, 1, memory_order_acq_rel) - 1;
}
static inline bool pthreadpool_try_decrement_relaxed_size_t(
pthreadpool_atomic_size_t* value) {
size_t actual_value = atomic_load_explicit(value, memory_order_acquire);
while (actual_value != 0) {
if (atomic_compare_exchange_strong_explicit(
value, &actual_value, actual_value - 1, memory_order_relaxed,
memory_order_relaxed)) {
return true;
}
}
return false;
}
static inline size_t pthreadpool_fetch_add_relaxed_size_t(
pthreadpool_atomic_size_t* address, size_t value) {
return atomic_fetch_add_explicit(address, value, memory_order_relaxed);
}
static inline uint32_t pthreadpool_fetch_add_acquire_release_uint32_t(
pthreadpool_atomic_uint32_t* address, uint32_t value) {
return atomic_fetch_add_explicit(address, value, memory_order_acq_rel);
}
static inline int32_t pthreadpool_fetch_add_acquire_release_int32_t(
pthreadpool_atomic_int32_t* address, uint32_t value) {
return atomic_fetch_add_explicit(address, value, memory_order_acq_rel);
}
static inline int32_t pthreadpool_fetch_add_sequentially_consistent_int32_t(
pthreadpool_atomic_int32_t* address, uint32_t value) {
return atomic_fetch_add_explicit(address, value, memory_order_seq_cst);
}
static inline uint32_t pthreadpool_exchange_acquire_uint32_t(
pthreadpool_atomic_uint32_t* address, uint32_t value) {
return atomic_exchange_explicit(address, value, memory_order_acquire);
}
static inline int32_t pthreadpool_exchange_release_int32_t(
pthreadpool_atomic_int32_t* address, uint32_t value) {
return atomic_exchange_explicit(address, value, memory_order_release);
}
static inline uint32_t pthreadpool_exchange_acquire_release_uint32_t(
pthreadpool_atomic_uint32_t* address, uint32_t value) {
return atomic_exchange_explicit(address, value, memory_order_acq_rel);
}
static inline uint32_t pthreadpool_exchange_sequentially_consistent_uint32_t(
pthreadpool_atomic_uint32_t* address, uint32_t value) {
return atomic_exchange_explicit(address, value, memory_order_seq_cst);
}
static inline bool pthreadpool_compare_exchange_acquire_release_int32_t(
pthreadpool_atomic_int32_t* address, int_least32_t* expected_value,
int32_t new_value) {
return atomic_compare_exchange_strong_explicit(address, expected_value,
new_value, memory_order_acq_rel,
memory_order_acquire);
}
static inline bool pthreadpool_compare_exchange_sequentially_consistent_int32_t(
pthreadpool_atomic_int32_t* address, int_least32_t* expected_value,
int32_t new_value) {
return atomic_compare_exchange_strong_explicit(address, expected_value,
new_value, memory_order_seq_cst,
memory_order_seq_cst);
}
static inline void pthreadpool_fence_acquire() {
atomic_thread_fence(memory_order_acquire);
}
static inline void pthreadpool_fence_release() {
atomic_thread_fence(memory_order_release);
}
static inline void pthreadpool_yield(uint32_t step) {
if (step < PTHREADPOOL_SPIN_PAUSE_ITERATIONS) {
#if defined(__ARM_ACLE) || \
defined(_MSC_VER) && \
(defined(_M_ARM) || defined(_M_ARM64) || defined(_M_ARM64EC))
__yield();
#elif defined(__GNUC__) && \
(defined(__ARM_ARCH) && (__ARM_ARCH >= 7) || \
(defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6KZ__)) && \
!defined(__thumb__))
__asm__ __volatile__("yield");
#elif defined(__i386__) || defined(__i686__) || defined(__x86_64__) || \
defined(_M_IX86) || defined(_M_X64)
_mm_pause();
#else
pthreadpool_fence_acquire();
#endif
} else {
#ifdef _WIN32
Sleep(0);
#else
sched_yield();
#endif
}
}
#endif // __PTHREADPOOL_SRC_THREADPOOL_ATOMICS_H_