-
Notifications
You must be signed in to change notification settings - Fork 2
/
tc_thread.h
2376 lines (2293 loc) · 102 KB
/
tc_thread.h
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* tc_thread.h: Cross-platform threading & atomics.
*
* DEPENDS:
* VERSION: 0.2.0 (2021-07-10)
* LICENSE: CC0 & Boost (dual-licensed)
* AUTHOR: Tim Cas
* URL: https://github.com/darkuranium/tclib
*
* VERSION HISTORY:
* 0.2.2 fixed some instances of malloc() not using TC_MALLOC
* 0.2.1 fixed macros tcthread_atomic{32,sz}_{inc,dec}
* 0.2.0 implemented semaphores & RW locks
* 0.1.0 initial public release
*
* TODOs:
* - document semaphores, RW locks, and atomics
* - tcthread_atexit; maybe tcthread_cancel (+ destructors)?
* - initializers (if possible)
* - spinlocks
* - high-level threading: threadpool + job system
*
*
*
* Portable threading primitives & atomics.
*
* A single file should contain the following `#define` before including the header:
*
* #define TC_THREAD_IMPLEMENTATION
* #include "tc_thread.h"
*
* Currently supports Windows (MSVC, gcc, clang) and various POSIX platforms.
* Compilation should use the following options:
* - POSIX: `-pthread`
* - MSVC on Windows: One of `/MD`, `/MDd`, `/MT`, `/MTd` (see https://docs.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library)
* - gcc or clang (in gcc mode) on Windows: (no action needed)
* Contributions for support of other platforms are welcome.
*/
/* ========== API ==========
*
* The API follows the POSIX API quite closely (albeit not exactly).
*
*
* ========== UTIL ==========
*
* SYNOPSIS:
* uint32_t tcthread_get_cpu_count(void);
* RETURN VALUE:
* Number of *logical* processor cores, or `0` if value cannot be obtained.
* DESCRIPTION:
* Get the number of logical processor cores (# of hardware threads).
*
* Note that this may be higher than the number of physical cores in systems
* that use technologies such as hyper-threading.
*
* In multi-processor systems (SMP), this attempts to get the number of *all*
* such cores if it can; otherwise, it'll generally return only the number of
* the current processing group.
*
*
* ========== THREADING ==========
*
* SYNOPSIS:
* typedef void* tcthread_runner_t(void* udata);
* tcthread_t tcthread_create(size_t stack_size, tcthread_runner_t* runner, void* udata);
* bool tcthread_is_valid(tcthread_t thread);
* PARAMETERS:
* - stack_size: size of thread stack, or `0` to use a default
* - runner: function to run when the thread is created
* - udata: user data, passed to the function
* - thread: thread to check the validity of
* RETURN VALUE:
* - tcthread_runner_t: return value of this function will be passed onto `tcthread_join`
* - tcthread_create: the newly-created thread; use `tcthread_is_valid` to verify if creation was successful
* - tcthread_is_valid: whether the thread creation was successful; note that this will *not* catch other forms of invalid data, such as an uninitialized or already-joined `thread` value
* DESCRIPTION:
* Create a new thread, and run it immediately.
*
* Threads are created with `tcthread_create`, but should be verified with
* `tcthread_is_valid`. They are executed immediately (in a "running" state),
* and (on platforms that recognize the distinction) in a joinable state.
* SEE ALSO:
* - `tcthread_join` to join with a thread, and get the return value of `runner`
* - `tcthread_detach` to detach a thread, allowing it to run independently
*
*
* SYNOPSIS:
* void tcthread_join(tcthread_t thread, void** retval);
* PARAMETERS:
* - thread: thread to join with
* - retval: return value from user-supplied thread function; use `NULL` to discard
* DESCRIPTION:
* Wait for a thread to terminate, and join with it.
*
* If a thread has already terminated, this function returns immediately.
* Otherwise, it waits for the thread to terminate and returns once it has
* done so.
*
* After joining, the thread handle becomes invalid, and should not be used
* again. Attempting to call any function other than `tcthread_create` with
* said handle is undefined.
* SEE ALSO:
* - `tcthread_create`; `retval` is the return value of the `runner` function
* - `tcthread_detach` to detach a thread, allowing it to run independently
*
*
* SYNOPSIS:
* void tcthread_detach(tcthread_t thread);
* PARAMETERS:
* - thread: thread to detach
* - retval: return value from user-supplied thread function; use `NULL` to discard
* DESCRIPTION:
* Detach a thread, allowing it to run independently.
*
* When a detached thread terminates, its resources are released back to the
* system without the need for manual intervention.
*
* After detaching, the thread handle becomes invalid, and should not be used
* again. Attempting to call any function other than `tcthread_create` with
* said handle is undefined.
* TODO:
* Leaks memory on some platforms. This needs to be fixed.
* SEE ALSO:
* - `tcthread_create` to create a new thread
* - `tcthread_join` to join with a thread, allowing to fetch its return value
*
*
* SYNOPSIS:
* TC_HINT_NORETURN void tcthread_exit(void* retval);
* PARAMETERS:
* - retval: value to return from thread
* RETURN VALUE:
* This function never returns.
* DESCRIPTION:
* Terminate the calling thread, returning `retval` as the result.
*
* Warning: C++ destructors may or may not run, depending on platform. Do not
* use this function if destructors need to be ran; return from the thread
* normally in that case.
*
*
* SYNOPSIS:
* void tcthread_sleep(uint32_t ms);
* PARAMETERS:
* - ms: the amount of time to sleep, in milliseconds
* DESCRIPTION:
* Suspend execution of the current thread for an interval of time.
*
* Note that the amount of time may be longer than requested, due to rounding
* and system-scheduling reasons.
*
*
* SYNOPSIS:
* tcthread_t tcthread_self(void);
* RETURN VALUE:
* Returns a handle to own thread, or an invalid handle if this failed.
* DESCRIPTION:
* Get the handle to the currently-running thread.
*
* Value returned may be invalid if the operation failed or if the thread is
* one that was *not* created via `tcthread_create` (such as the main thread).
* TODO:
* Make this work with all threads, including those that were not created
* via `tcthread_create`.
* SEE ALSO:
* - `tcthread_is_valid` to verify is the returned handle is valid
*
*
* ========== MUTEXES ==========
*
* SYNOPSIS:
* tcthread_mutex_t tcthread_mutex_create(bool recursive);
* bool tcthread_mutex_is_valid(tcthread_mutex_t mutex);
* void tcthread_mutex_destroy(tcthread_mutex_t mutex);
* PARAMETERS:
* - recursive: whether to create a recursive mutex (which permits multiple locks from same thread) or not
* - mutex: mutex to check the validity of, or to destroy
* RETURN VALUE:
* - tcthread_mutex_create: the newly-created mutex; use `tcthread_mutex_is_valid` to verify if creation was successful
* - tcthread_mutex_is_valid: whether the mutex creation was successful; note that this will *not* catch other forms of invalid data, such as an uninitialized `mutex` value
* DESCRIPTION:
* Create a new optionally-recursive mutex.
*
* Recursive mutexes keep an internal reference count, so that they may be
* locked multiple times by the same thread. Attempting to do so with
* non-recursive mutexes is undefined.
*
* Attempting to destroy a mutex that is currently locked is undefined.
*
* Created mutex should be verified with `tcthread_mutex_is_valid`.
*
*
* SYNOPSIS:
* void tcthread_mutex_lock(tcthread_mutex_t mutex);
* bool tcthread_mutex_try_lock(tcthread_mutex_t mutex);
* void tcthread_mutex_unlock(tcthread_mutex_t mutex);
* PARAMETERS:
* - mutex: mutex to lock or unlock
* RETURN VALUE:
* - tcthread_mutex_try_lock: `true` if the mutex was successfully locked, `false` otherwise
* DESCRIPTION:
* Lock or unlock a mutex.
*
* A mutex is considered "owned" by a thread if said thread is currently
* holding a lock.
*
* `tcthread_mutex_lock` will block until the mutex is successfully locked by
* the current thread.
* `tcthread_mutex_try_lock` will attempt to do the same, but will never block;
* instead, it will return whether it was successful.
* Once a lock is successful, the current thread owns the mutex. Use
* `tcthread_mutex_unlock` to unlock the mutex and allow other threads to
* progress.
*
* Locking an owned recursive mutex with `tcthread_mutex_[try_]lock` will
* immediately return (as a success, in the case of `tcthread_mutex_try_lock`).
* The mutex will remaine owned (locked) until `tcthread_mutex_unlock` is
* called the same number of times. Attempting to lock a *non*-recursive mutex
* that is currently owned by the same thread is undefined.
*
* Attempting to unlock a mutex that the thread does not currently own is
* undefined.
*
*
* ========== CONDITION VARIABLES ==========
*
* SYNOPSIS:
* tcthread_cond_t tcthread_cond_create(void);
* bool tcthread_cond_is_valid(tcthread_cond_t cond);
* void tcthread_cond_destroy(tcthread_cond_t cond);
* PARAMETERS:
* - cond: condition variable to check the validity of, or to destroy
* RETURN VALUE:
* - tcthread_cond_create: the newly-created condition variable; use `tcthread_cond_is_valid` to verify if creation was successful
* - tcthread_cond_is_valid: whether the condition variable creation was successful; note that this will *not* catch other forms of invalid data, such as an uninitialized `cond` value
* DESCRIPTION:
* Create a new condition variable.
*
* A condition variable can be used to wait for a certain event; an event can
* be signaled (which wakes up a single waiting thread), or it can be broadcasted
* (which wakes up multiple waiting threads).
* Note that spurious wakeups are possible: wakeups which were not triggered
* by a signal or broadcast. See documentation of `tcthread_cond_wait` for an
* example of the correct use of a condition variable.
*
* Attempting to destroy a condition variable that a thread is currently
* waiting on is undefined.
*
* Created condition variable should be verified with `tcthread_cond_is_valid`.
*
*
* SYNOPSIS:
* void tcthread_cond_wait(tcthread_cond_t cond, tcthread_mutex_t mutex);
* bool tcthread_cond_timed_wait(tcthread_cond_t cond, tcthread_mutex_t mutex, uint32_t timeout_ms);
* PARAMETERS:
* - cond: condition variable to wait on
* - mutex: mutex protecting access to the condition-value we are trying to access
* - timeout_ms: amount of time to wait for, in milliseconds
* RETURN VALUE:
* - tcthread_cond_timed_wait: `true` if the condition variable was triggered in time, `false` if timeout was reached
* DESCRIPTION:
* Wait for a condition variable to be signaled.
*
* `mutex` *must* be locked before waiting is attempted; calling these
* functions with an unlocked mutex is undefined.
*
* The mutex will be unlocked before starting the wait for a signal. Once a
* thread is woken up, the mutex is automatically re-locked.
*
* `tcthread_cond_wait` blocks indefinitely until the function is woken up.
* `tcthread_cond_timed_wait` will only block until `timeout_ms` is reached;
* it returns `true` if it returned as a result of a signal, or `false` if it
* is due to the timeout.
*
* Note that spurious wakeups are possible: wakeups which were not triggered
* by a signal or broadcast. This also affects `tcthread_cond_timed_wait` in
* that it may return a false positive. The correct way of accounting for such
* a case is to re-check the condition(s) we are waiting for after
* `tcthread_cond_[timed_]wait` returns.
*
* Following is a typical pattern that handles spurious wakeups correctly.
* The example is that of a simple task queue, such as one that might be used
* in a threadpool:
*
* // mutex protects our condition, queue->num_tasks (we also reuse it for queue->tasks, as the two are updated together)
* tcthread_mutex_lock(queue->mutex);
* // handle spurious wakeups correctly: the condition we are waiting on is `queue->num_tasks != 0`
* while(queue->num_tasks == 0)
* {
* // unlocks queue->mutex before waiting
* // locks queue->mutex before returning
* tcthread_cond_wait(queue->cond_task_added, queue->mutex);
* // in case of spurious wakeup, `queue->num_tasks` may still be `0`; hence the loop
* }
* // queue->num_tasks is now guaranteed to be non-zero (i.e. condition is valid)
* // queue->mutex is still (or perhaps again, if we waited on a condition) locked
* queue_task* task = queue->tasks[queue->num_tasks - 1];
* --queue->num_tasks;
* tcthread_mutex_unlock(queue->mutex);
* // run the task (outside the mutex, to prevent the task from blocking other threads)
* task->user_function(task->user_data);
*
* Simultaneous waits on a condition variable with different mutexes is
* undefined. Different mutexes *may* be used with the same condition variable,
* but not at the same time.
*
* SEE ALSO:
* - `tcthread_mutex_create` to create a mutex that is required for using a condition variable
* - `tcthread_cond_signal` & `tcthread_cond_broadcast` to wakeup threads waiting via these functions
*
*
* SYNOPSIS:
* void tcthread_cond_signal(tcthread_cond_t cond);
* void tcthread_cond_broadcast(tcthread_cond_t cond);
* PARAMETERS:
* - cond: condition variable to signal
* DESCRIPTION:
* Signal a condition variable, waking up waiting threads.
*
* `tcthread_cond_signal` wakes up a single waiting thread, whereas
* `tcthread_cond_broadcast` wakes up *all* waiting threads. If no threads
* are currently waiting, then this operation is a no-op.
*
* It is recommended to unlock relevant wait-mutexes *before* signaling a
* condition variable (where possible), in order to reduce the amount of
* context-switching between threads.
*
* SEE ALSO:
* - `tcthread_cond_wait` & `tcthread_cond_timed_wait` to wait on a condition variable (the opposite operation)
*/
#ifndef TC_THREAD_H_
#define TC_THREAD_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#ifndef TC_HINT_NORETURN
#if __STDC_VERSION__ >= 201112L
#define TC_HINT_NORETURN _Noreturn
#elif defined(__GNUC__)
#define TC_HINT_NORETURN __attribute__((__noreturn__))
#elif defined(_MSC_VER)
#define TC_HINT_NORETURN __declspec(noreturn)
#else
#define TC_HINT_NORETURN
#endif
#endif
// this is the only one we need in the header (the rest are defined #ifdef TC_THREAD_IMPLEMENTATION)
#ifndef TC__REINTERPRET_CAST
#ifdef __cplusplus
#define TC__REINTERPRET_CAST(T,v) reinterpret_cast<T>(v)
#else
#define TC__REINTERPRET_CAST(T,v) ((T)(v))
#endif
#endif /* TC__REINTERPRET_CAST */
// wrapped in structs for type-safety, but otherwise raw handles
typedef struct tcthread { uintptr_t handle; } tcthread_t;
typedef struct tcthread_mutex { uintptr_t handle; } tcthread_mutex_t;
typedef struct tcthread_cond { uintptr_t handle; } tcthread_cond_t;
typedef struct tcthread_sem { uintptr_t handle; } tcthread_sem_t;
typedef struct tcthread_rwlock { uintptr_t handle; } tcthread_rwlock_t;
// Util
// Number of logical processor cores. May be higher than physical. Returns 0 if fetching failed.
uint32_t tcthread_get_cpu_count(void);
// Threads
typedef void* tcthread_runner_t(void* udata);
tcthread_t tcthread_create(size_t stack_size, tcthread_runner_t* runner, void* udata);
inline bool tcthread_is_valid(tcthread_t thread) { return thread.handle; }
void tcthread_join(tcthread_t thread, void** retval);
void tcthread_detach(tcthread_t thread);
// works with current thread
TC_HINT_NORETURN void tcthread_exit(void* retval); // WARNING: May or may not run C++ destructors
void tcthread_sleep(uint32_t ms);
// only works with threads started with `tcthread_create`; TODO: make it work for all
tcthread_t tcthread_self(void);
// Mutex
tcthread_mutex_t tcthread_mutex_create(bool recursive);
inline bool tcthread_mutex_is_valid(tcthread_mutex_t mutex) { return mutex.handle; }
void tcthread_mutex_destroy(tcthread_mutex_t mutex);
void tcthread_mutex_lock(tcthread_mutex_t mutex);
bool tcthread_mutex_try_lock(tcthread_mutex_t mutex);
// timed locks might be a problem in Windows, so I might have to drop this (unless I switch from critical section to a [less performant] mutex)
//bool tcthread_mutex_timed_lock(tcthread_mutex_t mutex, uint32_t timeout_ms);
void tcthread_mutex_unlock(tcthread_mutex_t mutex);
// Condition Variable
tcthread_cond_t tcthread_cond_create(void);
inline bool tcthread_cond_is_valid(tcthread_cond_t cond) { return cond.handle; }
void tcthread_cond_destroy(tcthread_cond_t cond);
void tcthread_cond_wait(tcthread_cond_t cond, tcthread_mutex_t mutex);
bool tcthread_cond_timed_wait(tcthread_cond_t cond, tcthread_mutex_t mutex, uint32_t timeout_ms);
void tcthread_cond_signal(tcthread_cond_t cond);
void tcthread_cond_broadcast(tcthread_cond_t cond);
// Semaphore
tcthread_sem_t tcthread_sem_create(uint32_t initial_value);
inline bool tcthread_sem_is_valid(tcthread_sem_t sem) { return sem.handle; }
void tcthread_sem_destroy(tcthread_sem_t sem);
void tcthread_sem_post(tcthread_sem_t sem);
void tcthread_sem_wait(tcthread_sem_t sem);
bool tcthread_sem_try_wait(tcthread_sem_t sem);
bool tcthread_sem_timed_wait(tcthread_sem_t sem, uint32_t timeout_ms);
// RWLock
tcthread_rwlock_t tcthread_rwlock_create(void);
inline bool tcthread_rwlock_is_valid(tcthread_rwlock_t rwlock) { return rwlock.handle; }
void tcthread_rwlock_destroy(tcthread_rwlock_t rwlock);
void tcthread_rwlock_lock_rd(tcthread_rwlock_t rwlock);
bool tcthread_rwlock_try_lock_rd(tcthread_rwlock_t rwlock);
void tcthread_rwlock_unlock_rd(tcthread_rwlock_t rwlock);
void tcthread_rwlock_lock_wr(tcthread_rwlock_t rwlock);
bool tcthread_rwlock_try_lock_wr(tcthread_rwlock_t rwlock);
void tcthread_rwlock_unlock_wr(tcthread_rwlock_t rwlock);
// ********** ATOMICS **********
typedef uint8_t tcthread_atomicbool_t; // uses the smallest available atomic type
typedef uint32_t tcthread_atomic32_t;
typedef uintptr_t tcthread_atomicsz_t;
typedef void* tcthread_atomicptr_t;
#if defined(__GNUC__)
#define TCTHREAD_MEMORDER_RELAXED __ATOMIC_RELAXED
#define TCTHREAD_MEMORDER_CONSUME __ATOMIC_CONSUME
#define TCTHREAD_MEMORDER_ACQUIRE __ATOMIC_ACQUIRE
#define TCTHREAD_MEMORDER_RELEASE __ATOMIC_RELEASE
#define TCTHREAD_MEMORDER_ACQ_REL __ATOMIC_ACQ_REL
#define TCTHREAD_MEMORDER_SEQ_CST __ATOMIC_SEQ_CST
// ***** boolean *****
// valid memorder: RELAXED, CONSUME, ACQUIRE, SEQ_CST
inline bool tcthread_atomicbool_load_explicit(volatile tcthread_atomicbool_t* ptr, int memorder) { return __atomic_load_n(ptr, memorder); }
// valid memorder: RELAXED, RELEASE, SEQ_CST
inline void tcthread_atomicbool_store_explicit(volatile tcthread_atomicbool_t* ptr, bool value, int memorder) { __atomic_store_n(ptr, value, memorder); }
// valid memorder: RELAXED, ACQUIRE, RELEASE, ACQ_REL, SEQ_CST
inline bool tcthread_atomicbool_exchange_explicit(volatile tcthread_atomicbool_t* ptr, tcthread_atomicbool_t desired, int memorder) { return __atomic_exchange_n(ptr, desired, memorder); }
// ***** load/store *****
// valid memorder: RELAXED, CONSUME, ACQUIRE, SEQ_CST
inline tcthread_atomic32_t tcthread_atomic32_load_explicit(volatile tcthread_atomic32_t* ptr, int memorder) { return __atomic_load_n(ptr, memorder); }
inline tcthread_atomicsz_t tcthread_atomicsz_load_explicit(volatile tcthread_atomicsz_t* ptr, int memorder) { return __atomic_load_n(ptr, memorder); }
// valid memorder: RELAXED, RELEASE, SEQ_CST
inline void tcthread_atomic32_store_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t value, int memorder) { __atomic_store_n(ptr, value, memorder); }
inline void tcthread_atomicsz_store_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t value, int memorder) { __atomic_store_n(ptr, value, memorder); }
// ***** (compare-and-)swap *****
// valid memorder: RELAXED, ACQUIRE, RELEASE, ACQ_REL, SEQ_CST
inline tcthread_atomic32_t tcthread_atomic32_exchange_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t desired, int memorder) { return __atomic_exchange_n(ptr, desired, memorder); }
inline tcthread_atomicsz_t tcthread_atomicsz_exchange_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t desired, int memorder) { return __atomic_exchange_n(ptr, desired, memorder); }
// valid memorder_success: (any)
// valid memorder_failure: RELAXED, CONSUME, ACQUIRE, SEQ_CST; must *not* be stronger than memorder_success
inline tcthread_atomic32_t tcthread_atomic32_compare_exchange_strong_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t expected, tcthread_atomic32_t desired, int memorder_success, int memorder_failure) { __atomic_compare_exchange_n(ptr, &expected, desired, false, memorder_success, memorder_failure); return expected; }
inline tcthread_atomicsz_t tcthread_atomicsz_compare_exchange_strong_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t expected, tcthread_atomicsz_t desired, int memorder_success, int memorder_failure) { __atomic_compare_exchange_n(ptr, &expected, desired, false, memorder_success, memorder_failure); return expected; }
inline tcthread_atomic32_t tcthread_atomic32_compare_exchange_weak_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t expected, tcthread_atomic32_t desired, int memorder_success, int memorder_failure) { __atomic_compare_exchange_n(ptr, &expected, desired, true, memorder_success, memorder_failure); return expected; }
inline tcthread_atomicsz_t tcthread_atomicsz_compare_exchange_weak_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t expected, tcthread_atomicsz_t desired, int memorder_success, int memorder_failure) { __atomic_compare_exchange_n(ptr, &expected, desired, true, memorder_success, memorder_failure); return expected; }
// ***** arithmetic & bitwise *****
// valid memorder: (any)
inline tcthread_atomic32_t tcthread_atomic32_fetch_add_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t value, int memorder) { return __atomic_fetch_add(ptr, value, memorder); }
inline tcthread_atomicsz_t tcthread_atomicsz_fetch_add_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t value, int memorder) { return __atomic_fetch_add(ptr, value, memorder); }
inline tcthread_atomic32_t tcthread_atomic32_fetch_sub_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t value, int memorder) { return __atomic_fetch_sub(ptr, value, memorder); }
inline tcthread_atomicsz_t tcthread_atomicsz_fetch_sub_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t value, int memorder) { return __atomic_fetch_sub(ptr, value, memorder); }
inline tcthread_atomic32_t tcthread_atomic32_fetch_and_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t value, int memorder) { return __atomic_fetch_and(ptr, value, memorder); }
inline tcthread_atomicsz_t tcthread_atomicsz_fetch_and_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t value, int memorder) { return __atomic_fetch_and(ptr, value, memorder); }
inline tcthread_atomic32_t tcthread_atomic32_fetch_xor_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t value, int memorder) { return __atomic_fetch_xor(ptr, value, memorder); }
inline tcthread_atomicsz_t tcthread_atomicsz_fetch_xor_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t value, int memorder) { return __atomic_fetch_xor(ptr, value, memorder); }
inline tcthread_atomic32_t tcthread_atomic32_fetch_or_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t value, int memorder) { return __atomic_fetch_or(ptr, value, memorder); }
inline tcthread_atomicsz_t tcthread_atomicsz_fetch_or_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t value, int memorder) { return __atomic_fetch_or(ptr, value, memorder); }
// GCC additionally supports `nand` ... should we expose it?
// ***** increment & decrement; these return the *new* value *****
// valid memorder: (any)
inline tcthread_atomic32_t tcthread_atomic32_inc_explicit(volatile tcthread_atomic32_t* ptr, int memorder) { return __atomic_add_fetch(ptr, 1, memorder); }
inline tcthread_atomicsz_t tcthread_atomicsz_inc_explicit(volatile tcthread_atomicsz_t* ptr, int memorder) { return __atomic_add_fetch(ptr, 1, memorder); }
inline tcthread_atomic32_t tcthread_atomic32_dec_explicit(volatile tcthread_atomic32_t* ptr, int memorder) { return __atomic_sub_fetch(ptr, 1, memorder); }
inline tcthread_atomicsz_t tcthread_atomicsz_dec_explicit(volatile tcthread_atomicsz_t* ptr, int memorder) { return __atomic_sub_fetch(ptr, 1, memorder); }
#elif defined(_MSC_VER)
/*
* Values are assigned so that ORing provides the "stronger" combination.
* DO NOT RELY ON THAT, it is merely used to simplify some internal logic and is
* subject to change without notice.
*/
// RELAXED = 0
// CONSUME = 1 | RELAXED (== 1)
// ACQUIRE = 2 | CONSUME (== 3)
// RELEASE = 4 | RELAXED (== 4)
// ACQ_REL = ACQUIRE | RELEASE (== 7)
// SEQ_CST = 8 | ACQ_REL (== 15)
#define TCTHREAD_MEMORDER_RELAXED 0x0
#define TCTHREAD_MEMORDER_CONSUME 0x1
#define TCTHREAD_MEMORDER_ACQUIRE 0x3
#define TCTHREAD_MEMORDER_RELEASE 0x4
#define TCTHREAD_MEMORDER_ACQ_REL 0x7
#define TCTHREAD_MEMORDER_SEQ_CST 0xF
// ***** boolean *****
// valid memorder: RELAXED, CONSUME, ACQUIRE, SEQ_CST
inline bool tcthread_atomicbool_load_explicit(volatile tcthread_atomicbool_t* ptr, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedCompareExchange8_nf((volatile char*)ptr, 0, 0);
#endif
case TCTHREAD_MEMORDER_CONSUME:
case TCTHREAD_MEMORDER_ACQUIRE:
#if defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedCompareExchange8_acq((volatile char*)ptr, 0, 0);
#endif
case TCTHREAD_MEMORDER_SEQ_CST:
return _InterlockedCompareExchange8((volatile char*)ptr, 0, 0);
}
__assume(0); // unreachable
//return 0;
}
// valid memorder: RELAXED, RELEASE, SEQ_CST
inline void tcthread_atomicbool_store_explicit(volatile tcthread_atomicbool_t* ptr, bool value, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM) || defined(_M_ARM64)
_InterlockedExchange8_nf((volatile char*)ptr, value);
return;
#endif
case TCTHREAD_MEMORDER_RELEASE:
#if defined(_M_ARM) || defined(_M_ARM64)
_InterlockedExchange8_rel((volatile char*)ptr, value);
return;
#endif
case TCTHREAD_MEMORDER_SEQ_CST:
_InterlockedExchange8((volatile char*)ptr, value);
return;
}
__assume(0); // unreachable
}
// valid memorder: RELAXED, ACQUIRE, RELEASE, ACQ_REL, SEQ_CST
inline bool tcthread_atomicbool_exchange_explicit(volatile tcthread_atomicbool_t* ptr, tcthread_atomicbool_t desired, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedExchange8_nf((volatile char*)ptr, desired);
#endif
//case TCTHREAD_MEMORDER_CONSUME: // seems to be unallowed
case TCTHREAD_MEMORDER_ACQUIRE:
#if defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedExchange8_acq((volatile char*)ptr, desired);
#endif
case TCTHREAD_MEMORDER_RELEASE:
#if defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedExchange8_rel((volatile char*)ptr, desired);
#endif
case TCTHREAD_MEMORDER_ACQ_REL:
case TCTHREAD_MEMORDER_SEQ_CST:
return _InterlockedExchange8((volatile char*)ptr, desired);
}
__assume(0); // unreachable
//return 0;
}
// ***** load/store *****
// valid memorder: RELAXED, CONSUME, ACQUIRE, SEQ_CST
inline tcthread_atomic32_t tcthread_atomic32_load_explicit(volatile tcthread_atomic32_t* ptr, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
// strangely, there is no _InterlockedCompareExchange_nf on ARM
#if defined(_M_ARM)
// On 32-bit, we can simulate it with the same operation on pointers.
return (tcthread_atomic32_t)_InterlockedCompareExchangePointer_nf((void* volatile*)ptr, NULL, NULL);
#elif defined(_M_ARM64)
// On 64-bit, we're out of luck. Let's try an atomic `or`.
// (TODO: Should we simply fallthrough to _acq here?)
return _InterlockedOr_nf((volatile long*)ptr, 0, 0);
#endif
case TCTHREAD_MEMORDER_CONSUME:
case TCTHREAD_MEMORDER_ACQUIRE:
#if defined(_M_IX86) || defined(_M_AMD64)
return _InterlockedCompareExchange_HLEAcquire((volatile long*)ptr, 0, 0);
#elif defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedCompareExchange_acq((volatile long*)ptr, 0, 0);
#endif
case TCTHREAD_MEMORDER_SEQ_CST:
return _InterlockedCompareExchange((volatile long*)ptr, 0, 0);
}
__assume(0); // unreachable
//return 0;
}
inline tcthread_atomicsz_t tcthread_atomicsz_load_explicit(volatile tcthread_atomicsz_t* ptr, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM) || defined(_M_ARM64)
return (tcthread_atomicsz_t)_InterlockedCompareExchangePointer_nf(ptr, NULL, NULL);
#endif
case TCTHREAD_MEMORDER_CONSUME:
case TCTHREAD_MEMORDER_ACQUIRE:
#if defined(_M_IX86) || defined(_M_AMD64)
return (tcthread_atomicsz_t)_InterlockedCompareExchangePointer_HLEAcquire(ptr, NULL, NULL);
#elif defined(_M_ARM) || defined(_M_ARM64)
return (tcthread_atomicsz_t)_InterlockedCompareExchangePointer_acq(ptr, NULL, NULL);
#endif
case TCTHREAD_MEMORDER_SEQ_CST:
return (tcthread_atomicsz_t)_InterlockedCompareExchangePointer(ptr, NULL, NULL);
}
__assume(0); // unreachable
//return 0;
}
// valid memorder: RELAXED, RELEASE, SEQ_CST
inline void tcthread_atomic32_store_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t value, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM) || defined(_M_ARM64)
_InterlockedExchange_nf((volatile long*)ptr, value);
return;
#endif
case TCTHREAD_MEMORDER_RELEASE:
#if defined(_M_IX86) || defined(_M_AMD64)
_InterlockedExchange_HLERelease((volatile long*)ptr, value);
return;
#elif defined(_M_ARM) || defined(_M_ARM64)
_InterlockedExchange_rel((volatile long*)ptr, value);
return;
#endif
case TCTHREAD_MEMORDER_SEQ_CST:
_InterlockedExchange((volatile long*)ptr, value);
return;
}
__assume(0); // unreachable
}
inline void tcthread_atomicsz_store_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t value, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM) || defined(_M_ARM64)
_InterlockedExchangePointer_nf(ptr, (void*)value);
return;
#endif
case TCTHREAD_MEMORDER_RELEASE:
#if defined(_M_IX86)
// x86 has no _InterlockedExchangePointer_HLERelease for some reason, but it has _InterlockedExchange_HLERelease
_InterlockedExchange_HLERelease((volatile long*)ptr, value);
return;
#elif defined(_M_AMD64)
_InterlockedExchangePointer_HLERelease(ptr, (void*)value);
return;
#elif defined(_M_ARM) || defined(_M_ARM64)
_InterlockedExchangePointer_rel(ptr, (void*)value);
return;
#endif
case TCTHREAD_MEMORDER_SEQ_CST:
_InterlockedExchangePointer(ptr, (void*)value);
return;
}
__assume(0); // unreachable
}
// ***** (compare-and-)swap *****
// valid memorder: RELAXED, ACQUIRE, RELEASE, ACQ_REL, SEQ_CST
inline tcthread_atomic32_t tcthread_atomic32_exchange_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t desired, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedExchange_nf((volatile long*)ptr, desired);
#endif
//case TCTHREAD_MEMORDER_CONSUME: // seems to be unallowed
case TCTHREAD_MEMORDER_ACQUIRE:
#if defined(_M_IX86) || defined(_M_AMD64)
return _InterlockedExchange_HLEAcquire((volatile long*)ptr, desired);
#elif defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedExchange_acq((volatile long*)ptr, desired);
#endif
case TCTHREAD_MEMORDER_RELEASE:
#if defined(_M_IX86) || defined(_M_AMD64)
return _InterlockedExchange_HLERelease((volatile long*)ptr, desired);
#elif defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedExchange_rel((volatile long*)ptr, desired);
#endif
case TCTHREAD_MEMORDER_ACQ_REL:
case TCTHREAD_MEMORDER_SEQ_CST:
return _InterlockedExchange((volatile long*)ptr, desired);
}
__assume(0); // unreachable
//return 0;
}
inline tcthread_atomicsz_t tcthread_atomicsz_exchange_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t desired, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM) || defined(_M_ARM64)
return (tcthread_atomicsz_t)_InterlockedExchangePointer_nf(ptr, (void*)desired);
#endif
//case TCTHREAD_MEMORDER_CONSUME: // seems to be unallowed
case TCTHREAD_MEMORDER_ACQUIRE:
#if defined(_M_IX86) || defined(_M_AMD64)
return (tcthread_atomicsz_t)_InterlockedExchangePointer_HLEAcquire(ptr, (void*)desired);
#elif defined(_M_ARM) || defined(_M_ARM64)
return (tcthread_atomicsz_t)_InterlockedExchangePointer_acq(ptr, (void*)desired);
#endif
case TCTHREAD_MEMORDER_RELEASE:
#if defined(_M_IX86)
// x86 has no _InterlockedExchangePointer_HLERelease for some reason, but it has _InterlockedExchange_HLERelease
return _InterlockedExchange_HLERelease((volatile long*)ptr, desired);
#elif defined(_M_AMD64)
return (tcthread_atomicsz_t)_InterlockedExchangePointer_HLERelease(ptr, (void*)desired);
#elif defined(_M_ARM) || defined(_M_ARM64)
return (tcthread_atomicsz_t)_InterlockedExchangePointer_rel(ptr, (void*)desired);
#endif
case TCTHREAD_MEMORDER_ACQ_REL:
case TCTHREAD_MEMORDER_SEQ_CST:
return (tcthread_atomicsz_t)_InterlockedExchangePointer(ptr, (void*)desired);
}
__assume(0); // unreachable
//return 0;
}
// valid memorder_success: (any)
// valid memorder_failure: RELAXED, CONSUME, ACQUIRE, SEQ_CST; must *not* be stronger than memorder_success
inline tcthread_atomic32_t tcthread_atomic32_compare_exchange_strong_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t expected, tcthread_atomic32_t desired, int memorder_success, int memorder_failure)
{
switch(memorder_success | memorder_failure)
{
case TCTHREAD_MEMORDER_RELAXED:
// strangely, there is no _InterlockedCompareExchange_nf on ARM
#if defined(_M_ARM)
// On 32-bit, we can simulate it with the same operation on pointers.
return (tcthread_atomic32_t)_InterlockedCompareExchangePointer_nf((void* volatile*)ptr, (void*)desired, (void*)expected);
//#elif defined(_M_ARM64) // On 64-bit, we're out of luck. Fallthrough to ACQUIRE
#endif
case TCTHREAD_MEMORDER_CONSUME:
case TCTHREAD_MEMORDER_ACQUIRE:
#if defined(_M_IX86) || defined(_M_AMD64)
return _InterlockedCompareExchange_HLEAcquire((volatile long*)ptr, desired, expected);
#elif defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedCompareExchange_acq((volatile long*)ptr, desired, expected);
#endif
case TCTHREAD_MEMORDER_RELEASE:
#if defined(_M_IX86) || defined(_M_AMD64)
return _InterlockedCompareExchange_HLERelease((volatile long*)ptr, desired, expected);
#elif defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedCompareExchange_rel((volatile long*)ptr, desired, expected);
#endif
case TCTHREAD_MEMORDER_ACQ_REL:
case TCTHREAD_MEMORDER_SEQ_CST:
return _InterlockedCompareExchange((volatile long*)ptr, desired, expected);
}
__assume(0); // unreachable
//return 0;
}
inline tcthread_atomicsz_t tcthread_atomicsz_compare_exchange_strong_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t expected, tcthread_atomicsz_t desired, int memorder_success, int memorder_failure)
{
switch(memorder_success | memorder_failure)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM) || defined(_M_ARM64)
return (tcthread_atomicsz_t)_InterlockedCompareExchangePointer_nf(ptr, (void*)desired, (void*)expected);
#endif
case TCTHREAD_MEMORDER_CONSUME:
case TCTHREAD_MEMORDER_ACQUIRE:
#if defined(_M_IX86) || defined(_M_AMD64)
return (tcthread_atomicsz_t)_InterlockedCompareExchangePointer_HLEAcquire(ptr, (void*)desired, (void*)expected);
#elif defined(_M_ARM) || defined(_M_ARM64)
return (tcthread_atomicsz_t)_InterlockedCompareExchangePointer_acq(ptr, (void*)desired, (void*)expected);
#endif
case TCTHREAD_MEMORDER_RELEASE:
#if defined(_M_IX86) || defined(_M_AMD64)
return (tcthread_atomicsz_t)_InterlockedCompareExchangePointer_HLERelease(ptr, (void*)desired, (void*)expected);
#elif defined(_M_ARM) || defined(_M_ARM64)
return (tcthread_atomicsz_t)_InterlockedCompareExchangePointer_rel(ptr, (void*)desired, (void*)expected);
#endif
case TCTHREAD_MEMORDER_ACQ_REL:
case TCTHREAD_MEMORDER_SEQ_CST:
return (tcthread_atomicsz_t)_InterlockedCompareExchangePointer(ptr, (void*)desired, (void*)expected);
}
__assume(0); // unreachable
//return 0;
}
// MSVC has no `weak` vs `strong` semantics
inline tcthread_atomic32_t tcthread_atomic32_compare_exchange_weak_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t expected, tcthread_atomic32_t desired, int memorder_success, int memorder_failure)
{
return tcthread_atomic32_compare_exchange_strong_explicit(ptr, expected, desired, memorder_success, memorder_failure);
}
inline tcthread_atomicsz_t tcthread_atomicsz_compare_exchange_weak_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t expected, tcthread_atomicsz_t desired, int memorder_success, int memorder_failure)
{
return tcthread_atomicsz_compare_exchange_strong_explicit(ptr, expected, desired, memorder_success, memorder_failure);
}
// ***** arithmetic & bitwise *****
// valid memorder: (any)
inline tcthread_atomic32_t tcthread_atomic32_fetch_add_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t value, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedExchangeAdd_nf((volatile long*)ptr, value);
#endif
case TCTHREAD_MEMORDER_CONSUME:
case TCTHREAD_MEMORDER_ACQUIRE:
#if defined(_M_IX86) || defined(_M_AMD64)
return _InterlockedExchangeAdd_HLEAcquire((volatile long*)ptr, value);
#elif defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedExchangeAdd_acq((volatile long*)ptr, value);
#endif
case TCTHREAD_MEMORDER_RELEASE:
#if defined(_M_IX86) || defined(_M_AMD64)
return _InterlockedExchangeAdd_HLERelease((volatile long*)ptr, value);
#elif defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedExchangeAdd_rel((volatile long*)ptr, value);
#endif
case TCTHREAD_MEMORDER_ACQ_REL:
case TCTHREAD_MEMORDER_SEQ_CST:
return _InterlockedExchangeAdd((volatile long*)ptr, value);
}
__assume(0); // unreachable
//return 0;
}
inline tcthread_atomicsz_t tcthread_atomicsz_fetch_add_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t value, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM)
return _InterlockedExchangeAdd_nf(ptr, value);
#elif defined(_M_ARM64)
return _InterlockedExchangeAdd64_nf(ptr, value);
#endif
case TCTHREAD_MEMORDER_CONSUME:
case TCTHREAD_MEMORDER_ACQUIRE:
#if defined(_M_IX86)
return _InterlockedExchangeAdd_HLEAcquire(ptr, value);
#elif defined(_M_AMD64)
return _InterlockedExchangeAdd64_HLEAcquire(ptr, value);
#elif defined(_M_ARM)
return _InterlockedExchangeAdd_acq(ptr, value);
#elif defined(_M_ARM64)
return _InterlockedExchangeAdd64_acq(ptr, value);
#endif
case TCTHREAD_MEMORDER_RELEASE:
#if defined(_M_IX86)
return _InterlockedExchangeAdd_HLERelease(ptr, value);
#elif defined(_M_AMD64)
return _InterlockedExchangeAdd64_HLERelease(ptr, value);
#elif defined(_M_ARM)
return _InterlockedExchangeAdd_rel(ptr, value);
#elif defined(_M_ARM64)
return _InterlockedExchangeAdd64_rel(ptr, value);
#endif
case TCTHREAD_MEMORDER_ACQ_REL:
case TCTHREAD_MEMORDER_SEQ_CST:
#if defined(_M_IX86) || defined(_M_ARM)
return _InterlockedExchangeAdd(ptr, value);
#elif defined(_M_AMD64) || defined(_M_ARM64)
return _InterlockedExchangeAdd64(ptr, value);
#else
#error "Unknown CPU architecture"
#endif
}
__assume(0); // unreachable
//return 0;
}
#pragma warning( push )
// Disable "C4146: unary minus operator applied to unsigned type, result still unsigned"
#pragma warning( disable : 4146 )
inline tcthread_atomic32_t tcthread_atomic32_fetch_sub_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t value, int memorder)
{
return tcthread_atomic32_fetch_sub_explicit(ptr, -value, memorder);
}
inline tcthread_atomicsz_t tcthread_atomicsz_fetch_sub_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t value, int memorder)
{
return tcthread_atomicsz_fetch_add_explicit(ptr, -value, memorder);
}
#pragma warning( pop )
inline tcthread_atomic32_t tcthread_atomic32_fetch_and_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t value, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedAnd_nf((volatile long*)ptr, value);
#endif
case TCTHREAD_MEMORDER_CONSUME:
case TCTHREAD_MEMORDER_ACQUIRE:
#if defined(_M_IX86) || defined(_M_AMD64)
return _InterlockedAnd_HLEAcquire((volatile long*)ptr, value);
#elif defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedAnd_acq((volatile long*)ptr, value);
#endif
case TCTHREAD_MEMORDER_RELEASE:
#if defined(_M_IX86) || defined(_M_AMD64)
return _InterlockedAnd_HLERelease((volatile long*)ptr, value);
#elif defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedAnd_rel((volatile long*)ptr, value);
#endif
case TCTHREAD_MEMORDER_ACQ_REL:
case TCTHREAD_MEMORDER_SEQ_CST:
return _InterlockedAnd((volatile long*)ptr, value);
}
__assume(0); // unreachable
//return 0;
}
inline tcthread_atomicsz_t tcthread_atomicsz_fetch_and_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t value, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM)
return _InterlockedAnd_nf(ptr, value);
#elif defined(_M_ARM64)
return _InterlockedAnd64_nf(ptr, value);
#endif
case TCTHREAD_MEMORDER_CONSUME:
case TCTHREAD_MEMORDER_ACQUIRE:
#if defined(_M_IX86)
return _InterlockedAnd_HLEAcquire(ptr, value);
#elif defined(_M_AMD64)
return _InterlockedAnd64_HLEAcquire(ptr, value);
#elif defined(_M_ARM)
return _InterlockedAnd_acq(ptr, value);
#elif defined(_M_ARM64)
return _InterlockedAnd64_acq(ptr, value);
#endif
case TCTHREAD_MEMORDER_RELEASE:
#if defined(_M_IX86)
return _InterlockedAnd_HLERelease(ptr, value);
#elif defined(_M_AMD64)
return _InterlockedAnd64_HLERelease(ptr, value);
#elif defined(_M_ARM)
return _InterlockedAnd_rel(ptr, value);
#elif defined(_M_ARM64)
return _InterlockedAnd64_rel(ptr, value);
#endif
case TCTHREAD_MEMORDER_ACQ_REL:
case TCTHREAD_MEMORDER_SEQ_CST:
#if defined(_M_IX86) || defined(_M_ARM)
return _InterlockedAnd(ptr, value);
#elif defined(_M_AMD64) || defined(_M_ARM64)
return _InterlockedAnd64(ptr, value);
#else
#error "Unknown CPU architecture"
#endif
}
__assume(0); // unreachable
//return 0;
}
inline tcthread_atomic32_t tcthread_atomic32_fetch_xor_explicit(volatile tcthread_atomic32_t* ptr, tcthread_atomic32_t value, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED:
#if defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedXor_nf((volatile long*)ptr, value);
#endif
case TCTHREAD_MEMORDER_CONSUME:
case TCTHREAD_MEMORDER_ACQUIRE:
#if defined(_M_IX86) || defined(_M_AMD64)
return _InterlockedXor_HLEAcquire((volatile long*)ptr, value);
#elif defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedXor_acq((volatile long*)ptr, value);
#endif
case TCTHREAD_MEMORDER_RELEASE:
#if defined(_M_IX86) || defined(_M_AMD64)
return _InterlockedXor_HLERelease((volatile long*)ptr, value);
#elif defined(_M_ARM) || defined(_M_ARM64)
return _InterlockedXor_rel((volatile long*)ptr, value);
#endif
case TCTHREAD_MEMORDER_ACQ_REL:
case TCTHREAD_MEMORDER_SEQ_CST:
return _InterlockedXor((volatile long*)ptr, value);
}
__assume(0); // unreachable
//return 0;
}
inline tcthread_atomicsz_t tcthread_atomicsz_fetch_xor_explicit(volatile tcthread_atomicsz_t* ptr, tcthread_atomicsz_t value, int memorder)
{
switch(memorder)
{
case TCTHREAD_MEMORDER_RELAXED: