shared_mutex.hpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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/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