std_fenced_block.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // detail/std_fenced_block.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_STD_FENCED_BLOCK_HPP
  11. #define BOOST_ASIO_DETAIL_STD_FENCED_BLOCK_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_STD_ATOMIC)
  17. #include <atomic>
  18. #include <boost/asio/detail/noncopyable.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace detail {
  23. class std_fenced_block
  24. : private noncopyable
  25. {
  26. public:
  27. enum half_t { half };
  28. enum full_t { full };
  29. // Constructor for a half fenced block.
  30. explicit std_fenced_block(half_t)
  31. {
  32. }
  33. // Constructor for a full fenced block.
  34. explicit std_fenced_block(full_t)
  35. {
  36. std::atomic_thread_fence(std::memory_order_acquire);
  37. }
  38. // Destructor.
  39. ~std_fenced_block()
  40. {
  41. std::atomic_thread_fence(std::memory_order_release);
  42. }
  43. };
  44. } // namespace detail
  45. } // namespace asio
  46. } // namespace boost
  47. #include <boost/asio/detail/pop_options.hpp>
  48. #endif // defined(BOOST_ASIO_HAS_STD_ATOMIC)
  49. #endif // BOOST_ASIO_DETAIL_STD_FENCED_BLOCK_HPP