Coverage Report

Created: 2026-12-01 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/external/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
#define FAAC_ONCE_INIT 0
28
#define faac_once_load(p)    atomic_load_explicit((p), memory_order_acquire)
29
#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
0
{
32
0
    int expected = 0;
33
0
    return atomic_compare_exchange_strong_explicit(p, &expected, 1,
34
0
                                                   memory_order_acq_rel,
35
0
                                                   memory_order_acquire);
36
0
}
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
0
{
57
0
    if (faac_once_load(state) == 2)
58
0
        return;
59
0
    if (faac_once_claim(state))
60
0
    {
61
0
        init();
62
0
        faac_once_publish(state);
63
0
        return;
64
0
    }
65
0
    while (faac_once_load(state) != 2)
66
0
        ;
67
0
}
68
69
#endif /* FAAC_ATOMIC_H */