shared_mutex.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. #ifndef BOOST_THREAD_PTHREAD_SHARED_MUTEX_HPP
  2. #define BOOST_THREAD_PTHREAD_SHARED_MUTEX_HPP
  3. // (C) Copyright 2006-8 Anthony Williams
  4. // (C) Copyright 2012 Vicente J. Botet Escriba
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #include <boost/assert.hpp>
  10. #include <boost/static_assert.hpp>
  11. #include <boost/thread/mutex.hpp>
  12. #include <boost/thread/condition_variable.hpp>
  13. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  14. #include <boost/thread/detail/thread_interruption.hpp>
  15. #endif
  16. #ifdef BOOST_THREAD_USES_CHRONO
  17. #include <boost/chrono/system_clocks.hpp>
  18. #include <boost/chrono/ceil.hpp>
  19. #endif
  20. #include <boost/thread/detail/delete.hpp>
  21. #include <boost/assert.hpp>
  22. #include <boost/config/abi_prefix.hpp>
  23. namespace boost
  24. {
  25. class shared_mutex
  26. {
  27. private:
  28. class state_data
  29. {
  30. public:
  31. state_data () :
  32. shared_count(0),
  33. exclusive(false),
  34. upgrade(false),
  35. exclusive_waiting_blocked(false)
  36. {}
  37. void assert_free() const
  38. {
  39. BOOST_ASSERT( ! exclusive );
  40. BOOST_ASSERT( ! upgrade );
  41. BOOST_ASSERT( shared_count==0 );
  42. }
  43. void assert_locked() const
  44. {
  45. BOOST_ASSERT( exclusive );
  46. BOOST_ASSERT( shared_count==0 );
  47. BOOST_ASSERT( ! upgrade );
  48. }
  49. void assert_lock_shared () const
  50. {
  51. BOOST_ASSERT( ! exclusive );
  52. BOOST_ASSERT( shared_count>0 );
  53. //BOOST_ASSERT( (! upgrade) || (shared_count>1));
  54. // if upgraded there are at least 2 threads sharing the mutex,
  55. // except when unlock_upgrade_and_lock has decreased the number of readers but has not taken yet exclusive ownership.
  56. }
  57. void assert_lock_upgraded () const
  58. {
  59. BOOST_ASSERT( ! exclusive );
  60. BOOST_ASSERT( upgrade );
  61. BOOST_ASSERT( shared_count>0 );
  62. }
  63. void assert_lock_not_upgraded () const
  64. {
  65. BOOST_ASSERT( ! upgrade );
  66. }
  67. bool can_lock () const
  68. {
  69. return ! (shared_count || exclusive);
  70. }
  71. void exclusive_blocked (bool blocked)
  72. {
  73. exclusive_waiting_blocked = blocked;
  74. }
  75. void lock ()
  76. {
  77. exclusive = true;
  78. }
  79. void unlock ()
  80. {
  81. exclusive = false;
  82. exclusive_waiting_blocked = false;
  83. }
  84. bool can_lock_shared () const
  85. {
  86. return ! (exclusive || exclusive_waiting_blocked);
  87. }
  88. bool more_shared () const
  89. {
  90. return shared_count > 0 ;
  91. }
  92. unsigned get_shared_count () const
  93. {
  94. return shared_count ;
  95. }
  96. unsigned lock_shared ()
  97. {
  98. return ++shared_count;
  99. }
  100. void unlock_shared ()
  101. {
  102. --shared_count;
  103. }
  104. bool unlock_shared_downgrades()
  105. {
  106. if (upgrade) {
  107. upgrade=false;
  108. exclusive=true;
  109. return true;
  110. } else {
  111. exclusive_waiting_blocked=false;
  112. return false;
  113. }
  114. }
  115. void lock_upgrade ()
  116. {
  117. ++shared_count;
  118. upgrade=true;
  119. }
  120. bool can_lock_upgrade () const
  121. {
  122. return ! (exclusive || exclusive_waiting_blocked || upgrade);
  123. }
  124. void unlock_upgrade ()
  125. {
  126. upgrade=false;
  127. --shared_count;
  128. }
  129. //private:
  130. unsigned shared_count;
  131. bool exclusive;
  132. bool upgrade;
  133. bool exclusive_waiting_blocked;
  134. };
  135. state_data state;
  136. boost::mutex state_change;
  137. boost::condition_variable shared_cond;
  138. boost::condition_variable exclusive_cond;
  139. boost::condition_variable upgrade_cond;
  140. void release_waiters()
  141. {
  142. exclusive_cond.notify_one();
  143. shared_cond.notify_all();
  144. }
  145. public:
  146. BOOST_THREAD_NO_COPYABLE(shared_mutex)
  147. shared_mutex()
  148. {
  149. }
  150. ~shared_mutex()
  151. {
  152. }
  153. void lock_shared()
  154. {
  155. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  156. boost::this_thread::disable_interruption do_not_disturb;
  157. #endif
  158. boost::unique_lock<boost::mutex> lk(state_change);
  159. while(!state.can_lock_shared())
  160. {
  161. shared_cond.wait(lk);
  162. }
  163. state.lock_shared();
  164. }
  165. bool try_lock_shared()
  166. {
  167. boost::unique_lock<boost::mutex> lk(state_change);
  168. if(!state.can_lock_shared())
  169. {
  170. return false;
  171. }
  172. state.lock_shared();
  173. return true;
  174. }
  175. #if defined BOOST_THREAD_USES_DATETIME
  176. bool timed_lock_shared(system_time const& timeout)
  177. {
  178. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  179. boost::this_thread::disable_interruption do_not_disturb;
  180. #endif
  181. boost::unique_lock<boost::mutex> lk(state_change);
  182. while(!state.can_lock_shared())
  183. {
  184. if(!shared_cond.timed_wait(lk,timeout))
  185. {
  186. return false;
  187. }
  188. }
  189. state.lock_shared();
  190. return true;
  191. }
  192. template<typename TimeDuration>
  193. bool timed_lock_shared(TimeDuration const & relative_time)
  194. {
  195. return timed_lock_shared(get_system_time()+relative_time);
  196. }
  197. #endif
  198. #ifdef BOOST_THREAD_USES_CHRONO
  199. template <class Rep, class Period>
  200. bool try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time)
  201. {
  202. return try_lock_shared_until(chrono::steady_clock::now() + rel_time);
  203. }
  204. template <class Clock, class Duration>
  205. bool try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time)
  206. {
  207. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  208. boost::this_thread::disable_interruption do_not_disturb;
  209. #endif
  210. boost::unique_lock<boost::mutex> lk(state_change);
  211. while(!state.can_lock_shared())
  212. //while(state.exclusive || state.exclusive_waiting_blocked)
  213. {
  214. if(cv_status::timeout==shared_cond.wait_until(lk,abs_time))
  215. {
  216. return false;
  217. }
  218. }
  219. state.lock_shared();
  220. return true;
  221. }
  222. #endif
  223. void unlock_shared()
  224. {
  225. boost::unique_lock<boost::mutex> lk(state_change);
  226. state.assert_lock_shared();
  227. state.unlock_shared();
  228. if (! state.more_shared())
  229. {
  230. if (state.upgrade)
  231. {
  232. // As there is a thread doing a unlock_upgrade_and_lock that is waiting for ! state.more_shared()
  233. // avoid other threads to lock, lock_upgrade or lock_shared, so only this thread is notified.
  234. state.upgrade=false;
  235. state.exclusive=true;
  236. lk.unlock();
  237. upgrade_cond.notify_one();
  238. }
  239. else
  240. {
  241. state.exclusive_waiting_blocked=false;
  242. lk.unlock();
  243. }
  244. release_waiters();
  245. }
  246. }
  247. void lock()
  248. {
  249. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  250. boost::this_thread::disable_interruption do_not_disturb;
  251. #endif
  252. boost::unique_lock<boost::mutex> lk(state_change);
  253. while (state.shared_count || state.exclusive)
  254. {
  255. state.exclusive_waiting_blocked=true;
  256. exclusive_cond.wait(lk);
  257. }
  258. state.exclusive=true;
  259. }
  260. #if defined BOOST_THREAD_USES_DATETIME
  261. bool timed_lock(system_time const& timeout)
  262. {
  263. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  264. boost::this_thread::disable_interruption do_not_disturb;
  265. #endif
  266. boost::unique_lock<boost::mutex> lk(state_change);
  267. while(state.shared_count || state.exclusive)
  268. {
  269. state.exclusive_waiting_blocked=true;
  270. if(!exclusive_cond.timed_wait(lk,timeout))
  271. {
  272. if(state.shared_count || state.exclusive)
  273. {
  274. state.exclusive_waiting_blocked=false;
  275. release_waiters();
  276. return false;
  277. }
  278. break;
  279. }
  280. }
  281. state.exclusive=true;
  282. return true;
  283. }
  284. template<typename TimeDuration>
  285. bool timed_lock(TimeDuration const & relative_time)
  286. {
  287. return timed_lock(get_system_time()+relative_time);
  288. }
  289. #endif
  290. #ifdef BOOST_THREAD_USES_CHRONO
  291. template <class Rep, class Period>
  292. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
  293. {
  294. return try_lock_until(chrono::steady_clock::now() + rel_time);
  295. }
  296. template <class Clock, class Duration>
  297. bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time)
  298. {
  299. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  300. boost::this_thread::disable_interruption do_not_disturb;
  301. #endif
  302. boost::unique_lock<boost::mutex> lk(state_change);
  303. while(state.shared_count || state.exclusive)
  304. {
  305. state.exclusive_waiting_blocked=true;
  306. if(cv_status::timeout == exclusive_cond.wait_until(lk,abs_time))
  307. {
  308. if(state.shared_count || state.exclusive)
  309. {
  310. state.exclusive_waiting_blocked=false;
  311. release_waiters();
  312. return false;
  313. }
  314. break;
  315. }
  316. }
  317. state.exclusive=true;
  318. return true;
  319. }
  320. #endif
  321. bool try_lock()
  322. {
  323. boost::unique_lock<boost::mutex> lk(state_change);
  324. if(state.shared_count || state.exclusive)
  325. {
  326. return false;
  327. }
  328. else
  329. {
  330. state.exclusive=true;
  331. return true;
  332. }
  333. }
  334. void unlock()
  335. {
  336. boost::unique_lock<boost::mutex> lk(state_change);
  337. state.assert_locked();
  338. state.exclusive=false;
  339. state.exclusive_waiting_blocked=false;
  340. state.assert_free();
  341. release_waiters();
  342. }
  343. void lock_upgrade()
  344. {
  345. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  346. boost::this_thread::disable_interruption do_not_disturb;
  347. #endif
  348. boost::unique_lock<boost::mutex> lk(state_change);
  349. while(state.exclusive || state.exclusive_waiting_blocked || state.upgrade)
  350. {
  351. shared_cond.wait(lk);
  352. }
  353. state.lock_shared();
  354. state.upgrade=true;
  355. }
  356. #if defined BOOST_THREAD_USES_DATETIME
  357. bool timed_lock_upgrade(system_time const& timeout)
  358. {
  359. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  360. boost::this_thread::disable_interruption do_not_disturb;
  361. #endif
  362. boost::unique_lock<boost::mutex> lk(state_change);
  363. while(state.exclusive || state.exclusive_waiting_blocked || state.upgrade)
  364. {
  365. if(!shared_cond.timed_wait(lk,timeout))
  366. {
  367. if(state.exclusive || state.exclusive_waiting_blocked || state.upgrade)
  368. {
  369. return false;
  370. }
  371. break;
  372. }
  373. }
  374. state.lock_shared();
  375. state.upgrade=true;
  376. return true;
  377. }
  378. template<typename TimeDuration>
  379. bool timed_lock_upgrade(TimeDuration const & relative_time)
  380. {
  381. return timed_lock_upgrade(get_system_time()+relative_time);
  382. }
  383. #endif
  384. #ifdef BOOST_THREAD_USES_CHRONO
  385. template <class Rep, class Period>
  386. bool try_lock_upgrade_for(const chrono::duration<Rep, Period>& rel_time)
  387. {
  388. return try_lock_upgrade_until(chrono::steady_clock::now() + rel_time);
  389. }
  390. template <class Clock, class Duration>
  391. bool try_lock_upgrade_until(const chrono::time_point<Clock, Duration>& abs_time)
  392. {
  393. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  394. boost::this_thread::disable_interruption do_not_disturb;
  395. #endif
  396. boost::unique_lock<boost::mutex> lk(state_change);
  397. while(state.exclusive || state.exclusive_waiting_blocked || state.upgrade)
  398. {
  399. if(cv_status::timeout == shared_cond.wait_until(lk,abs_time))
  400. {
  401. if(state.exclusive || state.exclusive_waiting_blocked || state.upgrade)
  402. {
  403. return false;
  404. }
  405. break;
  406. }
  407. }
  408. state.lock_shared();
  409. state.upgrade=true;
  410. return true;
  411. }
  412. #endif
  413. bool try_lock_upgrade()
  414. {
  415. boost::unique_lock<boost::mutex> lk(state_change);
  416. if(state.exclusive || state.exclusive_waiting_blocked || state.upgrade)
  417. {
  418. return false;
  419. }
  420. else
  421. {
  422. state.lock_shared();
  423. state.upgrade=true;
  424. state.assert_lock_upgraded();
  425. return true;
  426. }
  427. }
  428. void unlock_upgrade()
  429. {
  430. boost::unique_lock<boost::mutex> lk(state_change);
  431. //state.upgrade=false;
  432. state.unlock_upgrade();
  433. if(! state.more_shared() )
  434. {
  435. state.exclusive_waiting_blocked=false;
  436. release_waiters();
  437. } else {
  438. shared_cond.notify_all();
  439. }
  440. }
  441. // Upgrade <-> Exclusive
  442. void unlock_upgrade_and_lock()
  443. {
  444. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  445. boost::this_thread::disable_interruption do_not_disturb;
  446. #endif
  447. boost::unique_lock<boost::mutex> lk(state_change);
  448. state.assert_lock_upgraded();
  449. state.unlock_shared();
  450. while (state.more_shared())
  451. {
  452. upgrade_cond.wait(lk);
  453. }
  454. state.upgrade=false;
  455. state.exclusive=true;
  456. state.assert_locked();
  457. }
  458. void unlock_and_lock_upgrade()
  459. {
  460. boost::unique_lock<boost::mutex> lk(state_change);
  461. state.assert_locked();
  462. state.exclusive=false;
  463. state.upgrade=true;
  464. state.lock_shared();
  465. state.exclusive_waiting_blocked=false;
  466. state.assert_lock_upgraded();
  467. release_waiters();
  468. }
  469. bool try_unlock_upgrade_and_lock()
  470. {
  471. boost::unique_lock<boost::mutex> lk(state_change);
  472. state.assert_lock_upgraded();
  473. if( !state.exclusive
  474. && !state.exclusive_waiting_blocked
  475. && state.upgrade
  476. && state.shared_count==1)
  477. {
  478. state.shared_count=0;
  479. state.exclusive=true;
  480. state.upgrade=false;
  481. state.assert_locked();
  482. return true;
  483. }
  484. return false;
  485. }
  486. #ifdef BOOST_THREAD_USES_CHRONO
  487. template <class Rep, class Period>
  488. bool
  489. try_unlock_upgrade_and_lock_for(
  490. const chrono::duration<Rep, Period>& rel_time)
  491. {
  492. return try_unlock_upgrade_and_lock_until(
  493. chrono::steady_clock::now() + rel_time);
  494. }
  495. template <class Clock, class Duration>
  496. bool
  497. try_unlock_upgrade_and_lock_until(
  498. const chrono::time_point<Clock, Duration>& abs_time)
  499. {
  500. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  501. boost::this_thread::disable_interruption do_not_disturb;
  502. #endif
  503. boost::unique_lock<boost::mutex> lk(state_change);
  504. state.assert_lock_upgraded();
  505. if (state.shared_count != 1)
  506. {
  507. for (;;)
  508. {
  509. cv_status status = shared_cond.wait_until(lk,abs_time);
  510. if (state.shared_count == 1)
  511. break;
  512. if(status == cv_status::timeout)
  513. return false;
  514. }
  515. }
  516. state.upgrade=false;
  517. state.exclusive=true;
  518. state.exclusive_waiting_blocked=false;
  519. state.shared_count=0;
  520. return true;
  521. }
  522. #endif
  523. // Shared <-> Exclusive
  524. void unlock_and_lock_shared()
  525. {
  526. boost::unique_lock<boost::mutex> lk(state_change);
  527. state.assert_locked();
  528. state.exclusive=false;
  529. state.lock_shared();
  530. state.exclusive_waiting_blocked=false;
  531. release_waiters();
  532. }
  533. #ifdef BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
  534. bool try_unlock_shared_and_lock()
  535. {
  536. boost::unique_lock<boost::mutex> lk(state_change);
  537. state.assert_lock_shared();
  538. if( !state.exclusive
  539. && !state.exclusive_waiting_blocked
  540. && !state.upgrade
  541. && state.shared_count==1)
  542. {
  543. state.shared_count=0;
  544. state.exclusive=true;
  545. return true;
  546. }
  547. return false;
  548. }
  549. #ifdef BOOST_THREAD_USES_CHRONO
  550. template <class Rep, class Period>
  551. bool
  552. try_unlock_shared_and_lock_for(
  553. const chrono::duration<Rep, Period>& rel_time)
  554. {
  555. return try_unlock_shared_and_lock_until(
  556. chrono::steady_clock::now() + rel_time);
  557. }
  558. template <class Clock, class Duration>
  559. bool
  560. try_unlock_shared_and_lock_until(
  561. const chrono::time_point<Clock, Duration>& abs_time)
  562. {
  563. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  564. boost::this_thread::disable_interruption do_not_disturb;
  565. #endif
  566. boost::unique_lock<boost::mutex> lk(state_change);
  567. state.assert_lock_shared();
  568. if (state.shared_count != 1)
  569. {
  570. for (;;)
  571. {
  572. cv_status status = shared_cond.wait_until(lk,abs_time);
  573. if (state.shared_count == 1)
  574. break;
  575. if(status == cv_status::timeout)
  576. return false;
  577. }
  578. }
  579. state.upgrade=false;
  580. state.exclusive=true;
  581. state.exclusive_waiting_blocked=false;
  582. state.shared_count=0;
  583. return true;
  584. }
  585. #endif
  586. #endif
  587. // Shared <-> Upgrade
  588. void unlock_upgrade_and_lock_shared()
  589. {
  590. boost::unique_lock<boost::mutex> lk(state_change);
  591. state.assert_lock_upgraded();
  592. state.upgrade=false;
  593. state.exclusive_waiting_blocked=false;
  594. release_waiters();
  595. }
  596. #ifdef BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
  597. bool try_unlock_shared_and_lock_upgrade()
  598. {
  599. boost::unique_lock<boost::mutex> lk(state_change);
  600. state.assert_lock_shared();
  601. if( !state.exclusive
  602. && !state.exclusive_waiting_blocked
  603. && !state.upgrade
  604. )
  605. {
  606. state.upgrade=true;
  607. return true;
  608. }
  609. return false;
  610. }
  611. #ifdef BOOST_THREAD_USES_CHRONO
  612. template <class Rep, class Period>
  613. bool
  614. try_unlock_shared_and_lock_upgrade_for(
  615. const chrono::duration<Rep, Period>& rel_time)
  616. {
  617. return try_unlock_shared_and_lock_upgrade_until(
  618. chrono::steady_clock::now() + rel_time);
  619. }
  620. template <class Clock, class Duration>
  621. bool
  622. try_unlock_shared_and_lock_upgrade_until(
  623. const chrono::time_point<Clock, Duration>& abs_time)
  624. {
  625. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  626. boost::this_thread::disable_interruption do_not_disturb;
  627. #endif
  628. boost::unique_lock<boost::mutex> lk(state_change);
  629. state.assert_lock_shared();
  630. if( state.exclusive
  631. || state.exclusive_waiting_blocked
  632. || state.upgrade
  633. )
  634. {
  635. for (;;)
  636. {
  637. cv_status status = exclusive_cond.wait_until(lk,abs_time);
  638. if( ! state.exclusive
  639. && ! state.exclusive_waiting_blocked
  640. && ! state.upgrade
  641. )
  642. break;
  643. if(status == cv_status::timeout)
  644. return false;
  645. }
  646. }
  647. state.upgrade=true;
  648. return true;
  649. }
  650. #endif
  651. #endif
  652. };
  653. typedef shared_mutex upgrade_mutex;
  654. }
  655. #include <boost/config/abi_suffix.hpp>
  656. #endif