-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathatomic_utils.h
More file actions
1756 lines (1662 loc) · 64 KB
/
atomic_utils.h
File metadata and controls
1756 lines (1662 loc) · 64 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
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
/*
* Copyright (C) 2020 Otto-von-Guericke-Universität Magdeburg
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
#pragma once
/**
* @defgroup sys_atomic_utils Utility functions for atomic access
* @ingroup sys
*
* This modules adds some utility functions to perform atomic accesses.
*
* # Usage
*
* The atomic utilitys allow atomic access to regular variables.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.c}
* uint32_t global_counter;
*
* void irq_handler(void)
* {
* // No need to use atomic access in IRQ handlers, if other IRQ handlers
* // never touch global_counter: At the beginning and at the end of every
* // ISR a memory barrier is in place, so that at the end of the ISR the
* // memory will be in a state as if all memory accesses within the ISR
* // took place in sequential order.
* //
* // Extra detail only RIOT kernel hackers need to know: If all ISRs
* // accessing the same variable cannot interrupt each other, atomic
* // access is still not needed. (Currently only PendSV on ARM can be
* // interrupted by other IRQs with RIOTs default IRQ priorities. If
* // application developers modifies those, they can be assumed to know
* // what they are doing - or to happily face the consequences otherwise.)
* global_counter++;
* }
*
* void called_by_thread_a(void) {
* if (atomic_load_u32(&global_counter) > THRESHOLD) {
* on_threshold_reached();
* atomic_store_u32(&global_counter, 0);
* }
* }
*
* void called_by_thread_b(void) {
* atomic_add_u32(&global_counter, 42);
* }
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* # Motivation
* There are some reasons why these functions might be chosen over the
* [C11 Atomic Operations Library](https://en.cppreference.com/w/c/atomic) in
* some advanced use cases:
*
* - The functions allow mixing of atomic and non-atomic accesses. E.g. while
* IRQs are disabled anyway, even plain accesses cannot be interrupted but
* are often more efficient.
* - On platforms not supporting lock-free access, a library call is generated
* instead. The fallback implementation used here is more efficient in terms
* of both CPU instructions and ROM size.
* - On platforms where some operations can be implemented lock free while
* others can't, at least LLVM will use the library call even for those
* accesses that can be implemented lock-free. This is because without
* assuming how the library call implements atomic access for the other
* functions, mixing library calls and lock free accesses could result in data
* corruption. But this implementation resorts to disabling IRQs when
* lock-free implementations are not possible, which mixes well with lock-free
* accesses. Thus, additional overhead for atomic accesses is only spent where
* needed.
* - In some cases the fallback implementation performs better than the lock
* free implementation. E.g. if a specific platform has an atomic compare and
* swap instruction, this could be used to perform a read-modify-write in a
* loop until the value initially read was not changed in between. Just
* disabling IRQs to implement an atomic read-modify-write operation is likely
* more efficient. C11 atomics will however always use the lock free
* implementation (if such exists), assuming that this is more efficient.
* This assumption was made with desktop class hardware in mind, but is not
* generally true for bare metal targets. These function allow to optimize
* for the actual hardware RIOT is running on.
* - This library provides "semi-atomic" read-modify-write operations, which are
* useful when at most one thread is ever writing to memory. In that case,
* only the write part of the read-modify-write operation needs to be
* performed in an atomic fashion in order for the reading threads to perceive
* atomic updates of the variable. This is significantly cheaper than atomic
* read-modify-write operations for many platforms
*
* # Guarantees
*
* - Every utility function here acts as a barrier for code reordering regarding
* - For the `atomic_*()` family of functions: The whole operation will be done
* in an non-interruptible fashion
* - For the `semi_atomic_*()` family of functions: The write part of the
* operation is done atomically. If at most one thread is ever performing
* changes to a variable using the `semi_atomic_()` functions, those changes
* will appear as if they were atomic to all other threads.
*
* # Porting to new CPUs
*
* At the bare minimum, create an empty `atomic_utils_arch.h` file. This will
* result in the fallback implementations being used.
*
* To expose lock-free atomic operations, add an implementation to the
* `atomic_utils_arch.h` file and disable the fallback implementation by
* defining `HAS_<FN_NAME_ALL_CAPS>`, where `<FN_NAME_ALL_CAPS>` is the name
* of the function provided in all upper case. E.g. most platforms will be able
* to provide lock-free reads and writes up to their word size and can expose
* this as follows for GCC:
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.c}
* // All the user header boilerplate
* #define HAS_ATOMIC_LOAD_U8
* static inline uint8_t atomic_load_u8(const uint8_t *var)
* {
* return __atomic_load_1(var, __ATOMIC_SEQ_CST);
* }
*
* #define HAS_ATOMIC_STORE_U8
* static inline void atomic_store_u8(uint8_t *dest, uint8_t val)
* {
* __atomic_store_1(dest, val, __ATOMIC_SEQ_CST);
* }
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Note: The `semi_atomic_*()` family of functions is always provided using
* `atomic_*()` functions in the cheapest way possible.
*
* @{
*
* @file
* @brief API of the utility functions for atomic accesses
* @author Marian Buschsieweke <marian.buschsieweke@ovgu.de>
*/
#include <limits.h>
#include <stdint.h>
#include "irq.h"
#include "macros/utils.h"
#include "sched.h"
#include "atomic_utils_arch.h" /* IWYU pragma: export */
#ifdef __cplusplus
extern "C" {
#endif
/* NOLINTBEGIN(bugprone-macro-parentheses, readability-inconsistent-declaration-parameter-name)
*
* The macros ATOMIC_LOAD_IMPL() and friends do not surround the argument used
* to pass the type with parenthesis. Suppressing the clang-tidy warning here,
* as adding parenthesis around a type would be a synstax error.
*
* The macro ATOMIC_FETCH_OP_IMPL() uses `val` as argument value. But we want
* the declaration may be more specific (e.g. summand instead of val).
*/
/* Declarations and documentation: */
#if !defined(HAS_ATOMIC_BIT) || defined(DOXYGEN)
/**
* @name Types used to specify atomic bit access operations
*
* @warning These types are implementation dependent to allow exploiting
* hardware specific features like bit-banding. Use the provided helper
* functions to get or set the bit or destination they refer to.
*
* The motivation of a dedicated type is to allow ahead of time computation of
* e.g. bit-banding addresses, so that actually access can be done in a single
* CPU cycles even if the bit reference was initialized in a separate
* compilation unit and no link time optimization is used.
*
* @{
*/
/**
* @brief Type specifying a bit in an `uint8_t`
*
* @warning This is an implementation specific type!
*/
typedef struct {
volatile uint8_t *dest; /**< Memory containing the bit to set/clear */
uint8_t mask; /**< Bitmask used for setting the bit */
} atomic_bit_u8_t;
/**
* @brief Type specifying a bit in an `uint16_t`
*
* @warning This is an implementation specific type!
*/
typedef struct {
volatile uint16_t *dest; /**< Memory containing the bit to set/clear */
uint16_t mask; /**< Bitmask used for setting the bit */
} atomic_bit_u16_t;
/**
* @brief Type specifying a bit in an `uint32_t`
*
* @warning This is an implementation specific type!
*/
typedef struct {
volatile uint32_t *dest; /**< Memory containing the bit to set/clear */
uint32_t mask; /**< Bitmask used for setting the bit */
} atomic_bit_u32_t;
/**
* @brief Type specifying a bit in an `uint64_t`
*
* @warning This is an implementation specific type!
*/
typedef struct {
volatile uint64_t *dest; /**< Memory containing the bit to set/clear */
uint64_t mask; /**< Bitmask used for setting the bit */
} atomic_bit_u64_t;
/** @} */
#endif /* HAS_ATOMIC_BIT */
/**
* @brief Type specifying a bit in an `unsigned int`
*/
#if UINT_MAX == UINT16_MAX
typedef atomic_bit_u16_t atomic_bit_unsigned_t;
#elif UINT_MAX == UINT32_MAX
typedef atomic_bit_u32_t atomic_bit_unsigned_t;
#else
typedef atomic_bit_u64_t atomic_bit_unsigned_t;
#endif
/**
* @name Atomic Loads
* @{
*/
/**
* @brief Load an `uint8_t` atomically
*
* @param[in] var Variable to load atomically
* @return The value stored in @p var
*/
static inline uint8_t atomic_load_u8(const volatile uint8_t *var);
/**
* @brief Load an `uint16_t` atomically
*
* @param[in] var Variable to load atomically
* @return The value stored in @p var
*/
static inline uint16_t atomic_load_u16(const volatile uint16_t *var);
/**
* @brief Load an `uint32_t` atomically
*
* @param[in] var Variable to load atomically
* @return The value stored in @p var
*/
static inline uint32_t atomic_load_u32(const volatile uint32_t *var);
/**
* @brief Load an `uint64_t` atomically
*
* @param[in] var Variable to load atomically
* @return The value stored in @p var
*/
static inline uint64_t atomic_load_u64(const volatile uint64_t *var);
/**
* @brief Load an `unsigned int` atomically
*
* @param[in] var Variable to load atomically
* @return The value stored in @p var
*
* @note This effectively an alias for @ref atomic_load_u64,
* @ref atomic_load_u32, or @ref atomic_load_u16 depending on the size
* of `unsigned int`.
*/
static inline unsigned atomic_load_unsigned(const volatile unsigned *var)
{
if (sizeof(uint64_t) == sizeof(unsigned)) {
return atomic_load_u64((const volatile uint64_t *)(uintptr_t)var);
}
if (sizeof(uint32_t) == sizeof(unsigned)) {
return atomic_load_u32((const volatile uint32_t *)(uintptr_t)var);
}
return atomic_load_u16((const volatile uint16_t *)(uintptr_t)var);
}
/**
* @brief Load an `uintptr_t` atomically
*
* @param[in] var Variable to load atomically
* @return The value stored in @p var
*/
static inline uintptr_t atomic_load_uintptr(const volatile uintptr_t *var) {
if (sizeof(uintptr_t) == 2) {
return atomic_load_u16((const volatile uint16_t *)var);
}
if (sizeof(uintptr_t) == 4) {
return atomic_load_u32((const volatile uint32_t *)(uintptr_t)var);
}
return atomic_load_u64((const volatile uint64_t *)(uintptr_t)var);
}
/**
* @brief Load an `void *` atomically
*
* @param[in] ptr_addr Address to the pointer to load
* @return Value of the loaded pointer
*/
static inline void * atomic_load_ptr(void **ptr_addr) {
return (void *)atomic_load_uintptr((const volatile uintptr_t *)ptr_addr);
}
/**
* @brief Load an `kernel_pid_t` atomically
*
* @param[in] var Variable to load atomically
* @return The value stored in @p var
*/
static inline kernel_pid_t atomic_load_kernel_pid(const volatile kernel_pid_t *var)
{
return (kernel_pid_t)atomic_load_u16((const volatile uint16_t *)var);
}
/** @} */
/**
* @name Atomic Stores
* @{
*/
/**
* @brief Store an `uint8_t` atomically
* @param[out] dest Location to atomically write the new value to
* @param[in] val Value to write
*/
static inline void atomic_store_u8(volatile uint8_t *dest, uint8_t val);
/**
* @brief Store an `uint16_t` atomically
* @param[out] dest Location to atomically write the new value to
* @param[in] val Value to write
*/
static inline void atomic_store_u16(volatile uint16_t *dest, uint16_t val);
/**
* @brief Store an `uint32_t` atomically
* @param[out] dest Location to atomically write the new value to
* @param[in] val Value to write
*/
static inline void atomic_store_u32(volatile uint32_t *dest, uint32_t val);
/**
* @brief Store an `uint64_t` atomically
* @param[out] dest Location to atomically write the new value to
* @param[in] val Value to write
*/
static inline void atomic_store_u64(volatile uint64_t *dest, uint64_t val);
/**
* @brief Store an `uint64_t` atomically
* @param[out] dest Location to atomically write the new value to
* @param[in] val Value to write
*
* @note This is effectively an alias of @ref atomic_store_u64,
* @ref atomic_store_u32, or @ref atomic_store_u16 depending on the
* size of `unsigned int`.
*/
static inline void atomic_store_unsigned(volatile unsigned *dest, unsigned val)
{
if (sizeof(uint64_t) == sizeof(unsigned)) {
atomic_store_u64((volatile uint64_t *)(uintptr_t)dest, val);
}
else if (sizeof(uint32_t) == sizeof(unsigned)) {
atomic_store_u32((volatile uint32_t *)(uintptr_t)dest, val);
}
else {
atomic_store_u16((volatile uint16_t *)(uintptr_t)dest, val);
}
}
/**
* @brief Store an `uintptr_t` atomically
*
* @param[out] dest Location to atomically write the new value to
* @param[in] val Value to write
*/
static inline void atomic_store_uintptr(volatile uintptr_t *dest, uintptr_t val)
{
if (sizeof(uintptr_t) == 2) {
atomic_store_u16((volatile uint16_t *)dest, (uint16_t)val);
}
else if (sizeof(uintptr_t) == 4) {
atomic_store_u32((volatile uint32_t *)(uintptr_t)dest, (uint32_t)val);
}
else {
atomic_store_u64((volatile uint64_t *)(uintptr_t)dest, (uint64_t)val);
}
}
/**
* @brief Store an `void *` atomically
*
* @param[out] dest Location to atomically write the new value to
* @param[in] val Value to write
*/
static inline void atomic_store_ptr(void **dest, const void *val) {
atomic_store_uintptr((volatile uintptr_t *)dest, (uintptr_t)val);
}
/**
* @brief Store an `kernel_pid_t` atomically
*
* @param[out] dest Location to atomically write the new value to
* @param[in] val Value to write
*/
static inline void atomic_store_kernel_pid(volatile kernel_pid_t *dest,
kernel_pid_t val)
{
atomic_store_u16((volatile uint16_t *)dest, (uint16_t)val);
}
/** @} */
/**
* @name Atomic In-Place Addition
* @{
*/
/**
* @brief Atomically add a value onto a given value
* @param[in,out] dest Add @p summand onto this value atomically in-place
* @param[in] summand Value to add onto @p dest
* @return The value previously stored @p dest
*/
static inline uint8_t atomic_fetch_add_u8(volatile uint8_t *dest,
uint8_t summand);
/**
* @brief Atomically add a value onto a given value
* @param[in,out] dest Add @p summand onto this value atomically in-place
* @param[in] summand Value to add onto @p dest
* @return The value previously stored @p dest
*/
static inline uint16_t atomic_fetch_add_u16(volatile uint16_t *dest,
uint16_t summand);
/**
* @brief Atomically add a value onto a given value
* @param[in,out] dest Add @p summand onto this value atomically in-place
* @param[in] summand Value to add onto @p dest
* @return The value previously stored @p dest
*/
static inline uint32_t atomic_fetch_add_u32(volatile uint32_t *dest,
uint32_t summand);
/**
* @brief Atomically add a value onto a given value
* @param[in,out] dest Add @p summand onto this value atomically in-place
* @param[in] summand Value to add onto @p dest
* @return The value previously stored @p dest
*/
static inline uint64_t atomic_fetch_add_u64(volatile uint64_t *dest,
uint64_t summand);
/**
* @brief Atomically add a value onto a given value
* @param[in,out] dest Add @p summand onto this value atomically in-place
* @param[in] summand Value to add onto @p dest
* @return The value previously stored @p dest
*
* @note This is effectively an alias of @ref atomic_fetch_add_u64,
* @ref atomic_fetch_add_u32, or @ref atomic_fetch_add_u16 depending
* on the size of `unsigned int`.
*/
static inline unsigned atomic_fetch_add_unsigned(volatile unsigned *dest,
unsigned summand)
{
if (sizeof(unsigned) == sizeof(uint64_t)) {
return atomic_fetch_add_u64((volatile uint64_t *)(uintptr_t)dest, summand);
}
if (sizeof(unsigned) == sizeof(uint32_t)) {
return atomic_fetch_add_u32((volatile uint32_t *)(uintptr_t)dest, summand);
}
return atomic_fetch_add_u16((volatile uint16_t *)(uintptr_t)dest, summand);
}
/** @} */
/**
* @name Atomic In-Place Subtraction
* @{
*/
/**
* @brief Atomically subtract a value from a given value
* @param[in,out] dest Subtract @p subtrahend from this value
* atomically in-place
* @param[in] subtrahend Value to subtract from @p dest
* @return The value previously stored @p dest
*/
static inline uint8_t atomic_fetch_sub_u8(volatile uint8_t *dest,
uint8_t subtrahend);
/**
* @brief Atomically subtract a value from a given value
* @param[in,out] dest Subtract @p subtrahend from this value
* atomically in-place
* @param[in] subtrahend Value to subtract from @p dest
* @return The value previously stored @p dest
*/
static inline uint16_t atomic_fetch_sub_u16(volatile uint16_t *dest,
uint16_t subtrahend);
/**
* @brief Atomically subtract a value from a given value
* @param[in,out] dest Subtract @p subtrahend from this value
* atomically in-place
* @param[in] subtrahend Value to subtract from @p dest
* @return The value previously stored @p dest
*/
static inline uint32_t atomic_fetch_sub_u32(volatile uint32_t *dest,
uint32_t subtrahend);
/**
* @brief Atomically subtract a value from a given value
* @param[in,out] dest Subtract @p subtrahend from this value
* atomically in-place
* @param[in] subtrahend Value to subtract from @p dest
* @return The value previously stored @p dest
*/
static inline uint64_t atomic_fetch_sub_u64(volatile uint64_t *dest,
uint64_t subtrahend);
/**
* @brief Atomically subtract a value from a given value
* @param[in,out] dest Subtract @p subtrahend from this value
* atomically in-place
* @param[in] subtrahend Value to subtract from @p dest
* @return The value previously stored @p dest
*
* @note This is effectively an alias of @ref atomic_fetch_sub_u64,
* @ref atomic_fetch_sub_u32, or @ref atomic_fetch_sub_u16 depending
* on the size of `unsigned int`.
*/
static inline unsigned atomic_fetch_sub_unsigned(volatile unsigned *dest,
unsigned subtrahend)
{
if (sizeof(unsigned) == sizeof(uint64_t)) {
return atomic_fetch_sub_u64((volatile uint64_t *)(uintptr_t)dest, subtrahend);
}
if (sizeof(unsigned) == sizeof(uint32_t)) {
return atomic_fetch_sub_u32((volatile uint32_t *)(uintptr_t)dest, subtrahend);
}
return atomic_fetch_sub_u16((volatile uint16_t *)(uintptr_t)dest, subtrahend);
}
/** @} */
/**
* @name Atomic In-Place Bitwise OR
* @{
*/
/**
* @brief Atomic version of `*dest |= val`
* @param[in,out] dest Replace this value with the result of
* `*dest | val`
* @param[in] val Value to bitwise or into @p dest in-place
* @return The value previously stored @p dest
*/
static inline uint8_t atomic_fetch_or_u8(volatile uint8_t *dest, uint8_t val);
/**
* @brief Atomic version of `*dest |= val`
* @param[in,out] dest Replace this value with the result of
* `*dest | val`
* @param[in] val Value to bitwise or into @p dest in-place
* @return The value previously stored @p dest
*/
static inline uint16_t atomic_fetch_or_u16(volatile uint16_t *dest,
uint16_t val);
/**
* @brief Atomic version of `*dest |= val`
* @param[in,out] dest Replace this value with the result of
* `*dest | val`
* @param[in] val Value to bitwise or into @p dest in-place
* @return The value previously stored @p dest
*/
static inline uint32_t atomic_fetch_or_u32(volatile uint32_t *dest,
uint32_t val);
/**
* @brief Atomic version of `*dest |= val`
* @param[in,out] dest Replace this value with the result of
* `*dest | val`
* @param[in] val Value to bitwise or into @p dest in-place
* @return The value previously stored @p dest
*/
static inline uint64_t atomic_fetch_or_u64(volatile uint64_t *dest,
uint64_t val);
/**
* @brief Atomic version of `*dest |= val`
* @param[in,out] dest Replace this value with the result of
* `*dest | val`
* @param[in] val Value to bitwise or into @p dest in-place
* @return The value previously stored @p dest
*
* @note This is effectively an alias of @ref atomic_fetch_or_u64,
* @ref atomic_fetch_or_u32, or @ref atomic_fetch_or_u16 depending
* on the size of `unsigned int`.
*/
static inline unsigned atomic_fetch_or_unsigned(volatile unsigned *dest,
unsigned val)
{
if (sizeof(unsigned) == sizeof(uint64_t)) {
return atomic_fetch_or_u64((volatile uint64_t *)(uintptr_t)dest, val);
}
if (sizeof(unsigned) == sizeof(uint32_t)) {
return atomic_fetch_or_u32((volatile uint32_t *)(uintptr_t)dest, val);
}
return atomic_fetch_or_u16((volatile uint16_t *)(uintptr_t)dest, val);
}
/** @} */
/**
* @name Atomic In-Place Bitwise XOR
* @{
*/
/**
* @brief Atomic version of `*dest ^= val`
* @param[in,out] dest Replace this value with the result of
* `*dest ^ val`
* @param[in] val Value to bitwise xor into @p dest in-place
* @return The value previously stored @p dest
*/
static inline uint8_t atomic_fetch_xor_u8(volatile uint8_t *dest, uint8_t val);
/**
* @brief Atomic version of `*dest ^= val`
* @param[in,out] dest Replace this value with the result of
* `*dest ^ val`
* @param[in] val Value to bitwise xor into @p dest in-place
* @return The value previously stored @p dest
*/
static inline uint16_t atomic_fetch_xor_u16(volatile uint16_t *dest,
uint16_t val);
/**
* @brief Atomic version of `*dest ^= val`
* @param[in,out] dest Replace this value with the result of
* `*dest ^ val`
* @param[in] val Value to bitwise xor into @p dest in-place
* @return The value previously stored @p dest
*/
static inline uint32_t atomic_fetch_xor_u32(volatile uint32_t *dest,
uint32_t val);
/**
* @brief Atomic version of `*dest ^= val`
* @param[in,out] dest Replace this value with the result of
* `*dest ^ val`
* @param[in] val Value to bitwise xor into @p dest in-place
* @return The value previously stored @p dest
*/
static inline uint64_t atomic_fetch_xor_u64(volatile uint64_t *dest,
uint64_t val);
/**
* @brief Atomic version of `*dest ^= val`
* @param[in,out] dest Replace this value with the result of
* `*dest ^ val`
* @param[in] val Value to bitwise xor into @p dest in-place
* @return The value previously stored @p dest
*
* @note This is effectively an alias of @ref atomic_fetch_xor_u64,
* @ref atomic_fetch_xor_u32, xor @ref atomic_fetch_xor_u16 depending
* on the size of `unsigned int`.
*/
static inline unsigned atomic_fetch_xor_unsigned(volatile unsigned *dest,
unsigned val)
{
if (sizeof(unsigned) == sizeof(uint64_t)) {
return atomic_fetch_xor_u64((volatile uint64_t *)(uintptr_t)dest, val);
}
if (sizeof(unsigned) == sizeof(uint32_t)) {
return atomic_fetch_xor_u32((volatile uint32_t *)(uintptr_t)dest, val);
}
return atomic_fetch_xor_u16((volatile uint16_t *)(uintptr_t)dest, val);
}
/** @} */
/**
* @name Atomic In-Place Bitwise AND
* @{
*/
/**
* @brief Atomic version of `*dest &= val`
* @param[in,out] dest Replace this value with the result of
* `*dest & val`
* @param[in] val Value to bitwise and into @p dest in-place
* @return The value previously stored @p dest
*/
static inline uint8_t atomic_fetch_and_u8(volatile uint8_t *dest, uint8_t val);
/**
* @brief Atomic version of `*dest &= val`
* @param[in,out] dest Replace this value with the result of
* `*dest & val`
* @param[in] val Value to bitwise and into @p dest in-place
* @return The value previously stored @p dest
*/
static inline uint16_t atomic_fetch_and_u16(volatile uint16_t *dest,
uint16_t val);
/**
* @brief Atomic version of `*dest &= val`
* @param[in,out] dest Replace this value with the result of
* `*dest & val`
* @param[in] val Value to bitwise and into @p dest in-place
* @return The value previously stored @p dest
*/
static inline uint32_t atomic_fetch_and_u32(volatile uint32_t *dest,
uint32_t val);
/**
* @brief Atomic version of `*dest &= val`
* @param[in,out] dest Replace this value with the result of
* `*dest & val`
* @param[in] val Value to bitwise and into @p dest in-place
* @return The value previously stored @p dest
*/
static inline uint64_t atomic_fetch_and_u64(volatile uint64_t *dest,
uint64_t val);
/**
* @brief Atomic version of `*dest &= val`
* @param[in,out] dest Replace this value with the result of
* `*dest & val`
* @param[in] val Value to bitwise and into @p dest in-place
* @return The value previously stored @p dest
*
* @note This is effectively an alias of @ref atomic_fetch_and_u64,
* @ref atomic_fetch_and_u32, and @ref atomic_fetch_and_u16 depending
* on the size of `unsigned int`.
*/
static inline unsigned atomic_fetch_and_unsigned(volatile unsigned *dest,
unsigned val)
{
if (sizeof(unsigned) == sizeof(uint64_t)) {
return atomic_fetch_and_u64((volatile uint64_t *)(uintptr_t)dest, val);
}
if (sizeof(unsigned) == sizeof(uint32_t)) {
return atomic_fetch_and_u32((volatile uint32_t *)(uintptr_t)dest, val);
}
return atomic_fetch_and_u16((volatile uint16_t *)(uintptr_t)dest, val);
}
/** @} */
/**
* @name Helper Functions to Handle Atomic Bit References
* @{
*/
/**
* @brief Create a reference to a bit in an `uint8_t`
* @param[in] dest Memory containing the bit
* @param[in] bit Bit number (`0` refers to the least significant)
*
* @return Opaque reference to the bit.
*/
static inline atomic_bit_u8_t atomic_bit_u8(volatile uint8_t *dest,
uint8_t bit);
/**
* @brief Create a reference to a bit in an `uint16_t`
* @param[in] dest Memory containing the bit
* @param[in] bit Bit number (`0` refers to the least significant)
*
* @return Opaque reference to the bit.
*/
static inline atomic_bit_u16_t atomic_bit_u16(volatile uint16_t *dest,
uint8_t bit);
/**
* @brief Create a reference to a bit in an `uint32_t`
* @param[in] dest Memory containing the bit
* @param[in] bit Bit number (`0` refers to the least significant)
*
* @return Opaque reference to the bit.
*/
static inline atomic_bit_u32_t atomic_bit_u32(volatile uint32_t *dest,
uint8_t bit);
/**
* @brief Create a reference to a bit in an `uint64_t`
* @param[in] dest Memory containing the bit
* @param[in] bit Bit number (`0` refers to the least significant)
*
* @return Opaque reference to the bit.
*/
static inline atomic_bit_u64_t atomic_bit_u64(volatile uint64_t *dest,
uint8_t bit);
/**
* @brief Create a reference to a bit in an `unsigned int`
* @param[in] dest Memory containing the bit
* @param[in] bit Bit number (`0` refers to the least significant)
*
* @return Opaque reference to the bit.
*/
static inline atomic_bit_unsigned_t atomic_bit_unsigned(volatile unsigned *dest,
uint8_t bit)
{
/* Some archs define uint32_t as unsigned long, uint16_t as short etc.,
* we need to cast. */
#if UINT_MAX == UINT16_MAX
return atomic_bit_u16((uint16_t volatile *)dest, bit);
#elif UINT_MAX == UINT32_MAX
return atomic_bit_u32((uint32_t volatile *)dest, bit);
#else
return atomic_bit_u64((uint64_t volatile *)dest, bit);
#endif
}
/** @} */
/**
* @name Atomic Bit Setting
* @{
*/
/**
* @brief Atomic version of `*dest |= (1 << bit)`
* @param[in,out] bit bit to set
*/
static inline void atomic_set_bit_u8(atomic_bit_u8_t bit);
/**
* @brief Atomic version of `*dest |= (1 << bit)`
* @param[in,out] bit bit to set
*/
static inline void atomic_set_bit_u16(atomic_bit_u16_t bit);
/**
* @brief Atomic version of `*dest |= (1 << bit)`
* @param[in,out] bit bit to set
*/
static inline void atomic_set_bit_u32(atomic_bit_u32_t bit);
/**
* @brief Atomic version of `*dest |= (1 << bit)`
* @param[in,out] bit bit to set
*/
static inline void atomic_set_bit_u64(atomic_bit_u64_t bit);
/**
* @brief Atomic version of `*dest |= (1 << bit)`
* @param[in,out] bit bit to set
*/
static inline void atomic_set_bit_unsigned(atomic_bit_unsigned_t bit)
{
#if UINT_MAX == UINT16_MAX
atomic_set_bit_u16(bit);
#elif UINT_MAX == UINT32_MAX
atomic_set_bit_u32(bit);
#else
atomic_set_bit_u64(bit);
#endif
}
/** @} */
/**
* @name Atomic Bit Clearing
* @{
*/
/**
* @brief Atomic version of `*dest &= ~(1 << bit)`
* @param[in,out] bit bit to set
*/
static inline void atomic_clear_bit_u8(atomic_bit_u8_t bit);
/**
* @brief Atomic version of `*dest &= ~(1 << bit)`
* @param[in,out] bit bit to set
*/
static inline void atomic_clear_bit_u16(atomic_bit_u16_t bit);
/**
* @brief Atomic version of `*dest &= ~(1 << bit)`
* @param[in,out] bit bit to set
*/
static inline void atomic_clear_bit_u32(atomic_bit_u32_t bit);
/**
* @brief Atomic version of `*dest &= ~(1 << bit)`
* @param[in,out] bit bit to set
*/
static inline void atomic_clear_bit_u64(atomic_bit_u64_t bit);
/**
* @brief Atomic version of `*dest &= ~(1 << bit)`
* @param[in,out] bit bit to set
*/
static inline void atomic_clear_bit_unsigned(atomic_bit_unsigned_t bit)
{
#if UINT_MAX == UINT16_MAX
atomic_clear_bit_u16(bit);
#elif UINT_MAX == UINT32_MAX
atomic_clear_bit_u32(bit);
#else
atomic_clear_bit_u64(bit);
#endif
}
/** @} */
/**
* @name Semi-Atomic In-Place Addition
* @{
*/
/**
* @brief Semi-atomically add a value onto a given value
* @param[in,out] dest Add @p summand onto this value semi-atomically
* in-place
* @param[in] summand Value to add onto @p dest
* @return The value previously stored @p dest
*/
static inline uint8_t semi_atomic_fetch_add_u8(volatile uint8_t *dest,
uint8_t summand);
/**
* @brief Semi-atomically add a value onto a given value
* @param[in,out] dest Add @p summand onto this value semi-atomically
* in-place
* @param[in] summand Value to add onto @p dest
* @return The value previously stored @p dest
*/
static inline uint16_t semi_atomic_fetch_add_u16(volatile uint16_t *dest,
uint16_t summand);
/**
* @brief Semi-atomically add a value onto a given value
* @param[in,out] dest Add @p summand onto this value semi-atomically
* in-place
* @param[in] summand Value to add onto @p dest
* @return The value previously stored @p dest
*/
static inline uint32_t semi_atomic_fetch_add_u32(volatile uint32_t *dest,
uint32_t summand);
/**
* @brief Semi-atomically add a value onto a given value
* @param[in,out] dest Add @p summand onto this value semi-atomically
* in-place
* @param[in] summand Value to add onto @p dest
* @return The value previously stored @p dest
*/
static inline uint64_t semi_atomic_fetch_add_u64(volatile uint64_t *dest,
uint64_t summand);
/**
* @brief Semi-atomically add a value onto a given value
* @param[in,out] dest Add @p summand onto this value semi-atomically
* in-place
* @param[in] summand Value to add onto @p dest
* @return The value previously stored @p dest
*
* @note This is effectively an alias of @ref semi_atomic_fetch_add_u64,
* @ref semi_atomic_fetch_add_u32, or @ref semi_atomic_fetch_add_u16,
* depending on the size of `unsigned int`.
*/
static inline unsigned semi_atomic_fetch_add_unsigned(volatile unsigned *dest,
unsigned summand)
{
if (sizeof(unsigned) == sizeof(uint64_t)) {
return semi_atomic_fetch_add_u64((volatile uint64_t *)(uintptr_t)dest, summand);
}
if (sizeof(unsigned) == sizeof(uint32_t)) {
return semi_atomic_fetch_add_u32((volatile uint32_t *)(uintptr_t)dest, summand);
}
return semi_atomic_fetch_add_u16((volatile uint16_t *)(uintptr_t)dest, summand);
}
/** @} */
/**
* @name Semi-Atomic In-Place Subtraction
* @{
*/
/**
* @brief Semi-atomically subtract a value from a given value
* @param[in,out] dest Subtract @p subtrahend from this value
* semi-atomically in-place
* @param[in] subtrahend Value to subtract from @p dest
* @return The value previously stored @p dest
*/
static inline uint8_t semi_atomic_fetch_sub_u8(volatile uint8_t *dest,
uint8_t subtrahend);
/**
* @brief Semi-atomically subtract a value from a given value
* @param[in,out] dest Subtract @p subtrahend from this value
* semi-atomically in-place
* @param[in] subtrahend Value to subtract from @p dest
* @return The value previously stored @p dest
*/
static inline uint16_t semi_atomic_fetch_sub_u16(volatile uint16_t *dest,
uint16_t subtrahend);
/**
* @brief Semi-atomically subtract a value from a given value
* @param[in,out] dest Subtract @p subtrahend from this value
* semi-atomically in-place
* @param[in] subtrahend Value to subtract from @p dest
* @return The value previously stored @p dest
*/
static inline uint32_t semi_atomic_fetch_sub_u32(volatile uint32_t *dest,
uint32_t subtrahend);
/**
* @brief Semi-atomically subtract a value from a given value
* @param[in,out] dest Subtract @p subtrahend from this value
* semi-atomically in-place
* @param[in] subtrahend Value to subtract from @p dest
* @return The value previously stored @p dest
*/
static inline uint64_t semi_atomic_fetch_sub_u64(volatile uint64_t *dest,
uint64_t subtrahend);
/**
* @brief Semi-atomically subtract a value from a given value
* @param[in,out] dest Subtract @p subtrahend from this value
* semi-atomically in-place
* @param[in] subtrahend Value to subtract from @p dest
* @return The value previously stored @p dest
*
* @note This is effectively an alias of @ref semi_atomic_fetch_sub_u64,
* @ref semi_atomic_fetch_sub_u32, or @ref semi_atomic_fetch_sub_u16,
* depending on the size of `unsigned int`.
*/
static inline unsigned semi_atomic_fetch_sub_unsigned(volatile unsigned *dest,