1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2016 Intel Corporation. 4 ** 5 ** Permission is hereby granted, free of charge, to any person obtaining a copy 6 ** of this software and associated documentation files (the "Software"), to deal 7 ** in the Software without restriction, including without limitation the rights 8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 ** copies of the Software, and to permit persons to whom the Software is 10 ** furnished to do so, subject to the following conditions: 11 ** 12 ** The above copyright notice and this permission notice shall be included in 13 ** all copies or substantial portions of the Software. 14 ** 15 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 ** THE SOFTWARE. 22 ** 23 ****************************************************************************/ 24 25 #ifndef FFD_ATOMIC_GCC_H 26 #define FFD_ATOMIC_GCC_H 27 28 /* atomics */ 29 /* we'll use the GCC 4.7 atomic builtins 30 * See http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html#_005f_005fatomic-Builtins 31 * Or in texinfo: C Extensions > __atomic Builtins 32 */ 33 typedef int ffd_atomic_int; 34 #define ffd_atomic_pointer(type) type* 35 36 #define FFD_ATOMIC_INIT(val) (val) 37 38 #define FFD_ATOMIC_RELAXED __ATOMIC_RELAXED 39 #define FFD_ATOMIC_ACQUIRE __ATOMIC_ACQUIRE 40 #define FFD_ATOMIC_RELEASE __ATOMIC_RELEASE 41 // acq_rel & cst not necessary 42 43 #if !defined(__GNUC__) || \ 44 ((__GNUC__ - 0) * 100 + (__GNUC_MINOR__ - 0)) < 407 || \ 45 (defined(__INTEL_COMPILER) && __INTEL_COMPILER-0 < 1310) || \ 46 (defined(__clang__) && ((__clang_major__-0) * 100 + (__clang_minor-0)) < 303) 47 #define ffd_atomic_load_n(ptr,order) *(ptr) 48 #define ffd_atomic_store_n(ptr,val,order) (*(ptr) = (val), (void)0) 49 #define ffd_atomic_exchange_n(ptr,val,order) __sync_lock_test_and_set(ptr, val) 50 #define ffd_atomic_compare_exchange_n(ptr,expected,desired,weak,order1,order2) \ 51 __sync_bool_compare_and_swap(ptr, *(expected), desired) ? 1 : \ 52 (*(expected) = *(ptr), 0) 53 #define ffd_atomic_add_fetch(ptr,val,order) __sync_add_and_fetch(ptr, val) 54 #else 55 #define ffd_atomic_load(ptr,order) __atomic_load_n(ptr, order) 56 #define ffd_atomic_store(ptr,val,order) __atomic_store_n(ptr, val, order) 57 #define ffd_atomic_exchange(ptr,val,order) __atomic_exchange_n(ptr, val, order) 58 #define ffd_atomic_compare_exchange(ptr,expected,desired,order1,order2) \ 59 __atomic_compare_exchange_n(ptr, expected, desired, 1, order1, order2) 60 #define ffd_atomic_add_fetch(ptr,val,order) __atomic_add_fetch(ptr, val, order) 61 #endif 62 63 #endif 64