Add a compat stdatomic.h implementation based on GCC atomics
[ffmpeg.git] / compat / atomics / gcc / stdatomic.h
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 /*
20  * based on vlc_atomic.h from VLC
21  * Copyright (C) 2010 RĂ©mi Denis-Courmont
22  */
23
24 #ifndef FFMPEG_COMPAT_ATOMICS_GCC_STDATOMIC_H
25 #define FFMPEG_COMPAT_ATOMICS_GCC_STDATOMIC_H
26
27 #include <stddef.h>
28 #include <stdint.h>
29
30 #define ATOMIC_FLAG_INIT 0
31
32 #define ATOMIC_VAR_INIT(value) (value)
33
34 #define atomic_init(obj, value) \
35 do {                            \
36     *(obj) = (value);           \
37 } while(0)
38
39 #define kill_dependency(y) ((void)0)
40
41 #define atomic_thread_fence(order) \
42     __sync_synchronize()
43
44 #define atomic_signal_fence(order) \
45     ((void)0)
46
47 #define atomic_is_lock_free(obj) 0
48
49 typedef         _Bool      atomic_flag;
50 typedef         _Bool      atomic_bool;
51 typedef          char      atomic_char;
52 typedef   signed char      atomic_schar;
53 typedef unsigned char      atomic_uchar;
54 typedef          short     atomic_short;
55 typedef unsigned short     atomic_ushort;
56 typedef          int       atomic_int;
57 typedef unsigned int       atomic_uint;
58 typedef          long      atomic_long;
59 typedef unsigned long      atomic_ulong;
60 typedef          long long atomic_llong;
61 typedef unsigned long long atomic_ullong;
62 typedef          wchar_t   atomic_wchar_t;
63 typedef       int_least8_t atomic_int_least8_t;
64 typedef      uint_least8_t atomic_uint_least8_t;
65 typedef      int_least16_t atomic_int_least16_t;
66 typedef     uint_least16_t atomic_uint_least16_t;
67 typedef      int_least32_t atomic_int_least32_t;
68 typedef     uint_least32_t atomic_uint_least32_t;
69 typedef      int_least64_t atomic_int_least64_t;
70 typedef     uint_least64_t atomic_uint_least64_t;
71 typedef       int_fast8_t atomic_int_fast8_t;
72 typedef      uint_fast8_t atomic_uint_fast8_t;
73 typedef      int_fast16_t atomic_int_fast16_t;
74 typedef     uint_fast16_t atomic_uint_fast16_t;
75 typedef      int_fast32_t atomic_int_fast32_t;
76 typedef     uint_fast32_t atomic_uint_fast32_t;
77 typedef      int_fast64_t atomic_int_fast64_t;
78 typedef     uint_fast64_t atomic_uint_fast64_t;
79 typedef          intptr_t atomic_intptr_t;
80 typedef         uintptr_t atomic_uintptr_t;
81 typedef            size_t atomic_size_t;
82 typedef         ptrdiff_t atomic_ptrdiff_t;
83 typedef          intmax_t atomic_intmax_t;
84 typedef         uintmax_t atomic_uintmax_t;
85
86 #define atomic_store(object, desired)   \
87 do {                                    \
88     *(object) = (desired);              \
89     __sync_synchronize();               \
90 } while (0)
91
92 #define atomic_store_explicit(object, desired, order) \
93     atomic_store(object, desired)
94
95 #define atomic_load(object) \
96     (__sync_synchronize(), *(object))
97
98 #define atomic_load_explicit(object, order) \
99     atomic_load(object)
100
101 #define atomic_exchange(object, desired)                            \
102 ({                                                                  \
103     typeof(object) _obj = (object);                                 \
104     typeof(*object) _old;                                           \
105     do                                                              \
106         _old = atomic_load(_obj);                                   \
107     while (!__sync_bool_compare_and_swap(_obj, _old, (desired)));   \
108     _old;                                                           \
109 })
110
111 #define atomic_exchange_explicit(object, desired, order) \
112     atomic_exchange(object, desired)
113
114 #define atomic_compare_exchange_strong(object, expected, desired)   \
115 ({                                                                  \
116     typeof(object) _exp = (expected);                               \
117     typeof(*object) _old = *_exp;                                   \
118     *_exp = __sync_val_compare_and_swap((object), _old, (desired)); \
119     *_exp == _old;                                                  \
120 })
121
122 #define atomic_compare_exchange_strong_explicit(object, expected, desired, success, failure) \
123     atomic_compare_exchange_strong(object, expected, desired)
124
125 #define atomic_compare_exchange_weak(object, expected, desired) \
126     atomic_compare_exchange_strong(object, expected, desired)
127
128 #define atomic_compare_exchange_weak_explicit(object, expected, desired, success, failure) \
129     atomic_compare_exchange_weak(object, expected, desired)
130
131 #define atomic_fetch_add(object, operand) \
132     __sync_fetch_and_add(object, operand)
133
134 #define atomic_fetch_add_explicit(object, operand, order) \
135     atomic_fetch_add(object, operand)
136
137 #define atomic_fetch_sub(object, operand) \
138     __sync_fetch_and_sub(object, operand)
139
140 #define atomic_fetch_sub_explicit(object, operand, order) \
141     atomic_fetch_sub(object, operand)
142
143 #define atomic_fetch_or(object, operand) \
144     __sync_fetch_and_or(object, operand)
145
146 #define atomic_fetch_or_explicit(object, operand, order) \
147     atomic_fetch_or(object, operand)
148
149 #define atomic_fetch_xor(object, operand) \
150     __sync_fetch_and_sub(object, operand)
151
152 #define atomic_fetch_xor_explicit(object, operand, order) \
153     atomic_fetch_sub(object, operand)
154
155 #define atomic_fetch_and(object, operand) \
156     __sync_fetch_and_and(object, operand)
157
158 #define atomic_fetch_and_explicit(object, operand, order) \
159     atomic_fetch_and(object, operand)
160
161 #define atomic_flag_test_and_set(object) \
162     atomic_exchange(object, 1)
163
164 #define atomic_flag_test_and_set_explicit(object, order) \
165     atomic_flag_test_and_set(object)
166
167 #define atomic_flag_clear(object) \
168     atomic_store(object, 0)
169
170 #define atomic_flag_clear_explicit(object, order) \
171     atomic_flag_clear(object)
172
173 #endif /* FFMPEG_COMPAT_ATOMICS_GCC_STDATOMIC_H */