shared_mutex.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. #ifndef BOOST_THREAD_V2_SHARED_MUTEX_HPP
  2. #define BOOST_THREAD_V2_SHARED_MUTEX_HPP
  3. // shared_mutex.hpp
  4. //
  5. // Copyright Howard Hinnant 2007-2010.
  6. // Copyright Vicente J. Botet Escriba 2012.
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See
  9. // accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. /*
  12. <shared_mutex> synopsis
  13. namespace boost
  14. {
  15. namespace thread_v2
  16. {
  17. class shared_mutex
  18. {
  19. public:
  20. shared_mutex();
  21. ~shared_mutex();
  22. shared_mutex(const shared_mutex&) = delete;
  23. shared_mutex& operator=(const shared_mutex&) = delete;
  24. // Exclusive ownership
  25. void lock();
  26. bool try_lock();
  27. template <class Rep, class Period>
  28. bool try_lock_for(const boost::chrono::duration<Rep, Period>& rel_time);
  29. template <class Clock, class Duration>
  30. bool
  31. try_lock_until(
  32. const boost::chrono::time_point<Clock, Duration>& abs_time);
  33. void unlock();
  34. // Shared ownership
  35. void lock_shared();
  36. bool try_lock_shared();
  37. template <class Rep, class Period>
  38. bool
  39. try_lock_shared_for(const boost::chrono::duration<Rep, Period>& rel_time);
  40. template <class Clock, class Duration>
  41. bool
  42. try_lock_shared_until(
  43. const boost::chrono::time_point<Clock, Duration>& abs_time);
  44. void unlock_shared();
  45. };
  46. class upgrade_mutex
  47. {
  48. public:
  49. upgrade_mutex();
  50. ~upgrade_mutex();
  51. upgrade_mutex(const upgrade_mutex&) = delete;
  52. upgrade_mutex& operator=(const upgrade_mutex&) = delete;
  53. // Exclusive ownership
  54. void lock();
  55. bool try_lock();
  56. template <class Rep, class Period>
  57. bool try_lock_for(const boost::chrono::duration<Rep, Period>& rel_time);
  58. template <class Clock, class Duration>
  59. bool
  60. try_lock_until(
  61. const boost::chrono::time_point<Clock, Duration>& abs_time);
  62. void unlock();
  63. // Shared ownership
  64. void lock_shared();
  65. bool try_lock_shared();
  66. template <class Rep, class Period>
  67. bool
  68. try_lock_shared_for(const boost::chrono::duration<Rep, Period>& rel_time);
  69. template <class Clock, class Duration>
  70. bool
  71. try_lock_shared_until(
  72. const boost::chrono::time_point<Clock, Duration>& abs_time);
  73. void unlock_shared();
  74. // Upgrade ownership
  75. void lock_upgrade();
  76. bool try_lock_upgrade();
  77. template <class Rep, class Period>
  78. bool
  79. try_lock_upgrade_for(
  80. const boost::chrono::duration<Rep, Period>& rel_time);
  81. template <class Clock, class Duration>
  82. bool
  83. try_lock_upgrade_until(
  84. const boost::chrono::time_point<Clock, Duration>& abs_time);
  85. void unlock_upgrade();
  86. // Shared <-> Exclusive
  87. bool try_unlock_shared_and_lock();
  88. template <class Rep, class Period>
  89. bool
  90. try_unlock_shared_and_lock_for(
  91. const boost::chrono::duration<Rep, Period>& rel_time);
  92. template <class Clock, class Duration>
  93. bool
  94. try_unlock_shared_and_lock_until(
  95. const boost::chrono::time_point<Clock, Duration>& abs_time);
  96. void unlock_and_lock_shared();
  97. // Shared <-> Upgrade
  98. bool try_unlock_shared_and_lock_upgrade();
  99. template <class Rep, class Period>
  100. bool
  101. try_unlock_shared_and_lock_upgrade_for(
  102. const boost::chrono::duration<Rep, Period>& rel_time);
  103. template <class Clock, class Duration>
  104. bool
  105. try_unlock_shared_and_lock_upgrade_until(
  106. const boost::chrono::time_point<Clock, Duration>& abs_time);
  107. void unlock_upgrade_and_lock_shared();
  108. // Upgrade <-> Exclusive
  109. void unlock_upgrade_and_lock();
  110. bool try_unlock_upgrade_and_lock();
  111. template <class Rep, class Period>
  112. bool
  113. try_unlock_upgrade_and_lock_for(
  114. const boost::chrono::duration<Rep, Period>& rel_time);
  115. template <class Clock, class Duration>
  116. bool
  117. try_unlock_upgrade_and_lock_until(
  118. const boost::chrono::time_point<Clock, Duration>& abs_time);
  119. void unlock_and_lock_upgrade();
  120. };
  121. } // thread_v2
  122. } // boost
  123. */
  124. #include <boost/thread/detail/config.hpp>
  125. #include <boost/thread/mutex.hpp>
  126. #include <boost/thread/condition_variable.hpp>
  127. #include <boost/thread/mutex.hpp>
  128. #ifdef BOOST_THREAD_USES_CHRONO
  129. #include <boost/chrono.hpp>
  130. #endif
  131. #include <climits>
  132. #include <boost/system/system_error.hpp>
  133. #include <boost/bind.hpp>
  134. namespace boost {
  135. namespace thread_v2 {
  136. class shared_mutex
  137. {
  138. typedef boost::mutex mutex_t;
  139. typedef boost::condition_variable cond_t;
  140. typedef unsigned count_t;
  141. mutex_t mut_;
  142. cond_t gate1_;
  143. // the gate2_ condition variable is only used by functions that
  144. // have taken write_entered_ but are waiting for no_readers()
  145. cond_t gate2_;
  146. count_t state_;
  147. static const count_t write_entered_ = 1U << (sizeof(count_t)*CHAR_BIT - 1);
  148. static const count_t n_readers_ = ~write_entered_;
  149. bool no_writer() const
  150. {
  151. return (state_ & write_entered_) == 0;
  152. }
  153. bool one_writer() const
  154. {
  155. return (state_ & write_entered_) != 0;
  156. }
  157. bool no_writer_no_readers() const
  158. {
  159. //return (state_ & write_entered_) == 0 &&
  160. // (state_ & n_readers_) == 0;
  161. return state_ == 0;
  162. }
  163. bool no_writer_no_max_readers() const
  164. {
  165. return (state_ & write_entered_) == 0 &&
  166. (state_ & n_readers_) != n_readers_;
  167. }
  168. bool no_readers() const
  169. {
  170. return (state_ & n_readers_) == 0;
  171. }
  172. bool one_or_more_readers() const
  173. {
  174. return (state_ & n_readers_) > 0;
  175. }
  176. shared_mutex(shared_mutex const&);
  177. shared_mutex& operator=(shared_mutex const&);
  178. public:
  179. shared_mutex();
  180. ~shared_mutex();
  181. // Exclusive ownership
  182. void lock();
  183. bool try_lock();
  184. #ifdef BOOST_THREAD_USES_CHRONO
  185. template <class Rep, class Period>
  186. bool try_lock_for(const boost::chrono::duration<Rep, Period>& rel_time)
  187. {
  188. return try_lock_until(chrono::steady_clock::now() + rel_time);
  189. }
  190. template <class Clock, class Duration>
  191. bool try_lock_until(
  192. const boost::chrono::time_point<Clock, Duration>& abs_time);
  193. #endif
  194. #if defined BOOST_THREAD_USES_DATETIME
  195. template<typename T>
  196. bool timed_lock(T const & abs_or_rel_time);
  197. #endif
  198. void unlock();
  199. // Shared ownership
  200. void lock_shared();
  201. bool try_lock_shared();
  202. #ifdef BOOST_THREAD_USES_CHRONO
  203. template <class Rep, class Period>
  204. bool try_lock_shared_for(const boost::chrono::duration<Rep, Period>& rel_time)
  205. {
  206. return try_lock_shared_until(chrono::steady_clock::now() + rel_time);
  207. }
  208. template <class Clock, class Duration>
  209. bool try_lock_shared_until(
  210. const boost::chrono::time_point<Clock, Duration>& abs_time);
  211. #endif
  212. #if defined BOOST_THREAD_USES_DATETIME
  213. template<typename T>
  214. bool timed_lock_shared(T const & abs_or_rel_time);
  215. #endif
  216. void unlock_shared();
  217. };
  218. inline shared_mutex::shared_mutex()
  219. : state_(0)
  220. {
  221. }
  222. inline shared_mutex::~shared_mutex()
  223. {
  224. boost::lock_guard<mutex_t> _(mut_);
  225. }
  226. // Exclusive ownership
  227. inline void shared_mutex::lock()
  228. {
  229. boost::unique_lock<mutex_t> lk(mut_);
  230. gate1_.wait(lk, boost::bind(&shared_mutex::no_writer, boost::ref(*this)));
  231. state_ |= write_entered_;
  232. gate2_.wait(lk, boost::bind(&shared_mutex::no_readers, boost::ref(*this)));
  233. }
  234. inline bool shared_mutex::try_lock()
  235. {
  236. boost::unique_lock<mutex_t> lk(mut_);
  237. if (!no_writer_no_readers())
  238. {
  239. return false;
  240. }
  241. state_ = write_entered_;
  242. return true;
  243. }
  244. #ifdef BOOST_THREAD_USES_CHRONO
  245. template <class Clock, class Duration>
  246. bool shared_mutex::try_lock_until(
  247. const boost::chrono::time_point<Clock, Duration>& abs_time)
  248. {
  249. boost::unique_lock<mutex_t> lk(mut_);
  250. if (!gate1_.wait_until(lk, abs_time, boost::bind(
  251. &shared_mutex::no_writer, boost::ref(*this))))
  252. {
  253. return false;
  254. }
  255. state_ |= write_entered_;
  256. if (!gate2_.wait_until(lk, abs_time, boost::bind(
  257. &shared_mutex::no_readers, boost::ref(*this))))
  258. {
  259. state_ &= ~write_entered_;
  260. return false;
  261. }
  262. return true;
  263. }
  264. #endif
  265. #if defined BOOST_THREAD_USES_DATETIME
  266. template<typename T>
  267. bool shared_mutex::timed_lock(T const & abs_or_rel_time)
  268. {
  269. boost::unique_lock<mutex_t> lk(mut_);
  270. if (!gate1_.timed_wait(lk, abs_or_rel_time, boost::bind(
  271. &shared_mutex::no_writer, boost::ref(*this))))
  272. {
  273. return false;
  274. }
  275. state_ |= write_entered_;
  276. if (!gate2_.timed_wait(lk, abs_or_rel_time, boost::bind(
  277. &shared_mutex::no_readers, boost::ref(*this))))
  278. {
  279. state_ &= ~write_entered_;
  280. return false;
  281. }
  282. return true;
  283. }
  284. #endif
  285. inline void shared_mutex::unlock()
  286. {
  287. boost::lock_guard<mutex_t> _(mut_);
  288. BOOST_ASSERT(one_writer());
  289. BOOST_ASSERT(no_readers());
  290. state_ = 0;
  291. // notify all since multiple *lock_shared*() calls may be able
  292. // to proceed in response to this notification
  293. gate1_.notify_all();
  294. }
  295. // Shared ownership
  296. inline void shared_mutex::lock_shared()
  297. {
  298. boost::unique_lock<mutex_t> lk(mut_);
  299. gate1_.wait(lk, boost::bind(&shared_mutex::no_writer_no_max_readers, boost::ref(*this)));
  300. count_t num_readers = (state_ & n_readers_) + 1;
  301. state_ &= ~n_readers_;
  302. state_ |= num_readers;
  303. }
  304. inline bool shared_mutex::try_lock_shared()
  305. {
  306. boost::unique_lock<mutex_t> lk(mut_);
  307. if (!no_writer_no_max_readers())
  308. {
  309. return false;
  310. }
  311. count_t num_readers = (state_ & n_readers_) + 1;
  312. state_ &= ~n_readers_;
  313. state_ |= num_readers;
  314. return true;
  315. }
  316. #ifdef BOOST_THREAD_USES_CHRONO
  317. template <class Clock, class Duration>
  318. bool shared_mutex::try_lock_shared_until(
  319. const boost::chrono::time_point<Clock, Duration>& abs_time)
  320. {
  321. boost::unique_lock<mutex_t> lk(mut_);
  322. if (!gate1_.wait_until(lk, abs_time, boost::bind(
  323. &shared_mutex::no_writer_no_max_readers, boost::ref(*this))))
  324. {
  325. return false;
  326. }
  327. count_t num_readers = (state_ & n_readers_) + 1;
  328. state_ &= ~n_readers_;
  329. state_ |= num_readers;
  330. return true;
  331. }
  332. #endif
  333. #if defined BOOST_THREAD_USES_DATETIME
  334. template<typename T>
  335. bool shared_mutex::timed_lock_shared(T const & abs_or_rel_time)
  336. {
  337. boost::unique_lock<mutex_t> lk(mut_);
  338. if (!gate1_.timed_wait(lk, abs_or_rel_time, boost::bind(
  339. &shared_mutex::no_writer_no_max_readers, boost::ref(*this))))
  340. {
  341. return false;
  342. }
  343. count_t num_readers = (state_ & n_readers_) + 1;
  344. state_ &= ~n_readers_;
  345. state_ |= num_readers;
  346. return true;
  347. }
  348. #endif
  349. inline void shared_mutex::unlock_shared()
  350. {
  351. boost::lock_guard<mutex_t> _(mut_);
  352. BOOST_ASSERT(one_or_more_readers());
  353. count_t num_readers = (state_ & n_readers_) - 1;
  354. state_ &= ~n_readers_;
  355. state_ |= num_readers;
  356. if (no_writer())
  357. {
  358. if (num_readers == n_readers_ - 1)
  359. gate1_.notify_one();
  360. }
  361. else
  362. {
  363. if (num_readers == 0)
  364. gate2_.notify_one();
  365. }
  366. }
  367. } // thread_v2
  368. } // boost
  369. namespace boost {
  370. namespace thread_v2 {
  371. class upgrade_mutex
  372. {
  373. typedef boost::mutex mutex_t;
  374. typedef boost::condition_variable cond_t;
  375. typedef unsigned count_t;
  376. mutex_t mut_;
  377. cond_t gate1_;
  378. // the gate2_ condition variable is only used by functions that
  379. // have taken write_entered_ but are waiting for no_readers()
  380. cond_t gate2_;
  381. count_t state_;
  382. static const unsigned write_entered_ = 1U << (sizeof(count_t)*CHAR_BIT - 1);
  383. static const unsigned upgradable_entered_ = write_entered_ >> 1;
  384. static const unsigned n_readers_ = ~(write_entered_ | upgradable_entered_);
  385. bool no_writer() const
  386. {
  387. return (state_ & write_entered_) == 0;
  388. }
  389. bool one_writer() const
  390. {
  391. return (state_ & write_entered_) != 0;
  392. }
  393. bool no_writer_no_max_readers() const
  394. {
  395. return (state_ & write_entered_) == 0 &&
  396. (state_ & n_readers_) != n_readers_;
  397. }
  398. bool no_writer_no_upgrader() const
  399. {
  400. return (state_ & (write_entered_ | upgradable_entered_)) == 0;
  401. }
  402. bool no_writer_no_upgrader_no_readers() const
  403. {
  404. //return (state_ & (write_entered_ | upgradable_entered_)) == 0 &&
  405. // (state_ & n_readers_) == 0;
  406. return state_ == 0;
  407. }
  408. bool no_writer_no_upgrader_one_reader() const
  409. {
  410. //return (state_ & (write_entered_ | upgradable_entered_)) == 0 &&
  411. // (state_ & n_readers_) == 1;
  412. return state_ == 1;
  413. }
  414. bool no_writer_no_upgrader_no_max_readers() const
  415. {
  416. return (state_ & (write_entered_ | upgradable_entered_)) == 0 &&
  417. (state_ & n_readers_) != n_readers_;
  418. }
  419. bool no_upgrader() const
  420. {
  421. return (state_ & upgradable_entered_) == 0;
  422. }
  423. bool one_upgrader() const
  424. {
  425. return (state_ & upgradable_entered_) != 0;
  426. }
  427. bool no_readers() const
  428. {
  429. return (state_ & n_readers_) == 0;
  430. }
  431. bool one_reader() const
  432. {
  433. return (state_ & n_readers_) == 1;
  434. }
  435. bool one_or_more_readers() const
  436. {
  437. return (state_ & n_readers_) > 0;
  438. }
  439. upgrade_mutex(const upgrade_mutex&);
  440. upgrade_mutex& operator=(const upgrade_mutex&);
  441. public:
  442. upgrade_mutex();
  443. ~upgrade_mutex();
  444. // Exclusive ownership
  445. void lock();
  446. bool try_lock();
  447. #ifdef BOOST_THREAD_USES_CHRONO
  448. template <class Rep, class Period>
  449. bool try_lock_for(const boost::chrono::duration<Rep, Period>& rel_time)
  450. {
  451. return try_lock_until(chrono::steady_clock::now() + rel_time);
  452. }
  453. template <class Clock, class Duration>
  454. bool try_lock_until(
  455. const boost::chrono::time_point<Clock, Duration>& abs_time);
  456. #endif
  457. #if defined BOOST_THREAD_USES_DATETIME
  458. template<typename T>
  459. bool timed_lock(T const & abs_or_rel_time);
  460. #endif
  461. void unlock();
  462. // Shared ownership
  463. void lock_shared();
  464. bool try_lock_shared();
  465. #ifdef BOOST_THREAD_USES_CHRONO
  466. template <class Rep, class Period>
  467. bool try_lock_shared_for(const boost::chrono::duration<Rep, Period>& rel_time)
  468. {
  469. return try_lock_shared_until(chrono::steady_clock::now() + rel_time);
  470. }
  471. template <class Clock, class Duration>
  472. bool try_lock_shared_until(
  473. const boost::chrono::time_point<Clock, Duration>& abs_time);
  474. #endif
  475. #if defined BOOST_THREAD_USES_DATETIME
  476. template<typename T>
  477. bool timed_lock_shared(T const & abs_or_rel_time);
  478. #endif
  479. void unlock_shared();
  480. // Upgrade ownership
  481. void lock_upgrade();
  482. bool try_lock_upgrade();
  483. #ifdef BOOST_THREAD_USES_CHRONO
  484. template <class Rep, class Period>
  485. bool try_lock_upgrade_for(
  486. const boost::chrono::duration<Rep, Period>& rel_time)
  487. {
  488. return try_lock_upgrade_until(chrono::steady_clock::now() + rel_time);
  489. }
  490. template <class Clock, class Duration>
  491. bool try_lock_upgrade_until(
  492. const boost::chrono::time_point<Clock, Duration>& abs_time);
  493. #endif
  494. #if defined BOOST_THREAD_USES_DATETIME
  495. template<typename T>
  496. bool timed_lock_upgrade(T const & abs_or_rel_time);
  497. #endif
  498. void unlock_upgrade();
  499. // Shared <-> Exclusive
  500. #ifdef BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
  501. //bool unlock_shared_and_lock(); // can cause a deadlock if used
  502. bool try_unlock_shared_and_lock();
  503. #ifdef BOOST_THREAD_USES_CHRONO
  504. template <class Rep, class Period>
  505. bool try_unlock_shared_and_lock_for(
  506. const boost::chrono::duration<Rep, Period>& rel_time)
  507. {
  508. return try_unlock_shared_and_lock_until(chrono::steady_clock::now() + rel_time);
  509. }
  510. template <class Clock, class Duration>
  511. bool try_unlock_shared_and_lock_until(
  512. const boost::chrono::time_point<Clock, Duration>& abs_time);
  513. #endif
  514. #endif
  515. void unlock_and_lock_shared();
  516. // Shared <-> Upgrade
  517. #ifdef BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
  518. //bool unlock_shared_and_lock_upgrade(); // can cause a deadlock if used
  519. bool try_unlock_shared_and_lock_upgrade();
  520. #ifdef BOOST_THREAD_USES_CHRONO
  521. template <class Rep, class Period>
  522. bool try_unlock_shared_and_lock_upgrade_for(
  523. const boost::chrono::duration<Rep, Period>& rel_time)
  524. {
  525. return try_unlock_shared_and_lock_upgrade_until(chrono::steady_clock::now() + rel_time);
  526. }
  527. template <class Clock, class Duration>
  528. bool try_unlock_shared_and_lock_upgrade_until(
  529. const boost::chrono::time_point<Clock, Duration>& abs_time);
  530. #endif
  531. #endif
  532. void unlock_upgrade_and_lock_shared();
  533. // Upgrade <-> Exclusive
  534. void unlock_upgrade_and_lock();
  535. bool try_unlock_upgrade_and_lock();
  536. #ifdef BOOST_THREAD_USES_CHRONO
  537. template <class Rep, class Period>
  538. bool try_unlock_upgrade_and_lock_for(
  539. const boost::chrono::duration<Rep, Period>& rel_time)
  540. {
  541. return try_unlock_upgrade_and_lock_until(chrono::steady_clock::now() + rel_time);
  542. }
  543. template <class Clock, class Duration>
  544. bool try_unlock_upgrade_and_lock_until(
  545. const boost::chrono::time_point<Clock, Duration>& abs_time);
  546. #endif
  547. void unlock_and_lock_upgrade();
  548. };
  549. inline upgrade_mutex::upgrade_mutex()
  550. : gate1_(),
  551. gate2_(),
  552. state_(0)
  553. {
  554. }
  555. inline upgrade_mutex::~upgrade_mutex()
  556. {
  557. boost::lock_guard<mutex_t> _(mut_);
  558. }
  559. // Exclusive ownership
  560. inline void upgrade_mutex::lock()
  561. {
  562. boost::unique_lock<mutex_t> lk(mut_);
  563. gate1_.wait(lk, boost::bind(&upgrade_mutex::no_writer_no_upgrader, boost::ref(*this)));
  564. state_ |= write_entered_;
  565. gate2_.wait(lk, boost::bind(&upgrade_mutex::no_readers, boost::ref(*this)));
  566. }
  567. inline bool upgrade_mutex::try_lock()
  568. {
  569. boost::unique_lock<mutex_t> lk(mut_);
  570. if (!no_writer_no_upgrader_no_readers())
  571. {
  572. return false;
  573. }
  574. state_ = write_entered_;
  575. return true;
  576. }
  577. #ifdef BOOST_THREAD_USES_CHRONO
  578. template <class Clock, class Duration>
  579. bool upgrade_mutex::try_lock_until(
  580. const boost::chrono::time_point<Clock, Duration>& abs_time)
  581. {
  582. boost::unique_lock<mutex_t> lk(mut_);
  583. if (!gate1_.wait_until(lk, abs_time, boost::bind(
  584. &upgrade_mutex::no_writer_no_upgrader, boost::ref(*this))))
  585. {
  586. return false;
  587. }
  588. state_ |= write_entered_;
  589. if (!gate2_.wait_until(lk, abs_time, boost::bind(
  590. &upgrade_mutex::no_readers, boost::ref(*this))))
  591. {
  592. state_ &= ~write_entered_;
  593. return false;
  594. }
  595. return true;
  596. }
  597. #endif
  598. #if defined BOOST_THREAD_USES_DATETIME
  599. template<typename T>
  600. bool upgrade_mutex::timed_lock(T const & abs_or_rel_time)
  601. {
  602. boost::unique_lock<mutex_t> lk(mut_);
  603. if (!gate1_.timed_wait(lk, abs_or_rel_time, boost::bind(
  604. &upgrade_mutex::no_writer_no_upgrader, boost::ref(*this))))
  605. {
  606. return false;
  607. }
  608. state_ |= write_entered_;
  609. if (!gate2_.timed_wait(lk, abs_or_rel_time, boost::bind(
  610. &upgrade_mutex::no_readers, boost::ref(*this))))
  611. {
  612. state_ &= ~write_entered_;
  613. return false;
  614. }
  615. return true;
  616. }
  617. #endif
  618. inline void upgrade_mutex::unlock()
  619. {
  620. boost::lock_guard<mutex_t> _(mut_);
  621. BOOST_ASSERT(one_writer());
  622. BOOST_ASSERT(no_upgrader());
  623. BOOST_ASSERT(no_readers());
  624. state_ = 0;
  625. // notify all since multiple *lock_shared*() calls and a *lock_upgrade*()
  626. // call may be able to proceed in response to this notification
  627. gate1_.notify_all();
  628. }
  629. // Shared ownership
  630. inline void upgrade_mutex::lock_shared()
  631. {
  632. boost::unique_lock<mutex_t> lk(mut_);
  633. gate1_.wait(lk, boost::bind(&upgrade_mutex::no_writer_no_max_readers, boost::ref(*this)));
  634. count_t num_readers = (state_ & n_readers_) + 1;
  635. state_ &= ~n_readers_;
  636. state_ |= num_readers;
  637. }
  638. inline bool upgrade_mutex::try_lock_shared()
  639. {
  640. boost::unique_lock<mutex_t> lk(mut_);
  641. if (!no_writer_no_max_readers())
  642. {
  643. return false;
  644. }
  645. count_t num_readers = (state_ & n_readers_) + 1;
  646. state_ &= ~n_readers_;
  647. state_ |= num_readers;
  648. return true;
  649. }
  650. #ifdef BOOST_THREAD_USES_CHRONO
  651. template <class Clock, class Duration>
  652. bool upgrade_mutex::try_lock_shared_until(
  653. const boost::chrono::time_point<Clock, Duration>& abs_time)
  654. {
  655. boost::unique_lock<mutex_t> lk(mut_);
  656. if (!gate1_.wait_until(lk, abs_time, boost::bind(
  657. &upgrade_mutex::no_writer_no_max_readers, boost::ref(*this))))
  658. {
  659. return false;
  660. }
  661. count_t num_readers = (state_ & n_readers_) + 1;
  662. state_ &= ~n_readers_;
  663. state_ |= num_readers;
  664. return true;
  665. }
  666. #endif
  667. #if defined BOOST_THREAD_USES_DATETIME
  668. template<typename T>
  669. bool upgrade_mutex::timed_lock_shared(T const & abs_or_rel_time)
  670. {
  671. boost::unique_lock<mutex_t> lk(mut_);
  672. if (!gate1_.timed_wait(lk, abs_or_rel_time, boost::bind(
  673. &upgrade_mutex::no_writer_no_max_readers, boost::ref(*this))))
  674. {
  675. return false;
  676. }
  677. count_t num_readers = (state_ & n_readers_) + 1;
  678. state_ &= ~n_readers_;
  679. state_ |= num_readers;
  680. return true;
  681. }
  682. #endif
  683. inline void upgrade_mutex::unlock_shared()
  684. {
  685. boost::lock_guard<mutex_t> _(mut_);
  686. BOOST_ASSERT(one_or_more_readers());
  687. count_t num_readers = (state_ & n_readers_) - 1;
  688. state_ &= ~n_readers_;
  689. state_ |= num_readers;
  690. if (no_writer())
  691. {
  692. if (num_readers == n_readers_ - 1)
  693. gate1_.notify_one();
  694. }
  695. else
  696. {
  697. if (num_readers == 0)
  698. gate2_.notify_one();
  699. }
  700. }
  701. // Upgrade ownership
  702. inline void upgrade_mutex::lock_upgrade()
  703. {
  704. boost::unique_lock<mutex_t> lk(mut_);
  705. gate1_.wait(lk, boost::bind(&upgrade_mutex::no_writer_no_upgrader_no_max_readers, boost::ref(*this)));
  706. count_t num_readers = (state_ & n_readers_) + 1;
  707. state_ &= ~n_readers_;
  708. state_ |= upgradable_entered_ | num_readers;
  709. }
  710. inline bool upgrade_mutex::try_lock_upgrade()
  711. {
  712. boost::unique_lock<mutex_t> lk(mut_);
  713. if (!no_writer_no_upgrader_no_max_readers())
  714. {
  715. return false;
  716. }
  717. count_t num_readers = (state_ & n_readers_) + 1;
  718. state_ &= ~n_readers_;
  719. state_ |= upgradable_entered_ | num_readers;
  720. return true;
  721. }
  722. #ifdef BOOST_THREAD_USES_CHRONO
  723. template <class Clock, class Duration>
  724. bool upgrade_mutex::try_lock_upgrade_until(
  725. const boost::chrono::time_point<Clock, Duration>& abs_time)
  726. {
  727. boost::unique_lock<mutex_t> lk(mut_);
  728. if (!gate1_.wait_until(lk, abs_time, boost::bind(
  729. &upgrade_mutex::no_writer_no_upgrader_no_max_readers, boost::ref(*this))))
  730. {
  731. return false;
  732. }
  733. count_t num_readers = (state_ & n_readers_) + 1;
  734. state_ &= ~n_readers_;
  735. state_ |= upgradable_entered_ | num_readers;
  736. return true;
  737. }
  738. #endif
  739. #if defined BOOST_THREAD_USES_DATETIME
  740. template<typename T>
  741. bool upgrade_mutex::timed_lock_upgrade(T const & abs_or_rel_time)
  742. {
  743. boost::unique_lock<mutex_t> lk(mut_);
  744. if (!gate1_.timed_wait(lk, abs_or_rel_time, boost::bind(
  745. &upgrade_mutex::no_writer_no_upgrader_no_max_readers, boost::ref(*this))))
  746. {
  747. return false;
  748. }
  749. count_t num_readers = (state_ & n_readers_) + 1;
  750. state_ &= ~n_readers_;
  751. state_ |= upgradable_entered_ | num_readers;
  752. return true;
  753. }
  754. #endif
  755. inline void upgrade_mutex::unlock_upgrade()
  756. {
  757. boost::lock_guard<mutex_t> _(mut_);
  758. BOOST_ASSERT(no_writer());
  759. BOOST_ASSERT(one_upgrader());
  760. BOOST_ASSERT(one_or_more_readers());
  761. count_t num_readers = (state_ & n_readers_) - 1;
  762. state_ &= ~(upgradable_entered_ | n_readers_);
  763. state_ |= num_readers;
  764. // notify all since both a *lock*() and a *lock_shared*() call
  765. // may be able to proceed in response to this notification
  766. gate1_.notify_all();
  767. }
  768. // Shared <-> Exclusive
  769. #ifdef BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
  770. inline bool upgrade_mutex::try_unlock_shared_and_lock()
  771. {
  772. boost::unique_lock<mutex_t> lk(mut_);
  773. BOOST_ASSERT(one_or_more_readers());
  774. if (!no_writer_no_upgrader_one_reader())
  775. {
  776. return false;
  777. }
  778. state_ = write_entered_;
  779. return true;
  780. }
  781. #ifdef BOOST_THREAD_USES_CHRONO
  782. template <class Clock, class Duration>
  783. bool upgrade_mutex::try_unlock_shared_and_lock_until(
  784. const boost::chrono::time_point<Clock, Duration>& abs_time)
  785. {
  786. boost::unique_lock<mutex_t> lk(mut_);
  787. BOOST_ASSERT(one_or_more_readers());
  788. if (!gate1_.wait_until(lk, abs_time, boost::bind(
  789. &upgrade_mutex::no_writer_no_upgrader, boost::ref(*this))))
  790. {
  791. return false;
  792. }
  793. count_t num_readers = (state_ & n_readers_) - 1;
  794. state_ &= ~n_readers_;
  795. state_ |= (write_entered_ | num_readers);
  796. if (!gate2_.wait_until(lk, abs_time, boost::bind(
  797. &upgrade_mutex::no_readers, boost::ref(*this))))
  798. {
  799. ++num_readers;
  800. state_ &= ~(write_entered_ | n_readers_);
  801. state_ |= num_readers;
  802. return false;
  803. }
  804. return true;
  805. }
  806. #endif
  807. #endif
  808. inline void upgrade_mutex::unlock_and_lock_shared()
  809. {
  810. boost::lock_guard<mutex_t> _(mut_);
  811. BOOST_ASSERT(one_writer());
  812. BOOST_ASSERT(no_upgrader());
  813. BOOST_ASSERT(no_readers());
  814. state_ = 1;
  815. // notify all since multiple *lock_shared*() calls and a *lock_upgrade*()
  816. // call may be able to proceed in response to this notification
  817. gate1_.notify_all();
  818. }
  819. // Shared <-> Upgrade
  820. #ifdef BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
  821. inline bool upgrade_mutex::try_unlock_shared_and_lock_upgrade()
  822. {
  823. boost::unique_lock<mutex_t> lk(mut_);
  824. BOOST_ASSERT(one_or_more_readers());
  825. if (!no_writer_no_upgrader())
  826. {
  827. return false;
  828. }
  829. state_ |= upgradable_entered_;
  830. return true;
  831. }
  832. #ifdef BOOST_THREAD_USES_CHRONO
  833. template <class Clock, class Duration>
  834. bool upgrade_mutex::try_unlock_shared_and_lock_upgrade_until(
  835. const boost::chrono::time_point<Clock, Duration>& abs_time)
  836. {
  837. boost::unique_lock<mutex_t> lk(mut_);
  838. BOOST_ASSERT(one_or_more_readers());
  839. if (!gate1_.wait_until(lk, abs_time, boost::bind(
  840. &upgrade_mutex::no_writer_no_upgrader, boost::ref(*this))))
  841. {
  842. return false;
  843. }
  844. state_ |= upgradable_entered_;
  845. return true;
  846. }
  847. #endif
  848. #endif
  849. inline void upgrade_mutex::unlock_upgrade_and_lock_shared()
  850. {
  851. boost::lock_guard<mutex_t> _(mut_);
  852. BOOST_ASSERT(no_writer());
  853. BOOST_ASSERT(one_upgrader());
  854. BOOST_ASSERT(one_or_more_readers());
  855. state_ &= ~upgradable_entered_;
  856. // notify all since only one *lock*() or *lock_upgrade*() call can win and
  857. // proceed in response to this notification, but a *lock_shared*() call may
  858. // also be waiting and could steal the notification
  859. gate1_.notify_all();
  860. }
  861. // Upgrade <-> Exclusive
  862. inline void upgrade_mutex::unlock_upgrade_and_lock()
  863. {
  864. boost::unique_lock<mutex_t> lk(mut_);
  865. BOOST_ASSERT(no_writer());
  866. BOOST_ASSERT(one_upgrader());
  867. BOOST_ASSERT(one_or_more_readers());
  868. count_t num_readers = (state_ & n_readers_) - 1;
  869. state_ &= ~(upgradable_entered_ | n_readers_);
  870. state_ |= write_entered_ | num_readers;
  871. gate2_.wait(lk, boost::bind(&upgrade_mutex::no_readers, boost::ref(*this)));
  872. }
  873. inline bool upgrade_mutex::try_unlock_upgrade_and_lock()
  874. {
  875. boost::unique_lock<mutex_t> lk(mut_);
  876. BOOST_ASSERT(no_writer());
  877. BOOST_ASSERT(one_upgrader());
  878. BOOST_ASSERT(one_or_more_readers());
  879. if (!one_reader())
  880. {
  881. return false;
  882. }
  883. state_ = write_entered_;
  884. return true;
  885. }
  886. #ifdef BOOST_THREAD_USES_CHRONO
  887. template <class Clock, class Duration>
  888. bool upgrade_mutex::try_unlock_upgrade_and_lock_until(
  889. const boost::chrono::time_point<Clock, Duration>& abs_time)
  890. {
  891. boost::unique_lock<mutex_t> lk(mut_);
  892. BOOST_ASSERT(no_writer());
  893. BOOST_ASSERT(one_upgrader());
  894. BOOST_ASSERT(one_or_more_readers());
  895. count_t num_readers = (state_ & n_readers_) - 1;
  896. state_ &= ~(upgradable_entered_ | n_readers_);
  897. state_ |= (write_entered_ | num_readers);
  898. if (!gate2_.wait_until(lk, abs_time, boost::bind(
  899. &upgrade_mutex::no_readers, boost::ref(*this))))
  900. {
  901. ++num_readers;
  902. state_ &= ~(write_entered_ | n_readers_);
  903. state_ |= (upgradable_entered_ | num_readers);
  904. return false;
  905. }
  906. return true;
  907. }
  908. #endif
  909. inline void upgrade_mutex::unlock_and_lock_upgrade()
  910. {
  911. boost::lock_guard<mutex_t> _(mut_);
  912. BOOST_ASSERT(one_writer());
  913. BOOST_ASSERT(no_upgrader());
  914. BOOST_ASSERT(no_readers());
  915. state_ = upgradable_entered_ | 1;
  916. // notify all since multiple *lock_shared*() calls may be able
  917. // to proceed in response to this notification
  918. gate1_.notify_all();
  919. }
  920. } // thread_v2
  921. } // boost
  922. namespace boost {
  923. //using thread_v2::shared_mutex;
  924. using thread_v2::upgrade_mutex;
  925. typedef thread_v2::upgrade_mutex shared_mutex;
  926. }
  927. #endif