atomic_flag.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_lockfree.hpp>
  19. #ifdef BOOST_HAS_PRAGMA_ONCE
  20. #pragma once
  21. #endif
  22. /*
  23. * IMPLEMENTATION NOTE: All interface functions MUST be declared with BOOST_FORCEINLINE,
  24. * see comment for convert_memory_order_to_gcc in ops_gcc_atomic.hpp.
  25. */
  26. namespace boost {
  27. namespace atomics {
  28. #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  29. #define BOOST_ATOMIC_NO_ATOMIC_FLAG_INIT
  30. #else
  31. #define BOOST_ATOMIC_FLAG_INIT {}
  32. #endif
  33. struct atomic_flag
  34. {
  35. typedef atomics::detail::operations< 1u, false > operations;
  36. typedef operations::storage_type storage_type;
  37. operations::aligned_storage_type m_storage;
  38. BOOST_FORCEINLINE BOOST_CONSTEXPR atomic_flag() BOOST_NOEXCEPT : m_storage(0)
  39. {
  40. }
  41. BOOST_FORCEINLINE bool test_and_set(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  42. {
  43. return operations::test_and_set(m_storage.value, order);
  44. }
  45. BOOST_FORCEINLINE void clear(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  46. {
  47. BOOST_ASSERT(order != memory_order_consume);
  48. BOOST_ASSERT(order != memory_order_acquire);
  49. BOOST_ASSERT(order != memory_order_acq_rel);
  50. operations::clear(m_storage.value, order);
  51. }
  52. BOOST_DELETED_FUNCTION(atomic_flag(atomic_flag const&))
  53. BOOST_DELETED_FUNCTION(atomic_flag& operator= (atomic_flag const&))
  54. };
  55. } // namespace atomics
  56. } // namespace boost
  57. #endif // BOOST_ATOMIC_DETAIL_ATOMIC_FLAG_HPP_INCLUDED_