/src/faac/common/atomic.h
Line | Count | Source |
1 | | /* |
2 | | * FAAC - Freeware Advanced Audio Coder |
3 | | * Copyright (C) 2026 Nils Schimmelmann |
4 | | * |
5 | | * This library is free software; you can redistribute it and/or |
6 | | * modify it under the terms of the GNU Lesser General Public |
7 | | * License as published by the Free Software Foundation; either |
8 | | * version 2.1 of the License, or (at your option) any later version. |
9 | | * |
10 | | * This library is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | | * Lesser General Public License for more details. |
14 | | */ |
15 | | |
16 | | #ifndef FAAC_ATOMIC_H |
17 | | #define FAAC_ATOMIC_H |
18 | | |
19 | | /* One-shot initialization shared by every handle in the process. |
20 | | * State runs 0 (untouched), 1 (init in flight), 2 (published). The first |
21 | | * caller to claim the state runs init and publishes with release; everyone |
22 | | * else observes with acquire, spinning only while init is in flight. */ |
23 | | |
24 | | #if !defined(__STDC_NO_ATOMICS__) && defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) |
25 | | #include <stdatomic.h> |
26 | | typedef atomic_int faac_once_t; |
27 | 1.03k | #define FAAC_ONCE_INIT 0 |
28 | 2.07k | #define faac_once_load(p) atomic_load_explicit((p), memory_order_acquire) |
29 | 2 | #define faac_once_publish(p) atomic_store_explicit((p), 2, memory_order_release) |
30 | | static inline int faac_once_claim(faac_once_t *p) |
31 | 2 | { |
32 | 2 | int expected = 0; |
33 | 2 | return atomic_compare_exchange_strong_explicit(p, &expected, 1, |
34 | 2 | memory_order_acq_rel, |
35 | 2 | memory_order_acquire); |
36 | 2 | } Line | Count | Source | 31 | 1 | { | 32 | 1 | int expected = 0; | 33 | | return atomic_compare_exchange_strong_explicit(p, &expected, 1, | 34 | 1 | memory_order_acq_rel, | 35 | 1 | memory_order_acquire); | 36 | 1 | } |
decoder.c:faac_once_claim Line | Count | Source | 31 | 1 | { | 32 | 1 | int expected = 0; | 33 | | return atomic_compare_exchange_strong_explicit(p, &expected, 1, | 34 | 1 | memory_order_acq_rel, | 35 | 1 | memory_order_acquire); | 36 | 1 | } |
|
37 | | #elif defined(_MSC_VER) |
38 | | #include <intrin.h> |
39 | | /* The Interlocked family are full barriers on every MSVC target, which a |
40 | | * volatile read is not outside /volatile:ms. */ |
41 | | typedef long faac_once_t; |
42 | | #define FAAC_ONCE_INIT 0 |
43 | | #define faac_once_load(p) _InterlockedCompareExchange((p), 0, 0) |
44 | | #define faac_once_publish(p) _InterlockedExchange((p), 2) |
45 | | #define faac_once_claim(p) (_InterlockedCompareExchange((p), 1, 0) == 0) |
46 | | #else |
47 | | /* No atomics on this toolchain: concurrent opens need caller serialization. */ |
48 | | typedef int faac_once_t; |
49 | | #define FAAC_ONCE_INIT 0 |
50 | | #define faac_once_load(p) (*(p)) |
51 | | #define faac_once_publish(p) (*(p) = 2) |
52 | | #define faac_once_claim(p) (*(p) == 0 ? (*(p) = 1) : 0) |
53 | | #endif |
54 | | |
55 | | static inline void faac_once_run(faac_once_t *state, void (*init)(void)) |
56 | 2.07k | { |
57 | 2.07k | if (faac_once_load(state) == 2) |
58 | 2.06k | return; |
59 | 2 | if (faac_once_claim(state)) |
60 | 2 | { |
61 | 2 | init(); |
62 | 2 | faac_once_publish(state); |
63 | 2 | return; |
64 | 2 | } |
65 | 0 | while (faac_once_load(state) != 2) |
66 | 0 | ; |
67 | 0 | } Line | Count | Source | 56 | 1.03k | { | 57 | 1.03k | if (faac_once_load(state) == 2) | 58 | 1.03k | return; | 59 | 1 | if (faac_once_claim(state)) | 60 | 1 | { | 61 | 1 | init(); | 62 | 1 | faac_once_publish(state); | 63 | 1 | return; | 64 | 1 | } | 65 | 0 | while (faac_once_load(state) != 2) | 66 | 0 | ; | 67 | 0 | } |
Line | Count | Source | 56 | 1.03k | { | 57 | 1.03k | if (faac_once_load(state) == 2) | 58 | 1.03k | return; | 59 | 1 | if (faac_once_claim(state)) | 60 | 1 | { | 61 | 1 | init(); | 62 | 1 | faac_once_publish(state); | 63 | 1 | return; | 64 | 1 | } | 65 | 0 | while (faac_once_load(state) != 2) | 66 | 0 | ; | 67 | 0 | } |
|
68 | | |
69 | | #endif /* FAAC_ATOMIC_H */ |