mutex 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #pragma once
  2. #if __cplusplus < 201103L
  3. #error "C++ version lower than C++11"
  4. #endif
  5. //#if defined(RT_USING_PTHREADS)
  6. #include <pthread.h>
  7. #include <system_error>
  8. #include <chrono>
  9. #include <utility>
  10. #include <functional>
  11. #include "__utils.h"
  12. #define rt_cpp_mutex_t pthread_mutex_t
  13. namespace std
  14. {
  15. // Base class on which to build std::mutex and std::timed_mutex
  16. class __mutex_base
  17. {
  18. protected:
  19. typedef rt_cpp_mutex_t __native_type;
  20. __native_type _m_mutex = PTHREAD_MUTEX_INITIALIZER;
  21. constexpr __mutex_base() noexcept = default;
  22. __mutex_base(const __mutex_base&) = delete;
  23. __mutex_base& operator=(const __mutex_base&) = delete;
  24. };
  25. class mutex : private __mutex_base
  26. {
  27. public:
  28. constexpr mutex() = default;
  29. ~mutex() = default;
  30. mutex(const mutex&) = delete;
  31. mutex& operator=(const mutex&) = delete;
  32. void lock()
  33. {
  34. int err = pthread_mutex_lock(&_m_mutex);
  35. if (err)
  36. {
  37. throw_system_error(err, "mutex:lock failed.");
  38. }
  39. }
  40. bool try_lock() noexcept
  41. {
  42. return !pthread_mutex_trylock(&_m_mutex);
  43. }
  44. void unlock() noexcept
  45. {
  46. pthread_mutex_unlock(&_m_mutex);
  47. }
  48. typedef __native_type* native_handle_type;
  49. native_handle_type native_handle()
  50. {
  51. return &_m_mutex;
  52. };
  53. };
  54. inline int __rt_cpp_recursive_mutex_init(rt_cpp_mutex_t* m)
  55. {
  56. pthread_mutexattr_t attr;
  57. int res;
  58. res = pthread_mutexattr_init(&attr);
  59. if (res)
  60. return res;
  61. res = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  62. if (res)
  63. goto attr_cleanup;
  64. res = pthread_mutex_init(m, &attr);
  65. attr_cleanup:
  66. int err = pthread_mutexattr_destroy(&attr);
  67. return res ? res : err;
  68. }
  69. class __recursive_mutex_base
  70. {
  71. protected:
  72. typedef rt_cpp_mutex_t __native_type;
  73. __native_type _m_recursive_mutex;
  74. __recursive_mutex_base(const __recursive_mutex_base&) = delete;
  75. __recursive_mutex_base& operator=(const __recursive_mutex_base&) = delete;
  76. __recursive_mutex_base()
  77. {
  78. int err = __rt_cpp_recursive_mutex_init(&_m_recursive_mutex);
  79. if (err)
  80. throw_system_error(err, "Recursive mutex failed to construct");
  81. }
  82. ~__recursive_mutex_base()
  83. {
  84. pthread_mutex_destroy(&_m_recursive_mutex);
  85. }
  86. };
  87. class recursive_mutex : private __recursive_mutex_base
  88. {
  89. public:
  90. typedef __native_type* native_handle_type;
  91. recursive_mutex() = default;
  92. ~recursive_mutex() = default;
  93. recursive_mutex(const recursive_mutex&) = delete;
  94. recursive_mutex& operator=(const recursive_mutex&) = delete;
  95. void lock()
  96. {
  97. int err = pthread_mutex_lock(&_m_recursive_mutex);
  98. if (err)
  99. throw_system_error(err, "recursive_mutex::lock failed");
  100. }
  101. bool try_lock() noexcept
  102. {
  103. return !pthread_mutex_trylock(&_m_recursive_mutex);
  104. }
  105. void unlock() noexcept
  106. {
  107. pthread_mutex_unlock(&_m_recursive_mutex);
  108. }
  109. native_handle_type native_handle()
  110. { return &_m_recursive_mutex; }
  111. };
  112. #ifdef RT_PTHREAD_TIMED_MUTEX
  113. class timed_mutex;
  114. class recursive_timed_mutex;
  115. #endif // RT_PTHREAD_TIMED_MUTEX
  116. struct defer_lock_t {};
  117. struct try_to_lock_t {};
  118. struct adopt_lock_t {}; // take ownership of a locked mtuex
  119. constexpr defer_lock_t defer_lock { };
  120. constexpr try_to_lock_t try_to_lock { };
  121. constexpr adopt_lock_t adopt_lock { };
  122. template <class Mutex>
  123. class lock_guard
  124. {
  125. public:
  126. typedef Mutex mutex_type;
  127. explicit lock_guard(mutex_type& m) : pm(m) { pm.lock(); }
  128. lock_guard(mutex_type& m, adopt_lock_t) noexcept : pm(m)
  129. { }
  130. ~lock_guard()
  131. { pm.unlock(); }
  132. lock_guard(lock_guard const&) = delete;
  133. lock_guard& operator=(lock_guard const&) = delete;
  134. private:
  135. mutex_type& pm;
  136. };
  137. template <class Mutex>
  138. class unique_lock
  139. {
  140. public:
  141. typedef Mutex mutex_type;
  142. unique_lock() noexcept : pm(nullptr), owns(false) { }
  143. explicit unique_lock(mutex_type& m)
  144. : pm(std::addressof(m)), owns(false)
  145. {
  146. lock();
  147. owns = true;
  148. }
  149. unique_lock(mutex_type& m, defer_lock_t) noexcept
  150. : pm(std::addressof(m)), owns(false)
  151. { }
  152. unique_lock(mutex_type& m, try_to_lock_t) noexcept
  153. : pm(std::addressof(m)), owns(pm->try_lock())
  154. { }
  155. unique_lock(mutex_type& m, adopt_lock_t) noexcept
  156. : pm(std::addressof(m)), owns(true)
  157. { }
  158. // any lock-involving timed mutex API is currently only for custom implementations
  159. // the standard ones are not available
  160. template <class Clock, class Duration>
  161. unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time) noexcept
  162. : pm(std::addressof(m)), owns(pm->try_lock_until(abs_time))
  163. { }
  164. template <class Rep, class Period>
  165. unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time) noexcept
  166. : pm(std::addressof(m)), owns(pm->try_lock_for(rel_time))
  167. { }
  168. ~unique_lock()
  169. {
  170. if (owns)
  171. unlock();
  172. }
  173. unique_lock(unique_lock const&) = delete;
  174. unique_lock& operator=(unique_lock const&) = delete;
  175. unique_lock(unique_lock&& u) noexcept
  176. : pm(u.pm), owns(u.owns)
  177. {
  178. u.pm = nullptr;
  179. u.owns = false;
  180. }
  181. unique_lock& operator=(unique_lock&& u) noexcept
  182. {
  183. if (owns)
  184. unlock();
  185. unique_lock(std::move(u)).swap(*this);
  186. u.pm = nullptr;
  187. u.owns = false;
  188. return *this;
  189. }
  190. void lock()
  191. {
  192. if (!pm)
  193. throw_system_error(int(errc::operation_not_permitted),
  194. "unique_lock::lock: references null mutex");
  195. else if (owns)
  196. throw_system_error(int(errc::resource_deadlock_would_occur),
  197. "unique_lock::lock: already locked" );
  198. else {
  199. pm->lock();
  200. owns = true;
  201. }
  202. }
  203. bool try_lock()
  204. {
  205. if (!pm)
  206. throw_system_error(int(errc::operation_not_permitted),
  207. "unique_lock::try_lock: references null mutex");
  208. else if (owns)
  209. throw_system_error(int(errc::resource_deadlock_would_occur),
  210. "unique_lock::try_lock: already locked" );
  211. else {
  212. owns = pm->try_lock();
  213. }
  214. return owns;
  215. }
  216. template <class Rep, class Period>
  217. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
  218. {
  219. if (!pm)
  220. throw_system_error(int(errc::operation_not_permitted),
  221. "unique_lock::try_lock_for: references null mutex");
  222. else if (owns)
  223. throw_system_error(int(errc::resource_deadlock_would_occur),
  224. "unique_lock::try_lock_for: already locked");
  225. else {
  226. owns = pm->try_lock_for(rel_time);
  227. }
  228. return owns;
  229. }
  230. template <class Clock, class Duration>
  231. bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time)
  232. {
  233. if (!pm)
  234. throw_system_error(int(errc::operation_not_permitted),
  235. "unique_lock::try_lock_until: references null mutex");
  236. else if (owns)
  237. throw_system_error(int(errc::resource_deadlock_would_occur),
  238. "unique_lock::try_lock_until: already locked");
  239. else {
  240. owns = pm->try_lock_until(abs_time);
  241. }
  242. return owns;
  243. }
  244. void unlock()
  245. {
  246. if (!owns)
  247. throw_system_error(int(errc::operation_not_permitted),
  248. "unique_lock::unlock: not locked");
  249. else {
  250. pm->unlock();
  251. owns = false;
  252. }
  253. }
  254. void swap(unique_lock& u) noexcept
  255. {
  256. std::swap(pm, u.pm);
  257. std::swap(owns, u.owns);
  258. }
  259. mutex_type *release() noexcept
  260. {
  261. mutex_type* ret_mutex = pm;
  262. pm = nullptr;
  263. owns = false;
  264. return ret_mutex;
  265. }
  266. bool owns_lock() const noexcept
  267. { return owns; }
  268. explicit operator bool() const noexcept
  269. { return owns_lock(); }
  270. mutex_type* mutex() const noexcept
  271. { return pm; }
  272. private:
  273. mutex_type *pm;
  274. bool owns;
  275. };
  276. template <class Mutex>
  277. void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
  278. {
  279. x.swap(y);
  280. }
  281. template <class L0, class L1>
  282. int try_lock(L0& l0, L1& l1)
  283. {
  284. unique_lock<L0> u0(l0, try_to_lock); // try to lock the first Lockable
  285. // using unique_lock since we don't want to unlock l0 manually if l1 fails to lock
  286. if (u0.owns_lock())
  287. {
  288. if (l1.try_lock()) // lock the second one
  289. {
  290. u0.release(); // do not let RAII of a unique_lock unlock l0
  291. return -1;
  292. }
  293. else
  294. return 1;
  295. }
  296. return 0;
  297. }
  298. template <class L0, class L1, class L2, class... L3>
  299. int try_lock(L0& l0, L1& l1, L2& l2, L3&... l3)
  300. {
  301. int r = 0;
  302. unique_lock<L0> u0(l0, try_to_lock);
  303. // automatically unlock is done through RAII of unique_lock
  304. if (u0.owns_lock())
  305. {
  306. r = try_lock(l1, l2, l3...);
  307. if (r == -1)
  308. u0.release();
  309. else
  310. ++r;
  311. }
  312. return r;
  313. }
  314. template <class L0, class L1, class L2, class ...L3>
  315. void
  316. __lock_first(int i, L0& l0, L1& l1, L2& l2, L3&... l3)
  317. {
  318. while (true)
  319. {
  320. // we first lock the one that is the most difficult to lock
  321. switch (i)
  322. {
  323. case 0:
  324. {
  325. unique_lock<L0> u0(l0);
  326. i = try_lock(l1, l2, l3...);
  327. if (i == -1)
  328. {
  329. u0.release();
  330. return;
  331. }
  332. }
  333. ++i;
  334. sched_yield();
  335. break;
  336. case 1:
  337. {
  338. unique_lock<L1> u1(l1);
  339. i = try_lock(l2, l3..., l0);
  340. if (i == -1)
  341. {
  342. u1.release();
  343. return;
  344. }
  345. }
  346. if (i == sizeof...(L3) + 1) // all except l0 are locked
  347. i = 0;
  348. else
  349. i += 2; // since i was two-based above
  350. sched_yield();
  351. break;
  352. default:
  353. __lock_first(i - 2, l2, l3..., l0, l1);
  354. return;
  355. }
  356. }
  357. }
  358. template <class L0, class L1>
  359. void lock(L0& l0, L1& l1)
  360. {
  361. while (true)
  362. {
  363. {
  364. unique_lock<L0> u0(l0);
  365. if (l1.try_lock())
  366. {
  367. u0.release();
  368. break;
  369. }
  370. }
  371. sched_yield();
  372. // wait and try the other way
  373. {
  374. unique_lock<L1> u1(l1);
  375. if (l0.try_lock())
  376. {
  377. u1.release();
  378. break;
  379. }
  380. }
  381. sched_yield();
  382. }
  383. }
  384. template <class L0, class L1, class... L2>
  385. void lock(L0& l0, L1& l1, L2&... l2)
  386. {
  387. __lock_first(0, l0, l1, l2...);
  388. }
  389. struct once_flag
  390. {
  391. constexpr once_flag() noexcept = default;
  392. once_flag(const once_flag&) = delete;
  393. once_flag& operator=(const once_flag&) = delete;
  394. template <class Callable, class... Args>
  395. friend void call_once(once_flag& flag, Callable&& func, Args&&... args);
  396. private:
  397. pthread_once_t _m_once = PTHREAD_ONCE_INIT;
  398. };
  399. mutex& get_once_mutex();
  400. extern function<void()> once_functor;
  401. extern void set_once_functor_lock_ptr(unique_lock<mutex>*);
  402. extern "C" void once_proxy(); // passed into pthread_once
  403. template <class Callable, class... Args>
  404. void call_once(once_flag& flag, Callable&& func, Args&&... args)
  405. {
  406. // use a lock to ensure the call to the functor
  407. // is exclusive to only the first calling thread
  408. unique_lock<mutex> functor_lock(get_once_mutex());
  409. auto call_wrapper = std::bind(std::forward<Callable>(func), std::forward<Args>(args)...);
  410. once_functor = [&]() { call_wrapper(); };
  411. set_once_functor_lock_ptr(&functor_lock); // so as to unlock when actually calling
  412. int err = pthread_once(&flag._m_once, &once_proxy);
  413. if (functor_lock)
  414. set_once_functor_lock_ptr(nullptr);
  415. if (err)
  416. throw_system_error(err, "call_once failed");
  417. }
  418. }
  419. //#endif //(RT_USING_PTHREADS)