std_mutex.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // std::mutex implementation -*- C++ -*-
  2. // Copyright (C) 2003-2018 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. #include <bits/move.h> // for std::swap
  34. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  35. namespace std _GLIBCXX_VISIBILITY(default)
  36. {
  37. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  38. /**
  39. * @defgroup mutexes Mutexes
  40. * @ingroup concurrency
  41. *
  42. * Classes for mutex support.
  43. * @{
  44. */
  45. #ifdef _GLIBCXX_HAS_GTHREADS
  46. // Common base class for std::mutex and std::timed_mutex
  47. class __mutex_base
  48. {
  49. protected:
  50. typedef __gthread_mutex_t __native_type;
  51. #ifdef __GTHREAD_MUTEX_INIT
  52. __native_type _M_mutex = __GTHREAD_MUTEX_INIT;
  53. constexpr __mutex_base() noexcept = default;
  54. #else
  55. __native_type _M_mutex;
  56. __mutex_base() noexcept
  57. {
  58. // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
  59. __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
  60. }
  61. ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
  62. #endif
  63. __mutex_base(const __mutex_base&) = delete;
  64. __mutex_base& operator=(const __mutex_base&) = delete;
  65. };
  66. /// The standard mutex type.
  67. class mutex : private __mutex_base
  68. {
  69. public:
  70. typedef __native_type* native_handle_type;
  71. #ifdef __GTHREAD_MUTEX_INIT
  72. constexpr
  73. #endif
  74. mutex() noexcept = default;
  75. ~mutex() = default;
  76. mutex(const mutex&) = delete;
  77. mutex& operator=(const mutex&) = delete;
  78. void
  79. lock()
  80. {
  81. int __e = __gthread_mutex_lock(&_M_mutex);
  82. // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
  83. if (__e)
  84. __throw_system_error(__e);
  85. }
  86. bool
  87. try_lock() noexcept
  88. {
  89. // XXX EINVAL, EAGAIN, EBUSY
  90. return !__gthread_mutex_trylock(&_M_mutex);
  91. }
  92. void
  93. unlock()
  94. {
  95. // XXX EINVAL, EAGAIN, EPERM
  96. __gthread_mutex_unlock(&_M_mutex);
  97. }
  98. native_handle_type
  99. native_handle() noexcept
  100. { return &_M_mutex; }
  101. };
  102. #endif // _GLIBCXX_HAS_GTHREADS
  103. /// Do not acquire ownership of the mutex.
  104. struct defer_lock_t { explicit defer_lock_t() = default; };
  105. /// Try to acquire ownership of the mutex without blocking.
  106. struct try_to_lock_t { explicit try_to_lock_t() = default; };
  107. /// Assume the calling thread has already obtained mutex ownership
  108. /// and manage it.
  109. struct adopt_lock_t { explicit adopt_lock_t() = default; };
  110. /// Tag used to prevent a scoped lock from acquiring ownership of a mutex.
  111. _GLIBCXX17_INLINE constexpr defer_lock_t defer_lock { };
  112. /// Tag used to prevent a scoped lock from blocking if a mutex is locked.
  113. _GLIBCXX17_INLINE constexpr try_to_lock_t try_to_lock { };
  114. /// Tag used to make a scoped lock take ownership of a locked mutex.
  115. _GLIBCXX17_INLINE constexpr adopt_lock_t adopt_lock { };
  116. /** @brief A simple scoped lock type.
  117. *
  118. * A lock_guard controls mutex ownership within a scope, releasing
  119. * ownership in the destructor.
  120. */
  121. template<typename _Mutex>
  122. class lock_guard
  123. {
  124. public:
  125. typedef _Mutex mutex_type;
  126. explicit lock_guard(mutex_type& __m) : _M_device(__m)
  127. { _M_device.lock(); }
  128. lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m)
  129. { } // calling thread owns mutex
  130. ~lock_guard()
  131. { _M_device.unlock(); }
  132. lock_guard(const lock_guard&) = delete;
  133. lock_guard& operator=(const lock_guard&) = delete;
  134. private:
  135. mutex_type& _M_device;
  136. };
  137. /** @brief A movable scoped lock type.
  138. *
  139. * A unique_lock controls mutex ownership within a scope. Ownership of the
  140. * mutex can be delayed until after construction and can be transferred
  141. * to another unique_lock by move construction or move assignment. If a
  142. * mutex lock is owned when the destructor runs ownership will be released.
  143. */
  144. template<typename _Mutex>
  145. class unique_lock
  146. {
  147. public:
  148. typedef _Mutex mutex_type;
  149. unique_lock() noexcept
  150. : _M_device(0), _M_owns(false)
  151. { }
  152. explicit unique_lock(mutex_type& __m)
  153. : _M_device(std::__addressof(__m)), _M_owns(false)
  154. {
  155. lock();
  156. _M_owns = true;
  157. }
  158. unique_lock(mutex_type& __m, defer_lock_t) noexcept
  159. : _M_device(std::__addressof(__m)), _M_owns(false)
  160. { }
  161. unique_lock(mutex_type& __m, try_to_lock_t)
  162. : _M_device(std::__addressof(__m)), _M_owns(_M_device->try_lock())
  163. { }
  164. unique_lock(mutex_type& __m, adopt_lock_t) noexcept
  165. : _M_device(std::__addressof(__m)), _M_owns(true)
  166. {
  167. // XXX calling thread owns mutex
  168. }
  169. template<typename _Clock, typename _Duration>
  170. unique_lock(mutex_type& __m,
  171. const chrono::time_point<_Clock, _Duration>& __atime)
  172. : _M_device(std::__addressof(__m)),
  173. _M_owns(_M_device->try_lock_until(__atime))
  174. { }
  175. template<typename _Rep, typename _Period>
  176. unique_lock(mutex_type& __m,
  177. const chrono::duration<_Rep, _Period>& __rtime)
  178. : _M_device(std::__addressof(__m)),
  179. _M_owns(_M_device->try_lock_for(__rtime))
  180. { }
  181. ~unique_lock()
  182. {
  183. if (_M_owns)
  184. unlock();
  185. }
  186. unique_lock(const unique_lock&) = delete;
  187. unique_lock& operator=(const unique_lock&) = delete;
  188. unique_lock(unique_lock&& __u) noexcept
  189. : _M_device(__u._M_device), _M_owns(__u._M_owns)
  190. {
  191. __u._M_device = 0;
  192. __u._M_owns = false;
  193. }
  194. unique_lock& operator=(unique_lock&& __u) noexcept
  195. {
  196. if(_M_owns)
  197. unlock();
  198. unique_lock(std::move(__u)).swap(*this);
  199. __u._M_device = 0;
  200. __u._M_owns = false;
  201. return *this;
  202. }
  203. void
  204. lock()
  205. {
  206. if (!_M_device)
  207. __throw_system_error(int(errc::operation_not_permitted));
  208. else if (_M_owns)
  209. __throw_system_error(int(errc::resource_deadlock_would_occur));
  210. else
  211. {
  212. _M_device->lock();
  213. _M_owns = true;
  214. }
  215. }
  216. bool
  217. try_lock()
  218. {
  219. if (!_M_device)
  220. __throw_system_error(int(errc::operation_not_permitted));
  221. else if (_M_owns)
  222. __throw_system_error(int(errc::resource_deadlock_would_occur));
  223. else
  224. {
  225. _M_owns = _M_device->try_lock();
  226. return _M_owns;
  227. }
  228. }
  229. template<typename _Clock, typename _Duration>
  230. bool
  231. try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
  232. {
  233. if (!_M_device)
  234. __throw_system_error(int(errc::operation_not_permitted));
  235. else if (_M_owns)
  236. __throw_system_error(int(errc::resource_deadlock_would_occur));
  237. else
  238. {
  239. _M_owns = _M_device->try_lock_until(__atime);
  240. return _M_owns;
  241. }
  242. }
  243. template<typename _Rep, typename _Period>
  244. bool
  245. try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
  246. {
  247. if (!_M_device)
  248. __throw_system_error(int(errc::operation_not_permitted));
  249. else if (_M_owns)
  250. __throw_system_error(int(errc::resource_deadlock_would_occur));
  251. else
  252. {
  253. _M_owns = _M_device->try_lock_for(__rtime);
  254. return _M_owns;
  255. }
  256. }
  257. void
  258. unlock()
  259. {
  260. if (!_M_owns)
  261. __throw_system_error(int(errc::operation_not_permitted));
  262. else if (_M_device)
  263. {
  264. _M_device->unlock();
  265. _M_owns = false;
  266. }
  267. }
  268. void
  269. swap(unique_lock& __u) noexcept
  270. {
  271. std::swap(_M_device, __u._M_device);
  272. std::swap(_M_owns, __u._M_owns);
  273. }
  274. mutex_type*
  275. release() noexcept
  276. {
  277. mutex_type* __ret = _M_device;
  278. _M_device = 0;
  279. _M_owns = false;
  280. return __ret;
  281. }
  282. bool
  283. owns_lock() const noexcept
  284. { return _M_owns; }
  285. explicit operator bool() const noexcept
  286. { return owns_lock(); }
  287. mutex_type*
  288. mutex() const noexcept
  289. { return _M_device; }
  290. private:
  291. mutex_type* _M_device;
  292. bool _M_owns; // XXX use atomic_bool
  293. };
  294. /// Swap overload for unique_lock objects.
  295. template<typename _Mutex>
  296. inline void
  297. swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) noexcept
  298. { __x.swap(__y); }
  299. // @} group mutexes
  300. _GLIBCXX_END_NAMESPACE_VERSION
  301. } // namespace
  302. #endif // _GLIBCXX_USE_C99_STDINT_TR1
  303. #endif // C++11
  304. #endif // _GLIBCXX_MUTEX_H