FFmpeg
Loading...
Searching...
No Matches
stdatomic_impl.h
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#ifndef COMPAT_ATOMICS_DUMMY_STDATOMIC_IMPL_H
20#define COMPAT_ATOMICS_DUMMY_STDATOMIC_IMPL_H
21
22#include <string.h>
23
24#define FF_ATOMIC_LOCK_FREE 0
25#define FF_ATOMIC_ALIGN64
26
27#define ff_atomic_thread_fence(order) \
28 ((void)(order))
29
30#define ff_atomic_signal_fence(order) \
31 ((void)(order))
32
33/*
34 * The accesses go through memcpy instead of casted pointers to stay clear
35 * of strict-aliasing violations for types like long that have the size of
36 * an intN_t without being compatible with it. They are ordinary accesses,
37 * a volatile qualification of the object is not honoured.
38 */
39#define DUMMY_ATOMIC_FETCH(op, OP, name, type) \
40static av_always_inline type ff_atomic_##op##name(volatile void *object, \
41 type operand, \
42 memory_order order) \
43{ \
44 type old = ff_atomic_load##name(object, order); \
45 ff_atomic_store##name(object, old OP operand, order); \
46 return old; \
47}
48
49#define DUMMY_ATOMIC_OPS(name, type) \
50static av_always_inline type \
51ff_atomic_load_##name(const volatile void *object, memory_order order) \
52{ \
53 (void)order; \
54 type v; \
55 memcpy(&v, (const void *)object, sizeof(v)); \
56 return v; \
57} \
58 \
59static av_always_inline void ff_atomic_store_##name(volatile void *object, \
60 type desired, \
61 memory_order order) \
62{ \
63 (void)order; \
64 memcpy((void *)object, &desired, sizeof(desired)); \
65} \
66 \
67static av_always_inline type ff_atomic_exchange_##name(volatile void *object, \
68 type desired, \
69 memory_order order) \
70{ \
71 type old = ff_atomic_load_##name(object, order); \
72 ff_atomic_store_##name(object, desired, order); \
73 return old; \
74} \
75 \
76static av_always_inline bool ff_atomic_cas_##name(volatile void *object, \
77 void *expected, \
78 type desired, \
79 memory_order order) \
80{ \
81 type old = ff_atomic_load_##name(object, order); \
82 if (old == ff_atomic_load_##name(expected, order)) { \
83 ff_atomic_store_##name(object, desired, order); \
84 return 1; \
85 } \
86 ff_atomic_store_##name(expected, old, order); \
87 return 0; \
88} \
89 \
90DUMMY_ATOMIC_FETCH(fetch_add, +, _##name, type) \
91DUMMY_ATOMIC_FETCH(fetch_sub, -, _##name, type) \
92DUMMY_ATOMIC_FETCH(fetch_or, |, _##name, type) \
93DUMMY_ATOMIC_FETCH(fetch_xor, ^, _##name, type) \
94DUMMY_ATOMIC_FETCH(fetch_and, &, _##name, type)
95
96/* bool objects get their own set, for normalization */
97DUMMY_ATOMIC_OPS(bool, bool)
98DUMMY_ATOMIC_OPS(8, uint8_t)
99DUMMY_ATOMIC_OPS(16, uint16_t)
100DUMMY_ATOMIC_OPS(32, uint32_t)
101DUMMY_ATOMIC_OPS(64, uint64_t)
102
103#undef DUMMY_ATOMIC_OPS
104#undef DUMMY_ATOMIC_FETCH
105
106#endif /* COMPAT_ATOMICS_DUMMY_STDATOMIC_IMPL_H */
#define DUMMY_ATOMIC_OPS(name, type)