wait_ops_darwin_ulock.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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) 2021 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/wait_ops_darwin_ulock.hpp
  10. *
  11. * This header contains implementation of the waiting/notifying atomic operations based on Darwin systems using ulock syscalls.
  12. *
  13. * https://github.com/apple/darwin-xnu/blob/master/bsd/sys/ulock.h
  14. * https://github.com/apple/darwin-xnu/blob/master/bsd/kern/sys_ulock.c
  15. */
  16. #ifndef BOOST_ATOMIC_DETAIL_WAIT_OPS_DARWIN_ULOCK_HPP_INCLUDED_
  17. #define BOOST_ATOMIC_DETAIL_WAIT_OPS_DARWIN_ULOCK_HPP_INCLUDED_
  18. #include <stdint.h>
  19. #include <cerrno>
  20. #include <boost/memory_order.hpp>
  21. #include <boost/atomic/detail/config.hpp>
  22. #include <boost/atomic/detail/wait_capabilities.hpp>
  23. #include <boost/atomic/detail/wait_operations_fwd.hpp>
  24. #include <boost/atomic/detail/header.hpp>
  25. #ifdef BOOST_HAS_PRAGMA_ONCE
  26. #pragma once
  27. #endif
  28. namespace boost {
  29. namespace atomics {
  30. namespace detail {
  31. extern "C" {
  32. // Timeout is in microseconds with zero meaning no timeout
  33. int __ulock_wait(uint32_t operation, void* addr, uint64_t value, uint32_t timeout);
  34. #if defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK_WAIT2)
  35. // Timeout is in nanoseconds with zero meaning no timeout
  36. int __ulock_wait2(uint32_t operation, void* addr, uint64_t value, uint64_t timeout, uint64_t value2);
  37. #endif // defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK_WAIT2)
  38. int __ulock_wake(uint32_t operation, void* addr, uint64_t wake_value);
  39. } // extern "C"
  40. enum ulock_op
  41. {
  42. ulock_op_compare_and_wait = 1,
  43. #if defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK_SHARED)
  44. ulock_op_compare_and_wait_shared = 3,
  45. #endif // defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK_SHARED)
  46. #if defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK64)
  47. ulock_op_compare_and_wait64 = 5,
  48. #if defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK_SHARED)
  49. ulock_op_compare_and_wait64_shared = 6,
  50. #endif // defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK_SHARED)
  51. #endif // defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK64)
  52. // Flags for __ulock_wake
  53. ulock_flag_wake_all = 0x00000100,
  54. // Generic flags
  55. ulock_flag_no_errno = 0x01000000
  56. };
  57. template< typename Base, uint32_t Opcode >
  58. struct wait_operations_darwin_ulock_common :
  59. public Base
  60. {
  61. typedef Base base_type;
  62. typedef typename base_type::storage_type storage_type;
  63. static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = true;
  64. static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT
  65. {
  66. return true;
  67. }
  68. static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT
  69. {
  70. storage_type new_val = base_type::load(storage, order);
  71. while (new_val == old_val)
  72. {
  73. #if defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK_WAIT2)
  74. __ulock_wait2(Opcode | ulock_flag_no_errno, const_cast< storage_type* >(&storage), old_val, 0u, 0u);
  75. #else
  76. __ulock_wait(Opcode | ulock_flag_no_errno, const_cast< storage_type* >(&storage), old_val, 0u);
  77. #endif
  78. new_val = base_type::load(storage, order);
  79. }
  80. return new_val;
  81. }
  82. static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT
  83. {
  84. while (true)
  85. {
  86. const int res = __ulock_wake(Opcode | ulock_flag_no_errno, const_cast< storage_type* >(&storage), 0u);
  87. if (BOOST_LIKELY(res != -EINTR))
  88. break;
  89. }
  90. }
  91. static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT
  92. {
  93. while (true)
  94. {
  95. const int res = __ulock_wake(Opcode | ulock_flag_wake_all | ulock_flag_no_errno, const_cast< storage_type* >(&storage), 0u);
  96. if (BOOST_LIKELY(res != -EINTR))
  97. break;
  98. }
  99. }
  100. };
  101. template< typename Base >
  102. struct wait_operations< Base, sizeof(uint32_t), true, false > :
  103. public wait_operations_darwin_ulock_common< Base, ulock_op_compare_and_wait >
  104. {
  105. };
  106. #if defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK_SHARED)
  107. template< typename Base >
  108. struct wait_operations< Base, sizeof(uint32_t), true, true > :
  109. public wait_operations_darwin_ulock_common< Base, ulock_op_compare_and_wait_shared >
  110. {
  111. };
  112. #endif // defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK_SHARED)
  113. #if defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK64)
  114. template< typename Base >
  115. struct wait_operations< Base, sizeof(uint64_t), true, false > :
  116. public wait_operations_darwin_ulock_common< Base, ulock_op_compare_and_wait64 >
  117. {
  118. };
  119. #if defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK_SHARED)
  120. template< typename Base >
  121. struct wait_operations< Base, sizeof(uint64_t), true, true > :
  122. public wait_operations_darwin_ulock_common< Base, ulock_op_compare_and_wait64_shared >
  123. {
  124. };
  125. #endif // defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK_SHARED)
  126. #endif // defined(BOOST_ATOMIC_DETAIL_HAS_DARWIN_ULOCK64)
  127. } // namespace detail
  128. } // namespace atomics
  129. } // namespace boost
  130. #include <boost/atomic/detail/footer.hpp>
  131. #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_DARWIN_ULOCK_HPP_INCLUDED_