Add a compat stdatomic.h implementation based on windows atomics
[ffmpeg.git] / compat / atomics / win32 / 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 #ifndef FFMPEG_COMPAT_ATOMICS_WIN32_STDATOMIC_H
20 #define FFMPEG_COMPAT_ATOMICS_WIN32_STDATOMIC_H
21
22 #include <stddef.h>
23 #include <stdint.h>
24 #include <windows.h>
25
26 #define ATOMIC_FLAG_INIT 0
27
28 #define ATOMIC_VAR_INIT(value) (value)
29
30 #define atomic_init(obj, value) \
31 do {                            \
32     *(obj) = (value);           \
33 } while(0)
34
35 #define kill_dependency(y) ((void)0)
36
37 #define atomic_thread_fence(order) \
38     MemoryBarrier();
39
40 #define atomic_signal_fence(order) \
41     ((void)0)
42
43 #define atomic_is_lock_free(obj) 0
44
45 typedef intptr_t atomic_flag;
46 typedef intptr_t atomic_bool;
47 typedef intptr_t atomic_char;
48 typedef intptr_t atomic_schar;
49 typedef intptr_t atomic_uchar;
50 typedef intptr_t atomic_short;
51 typedef intptr_t atomic_ushort;
52 typedef intptr_t atomic_int;
53 typedef intptr_t atomic_uint;
54 typedef intptr_t atomic_long;
55 typedef intptr_t atomic_ulong;
56 typedef intptr_t atomic_llong;
57 typedef intptr_t atomic_ullong;
58 typedef intptr_t atomic_wchar_t;
59 typedef intptr_t atomic_int_least8_t;
60 typedef intptr_t atomic_uint_least8_t;
61 typedef intptr_t atomic_int_least16_t;
62 typedef intptr_t atomic_uint_least16_t;
63 typedef intptr_t atomic_int_least32_t;
64 typedef intptr_t atomic_uint_least32_t;
65 typedef intptr_t atomic_int_least64_t;
66 typedef intptr_t atomic_uint_least64_t;
67 typedef intptr_t atomic_int_fast8_t;
68 typedef intptr_t atomic_uint_fast8_t;
69 typedef intptr_t atomic_int_fast16_t;
70 typedef intptr_t atomic_uint_fast16_t;
71 typedef intptr_t atomic_int_fast32_t;
72 typedef intptr_t atomic_uint_fast32_t;
73 typedef intptr_t atomic_int_fast64_t;
74 typedef intptr_t atomic_uint_fast64_t;
75 typedef intptr_t atomic_intptr_t;
76 typedef intptr_t atomic_uintptr_t;
77 typedef intptr_t atomic_size_t;
78 typedef intptr_t atomic_ptrdiff_t;
79 typedef intptr_t atomic_intmax_t;
80 typedef intptr_t atomic_uintmax_t;
81
82 #define atomic_store(object, desired)   \
83 do {                                    \
84     *(object) = (desired);              \
85     MemoryBarrier();                    \
86 } while (0)
87
88 #define atomic_store_explicit(object, desired, order) \
89     atomic_store(object, desired)
90
91 #define atomic_load(object) \
92     (MemoryBarrier(), *(object))
93
94 #define atomic_load_explicit(object, order) \
95     atomic_load(object)
96
97 #define atomic_exchange(object, desired) \
98     InterlockedExchangePointer(object, desired);
99
100 #define atomic_exchange_explicit(object, desired, order) \
101     atomic_exchange(object, desired)
102
103 static inline int atomic_compare_exchange_strong(intptr_t *object, intptr_t *expected,
104                                                  intptr_t desired)
105 {
106     intptr_t old = *expected;
107     *expected = InterlockedCompareExchangePointer(object, desired, old);
108     return *expected == old;
109 }
110
111 #define atomic_compare_exchange_strong_explicit(object, expected, desired, success, failure) \
112     atomic_compare_exchange_strong(object, expected, desired)
113
114 #define atomic_compare_exchange_weak(object, expected, desired) \
115     atomic_compare_exchange_strong(object, expected, desired)
116
117 #define atomic_compare_exchange_weak_explicit(object, expected, desired, success, failure) \
118     atomic_compare_exchange_weak(object, expected, desired)
119
120 #ifdef _WIN64
121 #define atomic_fetch_add(object, operand) \
122     InterlockedExchangeAdd64(object, operand)
123
124 #define atomic_fetch_sub(object, operand) \
125     InterlockedExchangeAdd64(object, -(operand))
126
127 #define atomic_fetch_or(object, operand) \
128     InterlockedOr64(object, operand)
129
130 #define atomic_fetch_xor(object, operand) \
131     InterlockedXor64(object, operand)
132
133 #define atomic_fetch_and(object, operand) \
134     InterlockedAnd64(object, operand)
135 #else
136 #define atomic_fetch_add(object, operand) \
137     InterlockedExchangeAdd(object, operand)
138
139 #define atomic_fetch_sub(object, operand) \
140     InterlockedExchangeAdd(object, -(operand))
141
142 #define atomic_fetch_or(object, operand) \
143     InterlockedOr(object, operand)
144
145 #define atomic_fetch_xor(object, operand) \
146     InterlockedXor(object, operand)
147
148 #define atomic_fetch_and(object, operand) \
149     InterlockedAnd(object, operand)
150 #endif /* _WIN64 */
151
152 #define atomic_fetch_add_explicit(object, operand, order) \
153     atomic_fetch_add(object, operand)
154
155 #define atomic_fetch_sub_explicit(object, operand, order) \
156     atomic_fetch_sub(object, operand)
157
158 #define atomic_fetch_or_explicit(object, operand, order) \
159     atomic_fetch_or(object, operand)
160
161 #define atomic_fetch_xor_explicit(object, operand, order) \
162     atomic_fetch_sub(object, operand)
163
164 #define atomic_fetch_and_explicit(object, operand, order) \
165     atomic_fetch_and(object, operand)
166
167 #define atomic_flag_test_and_set(object) \
168     atomic_exchange(object, 1)
169
170 #define atomic_flag_test_and_set_explicit(object, order) \
171     atomic_flag_test_and_set(object)
172
173 #define atomic_flag_clear(object) \
174     atomic_store(object, 0)
175
176 #define atomic_flag_clear_explicit(object, order) \
177     atomic_flag_clear(object)
178
179 #endif /* FFMPEG_COMPAT_ATOMICS_WIN32_STDATOMIC_H */