| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/util/atomic.h |
| Date: | 2026-08-09 02:40:25 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 41 | 43 | 95.3% |
| Branches: | 8 | 8 | 100.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | * | ||
| 4 | * Defines wrapper functions for atomic integer operations. Atomic operations | ||
| 5 | * are handled by GCC. | ||
| 6 | */ | ||
| 7 | |||
| 8 | #ifndef CVMFS_UTIL_ATOMIC_H_ | ||
| 9 | #define CVMFS_UTIL_ATOMIC_H_ | ||
| 10 | |||
| 11 | #include <stdint.h> | ||
| 12 | |||
| 13 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 14 | namespace CVMFS_NAMESPACE_GUARD { | ||
| 15 | #endif | ||
| 16 | |||
| 17 | typedef int32_t atomic_int32; | ||
| 18 | typedef int64_t atomic_int64; | ||
| 19 | |||
| 20 | 16165932 | static void inline __attribute__((used)) atomic_init32(atomic_int32 *a) { | |
| 21 | 16165932 | *a = 0; | |
| 22 | 16165932 | } | |
| 23 | |||
| 24 | 10256299 | static void inline __attribute__((used)) atomic_init64(atomic_int64 *a) { | |
| 25 | 10256299 | *a = 0; | |
| 26 | 10256299 | } | |
| 27 | |||
| 28 | 1143555905 | static int32_t inline __attribute__((used)) atomic_read32(atomic_int32 *a) { | |
| 29 | 1143555905 | return __sync_fetch_and_add(a, 0); | |
| 30 | } | ||
| 31 | |||
| 32 | 646308379 | static int64_t inline __attribute__((used)) atomic_read64(atomic_int64 *a) { | |
| 33 | 646308379 | return __sync_fetch_and_add(a, 0); | |
| 34 | } | ||
| 35 | |||
| 36 | 216563125 | static void inline __attribute__((used)) atomic_write32(atomic_int32 *a, | |
| 37 | int32_t value) { | ||
| 38 |
2/2✓ Branch 1 taken 596110800 times.
✓ Branch 2 taken 278662949 times.
|
812673925 | while (!__sync_bool_compare_and_swap(a, atomic_read32(a), value)) { |
| 39 | } | ||
| 40 | 278662949 | } | |
| 41 | |||
| 42 | 225778466 | static void inline __attribute__((used)) atomic_write64(atomic_int64 *a, | |
| 43 | int64_t value) { | ||
| 44 |
2/2✓ Branch 1 taken 404554736 times.
✓ Branch 2 taken 285383490 times.
|
630333202 | while (!__sync_bool_compare_and_swap(a, atomic_read64(a), value)) { |
| 45 | } | ||
| 46 | 285383490 | } | |
| 47 | |||
| 48 | 164499472 | static void inline __attribute__((used)) atomic_inc32(atomic_int32 *a) { | |
| 49 | 164499472 | (void)__sync_fetch_and_add(a, 1); | |
| 50 | 164499472 | } | |
| 51 | |||
| 52 | 243772377 | static void inline __attribute__((used)) atomic_inc64(atomic_int64 *a) { | |
| 53 | 243772377 | (void)__sync_fetch_and_add(a, 1); | |
| 54 | 243772377 | } | |
| 55 | |||
| 56 | 84585888 | static void inline __attribute__((used)) atomic_dec32(atomic_int32 *a) { | |
| 57 | 84585888 | (void)__sync_fetch_and_sub(a, 1); | |
| 58 | 84585888 | } | |
| 59 | |||
| 60 | 76111120 | static void inline __attribute__((used)) atomic_dec64(atomic_int64 *a) { | |
| 61 | 76111120 | (void)__sync_fetch_and_sub(a, 1); | |
| 62 | 76111120 | } | |
| 63 | |||
| 64 | 22114090 | static int32_t inline __attribute__((used)) atomic_xadd32(atomic_int32 *a, | |
| 65 | int32_t offset) { | ||
| 66 |
2/2✓ Branch 0 taken 3281813 times.
✓ Branch 1 taken 18832277 times.
|
22114090 | if (offset < 0) |
| 67 | 3281813 | return __sync_fetch_and_sub(a, -offset); | |
| 68 | 18832277 | return __sync_fetch_and_add(a, offset); | |
| 69 | } | ||
| 70 | |||
| 71 | 134675834 | static int64_t inline __attribute__((used)) atomic_xadd64(atomic_int64 *a, | |
| 72 | int64_t offset) { | ||
| 73 |
2/2✓ Branch 0 taken 31641673 times.
✓ Branch 1 taken 103034161 times.
|
134675834 | if (offset < 0) |
| 74 | 31641673 | return __sync_fetch_and_sub(a, -offset); | |
| 75 | 103034161 | return __sync_fetch_and_add(a, offset); | |
| 76 | } | ||
| 77 | |||
| 78 | 2342812 | static bool inline __attribute__((used)) atomic_cas32(atomic_int32 *a, | |
| 79 | int32_t cmp, | ||
| 80 | int32_t newval) { | ||
| 81 | 2342812 | return __sync_bool_compare_and_swap(a, cmp, newval); | |
| 82 | } | ||
| 83 | |||
| 84 | ✗ | static bool inline __attribute__((used)) atomic_cas64(atomic_int64 *a, | |
| 85 | int64_t cmp, | ||
| 86 | int64_t newval) { | ||
| 87 | // Clang 3.5 has a bug in optimized __sync_bool_compare_and_swap: | ||
| 88 | // https://bugs.llvm.org//show_bug.cgi?format=multiple&id=21499 | ||
| 89 | ✗ | return __sync_bool_compare_and_swap(a, cmp, newval); | |
| 90 | } | ||
| 91 | |||
| 92 | 330 | static void inline __attribute__((used)) MemoryFence() { | |
| 93 | 330 | asm __volatile__("" : : : "memory"); | |
| 94 | 330 | } | |
| 95 | |||
| 96 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 97 | } // namespace CVMFS_NAMESPACE_GUARD | ||
| 98 | #endif | ||
| 99 | |||
| 100 | #endif // CVMFS_UTIL_ATOMIC_H_ | ||
| 101 |