VLC  3.0.21
vlc_atomic.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * vlc_atomic.h:
3  *****************************************************************************
4  * Copyright (C) 2010 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20 
21 #ifndef VLC_ATOMIC_H
22 # define VLC_ATOMIC_H
23 
24 /**
25  * \file
26  * Atomic operations do not require locking, but they are not very powerful.
27  */
28 
29 /* Clang older versions support atomics but lacks the stdatomic.h header */
30 #if defined(__clang__)
31 # if !defined(__has_include) || !__has_include(<stdatomic.h>)
32 # define __STDC_NO_ATOMICS__ 1
33 # endif
34 #endif
35 
36 # ifndef __cplusplus
37 # if !defined (__STDC_NO_ATOMICS__)
38 /*** Native C11 atomics ***/
39 # include <stdatomic.h>
40 
41 # else
42 /*** Intel/GCC atomics ***/
43 
44 # define ATOMIC_FLAG_INIT false
45 
46 # define ATOMIC_VAR_INIT(value) (value)
47 
48 # define atomic_init(obj, value) \
49  do { *(obj) = (value); } while(0)
50 
51 # define kill_dependency(y) \
52  ((void)0)
53 
54 # define atomic_thread_fence(order) \
55  __sync_synchronize()
56 
57 # define atomic_signal_fence(order) \
58  ((void)0)
59 
60 # define atomic_is_lock_free(obj) \
61  false
62 
63 typedef bool atomic_flag;
64 typedef bool atomic_bool;
65 typedef char atomic_char;
66 typedef signed char atomic_schar;
67 typedef unsigned char atomic_uchar;
68 typedef short atomic_short;
69 typedef unsigned short atomic_ushort;
70 typedef int atomic_int;
71 typedef unsigned int atomic_uint;
72 typedef long atomic_long;
73 typedef unsigned long atomic_ulong;
74 typedef long long atomic_llong;
75 typedef unsigned long long atomic_ullong;
76 //typedef char16_t atomic_char16_t;
77 //typedef char32_t atomic_char32_t;
78 typedef wchar_t atomic_wchar_t;
79 typedef int_least8_t atomic_int_least8_t;
80 typedef uint_least8_t atomic_uint_least8_t;
81 typedef int_least16_t atomic_int_least16_t;
82 typedef uint_least16_t atomic_uint_least16_t;
83 typedef int_least32_t atomic_int_least32_t;
84 typedef uint_least32_t atomic_uint_least32_t;
85 typedef int_least64_t atomic_int_least64_t;
86 typedef uint_least64_t atomic_uint_least64_t;
87 typedef int_fast8_t atomic_int_fast8_t;
88 typedef uint_fast8_t atomic_uint_fast8_t;
89 typedef int_fast16_t atomic_int_fast16_t;
90 typedef uint_fast16_t atomic_uint_fast16_t;
91 typedef int_fast32_t atomic_int_fast32_t;
92 typedef uint_fast32_t atomic_uint_fast32_t;
93 typedef int_fast64_t atomic_int_fast64_t;
94 typedef uint_fast64_t atomic_uint_fast64_t;
95 typedef intptr_t atomic_intptr_t;
96 typedef uintptr_t atomic_uintptr_t;
97 typedef size_t atomic_size_t;
98 typedef ptrdiff_t atomic_ptrdiff_t;
99 typedef intmax_t atomic_intmax_t;
100 typedef uintmax_t atomic_uintmax_t;
101 
102 # define atomic_store(object,desired) \
103  do { \
104  *(object) = (desired); \
105  __sync_synchronize(); \
106  } while (0)
107 
108 # define atomic_store_explicit(object,desired,order) \
109  atomic_store(object,desired)
110 
111 # define atomic_load(object) \
112  (__sync_synchronize(), *(object))
113 
114 # define atomic_load_explicit(object,order) \
115  atomic_load(object)
116 
117 # define atomic_exchange(object,desired) \
118 ({ \
119  typeof (object) _obj = (object); \
120  typeof (*object) _old; \
121  do \
122  _old = atomic_load(_obj); \
123  while (!__sync_bool_compare_and_swap(_obj, _old, (desired))); \
124  _old; \
125 })
126 
127 # define atomic_exchange_explicit(object,desired,order) \
128  atomic_exchange(object,desired)
129 
130 # define atomic_compare_exchange(object,expected,desired) \
131 ({ \
132  typeof (object) _exp = (expected); \
133  typeof (*object) _old = *_exp; \
134  *_exp = __sync_val_compare_and_swap((object), _old, (desired)); \
135  *_exp == _old; \
136 })
137 
138 # define atomic_compare_exchange_strong(object,expected,desired) \
139  atomic_compare_exchange(object, expected, desired)
140 
141 # define atomic_compare_exchange_strong_explicit(object,expected,desired,order,order_different) \
142  atomic_compare_exchange_strong(object, expected, desired)
143 
144 # define atomic_compare_exchange_weak(object,expected,desired) \
145  atomic_compare_exchange(object, expected, desired)
146 
147 # define atomic_compare_exchange_weak_explicit(object,expected,desired,order_equal,order_different) \
148  atomic_compare_exchange_weak(object, expected, desired)
149 
150 # define atomic_fetch_add(object,operand) \
151  __sync_fetch_and_add(object, operand)
152 
153 # define atomic_fetch_add_explicit(object,operand,order) \
154  atomic_fetch_add(object,operand)
155 
156 # define atomic_fetch_sub(object,operand) \
157  __sync_fetch_and_sub(object, operand)
158 
159 # define atomic_fetch_sub_explicit(object,operand,order) \
160  atomic_fetch_sub(object,operand)
161 
162 # define atomic_fetch_or(object,operand) \
163  __sync_fetch_and_or(object, operand)
164 
165 # define atomic_fetch_or_explicit(object,operand,order) \
166  atomic_fetch_or(object,operand)
167 
168 # define atomic_fetch_xor(object,operand) \
169  __sync_fetch_and_sub(object, operand)
170 
171 # define atomic_fetch_xor_explicit(object,operand,order) \
172  atomic_fetch_sub(object,operand)
173 
174 # define atomic_fetch_and(object,operand) \
175  __sync_fetch_and_and(object, operand)
176 
177 # define atomic_fetch_and_explicit(object,operand,order) \
178  atomic_fetch_and(object,operand)
179 
180 # define atomic_flag_test_and_set(object) \
181  atomic_exchange(object, true)
182 
183 # define atomic_flag_test_and_set_explicit(object,order) \
184  atomic_flag_test_and_set(object)
185 
186 # define atomic_flag_clear(object) \
187  atomic_store(object, false)
188 
189 # define atomic_flag_clear_explicit(object,order) \
190  atomic_flag_clear(object)
191 
192 # endif /* !C11 */
193 
194 typedef atomic_uint_least32_t vlc_atomic_float;
195 
196 static inline void vlc_atomic_init_float(vlc_atomic_float *var, float f)
197 {
198  union { float f; uint32_t i; } u;
199  u.f = f;
200  atomic_init(var, u.i);
201 }
202 
203 /** Helper to retrieve a single precision from an atom. */
204 static inline float vlc_atomic_load_float(vlc_atomic_float *atom)
205 {
206  union { float f; uint32_t i; } u;
207  u.i = atomic_load(atom);
208  return u.f;
209 }
210 
211 /** Helper to store a single precision into an atom. */
212 static inline void vlc_atomic_store_float(vlc_atomic_float *atom, float f)
213 {
214  union { float f; uint32_t i; } u;
215  u.f = f;
216  atomic_store(atom, u.i);
217 }
218 
219 # else /* C++ */
220 /*** Native C++11 atomics ***/
221 # include <atomic>
222 # endif /* C++ */
223 
224 #endif
vlc_common.h
vlc_atomic_float
atomic_uint_least32_t vlc_atomic_float
Definition: vlc_atomic.h:194
vlc_atomic_store_float
static void vlc_atomic_store_float(vlc_atomic_float *atom, float f)
Helper to store a single precision into an atom.
Definition: vlc_atomic.h:212
vlc_atomic_load_float
static float vlc_atomic_load_float(vlc_atomic_float *atom)
Helper to retrieve a single precision from an atom.
Definition: vlc_atomic.h:204
vlc_atomic_init_float
static void vlc_atomic_init_float(vlc_atomic_float *var, float f)
Definition: vlc_atomic.h:196