Directory: | cvmfs/ |
---|---|
File: | cvmfs/util/atomic.h |
Date: | 2025-04-20 02:34:28 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 39 | 41 | 95.1% |
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 | 635979 | static void inline __attribute__((used)) atomic_init32(atomic_int32 *a) { | |
21 | 635979 | *a = 0; | |
22 | 635979 | } | |
23 | |||
24 | 265159 | static void inline __attribute__((used)) atomic_init64(atomic_int64 *a) { | |
25 | 265159 | *a = 0; | |
26 | 265159 | } | |
27 | |||
28 | 64783852 | static int32_t inline __attribute__((used)) atomic_read32(atomic_int32 *a) { | |
29 | 64783852 | return __sync_fetch_and_add(a, 0); | |
30 | } | ||
31 | |||
32 | 40448091 | static int64_t inline __attribute__((used)) atomic_read64(atomic_int64 *a) { | |
33 | 40448091 | return __sync_fetch_and_add(a, 0); | |
34 | } | ||
35 | |||
36 | static void inline __attribute__((used)) | ||
37 | 14406697 | atomic_write32(atomic_int32 *a, int32_t value) { | |
38 |
2/2✓ Branch 1 taken 24674466 times.
✓ Branch 2 taken 17826416 times.
|
39081163 | while (!__sync_bool_compare_and_swap(a, atomic_read32(a), value)) { |
39 | } | ||
40 | 17826416 | } | |
41 | |||
42 | static void inline __attribute__((used)) | ||
43 | 13763120 | atomic_write64(atomic_int64 *a, int64_t value) { | |
44 |
2/2✓ Branch 1 taken 29214535 times.
✓ Branch 2 taken 17049154 times.
|
42977655 | while (!__sync_bool_compare_and_swap(a, atomic_read64(a), value)) { |
45 | } | ||
46 | 17049154 | } | |
47 | |||
48 | 7738520 | static void inline __attribute__((used)) atomic_inc32(atomic_int32 *a) { | |
49 | 7738520 | (void)__sync_fetch_and_add(a, 1); | |
50 | 7738520 | } | |
51 | |||
52 | 11460898 | static void inline __attribute__((used)) atomic_inc64(atomic_int64 *a) { | |
53 | 11460898 | (void)__sync_fetch_and_add(a, 1); | |
54 | 11460898 | } | |
55 | |||
56 | 5304769 | static void inline __attribute__((used)) atomic_dec32(atomic_int32 *a) { | |
57 | 5304769 | (void)__sync_fetch_and_sub(a, 1); | |
58 | 5304769 | } | |
59 | |||
60 | 4468961 | static void inline __attribute__((used)) atomic_dec64(atomic_int64 *a) { | |
61 | 4468961 | (void)__sync_fetch_and_sub(a, 1); | |
62 | 4468961 | } | |
63 | |||
64 | static int32_t inline __attribute__((used)) | ||
65 | 959301 | atomic_xadd32(atomic_int32 *a, int32_t offset) { | |
66 |
2/2✓ Branch 0 taken 193678 times.
✓ Branch 1 taken 765623 times.
|
959301 | if (offset < 0) return __sync_fetch_and_sub(a, -offset); |
67 | 765623 | return __sync_fetch_and_add(a, offset); | |
68 | } | ||
69 | |||
70 | static int64_t inline __attribute__((used)) | ||
71 | 3409386 | atomic_xadd64(atomic_int64 *a, int64_t offset) { | |
72 |
2/2✓ Branch 0 taken 804442 times.
✓ Branch 1 taken 2604944 times.
|
3409386 | if (offset < 0) return __sync_fetch_and_sub(a, -offset); |
73 | 2604944 | return __sync_fetch_and_add(a, offset); | |
74 | } | ||
75 | |||
76 | static bool inline __attribute__((used)) | ||
77 | 147963 | atomic_cas32(atomic_int32 *a, int32_t cmp, int32_t newval) { | |
78 | 147963 | return __sync_bool_compare_and_swap(a, cmp, newval); | |
79 | } | ||
80 | |||
81 | static bool inline __attribute__((used)) | ||
82 | ✗ | atomic_cas64(atomic_int64 *a, int64_t cmp, int64_t newval) { | |
83 | // Clang 3.5 has a bug in optimized __sync_bool_compare_and_swap: | ||
84 | // https://bugs.llvm.org//show_bug.cgi?format=multiple&id=21499 | ||
85 | ✗ | return __sync_bool_compare_and_swap(a, cmp, newval); | |
86 | } | ||
87 | |||
88 | 42 | static void inline __attribute__((used)) MemoryFence() { | |
89 | 42 | asm __volatile__("" : : : "memory"); | |
90 | 42 | } | |
91 | |||
92 | #ifdef CVMFS_NAMESPACE_GUARD | ||
93 | } // namespace CVMFS_NAMESPACE_GUARD | ||
94 | #endif | ||
95 | |||
96 | #endif // CVMFS_UTIL_ATOMIC_H_ | ||
97 |