Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sol-atomic.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Soletta (TM) Project
3  *
4  * Copyright (C) 2015 Intel Corporation. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
166 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ > 201112L) \
167  || (defined(__GNUC__) && !defined(__clang__) && (__GNUC__ * 100 + __GNUC_MINOR__ > 408)) \
168  || (defined(__clang__) && (__clang_major__ * 100 + __clang_minor__ >= 307)) \
169  || defined(DOXYGEN_RUN)
170 /* GCC 4.9, Clang 3.7 provide <stdatomic.h>, a C11 header */
171 
172 #ifdef __cplusplus
173 #include <atomic>
174 #define _Atomic(T) std::atomic<T>
175 #define atomic_flag std::atomic_flag
176 #define atomic_int std::atomic_int
177 #define atomic_uint std::atomic_uint
178 #define atomic_size_t std::atomic_size_t
179 #define atomic_uintptr_t std::atomic_uintptr_t
180 #else
181 #include <stdatomic.h>
182 #endif
183 
184 /* Initialization */
185 #define SOL_ATOMIC_INIT ATOMIC_VAR_INIT
186 #define SOL_ATOMIC_FLAG_INIT ATOMIC_FLAG_INIT
187 
188 /* Order levels */
189 #define SOL_ATOMIC_RELAXED memory_order_relaxed
190 #define SOL_ATOMIC_CONSUME memory_order_consume
191 #define SOL_ATOMIC_ACQUIRE memory_order_acquire
192 #define SOL_ATOMIC_RELEASE memory_order_release
193 #define SOL_ATOMIC_ACQ_REL memory_order_acq_rel
194 #define SOL_ATOMIC_SEQ_CST memory_order_seq_cst
195 
196 /* Types */
197 typedef atomic_flag sol_atomic_flag;
198 typedef atomic_int sol_atomic_int;
199 typedef atomic_uint sol_atomic_uint;
200 typedef atomic_size_t sol_atomic_size_t;
201 typedef atomic_uintptr_t sol_atomic_uintptr_t;
202 
203 /* Functions */
204 #define sol_atomic_test_and_set atomic_flag_test_and_set_explicit
205 #define sol_atomic_clear atomic_flag_clear_explicit
206 #define sol_atomic_store atomic_store_explicit
207 #define sol_atomic_load atomic_load_explicit
208 #define sol_atomic_exchange atomic_exchange_explicit
209 #define sol_atomic_compare_exchange atomic_compare_exchange_strong_explicit
210 #define sol_atomic_fetch_add atomic_fetch_add_explicit
211 
212 #elif (defined(__GNUC__) && !defined(__clang__) && (__GNUC__ * 100 + __GNUC_MINOR__ > 406))
213 /* GCC 4.6 has intrinsics that allow us to implement the atomic API */
214 #include <stddef.h>
215 #include <stdint.h>
216 
217 /* Initialization */
218 #define SOL_ATOMIC_INIT(x) (x)
219 #define SOL_ATOMIC_FLAG_INIT false
220 
221 /* Order levels */
222 #define SOL_ATOMIC_RELAXED __ATOMIC_RELAXED
223 #define SOL_ATOMIC_CONSUME __ATOMIC_CONSUME
224 #define SOL_ATOMIC_ACQUIRE __ATOMIC_ACQUIRE
225 #define SOL_ATOMIC_RELEASE __ATOMIC_RELEASE
226 #define SOL_ATOMIC_ACQ_REL __ATOMIC_ACQ_REL
227 #define SOL_ATOMIC_SEQ_CST __ATOMIC_SEQ_CST
228 
229 /* Types */
230 typedef unsigned char sol_atomic_flag;
231 typedef int sol_atomic_int;
232 typedef unsigned sol_atomic_uint;
233 typedef size_t sol_atomic_size_t;
234 typedef uintptr_t sol_atomic_uintptr_t;
235 
236 /* Functions */
237 #define sol_atomic_test_and_set __atomic_test_and_set
238 #define sol_atomic_clear __atomic_clear
239 #define sol_atomic_store __atomic_store_n
240 #define sol_atomic_load __atomic_load_n
241 #define sol_atomic_exchange __atomic_exchange_n
242 #define sol_atomic_compare_exchange(object, expected, desired, mo_suc, mo_fail) \
243  __atomic_compare_exchange_n((object), (expected), (desired), 0, (mo_suc), (mo_fail))
244 #define sol_fetch_add __atomic_fetch_add
245 
246 #else
247 #error "Atomic API not supported on this platform, please contribute."
248 #endif
249 
250 
atomic_int sol_atomic_int
An atomic variable compatible with an int.
Definition: sol-atomic.h:198
atomic_flag sol_atomic_flag
An atomic variable that can contain only two states: set or unset.
Definition: sol-atomic.h:197
atomic_uintptr_t sol_atomic_uintptr_t
An atomic variable compatible with an uintptr_t.
Definition: sol-atomic.h:201
atomic_size_t sol_atomic_size_t
An atomic variable compatible with a size_t.
Definition: sol-atomic.h:200
atomic_uint sol_atomic_uint
An atomic variable compatible with an unsigned int.
Definition: sol-atomic.h:199