ipc_atomic.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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) 2020-2025 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/ipc_atomic.hpp
  10. *
  11. * This header contains definition of \c ipc_atomic template.
  12. */
  13. #ifndef BOOST_ATOMIC_IPC_ATOMIC_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_IPC_ATOMIC_HPP_INCLUDED_
  15. #include <cstddef>
  16. #include <type_traits>
  17. #include <boost/memory_order.hpp>
  18. #include <boost/atomic/capabilities.hpp>
  19. #include <boost/atomic/detail/config.hpp>
  20. #include <boost/atomic/detail/classify.hpp>
  21. #include <boost/atomic/detail/atomic_impl.hpp>
  22. #include <boost/atomic/detail/type_traits/is_trivially_copyable.hpp>
  23. #include <boost/atomic/detail/header.hpp>
  24. #ifdef BOOST_HAS_PRAGMA_ONCE
  25. #pragma once
  26. #endif
  27. namespace boost {
  28. namespace atomics {
  29. //! Atomic object for inter-process communication
  30. template< typename T >
  31. class ipc_atomic :
  32. public atomics::detail::base_atomic< T, typename atomics::detail::classify< T >::type, true >
  33. {
  34. private:
  35. using base_type = atomics::detail::base_atomic< T, typename atomics::detail::classify< T >::type, true >;
  36. using value_arg_type = typename base_type::value_arg_type;
  37. public:
  38. using value_type = typename base_type::value_type;
  39. static_assert(sizeof(value_type) > 0u, "boost::ipc_atomic<T> requires T to be a complete type");
  40. static_assert(atomics::detail::is_trivially_copyable< value_type >::value, "boost::ipc_atomic<T> requires T to be a trivially copyable type");
  41. public:
  42. ipc_atomic() = default;
  43. BOOST_FORCEINLINE constexpr ipc_atomic(value_arg_type v) noexcept : base_type(v)
  44. {
  45. }
  46. ipc_atomic(ipc_atomic const&) = delete;
  47. ipc_atomic& operator= (ipc_atomic const&) = delete;
  48. ipc_atomic& operator= (ipc_atomic const&) volatile = delete;
  49. BOOST_FORCEINLINE value_type operator= (value_arg_type v) noexcept
  50. {
  51. this->store(v);
  52. return v;
  53. }
  54. BOOST_FORCEINLINE value_type operator= (value_arg_type v) volatile noexcept
  55. {
  56. this->store(v);
  57. return v;
  58. }
  59. BOOST_FORCEINLINE operator value_type() const volatile noexcept
  60. {
  61. return this->load();
  62. }
  63. };
  64. } // namespace atomics
  65. using atomics::ipc_atomic;
  66. } // namespace boost
  67. #include <boost/atomic/detail/footer.hpp>
  68. #endif // BOOST_ATOMIC_IPC_ATOMIC_HPP_INCLUDED_