atomic_flag.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * Copyright (c) 2014 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/atomic_flag.hpp
  10. *
  11. * This header contains interface definition of \c atomic_flag.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_ATOMIC_FLAG_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_ATOMIC_FLAG_HPP_INCLUDED_
  15. #include <boost/assert.hpp>
  16. #include <boost/memory_order.hpp>
  17. #include <boost/atomic/detail/config.hpp>
  18. #include <boost/atomic/detail/operations.hpp>
  19. #if defined(BOOST_ATOMIC_DETAIL_NO_CXX11_ALIGNAS)
  20. #include <boost/type_traits/type_with_alignment.hpp>
  21. #endif
  22. #ifdef BOOST_HAS_PRAGMA_ONCE
  23. #pragma once
  24. #endif
  25. /*
  26. * IMPLEMENTATION NOTE: All interface functions MUST be declared with BOOST_FORCEINLINE,
  27. * see comment for convert_memory_order_to_gcc in ops_gcc_atomic.hpp.
  28. */
  29. namespace boost {
  30. namespace atomics {
  31. #if defined(BOOST_ATOMIC_DETAIL_NO_CXX11_CONSTEXPR_UNION_INIT) || defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  32. #define BOOST_ATOMIC_NO_ATOMIC_FLAG_INIT
  33. #else
  34. #define BOOST_ATOMIC_FLAG_INIT {}
  35. #endif
  36. struct atomic_flag
  37. {
  38. typedef atomics::detail::operations< 1u, false > operations;
  39. typedef operations::storage_type storage_type;
  40. #if !defined(BOOST_ATOMIC_DETAIL_NO_CXX11_ALIGNAS)
  41. alignas(operations::storage_alignment) storage_type m_storage;
  42. #else
  43. // Note: Some compilers cannot use constant expressions in alignment attributes, so we have to use the union trick
  44. union
  45. {
  46. storage_type m_storage;
  47. boost::type_with_alignment< operations::storage_alignment >::type m_aligner;
  48. };
  49. #endif
  50. BOOST_FORCEINLINE BOOST_ATOMIC_DETAIL_CONSTEXPR_UNION_INIT atomic_flag() BOOST_NOEXCEPT : m_storage(0u)
  51. {
  52. }
  53. BOOST_FORCEINLINE bool test(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
  54. {
  55. BOOST_ASSERT(order != memory_order_release);
  56. BOOST_ASSERT(order != memory_order_acq_rel);
  57. return !!operations::load(m_storage, order);
  58. }
  59. BOOST_FORCEINLINE bool test_and_set(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  60. {
  61. return operations::test_and_set(m_storage, order);
  62. }
  63. BOOST_FORCEINLINE void clear(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  64. {
  65. BOOST_ASSERT(order != memory_order_consume);
  66. BOOST_ASSERT(order != memory_order_acquire);
  67. BOOST_ASSERT(order != memory_order_acq_rel);
  68. operations::clear(m_storage, order);
  69. }
  70. BOOST_DELETED_FUNCTION(atomic_flag(atomic_flag const&))
  71. BOOST_DELETED_FUNCTION(atomic_flag& operator= (atomic_flag const&))
  72. };
  73. } // namespace atomics
  74. } // namespace boost
  75. #endif // BOOST_ATOMIC_DETAIL_ATOMIC_FLAG_HPP_INCLUDED_