std_mutex.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // std::mutex implementation -*- C++ -*-
  2. // Copyright (C) 2003-2022 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 <system_error>
  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. // Common base class for std::mutex and std::timed_mutex
  45. class __mutex_base
  46. {
  47. protected:
  48. typedef __gthread_mutex_t __native_type;
  49. #ifdef __GTHREAD_MUTEX_INIT
  50. __native_type _M_mutex = __GTHREAD_MUTEX_INIT;
  51. constexpr __mutex_base() noexcept = default;
  52. #else
  53. __native_type _M_mutex;
  54. __mutex_base() noexcept
  55. {
  56. // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
  57. __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
  58. }
  59. ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
  60. #endif
  61. __mutex_base(const __mutex_base&) = delete;
  62. __mutex_base& operator=(const __mutex_base&) = delete;
  63. };
  64. /// The standard mutex type.
  65. class mutex : private __mutex_base
  66. {
  67. public:
  68. typedef __native_type* native_handle_type;
  69. #ifdef __GTHREAD_MUTEX_INIT
  70. constexpr
  71. #endif
  72. mutex() noexcept = default;
  73. ~mutex() = default;
  74. mutex(const mutex&) = delete;
  75. mutex& operator=(const mutex&) = delete;
  76. void
  77. lock()
  78. {
  79. int __e = __gthread_mutex_lock(&_M_mutex);
  80. // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
  81. if (__e)
  82. __throw_system_error(__e);
  83. }
  84. bool
  85. try_lock() noexcept
  86. {
  87. // XXX EINVAL, EAGAIN, EBUSY
  88. return !__gthread_mutex_trylock(&_M_mutex);
  89. }
  90. void
  91. unlock()
  92. {
  93. // XXX EINVAL, EAGAIN, EPERM
  94. __gthread_mutex_unlock(&_M_mutex);
  95. }
  96. native_handle_type
  97. native_handle() noexcept
  98. { return &_M_mutex; }
  99. };
  100. // Implementation details for std::condition_variable
  101. class __condvar
  102. {
  103. using timespec = __gthread_time_t;
  104. public:
  105. __condvar() noexcept
  106. {
  107. #ifndef __GTHREAD_COND_INIT
  108. __GTHREAD_COND_INIT_FUNCTION(&_M_cond);
  109. #endif
  110. }
  111. ~__condvar()
  112. {
  113. int __e __attribute__((__unused__)) = __gthread_cond_destroy(&_M_cond);
  114. __glibcxx_assert(__e != EBUSY); // threads are still blocked
  115. }
  116. __condvar(const __condvar&) = delete;
  117. __condvar& operator=(const __condvar&) = delete;
  118. __gthread_cond_t* native_handle() noexcept { return &_M_cond; }
  119. // Expects: Calling thread has locked __m.
  120. void
  121. wait(mutex& __m)
  122. {
  123. int __e __attribute__((__unused__))
  124. = __gthread_cond_wait(&_M_cond, __m.native_handle());
  125. __glibcxx_assert(__e == 0);
  126. }
  127. void
  128. wait_until(mutex& __m, timespec& __abs_time)
  129. {
  130. __gthread_cond_timedwait(&_M_cond, __m.native_handle(), &__abs_time);
  131. }
  132. #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
  133. void
  134. wait_until(mutex& __m, clockid_t __clock, timespec& __abs_time)
  135. {
  136. pthread_cond_clockwait(&_M_cond, __m.native_handle(), __clock,
  137. &__abs_time);
  138. }
  139. #endif
  140. void
  141. notify_one() noexcept
  142. {
  143. int __e __attribute__((__unused__)) = __gthread_cond_signal(&_M_cond);
  144. __glibcxx_assert(__e == 0);
  145. }
  146. void
  147. notify_all() noexcept
  148. {
  149. int __e __attribute__((__unused__)) = __gthread_cond_broadcast(&_M_cond);
  150. __glibcxx_assert(__e == 0);
  151. }
  152. protected:
  153. #ifdef __GTHREAD_COND_INIT
  154. __gthread_cond_t _M_cond = __GTHREAD_COND_INIT;
  155. #else
  156. __gthread_cond_t _M_cond;
  157. #endif
  158. };
  159. #endif // _GLIBCXX_HAS_GTHREADS
  160. /// Do not acquire ownership of the mutex.
  161. struct defer_lock_t { explicit defer_lock_t() = default; };
  162. /// Try to acquire ownership of the mutex without blocking.
  163. struct try_to_lock_t { explicit try_to_lock_t() = default; };
  164. /// Assume the calling thread has already obtained mutex ownership
  165. /// and manage it.
  166. struct adopt_lock_t { explicit adopt_lock_t() = default; };
  167. /// Tag used to prevent a scoped lock from acquiring ownership of a mutex.
  168. _GLIBCXX17_INLINE constexpr defer_lock_t defer_lock { };
  169. /// Tag used to prevent a scoped lock from blocking if a mutex is locked.
  170. _GLIBCXX17_INLINE constexpr try_to_lock_t try_to_lock { };
  171. /// Tag used to make a scoped lock take ownership of a locked mutex.
  172. _GLIBCXX17_INLINE constexpr adopt_lock_t adopt_lock { };
  173. /** @brief A simple scoped lock type.
  174. *
  175. * A lock_guard controls mutex ownership within a scope, releasing
  176. * ownership in the destructor.
  177. */
  178. template<typename _Mutex>
  179. class lock_guard
  180. {
  181. public:
  182. typedef _Mutex mutex_type;
  183. explicit lock_guard(mutex_type& __m) : _M_device(__m)
  184. { _M_device.lock(); }
  185. lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m)
  186. { } // calling thread owns mutex
  187. ~lock_guard()
  188. { _M_device.unlock(); }
  189. lock_guard(const lock_guard&) = delete;
  190. lock_guard& operator=(const lock_guard&) = delete;
  191. private:
  192. mutex_type& _M_device;
  193. };
  194. /// @} group mutexes
  195. _GLIBCXX_END_NAMESPACE_VERSION
  196. } // namespace
  197. #endif // C++11
  198. #endif // _GLIBCXX_MUTEX_H