source: rtems-schedsim/schedsim/rtems/sched_cpu/stdatomic.h @ 2d51251

Last change on this file since 2d51251 was 2d51251, checked in by Jennifer Averett <jennifer.averett@…>, on May 9, 2014 at 1:35:58 PM

schedsim: Add smp support.

  • Property mode set to 100644
File size: 14.3 KB
Line 
1/*-
2 * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
3 *                    David Chisnall <theraven@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD$
28 */
29
30#ifndef _STDATOMIC_H_
31#define _STDATOMIC_H_
32
33#include <sys/cdefs.h>
34#include <sys/_types.h>
35
36#define __GNUC_ATOMICS   /* --jla */
37
38/*  --jla
39#if __has_extension(c_atomic) || __has_extension(cxx_atomic)
40#define __CLANG_ATOMICS
41#elif __GNUC_PREREQ__(4, 7)
42#define __GNUC_ATOMICS
43#elif defined(__GNUC__)
44#define __SYNC_ATOMICS
45#else
46#error "stdatomic.h does not support your compiler"
47#endif
48*/
49
50#define __unused   /* --jla */
51#define _Atomic(T)              struct { T volatile __val; }     /* --jla */
52
53/*
54 * 7.17.1 Atomic lock-free macros.
55 */
56
57#ifdef __GCC_ATOMIC_BOOL_LOCK_FREE
58#define ATOMIC_BOOL_LOCK_FREE           __GCC_ATOMIC_BOOL_LOCK_FREE
59#endif
60#ifdef __GCC_ATOMIC_CHAR_LOCK_FREE
61#define ATOMIC_CHAR_LOCK_FREE           __GCC_ATOMIC_CHAR_LOCK_FREE
62#endif
63#ifdef __GCC_ATOMIC_CHAR16_T_LOCK_FREE
64#define ATOMIC_CHAR16_T_LOCK_FREE       __GCC_ATOMIC_CHAR16_T_LOCK_FREE
65#endif
66#ifdef __GCC_ATOMIC_CHAR32_T_LOCK_FREE
67#define ATOMIC_CHAR32_T_LOCK_FREE       __GCC_ATOMIC_CHAR32_T_LOCK_FREE
68#endif
69#ifdef __GCC_ATOMIC_WCHAR_T_LOCK_FREE
70#define ATOMIC_WCHAR_T_LOCK_FREE        __GCC_ATOMIC_WCHAR_T_LOCK_FREE
71#endif
72#ifdef __GCC_ATOMIC_SHORT_LOCK_FREE
73#define ATOMIC_SHORT_LOCK_FREE          __GCC_ATOMIC_SHORT_LOCK_FREE
74#endif
75#ifdef __GCC_ATOMIC_INT_LOCK_FREE
76#define ATOMIC_INT_LOCK_FREE            __GCC_ATOMIC_INT_LOCK_FREE
77#endif
78#ifdef __GCC_ATOMIC_LONG_LOCK_FREE
79#define ATOMIC_LONG_LOCK_FREE           __GCC_ATOMIC_LONG_LOCK_FREE
80#endif
81#ifdef __GCC_ATOMIC_LLONG_LOCK_FREE
82#define ATOMIC_LLONG_LOCK_FREE          __GCC_ATOMIC_LLONG_LOCK_FREE
83#endif
84#ifdef __GCC_ATOMIC_POINTER_LOCK_FREE
85#define ATOMIC_POINTER_LOCK_FREE        __GCC_ATOMIC_POINTER_LOCK_FREE
86#endif
87
88/*
89 * 7.17.2 Initialization.
90 */
91
92#if defined(__CLANG_ATOMICS)
93#define ATOMIC_VAR_INIT(value)          (value)
94#define atomic_init(obj, value)         __c11_atomic_init(obj, value)
95#else
96#define ATOMIC_VAR_INIT(value)          { .__val = (value) }
97#define atomic_init(obj, value)         ((void)((obj)->__val = (value)))
98#endif
99
100/*
101 * Clang and recent GCC both provide predefined macros for the memory
102 * orderings.  If we are using a compiler that doesn't define them, use the
103 * clang values - these will be ignored in the fallback path.
104 */
105
106#ifndef __ATOMIC_RELAXED
107#define __ATOMIC_RELAXED                0
108#endif
109#ifndef __ATOMIC_CONSUME
110#define __ATOMIC_CONSUME                1
111#endif
112#ifndef __ATOMIC_ACQUIRE
113#define __ATOMIC_ACQUIRE                2
114#endif
115#ifndef __ATOMIC_RELEASE
116#define __ATOMIC_RELEASE                3
117#endif
118#ifndef __ATOMIC_ACQ_REL
119#define __ATOMIC_ACQ_REL                4
120#endif
121#ifndef __ATOMIC_SEQ_CST
122#define __ATOMIC_SEQ_CST                5
123#endif
124
125/*
126 * 7.17.3 Order and consistency.
127 *
128 * The memory_order_* constants that denote the barrier behaviour of the
129 * atomic operations.
130 */
131
132typedef enum {
133        memory_order_relaxed = __ATOMIC_RELAXED,
134        memory_order_consume = __ATOMIC_CONSUME,
135        memory_order_acquire = __ATOMIC_ACQUIRE,
136        memory_order_release = __ATOMIC_RELEASE,
137        memory_order_acq_rel = __ATOMIC_ACQ_REL,
138        memory_order_seq_cst = __ATOMIC_SEQ_CST
139} memory_order;
140
141/*
142 * 7.17.4 Fences.
143 */
144
145static __inline void
146atomic_thread_fence(memory_order __order __unused)
147{
148
149#ifdef __CLANG_ATOMICS
150        __c11_atomic_thread_fence(__order);
151#elif defined(__GNUC_ATOMICS)
152        __atomic_thread_fence(__order);
153#else
154        __sync_synchronize();
155#endif
156}
157
158static __inline void
159atomic_signal_fence(memory_order __order __unused)
160{
161
162#ifdef __CLANG_ATOMICS
163        __c11_atomic_signal_fence(__order);
164#elif defined(__GNUC_ATOMICS)
165        __atomic_signal_fence(__order);
166#else
167        __asm volatile ("" ::: "memory");
168#endif
169}
170
171/*
172 * 7.17.5 Lock-free property.
173 */
174
175#if defined(_KERNEL)
176/* Atomics in kernelspace are always lock-free. */
177#define atomic_is_lock_free(obj) \
178        ((void)(obj), (_Bool)1)
179#elif defined(__CLANG_ATOMICS)
180#define atomic_is_lock_free(obj) \
181        __atomic_is_lock_free(sizeof(*(obj)), obj)
182#elif defined(__GNUC_ATOMICS)
183#define atomic_is_lock_free(obj) \
184        __atomic_is_lock_free(sizeof((obj)->__val), &(obj)->__val)
185#else
186#define atomic_is_lock_free(obj) \
187        ((void)(obj), sizeof((obj)->__val) <= sizeof(void *))
188#endif
189
190/*
191 * 7.17.6 Atomic integer types.
192 */
193
194typedef _Atomic(_Bool)                  atomic_bool;
195typedef _Atomic(char)                   atomic_char;
196typedef _Atomic(signed char)            atomic_schar;
197typedef _Atomic(unsigned char)          atomic_uchar;
198typedef _Atomic(short)                  atomic_short;
199typedef _Atomic(unsigned short)         atomic_ushort;
200typedef _Atomic(int)                    atomic_int;
201typedef _Atomic(unsigned int)           atomic_uint;
202typedef _Atomic(long)                   atomic_long;
203typedef _Atomic(unsigned long)          atomic_ulong;
204typedef _Atomic(long long)              atomic_llong;
205typedef _Atomic(unsigned long long)     atomic_ullong;
206#if 0
207typedef _Atomic(__char16_t)             atomic_char16_t;
208typedef _Atomic(__char32_t)             atomic_char32_t;
209#endif
210typedef _Atomic(wchar_t)                atomic_wchar_t;
211typedef _Atomic(int_least8_t)           atomic_int_least8_t;
212typedef _Atomic(uint_least8_t)          atomic_uint_least8_t;
213typedef _Atomic(int_least16_t)          atomic_int_least16_t;
214typedef _Atomic(uint_least16_t)         atomic_uint_least16_t;
215typedef _Atomic(int_least32_t)          atomic_int_least32_t;
216typedef _Atomic(uint_least32_t)         atomic_uint_least32_t;
217typedef _Atomic(int_least64_t)          atomic_int_least64_t;
218typedef _Atomic(uint_least64_t)         atomic_uint_least64_t;
219typedef _Atomic(int_fast8_t)            atomic_int_fast8_t;
220typedef _Atomic(uint_fast8_t)           atomic_uint_fast8_t;
221typedef _Atomic(int_fast16_t)           atomic_int_fast16_t;
222typedef _Atomic(uint_fast16_t)          atomic_uint_fast16_t;
223typedef _Atomic(int_fast32_t)           atomic_int_fast32_t;
224typedef _Atomic(uint_fast32_t)          atomic_uint_fast32_t;
225typedef _Atomic(int_fast64_t)           atomic_int_fast64_t;
226typedef _Atomic(uint_fast64_t)          atomic_uint_fast64_t;
227typedef _Atomic(intptr_t)               atomic_intptr_t;
228typedef _Atomic(uintptr_t)              atomic_uintptr_t;
229typedef _Atomic(size_t)                 atomic_size_t;
230typedef _Atomic(ptrdiff_t)              atomic_ptrdiff_t;
231typedef _Atomic(intmax_t)               atomic_intmax_t;
232typedef _Atomic(uintmax_t)              atomic_uintmax_t;
233
234/*
235 * 7.17.7 Operations on atomic types.
236 */
237
238/*
239 * Compiler-specific operations.
240 */
241
242#if defined(__CLANG_ATOMICS)
243#define atomic_compare_exchange_strong_explicit(object, expected,       \
244    desired, success, failure)                                          \
245        __c11_atomic_compare_exchange_strong(object, expected, desired, \
246            success, failure)
247#define atomic_compare_exchange_weak_explicit(object, expected,         \
248    desired, success, failure)                                          \
249        __c11_atomic_compare_exchange_weak(object, expected, desired,   \
250            success, failure)
251#define atomic_exchange_explicit(object, desired, order)                \
252        __c11_atomic_exchange(object, desired, order)
253#define atomic_fetch_add_explicit(object, operand, order)               \
254        __c11_atomic_fetch_add(object, operand, order)
255#define atomic_fetch_and_explicit(object, operand, order)               \
256        __c11_atomic_fetch_and(object, operand, order)
257#define atomic_fetch_or_explicit(object, operand, order)                \
258        __c11_atomic_fetch_or(object, operand, order)
259#define atomic_fetch_sub_explicit(object, operand, order)               \
260        __c11_atomic_fetch_sub(object, operand, order)
261#define atomic_fetch_xor_explicit(object, operand, order)               \
262        __c11_atomic_fetch_xor(object, operand, order)
263#define atomic_load_explicit(object, order)                             \
264        __c11_atomic_load(object, order)
265#define atomic_store_explicit(object, desired, order)                   \
266        __c11_atomic_store(object, desired, order)
267#elif defined(__GNUC_ATOMICS)
268#define atomic_compare_exchange_strong_explicit(object, expected,       \
269    desired, success, failure)                                          \
270        __atomic_compare_exchange_n(&(object)->__val, expected,         \
271            desired, 0, success, failure)
272#define atomic_compare_exchange_weak_explicit(object, expected,         \
273    desired, success, failure)                                          \
274        __atomic_compare_exchange_n(&(object)->__val, expected,         \
275            desired, 1, success, failure)
276#define atomic_exchange_explicit(object, desired, order)                \
277        __atomic_exchange_n(&(object)->__val, desired, order)
278#define atomic_fetch_add_explicit(object, operand, order)               \
279        __atomic_fetch_add(&(object)->__val, operand, order)
280#define atomic_fetch_and_explicit(object, operand, order)               \
281        __atomic_fetch_and(&(object)->__val, operand, order)
282#define atomic_fetch_or_explicit(object, operand, order)                \
283        __atomic_fetch_or(&(object)->__val, operand, order)
284#define atomic_fetch_sub_explicit(object, operand, order)               \
285        __atomic_fetch_sub(&(object)->__val, operand, order)
286#define atomic_fetch_xor_explicit(object, operand, order)               \
287        __atomic_fetch_xor(&(object)->__val, operand, order)
288#define atomic_load_explicit(object, order)                             \
289        __atomic_load_n(&(object)->__val, order)
290#define atomic_store_explicit(object, desired, order)                   \
291        __atomic_store_n(&(object)->__val, desired, order)
292#else
293#define __atomic_apply_stride(object, operand) \
294        (((__typeof__((object)->__val))0) + (operand))
295#define atomic_compare_exchange_strong_explicit(object, expected,       \
296    desired, success, failure)  __extension__ ({                        \
297        __typeof__(expected) __ep = (expected);                         \
298        __typeof__(*__ep) __e = *__ep;                                  \
299        (void)(success); (void)(failure);                               \
300        (_Bool)((*__ep = __sync_val_compare_and_swap(&(object)->__val,  \
301            __e, desired)) == __e);                                     \
302})
303#define atomic_compare_exchange_weak_explicit(object, expected,         \
304    desired, success, failure)                                          \
305        atomic_compare_exchange_strong_explicit(object, expected,       \
306                desired, success, failure)
307#if 0   /* --jla comment out. */
308#if __has_builtin(__sync_swap)
309/* Clang provides a full-barrier atomic exchange - use it if available. */
310#define atomic_exchange_explicit(object, desired, order)                \
311        ((void)(order), __sync_swap(&(object)->__val, desired))
312#endif  /* --jla */
313#else
314/*
315 * __sync_lock_test_and_set() is only an acquire barrier in theory (although in
316 * practice it is usually a full barrier) so we need an explicit barrier before
317 * it.
318 */
319#define atomic_exchange_explicit(object, desired, order)                \
320__extension__ ({                                                        \
321        __typeof__(object) __o = (object);                              \
322        __typeof__(desired) __d = (desired);                            \
323        (void)(order);                                                  \
324        __sync_synchronize();                                           \
325        __sync_lock_test_and_set(&(__o)->__val, __d);                   \
326})
327#endif
328#define atomic_fetch_add_explicit(object, operand, order)               \
329        ((void)(order), __sync_fetch_and_add(&(object)->__val,          \
330            __atomic_apply_stride(object, operand)))
331#define atomic_fetch_and_explicit(object, operand, order)               \
332        ((void)(order), __sync_fetch_and_and(&(object)->__val, operand))
333#define atomic_fetch_or_explicit(object, operand, order)                \
334        ((void)(order), __sync_fetch_and_or(&(object)->__val, operand))
335#define atomic_fetch_sub_explicit(object, operand, order)               \
336        ((void)(order), __sync_fetch_and_sub(&(object)->__val,          \
337            __atomic_apply_stride(object, operand)))
338#define atomic_fetch_xor_explicit(object, operand, order)               \
339        ((void)(order), __sync_fetch_and_xor(&(object)->__val, operand))
340#define atomic_load_explicit(object, order)                             \
341        ((void)(order), __sync_fetch_and_add(&(object)->__val, 0))
342#define atomic_store_explicit(object, desired, order)                   \
343        ((void)atomic_exchange_explicit(object, desired, order))
344#endif
345
346/*
347 * Convenience functions.
348 *
349 * Don't provide these in kernel space. In kernel space, we should be
350 * disciplined enough to always provide explicit barriers.
351 */
352
353#ifndef _KERNEL
354#define atomic_compare_exchange_strong(object, expected, desired)       \
355        atomic_compare_exchange_strong_explicit(object, expected,       \
356            desired, memory_order_seq_cst, memory_order_seq_cst)
357#define atomic_compare_exchange_weak(object, expected, desired)         \
358        atomic_compare_exchange_weak_explicit(object, expected,         \
359            desired, memory_order_seq_cst, memory_order_seq_cst)
360#define atomic_exchange(object, desired)                                \
361        atomic_exchange_explicit(object, desired, memory_order_seq_cst)
362#define atomic_fetch_add(object, operand)                               \
363        atomic_fetch_add_explicit(object, operand, memory_order_seq_cst)
364#define atomic_fetch_and(object, operand)                               \
365        atomic_fetch_and_explicit(object, operand, memory_order_seq_cst)
366#define atomic_fetch_or(object, operand)                                \
367        atomic_fetch_or_explicit(object, operand, memory_order_seq_cst)
368#define atomic_fetch_sub(object, operand)                               \
369        atomic_fetch_sub_explicit(object, operand, memory_order_seq_cst)
370#define atomic_fetch_xor(object, operand)                               \
371        atomic_fetch_xor_explicit(object, operand, memory_order_seq_cst)
372#define atomic_load(object)                                             \
373        atomic_load_explicit(object, memory_order_seq_cst)
374#define atomic_store(object, desired)                                   \
375        atomic_store_explicit(object, desired, memory_order_seq_cst)
376#endif /* !_KERNEL */
377
378/*
379 * 7.17.8 Atomic flag type and operations.
380 *
381 * XXX: Assume atomic_bool can be used as an atomic_flag. Is there some
382 * kind of compiler built-in type we could use?
383 */
384
385typedef struct {
386        atomic_bool     __flag;
387} atomic_flag;
388
389#define ATOMIC_FLAG_INIT                { ATOMIC_VAR_INIT(0) }
390
391static __inline _Bool
392atomic_flag_test_and_set_explicit(volatile atomic_flag *__object,
393    memory_order __order)
394{
395        return (atomic_exchange_explicit(&__object->__flag, 1, __order));
396}
397
398static __inline void
399atomic_flag_clear_explicit(volatile atomic_flag *__object, memory_order __order)
400{
401
402        atomic_store_explicit(&__object->__flag, 0, __order);
403}
404
405#ifndef _KERNEL
406static __inline _Bool
407atomic_flag_test_and_set(volatile atomic_flag *__object)
408{
409
410        return (atomic_flag_test_and_set_explicit(__object,
411            memory_order_seq_cst));
412}
413
414static __inline void
415atomic_flag_clear(volatile atomic_flag *__object)
416{
417
418        atomic_flag_clear_explicit(__object, memory_order_seq_cst);
419}
420#endif /* !_KERNEL */
421
422#endif /* !_STDATOMIC_H_ */
Note: See TracBrowser for help on using the repository browser.