wait_ops_freebsd_umtx.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/wait_ops_freebsd_umtx.hpp
  10. *
  11. * This header contains implementation of the waiting/notifying atomic operations based on FreeBSD _umtx_op syscall.
  12. * https://www.freebsd.org/cgi/man.cgi?query=_umtx_op&apropos=0&sektion=2&manpath=FreeBSD+11-current&format=html
  13. */
  14. #ifndef BOOST_ATOMIC_DETAIL_WAIT_OPS_FREEBSD_UMTX_HPP_INCLUDED_
  15. #define BOOST_ATOMIC_DETAIL_WAIT_OPS_FREEBSD_UMTX_HPP_INCLUDED_
  16. #include <sys/types.h>
  17. #include <sys/umtx.h>
  18. #include <cstddef>
  19. #include <boost/memory_order.hpp>
  20. #include <boost/atomic/detail/config.hpp>
  21. #include <boost/atomic/detail/int_sizes.hpp>
  22. #include <boost/atomic/detail/wait_operations_fwd.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. namespace detail {
  30. #if defined(UMTX_OP_WAIT_UINT) || defined(UMTX_OP_WAIT)
  31. template< typename Base >
  32. struct wait_operations_freebsd_umtx_common :
  33. public Base
  34. {
  35. typedef Base base_type;
  36. typedef typename base_type::storage_type storage_type;
  37. static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = true;
  38. static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT
  39. {
  40. return true;
  41. }
  42. static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT
  43. {
  44. ::_umtx_op(const_cast< storage_type* >(&storage), UMTX_OP_WAKE, 1u, NULL, NULL);
  45. }
  46. static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT
  47. {
  48. ::_umtx_op(const_cast< storage_type* >(&storage), UMTX_OP_WAKE, (~static_cast< unsigned int >(0u)) >> 1, NULL, NULL);
  49. }
  50. };
  51. #endif // defined(UMTX_OP_WAIT_UINT) || defined(UMTX_OP_WAIT)
  52. // UMTX_OP_WAIT_UINT only appeared in FreeBSD 8.0
  53. #if defined(UMTX_OP_WAIT_UINT) && BOOST_ATOMIC_DETAIL_SIZEOF_INT < BOOST_ATOMIC_DETAIL_SIZEOF_LONG
  54. template< typename Base, bool Interprocess >
  55. struct wait_operations< Base, sizeof(unsigned int), true, Interprocess > :
  56. public wait_operations_freebsd_umtx_common< Base >
  57. {
  58. typedef wait_operations_freebsd_umtx_common< Base > base_type;
  59. typedef typename base_type::storage_type storage_type;
  60. static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT
  61. {
  62. storage_type new_val = base_type::load(storage, order);
  63. while (new_val == old_val)
  64. {
  65. ::_umtx_op(const_cast< storage_type* >(&storage), UMTX_OP_WAIT_UINT, old_val, NULL, NULL);
  66. new_val = base_type::load(storage, order);
  67. }
  68. return new_val;
  69. }
  70. };
  71. #endif // defined(UMTX_OP_WAIT_UINT) && BOOST_ATOMIC_DETAIL_SIZEOF_INT < BOOST_ATOMIC_DETAIL_SIZEOF_LONG
  72. #if defined(UMTX_OP_WAIT)
  73. template< typename Base, bool Interprocess >
  74. struct wait_operations< Base, sizeof(unsigned long), true, Interprocess > :
  75. public wait_operations_freebsd_umtx_common< Base >
  76. {
  77. typedef wait_operations_freebsd_umtx_common< Base > base_type;
  78. typedef typename base_type::storage_type storage_type;
  79. static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT
  80. {
  81. storage_type new_val = base_type::load(storage, order);
  82. while (new_val == old_val)
  83. {
  84. ::_umtx_op(const_cast< storage_type* >(&storage), UMTX_OP_WAIT, old_val, NULL, NULL);
  85. new_val = base_type::load(storage, order);
  86. }
  87. return new_val;
  88. }
  89. };
  90. #endif // defined(UMTX_OP_WAIT)
  91. } // namespace detail
  92. } // namespace atomics
  93. } // namespace boost
  94. #include <boost/atomic/detail/footer.hpp>
  95. #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_FREEBSD_UMTX_HPP_INCLUDED_