lock_algorithms.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // Distributed under the Boost Software License, Version 1.0. (See
  2. // accompanying file LICENSE_1_0.txt or copy at
  3. // http://www.boost.org/LICENSE_1_0.txt)
  4. // (C) Copyright 2007 Anthony Williams
  5. // (C) Copyright 2011-2012 Vicente J. Botet Escriba
  6. #ifndef BOOST_THREAD_LOCK_ALGORITHMS_HPP
  7. #define BOOST_THREAD_LOCK_ALGORITHMS_HPP
  8. #include <boost/thread/detail/config.hpp>
  9. #include <boost/thread/lock_types.hpp>
  10. #include <boost/thread/lockable_traits.hpp>
  11. #include <algorithm>
  12. #include <iterator>
  13. #include <boost/config/abi_prefix.hpp>
  14. namespace boost
  15. {
  16. namespace detail
  17. {
  18. template <typename MutexType1, typename MutexType2>
  19. unsigned try_lock_internal(MutexType1& m1, MutexType2& m2)
  20. {
  21. boost::unique_lock<MutexType1> l1(m1, boost::try_to_lock);
  22. if (!l1)
  23. {
  24. return 1;
  25. }
  26. if (!m2.try_lock())
  27. {
  28. return 2;
  29. }
  30. l1.release();
  31. return 0;
  32. }
  33. template <typename MutexType1, typename MutexType2, typename MutexType3>
  34. unsigned try_lock_internal(MutexType1& m1, MutexType2& m2, MutexType3& m3)
  35. {
  36. boost::unique_lock<MutexType1> l1(m1, boost::try_to_lock);
  37. if (!l1)
  38. {
  39. return 1;
  40. }
  41. if (unsigned const failed_lock=try_lock_internal(m2,m3))
  42. {
  43. return failed_lock + 1;
  44. }
  45. l1.release();
  46. return 0;
  47. }
  48. template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4>
  49. unsigned try_lock_internal(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4)
  50. {
  51. boost::unique_lock<MutexType1> l1(m1, boost::try_to_lock);
  52. if (!l1)
  53. {
  54. return 1;
  55. }
  56. if (unsigned const failed_lock=try_lock_internal(m2,m3,m4))
  57. {
  58. return failed_lock + 1;
  59. }
  60. l1.release();
  61. return 0;
  62. }
  63. template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4, typename MutexType5>
  64. unsigned try_lock_internal(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4, MutexType5& m5)
  65. {
  66. boost::unique_lock<MutexType1> l1(m1, boost::try_to_lock);
  67. if (!l1)
  68. {
  69. return 1;
  70. }
  71. if (unsigned const failed_lock=try_lock_internal(m2,m3,m4,m5))
  72. {
  73. return failed_lock + 1;
  74. }
  75. l1.release();
  76. return 0;
  77. }
  78. template <typename MutexType1, typename MutexType2>
  79. unsigned lock_helper(MutexType1& m1, MutexType2& m2)
  80. {
  81. boost::unique_lock<MutexType1> l1(m1);
  82. if (!m2.try_lock())
  83. {
  84. return 1;
  85. }
  86. l1.release();
  87. return 0;
  88. }
  89. template <typename MutexType1, typename MutexType2, typename MutexType3>
  90. unsigned lock_helper(MutexType1& m1, MutexType2& m2, MutexType3& m3)
  91. {
  92. boost::unique_lock<MutexType1> l1(m1);
  93. if (unsigned const failed_lock=try_lock_internal(m2,m3))
  94. {
  95. return failed_lock;
  96. }
  97. l1.release();
  98. return 0;
  99. }
  100. template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4>
  101. unsigned lock_helper(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4)
  102. {
  103. boost::unique_lock<MutexType1> l1(m1);
  104. if (unsigned const failed_lock=try_lock_internal(m2,m3,m4))
  105. {
  106. return failed_lock;
  107. }
  108. l1.release();
  109. return 0;
  110. }
  111. template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4, typename MutexType5>
  112. unsigned lock_helper(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4, MutexType5& m5)
  113. {
  114. boost::unique_lock<MutexType1> l1(m1);
  115. if (unsigned const failed_lock=try_lock_internal(m2,m3,m4,m5))
  116. {
  117. return failed_lock;
  118. }
  119. l1.release();
  120. return 0;
  121. }
  122. }
  123. namespace detail
  124. {
  125. template <bool x>
  126. struct is_mutex_type_wrapper
  127. {
  128. };
  129. template <typename MutexType1, typename MutexType2>
  130. void lock_impl(MutexType1& m1, MutexType2& m2, is_mutex_type_wrapper<true> )
  131. {
  132. unsigned const lock_count = 2;
  133. unsigned lock_first = 0;
  134. for (;;)
  135. {
  136. switch (lock_first)
  137. {
  138. case 0:
  139. lock_first = detail::lock_helper(m1, m2);
  140. if (!lock_first) return;
  141. break;
  142. case 1:
  143. lock_first = detail::lock_helper(m2, m1);
  144. if (!lock_first) return;
  145. lock_first = (lock_first + 1) % lock_count;
  146. break;
  147. }
  148. }
  149. }
  150. template <typename Iterator>
  151. void lock_impl(Iterator begin, Iterator end, is_mutex_type_wrapper<false> );
  152. }
  153. template <typename MutexType1, typename MutexType2>
  154. void lock(MutexType1& m1, MutexType2& m2)
  155. {
  156. detail::lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
  157. }
  158. template <typename MutexType1, typename MutexType2>
  159. void lock(const MutexType1& m1, MutexType2& m2)
  160. {
  161. detail::lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
  162. }
  163. template <typename MutexType1, typename MutexType2>
  164. void lock(MutexType1& m1, const MutexType2& m2)
  165. {
  166. detail::lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
  167. }
  168. template <typename MutexType1, typename MutexType2>
  169. void lock(const MutexType1& m1, const MutexType2& m2)
  170. {
  171. detail::lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
  172. }
  173. template <typename MutexType1, typename MutexType2, typename MutexType3>
  174. void lock(MutexType1& m1, MutexType2& m2, MutexType3& m3)
  175. {
  176. unsigned const lock_count = 3;
  177. unsigned lock_first = 0;
  178. for (;;)
  179. {
  180. switch (lock_first)
  181. {
  182. case 0:
  183. lock_first = detail::lock_helper(m1, m2, m3);
  184. if (!lock_first) return;
  185. break;
  186. case 1:
  187. lock_first = detail::lock_helper(m2, m3, m1);
  188. if (!lock_first) return;
  189. lock_first = (lock_first + 1) % lock_count;
  190. break;
  191. case 2:
  192. lock_first = detail::lock_helper(m3, m1, m2);
  193. if (!lock_first) return;
  194. lock_first = (lock_first + 2) % lock_count;
  195. break;
  196. }
  197. }
  198. }
  199. template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4>
  200. void lock(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4)
  201. {
  202. unsigned const lock_count = 4;
  203. unsigned lock_first = 0;
  204. for (;;)
  205. {
  206. switch (lock_first)
  207. {
  208. case 0:
  209. lock_first = detail::lock_helper(m1, m2, m3, m4);
  210. if (!lock_first) return;
  211. break;
  212. case 1:
  213. lock_first = detail::lock_helper(m2, m3, m4, m1);
  214. if (!lock_first) return;
  215. lock_first = (lock_first + 1) % lock_count;
  216. break;
  217. case 2:
  218. lock_first = detail::lock_helper(m3, m4, m1, m2);
  219. if (!lock_first) return;
  220. lock_first = (lock_first + 2) % lock_count;
  221. break;
  222. case 3:
  223. lock_first = detail::lock_helper(m4, m1, m2, m3);
  224. if (!lock_first) return;
  225. lock_first = (lock_first + 3) % lock_count;
  226. break;
  227. }
  228. }
  229. }
  230. template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4, typename MutexType5>
  231. void lock(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4, MutexType5& m5)
  232. {
  233. unsigned const lock_count = 5;
  234. unsigned lock_first = 0;
  235. for (;;)
  236. {
  237. switch (lock_first)
  238. {
  239. case 0:
  240. lock_first = detail::lock_helper(m1, m2, m3, m4, m5);
  241. if (!lock_first) return;
  242. break;
  243. case 1:
  244. lock_first = detail::lock_helper(m2, m3, m4, m5, m1);
  245. if (!lock_first) return;
  246. lock_first = (lock_first + 1) % lock_count;
  247. break;
  248. case 2:
  249. lock_first = detail::lock_helper(m3, m4, m5, m1, m2);
  250. if (!lock_first) return;
  251. lock_first = (lock_first + 2) % lock_count;
  252. break;
  253. case 3:
  254. lock_first = detail::lock_helper(m4, m5, m1, m2, m3);
  255. if (!lock_first) return;
  256. lock_first = (lock_first + 3) % lock_count;
  257. break;
  258. case 4:
  259. lock_first = detail::lock_helper(m5, m1, m2, m3, m4);
  260. if (!lock_first) return;
  261. lock_first = (lock_first + 4) % lock_count;
  262. break;
  263. }
  264. }
  265. }
  266. namespace detail
  267. {
  268. template <typename Mutex, bool x = is_mutex_type<Mutex>::value>
  269. struct try_lock_impl_return
  270. {
  271. typedef int type;
  272. };
  273. template <typename Iterator>
  274. struct try_lock_impl_return<Iterator, false>
  275. {
  276. typedef Iterator type;
  277. };
  278. template <typename MutexType1, typename MutexType2>
  279. int try_lock_impl(MutexType1& m1, MutexType2& m2, is_mutex_type_wrapper<true> )
  280. {
  281. return ((int) detail::try_lock_internal(m1, m2)) - 1;
  282. }
  283. template <typename Iterator>
  284. Iterator try_lock_impl(Iterator begin, Iterator end, is_mutex_type_wrapper<false> );
  285. }
  286. template <typename MutexType1, typename MutexType2>
  287. typename detail::try_lock_impl_return<MutexType1>::type try_lock(MutexType1& m1, MutexType2& m2)
  288. {
  289. return detail::try_lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
  290. }
  291. template <typename MutexType1, typename MutexType2>
  292. typename detail::try_lock_impl_return<MutexType1>::type try_lock(const MutexType1& m1, MutexType2& m2)
  293. {
  294. return detail::try_lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
  295. }
  296. template <typename MutexType1, typename MutexType2>
  297. typename detail::try_lock_impl_return<MutexType1>::type try_lock(MutexType1& m1, const MutexType2& m2)
  298. {
  299. return detail::try_lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
  300. }
  301. template <typename MutexType1, typename MutexType2>
  302. typename detail::try_lock_impl_return<MutexType1>::type try_lock(const MutexType1& m1, const MutexType2& m2)
  303. {
  304. return detail::try_lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
  305. }
  306. template <typename MutexType1, typename MutexType2, typename MutexType3>
  307. int try_lock(MutexType1& m1, MutexType2& m2, MutexType3& m3)
  308. {
  309. return ((int) detail::try_lock_internal(m1, m2, m3)) - 1;
  310. }
  311. template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4>
  312. int try_lock(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4)
  313. {
  314. return ((int) detail::try_lock_internal(m1, m2, m3, m4)) - 1;
  315. }
  316. template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4, typename MutexType5>
  317. int try_lock(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4, MutexType5& m5)
  318. {
  319. return ((int) detail::try_lock_internal(m1, m2, m3, m4, m5)) - 1;
  320. }
  321. namespace detail
  322. {
  323. template <typename Iterator>
  324. struct range_lock_guard
  325. {
  326. Iterator begin;
  327. Iterator end;
  328. range_lock_guard(Iterator begin_, Iterator end_) :
  329. begin(begin_), end(end_)
  330. {
  331. boost::lock(begin, end);
  332. }
  333. void release()
  334. {
  335. begin = end;
  336. }
  337. ~range_lock_guard()
  338. {
  339. for (; begin != end; ++begin)
  340. {
  341. begin->unlock();
  342. }
  343. }
  344. };
  345. template <typename Iterator>
  346. Iterator try_lock_impl(Iterator begin, Iterator end, is_mutex_type_wrapper<false> )
  347. {
  348. if (begin == end)
  349. {
  350. return end;
  351. }
  352. typedef typename std::iterator_traits<Iterator>::value_type lock_type;
  353. unique_lock<lock_type> guard(*begin, try_to_lock);
  354. if (!guard.owns_lock())
  355. {
  356. return begin;
  357. }
  358. Iterator const failed = boost::try_lock(++begin, end);
  359. if (failed == end)
  360. {
  361. guard.release();
  362. }
  363. return failed;
  364. }
  365. }
  366. namespace detail
  367. {
  368. template <typename Iterator>
  369. void lock_impl(Iterator begin, Iterator end, is_mutex_type_wrapper<false> )
  370. {
  371. typedef typename std::iterator_traits<Iterator>::value_type lock_type;
  372. if (begin == end)
  373. {
  374. return;
  375. }
  376. bool start_with_begin = true;
  377. Iterator second = begin;
  378. ++second;
  379. Iterator next = second;
  380. for (;;)
  381. {
  382. unique_lock<lock_type> begin_lock(*begin, defer_lock);
  383. if (start_with_begin)
  384. {
  385. begin_lock.lock();
  386. Iterator const failed_lock = boost::try_lock(next, end);
  387. if (failed_lock == end)
  388. {
  389. begin_lock.release();
  390. return;
  391. }
  392. start_with_begin = false;
  393. next = failed_lock;
  394. }
  395. else
  396. {
  397. detail::range_lock_guard<Iterator> guard(next, end);
  398. if (begin_lock.try_lock())
  399. {
  400. Iterator const failed_lock = boost::try_lock(second, next);
  401. if (failed_lock == next)
  402. {
  403. begin_lock.release();
  404. guard.release();
  405. return;
  406. }
  407. start_with_begin = false;
  408. next = failed_lock;
  409. }
  410. else
  411. {
  412. start_with_begin = true;
  413. next = second;
  414. }
  415. }
  416. }
  417. }
  418. }
  419. }
  420. #include <boost/config/abi_suffix.hpp>
  421. #endif