1// (C) Copyright John Maddock 2013 2 3// Use, modification and distribution are subject to the 4// Boost Software License, Version 1.0. (See accompanying file 5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 7// See http://www.boost.org/libs/config for more information. 8 9// MACRO: BOOST_NO_CXX11_HDR_ATOMIC 10// TITLE: C++11 <atomic> header is either not present or too broken to be used 11// DESCRIPTION: The compiler does not support the C++11 header <atomic> 12 13#include <atomic> 14 15#if !defined(ATOMIC_BOOL_LOCK_FREE) || !defined(ATOMIC_CHAR_LOCK_FREE) || !defined(ATOMIC_CHAR16_T_LOCK_FREE) \ 16 || !defined(ATOMIC_CHAR32_T_LOCK_FREE) || !defined(ATOMIC_WCHAR_T_LOCK_FREE) || !defined(ATOMIC_SHORT_LOCK_FREE)\ 17 || !defined(ATOMIC_INT_LOCK_FREE) || !defined(ATOMIC_LONG_LOCK_FREE) || !defined(ATOMIC_LLONG_LOCK_FREE)\ 18 || !defined(ATOMIC_POINTER_LOCK_FREE) 19# error "required macros not defined" 20#endif 21 22namespace boost_no_cxx11_hdr_atomic { 23 24 void consume(std::memory_order) 25 {} 26 27 int test() 28 { 29 consume(std::memory_order_relaxed); 30 consume(std::memory_order_consume); 31 consume(std::memory_order_acquire); 32 consume(std::memory_order_release); 33 consume(std::memory_order_acq_rel); 34 consume(std::memory_order_seq_cst); 35 36 std::atomic<int> a1; 37 std::atomic<unsigned> a2; 38 std::atomic<int*> a3; 39 a1.is_lock_free(); 40 a1.store(1); 41 a1.load(); 42 a1.exchange(2); 43 int v; 44 a1.compare_exchange_weak(v, 2, std::memory_order_relaxed, std::memory_order_relaxed); 45 a1.compare_exchange_strong(v, 2, std::memory_order_relaxed, std::memory_order_relaxed); 46 a1.fetch_add(2); 47 a1.fetch_sub(3); 48 a1.fetch_and(3); 49 a1.fetch_or(1); 50 a1.fetch_xor(1); 51 a1++; 52 ++a1; 53 a1--; 54 --a1; 55 a1 += 2; 56 a1 -= 2; 57 a1 &= 1; 58 a1 |= 2; 59 a1 ^= 3; 60 61 a2 = 0u; 62 63 a3.store(&v); 64 a3.fetch_add(1); 65 a3.fetch_sub(1); 66 ++a3; 67 --a3; 68 a3++; 69 a3--; 70 a3 += 1; 71 a3 -= 1; 72 73 std::atomic_is_lock_free(&a1); 74 // This produces linker errors on Mingw32 for some reason, probably not required anyway for most uses?? 75 //std::atomic_init(&a1, 2); 76 std::atomic_store(&a1, 3); 77 std::atomic_store_explicit(&a1, 3, std::memory_order_relaxed); 78 std::atomic_load(&a1); 79 std::atomic_load_explicit(&a1, std::memory_order_relaxed); 80 std::atomic_exchange(&a1, 3); 81 std::atomic_compare_exchange_weak(&a1, &v, 2); 82 std::atomic_compare_exchange_strong(&a1, &v, 2); 83 std::atomic_compare_exchange_weak_explicit(&a1, &v, 2, std::memory_order_relaxed, std::memory_order_relaxed); 84 std::atomic_compare_exchange_strong_explicit(&a1, &v, 2, std::memory_order_relaxed, std::memory_order_relaxed); 85 86 std::atomic_flag f = ATOMIC_FLAG_INIT; 87 f.test_and_set(std::memory_order_relaxed); 88 f.test_and_set(); 89 f.clear(std::memory_order_relaxed); 90 f.clear(); 91 92 std::atomic_thread_fence(std::memory_order_relaxed); 93 std::atomic_signal_fence(std::memory_order_relaxed); 94 95 return 0; 96 } 97 98} 99