boost_no_cxx11_hdr_atomic.ipp 3.1 KB

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