mutex.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //===------------------------- mutex.cpp ----------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "__config"
  10. #if !defined(_LIBCPP_SGX_CONFIG)
  11. #define _LIBCPP_BUILDING_MUTEX
  12. #include "mutex"
  13. #include "limits"
  14. #include "system_error"
  15. #include "cassert"
  16. #include "include/atomic_support.h"
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. #ifndef _LIBCPP_HAS_NO_THREADS
  19. const defer_lock_t defer_lock = {};
  20. const try_to_lock_t try_to_lock = {};
  21. const adopt_lock_t adopt_lock = {};
  22. mutex::~mutex()
  23. {
  24. __libcpp_mutex_destroy(&__m_);
  25. }
  26. void
  27. mutex::lock()
  28. {
  29. int ec = __libcpp_mutex_lock(&__m_);
  30. if (ec)
  31. __throw_system_error(ec, "mutex lock failed");
  32. }
  33. bool
  34. mutex::try_lock() _NOEXCEPT
  35. {
  36. return __libcpp_mutex_trylock(&__m_) == 0;
  37. }
  38. void
  39. mutex::unlock() _NOEXCEPT
  40. {
  41. int ec = __libcpp_mutex_unlock(&__m_);
  42. (void)ec;
  43. assert(ec == 0);
  44. }
  45. // recursive_mutex
  46. recursive_mutex::recursive_mutex()
  47. {
  48. int ec = __libcpp_recursive_mutex_init(&__m_);
  49. if (ec)
  50. __throw_system_error(ec, "recursive_mutex constructor failed");
  51. }
  52. recursive_mutex::~recursive_mutex()
  53. {
  54. int e = __libcpp_mutex_destroy(&__m_);
  55. (void)e;
  56. assert(e == 0);
  57. }
  58. void
  59. recursive_mutex::lock()
  60. {
  61. int ec = __libcpp_mutex_lock(&__m_);
  62. if (ec)
  63. __throw_system_error(ec, "recursive_mutex lock failed");
  64. }
  65. void
  66. recursive_mutex::unlock() _NOEXCEPT
  67. {
  68. int e = __libcpp_mutex_unlock(&__m_);
  69. (void)e;
  70. assert(e == 0);
  71. }
  72. bool
  73. recursive_mutex::try_lock() _NOEXCEPT
  74. {
  75. return __libcpp_mutex_trylock(&__m_) == 0;
  76. }
  77. // timed_mutex
  78. timed_mutex::timed_mutex()
  79. : __locked_(false)
  80. {
  81. }
  82. timed_mutex::~timed_mutex()
  83. {
  84. lock_guard<mutex> _(__m_);
  85. }
  86. void
  87. timed_mutex::lock()
  88. {
  89. unique_lock<mutex> lk(__m_);
  90. while (__locked_)
  91. __cv_.wait(lk);
  92. __locked_ = true;
  93. }
  94. bool
  95. timed_mutex::try_lock() _NOEXCEPT
  96. {
  97. unique_lock<mutex> lk(__m_, try_to_lock);
  98. if (lk.owns_lock() && !__locked_)
  99. {
  100. __locked_ = true;
  101. return true;
  102. }
  103. return false;
  104. }
  105. void
  106. timed_mutex::unlock() _NOEXCEPT
  107. {
  108. lock_guard<mutex> _(__m_);
  109. __locked_ = false;
  110. __cv_.notify_one();
  111. }
  112. // recursive_timed_mutex
  113. recursive_timed_mutex::recursive_timed_mutex()
  114. : __count_(0),
  115. __id_(0)
  116. {
  117. }
  118. recursive_timed_mutex::~recursive_timed_mutex()
  119. {
  120. lock_guard<mutex> _(__m_);
  121. }
  122. void
  123. recursive_timed_mutex::lock()
  124. {
  125. __libcpp_thread_id id = __libcpp_thread_get_current_id();
  126. unique_lock<mutex> lk(__m_);
  127. if (__libcpp_thread_id_equal(id, __id_))
  128. {
  129. if (__count_ == numeric_limits<size_t>::max())
  130. __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
  131. ++__count_;
  132. return;
  133. }
  134. while (__count_ != 0)
  135. __cv_.wait(lk);
  136. __count_ = 1;
  137. __id_ = id;
  138. }
  139. bool
  140. recursive_timed_mutex::try_lock() _NOEXCEPT
  141. {
  142. __libcpp_thread_id id = __libcpp_thread_get_current_id();
  143. unique_lock<mutex> lk(__m_, try_to_lock);
  144. if (lk.owns_lock() && (__count_ == 0 || __libcpp_thread_id_equal(id, __id_)))
  145. {
  146. if (__count_ == numeric_limits<size_t>::max())
  147. return false;
  148. ++__count_;
  149. __id_ = id;
  150. return true;
  151. }
  152. return false;
  153. }
  154. void
  155. recursive_timed_mutex::unlock() _NOEXCEPT
  156. {
  157. unique_lock<mutex> lk(__m_);
  158. if (--__count_ == 0)
  159. {
  160. __id_ = 0;
  161. lk.unlock();
  162. __cv_.notify_one();
  163. }
  164. }
  165. #endif // !_LIBCPP_HAS_NO_THREADS
  166. // If dispatch_once_f ever handles C++ exceptions, and if one can get to it
  167. // without illegal macros (unexpected macros not beginning with _UpperCase or
  168. // __lowercase), and if it stops spinning waiting threads, then call_once should
  169. // call into dispatch_once_f instead of here. Relevant radar this code needs to
  170. // keep in sync with: 7741191.
  171. #ifndef _LIBCPP_HAS_NO_THREADS
  172. static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
  173. static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
  174. #endif
  175. /// NOTE: Changes to flag are done via relaxed atomic stores
  176. /// even though the accesses are protected by a mutex because threads
  177. /// just entering 'call_once` concurrently read from flag.
  178. void
  179. __call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
  180. {
  181. #if defined(_LIBCPP_HAS_NO_THREADS)
  182. if (flag == 0)
  183. {
  184. #ifndef _LIBCPP_NO_EXCEPTIONS
  185. try
  186. {
  187. #endif // _LIBCPP_NO_EXCEPTIONS
  188. flag = 1;
  189. func(arg);
  190. flag = ~0ul;
  191. #ifndef _LIBCPP_NO_EXCEPTIONS
  192. }
  193. catch (...)
  194. {
  195. flag = 0ul;
  196. throw;
  197. }
  198. #endif // _LIBCPP_NO_EXCEPTIONS
  199. }
  200. #else // !_LIBCPP_HAS_NO_THREADS
  201. __libcpp_mutex_lock(&mut);
  202. while (flag == 1)
  203. __libcpp_condvar_wait(&cv, &mut);
  204. if (flag == 0)
  205. {
  206. #ifndef _LIBCPP_NO_EXCEPTIONS
  207. try
  208. {
  209. #endif // _LIBCPP_NO_EXCEPTIONS
  210. __libcpp_relaxed_store(&flag, 1ul);
  211. __libcpp_mutex_unlock(&mut);
  212. func(arg);
  213. __libcpp_mutex_lock(&mut);
  214. __libcpp_relaxed_store(&flag, ~0ul);
  215. __libcpp_mutex_unlock(&mut);
  216. __libcpp_condvar_broadcast(&cv);
  217. #ifndef _LIBCPP_NO_EXCEPTIONS
  218. }
  219. catch (...)
  220. {
  221. __libcpp_mutex_lock(&mut);
  222. __libcpp_relaxed_store(&flag, 0ul);
  223. __libcpp_mutex_unlock(&mut);
  224. __libcpp_condvar_broadcast(&cv);
  225. throw;
  226. }
  227. #endif // _LIBCPP_NO_EXCEPTIONS
  228. }
  229. else
  230. __libcpp_mutex_unlock(&mut);
  231. #endif // !_LIBCPP_HAS_NO_THREADS
  232. }
  233. _LIBCPP_END_NAMESPACE_STD
  234. #endif // !defined(_LIBCPP_SGX_CONFIG)