std_mutex.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // std::mutex implementation -*- C++ -*-
  2. // Copyright (C) 2003-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/std_mutex.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{mutex}
  23. */
  24. #ifndef _GLIBCXX_MUTEX_H
  25. #define _GLIBCXX_MUTEX_H 1
  26. #pragma GCC system_header
  27. #if __cplusplus < 201103L
  28. # include <bits/c++0x_warning.h>
  29. #else
  30. #include <errno.h> // EBUSY
  31. #include <bits/functexcept.h>
  32. #include <bits/gthr.h>
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  36. /**
  37. * @defgroup mutexes Mutexes
  38. * @ingroup concurrency
  39. *
  40. * Classes for mutex support.
  41. * @{
  42. */
  43. #ifdef _GLIBCXX_HAS_GTHREADS
  44. /// @cond undocumented
  45. // Common base class for std::mutex and std::timed_mutex
  46. class __mutex_base
  47. {
  48. protected:
  49. typedef __gthread_mutex_t __native_type;
  50. #ifdef __GTHREAD_MUTEX_INIT
  51. __native_type _M_mutex = __GTHREAD_MUTEX_INIT;
  52. constexpr __mutex_base() noexcept = default;
  53. #else
  54. __native_type _M_mutex;
  55. __mutex_base() noexcept
  56. {
  57. // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
  58. __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
  59. }
  60. ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
  61. #endif
  62. __mutex_base(const __mutex_base&) = delete;
  63. __mutex_base& operator=(const __mutex_base&) = delete;
  64. };
  65. /// @endcond
  66. /** The standard mutex type.
  67. *
  68. * A simple, non-recursive, non-timed mutex.
  69. *
  70. * Do not call `lock()` and `unlock()` directly, use a scoped lock type
  71. * such as `std::unique_lock`, `std::lock_guard`, or (since C++17)
  72. * `std::scoped_lock`.
  73. *
  74. * @headerfile mutex
  75. * @since C++11
  76. */
  77. class mutex : private __mutex_base
  78. {
  79. public:
  80. typedef __native_type* native_handle_type;
  81. #ifdef __GTHREAD_MUTEX_INIT
  82. constexpr
  83. #endif
  84. mutex() noexcept = default;
  85. ~mutex() = default;
  86. mutex(const mutex&) = delete;
  87. mutex& operator=(const mutex&) = delete;
  88. void
  89. lock()
  90. {
  91. int __e = __gthread_mutex_lock(&_M_mutex);
  92. // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
  93. if (__e)
  94. __throw_system_error(__e);
  95. }
  96. _GLIBCXX_NODISCARD
  97. bool
  98. try_lock() noexcept
  99. {
  100. // XXX EINVAL, EAGAIN, EBUSY
  101. return !__gthread_mutex_trylock(&_M_mutex);
  102. }
  103. void
  104. unlock()
  105. {
  106. // XXX EINVAL, EAGAIN, EPERM
  107. __gthread_mutex_unlock(&_M_mutex);
  108. }
  109. native_handle_type
  110. native_handle() noexcept
  111. { return &_M_mutex; }
  112. };
  113. /// @cond undocumented
  114. // Implementation details for std::condition_variable
  115. class __condvar
  116. {
  117. using timespec = __gthread_time_t;
  118. public:
  119. __condvar() noexcept
  120. {
  121. #ifndef __GTHREAD_COND_INIT
  122. __GTHREAD_COND_INIT_FUNCTION(&_M_cond);
  123. #endif
  124. }
  125. ~__condvar()
  126. {
  127. int __e __attribute__((__unused__)) = __gthread_cond_destroy(&_M_cond);
  128. __glibcxx_assert(__e != EBUSY); // threads are still blocked
  129. }
  130. __condvar(const __condvar&) = delete;
  131. __condvar& operator=(const __condvar&) = delete;
  132. __gthread_cond_t* native_handle() noexcept { return &_M_cond; }
  133. // Expects: Calling thread has locked __m.
  134. void
  135. wait(mutex& __m)
  136. {
  137. int __e __attribute__((__unused__))
  138. = __gthread_cond_wait(&_M_cond, __m.native_handle());
  139. __glibcxx_assert(__e == 0);
  140. }
  141. void
  142. wait_until(mutex& __m, timespec& __abs_time)
  143. {
  144. __gthread_cond_timedwait(&_M_cond, __m.native_handle(), &__abs_time);
  145. }
  146. #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
  147. void
  148. wait_until(mutex& __m, clockid_t __clock, timespec& __abs_time)
  149. {
  150. pthread_cond_clockwait(&_M_cond, __m.native_handle(), __clock,
  151. &__abs_time);
  152. }
  153. #endif
  154. void
  155. notify_one() noexcept
  156. {
  157. int __e __attribute__((__unused__)) = __gthread_cond_signal(&_M_cond);
  158. __glibcxx_assert(__e == 0);
  159. }
  160. void
  161. notify_all() noexcept
  162. {
  163. int __e __attribute__((__unused__)) = __gthread_cond_broadcast(&_M_cond);
  164. __glibcxx_assert(__e == 0);
  165. }
  166. protected:
  167. #ifdef __GTHREAD_COND_INIT
  168. __gthread_cond_t _M_cond = __GTHREAD_COND_INIT;
  169. #else
  170. __gthread_cond_t _M_cond;
  171. #endif
  172. };
  173. /// @endcond
  174. #endif // _GLIBCXX_HAS_GTHREADS
  175. /// Do not acquire ownership of the mutex.
  176. struct defer_lock_t { explicit defer_lock_t() = default; };
  177. /// Try to acquire ownership of the mutex without blocking.
  178. struct try_to_lock_t { explicit try_to_lock_t() = default; };
  179. /// Assume the calling thread has already obtained mutex ownership
  180. /// and manage it.
  181. struct adopt_lock_t { explicit adopt_lock_t() = default; };
  182. /// Tag used to prevent a scoped lock from acquiring ownership of a mutex.
  183. _GLIBCXX17_INLINE constexpr defer_lock_t defer_lock { };
  184. /// Tag used to prevent a scoped lock from blocking if a mutex is locked.
  185. _GLIBCXX17_INLINE constexpr try_to_lock_t try_to_lock { };
  186. /// Tag used to make a scoped lock take ownership of a locked mutex.
  187. _GLIBCXX17_INLINE constexpr adopt_lock_t adopt_lock { };
  188. /** @brief A simple scoped lock type.
  189. *
  190. * A lock_guard controls mutex ownership within a scope, releasing
  191. * ownership in the destructor.
  192. *
  193. * @headerfile mutex
  194. * @since C++11
  195. */
  196. template<typename _Mutex>
  197. class lock_guard
  198. {
  199. public:
  200. typedef _Mutex mutex_type;
  201. explicit lock_guard(mutex_type& __m) : _M_device(__m)
  202. { _M_device.lock(); }
  203. lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m)
  204. { } // calling thread owns mutex
  205. ~lock_guard()
  206. { _M_device.unlock(); }
  207. lock_guard(const lock_guard&) = delete;
  208. lock_guard& operator=(const lock_guard&) = delete;
  209. private:
  210. mutex_type& _M_device;
  211. };
  212. /// @} group mutexes
  213. _GLIBCXX_END_NAMESPACE_VERSION
  214. } // namespace
  215. #endif // C++11
  216. #endif // _GLIBCXX_MUTEX_H