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