shared_mutex.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. #ifndef BOOST_THREAD_WIN32_SHARED_MUTEX_HPP
  2. #define BOOST_THREAD_WIN32_SHARED_MUTEX_HPP
  3. // (C) Copyright 2006-8 Anthony Williams
  4. // (C) Copyright 2011-2012,2017-2018 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/detail/interlocked.hpp>
  11. #include <boost/thread/win32/thread_primitives.hpp>
  12. #include <boost/static_assert.hpp>
  13. #include <limits.h>
  14. #include <boost/thread/thread_time.hpp>
  15. #ifdef BOOST_THREAD_USES_CHRONO
  16. #include <boost/chrono/system_clocks.hpp>
  17. #include <boost/chrono/ceil.hpp>
  18. #endif
  19. #include <boost/thread/detail/delete.hpp>
  20. #include <boost/thread/detail/platform_time.hpp>
  21. #include <boost/config/abi_prefix.hpp>
  22. namespace boost
  23. {
  24. class shared_mutex
  25. {
  26. private:
  27. struct state_data
  28. {
  29. unsigned long shared_count:11,
  30. shared_waiting:11,
  31. exclusive:1,
  32. upgrade:1,
  33. exclusive_waiting:7,
  34. exclusive_waiting_blocked:1;
  35. friend bool operator==(state_data const& lhs,state_data const& rhs)
  36. {
  37. return *reinterpret_cast<unsigned long const*>(&lhs)==*reinterpret_cast<unsigned long const*>(&rhs);
  38. }
  39. };
  40. state_data interlocked_compare_exchange(state_data* target, state_data new_value, state_data comparand)
  41. {
  42. long const res=BOOST_INTERLOCKED_COMPARE_EXCHANGE(reinterpret_cast<long*>(target),
  43. *reinterpret_cast<long*>(&new_value),
  44. *reinterpret_cast<long*>(&comparand));
  45. return *reinterpret_cast<state_data const*>(&res);
  46. }
  47. enum
  48. {
  49. unlock_sem = 0,
  50. exclusive_sem = 1
  51. };
  52. state_data state;
  53. detail::win32::handle semaphores[2];
  54. detail::win32::handle upgrade_sem;
  55. void release_waiters(state_data old_state)
  56. {
  57. if(old_state.exclusive_waiting)
  58. {
  59. BOOST_VERIFY(winapi::ReleaseSemaphore(semaphores[exclusive_sem],1,0)!=0);
  60. }
  61. if(old_state.shared_waiting || old_state.exclusive_waiting)
  62. {
  63. BOOST_VERIFY(winapi::ReleaseSemaphore(semaphores[unlock_sem],old_state.shared_waiting + (old_state.exclusive_waiting?1:0),0)!=0);
  64. }
  65. }
  66. void release_shared_waiters(state_data old_state)
  67. {
  68. if(old_state.shared_waiting || old_state.exclusive_waiting)
  69. {
  70. BOOST_VERIFY(winapi::ReleaseSemaphore(semaphores[unlock_sem],old_state.shared_waiting + (old_state.exclusive_waiting?1:0),0)!=0);
  71. }
  72. }
  73. public:
  74. BOOST_THREAD_NO_COPYABLE(shared_mutex)
  75. shared_mutex()
  76. {
  77. semaphores[unlock_sem]=detail::win32::create_anonymous_semaphore(0,LONG_MAX);
  78. semaphores[exclusive_sem]=detail::win32::create_anonymous_semaphore_nothrow(0,LONG_MAX);
  79. if (!semaphores[exclusive_sem])
  80. {
  81. detail::win32::release_semaphore(semaphores[unlock_sem],LONG_MAX);
  82. boost::throw_exception(thread_resource_error());
  83. }
  84. upgrade_sem=detail::win32::create_anonymous_semaphore_nothrow(0,LONG_MAX);
  85. if (!upgrade_sem)
  86. {
  87. detail::win32::release_semaphore(semaphores[unlock_sem],LONG_MAX);
  88. detail::win32::release_semaphore(semaphores[exclusive_sem],LONG_MAX);
  89. boost::throw_exception(thread_resource_error());
  90. }
  91. state_data state_={0,0,0,0,0,0};
  92. state=state_;
  93. }
  94. ~shared_mutex()
  95. {
  96. winapi::CloseHandle(upgrade_sem);
  97. winapi::CloseHandle(semaphores[unlock_sem]);
  98. winapi::CloseHandle(semaphores[exclusive_sem]);
  99. }
  100. bool try_lock_shared()
  101. {
  102. state_data old_state=state;
  103. for(;;)
  104. {
  105. state_data new_state=old_state;
  106. if(!new_state.exclusive && !new_state.exclusive_waiting_blocked)
  107. {
  108. ++new_state.shared_count;
  109. if(!new_state.shared_count)
  110. {
  111. return false;
  112. }
  113. }
  114. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  115. if(current_state==old_state)
  116. {
  117. break;
  118. }
  119. old_state=current_state;
  120. }
  121. return !(old_state.exclusive| old_state.exclusive_waiting_blocked);
  122. }
  123. void lock_shared()
  124. {
  125. for(;;)
  126. {
  127. state_data old_state=state;
  128. for(;;)
  129. {
  130. state_data new_state=old_state;
  131. if(new_state.exclusive || new_state.exclusive_waiting_blocked)
  132. {
  133. ++new_state.shared_waiting;
  134. if(!new_state.shared_waiting)
  135. {
  136. boost::throw_exception(boost::lock_error());
  137. }
  138. }
  139. else
  140. {
  141. ++new_state.shared_count;
  142. if(!new_state.shared_count)
  143. {
  144. boost::throw_exception(boost::lock_error());
  145. }
  146. }
  147. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  148. if(current_state==old_state)
  149. {
  150. break;
  151. }
  152. old_state=current_state;
  153. }
  154. if(!(old_state.exclusive| old_state.exclusive_waiting_blocked))
  155. {
  156. return;
  157. }
  158. BOOST_VERIFY(winapi::WaitForSingleObjectEx(semaphores[unlock_sem],::boost::detail::win32::infinite,0)==0);
  159. }
  160. }
  161. private:
  162. unsigned long getMs(detail::platform_duration const& d)
  163. {
  164. return static_cast<unsigned long>(d.getMs());
  165. }
  166. template <typename Duration>
  167. unsigned long getMs(Duration const& d)
  168. {
  169. return static_cast<unsigned long>(chrono::ceil<chrono::milliseconds>(d).count());
  170. }
  171. template <typename Clock, typename Timepoint, typename Duration>
  172. bool do_lock_shared_until(Timepoint const& t, Duration const& max)
  173. {
  174. for(;;)
  175. {
  176. state_data old_state=state;
  177. for(;;)
  178. {
  179. state_data new_state=old_state;
  180. if(new_state.exclusive || new_state.exclusive_waiting_blocked)
  181. {
  182. ++new_state.shared_waiting;
  183. if(!new_state.shared_waiting)
  184. {
  185. boost::throw_exception(boost::lock_error());
  186. }
  187. }
  188. else
  189. {
  190. ++new_state.shared_count;
  191. if(!new_state.shared_count)
  192. {
  193. boost::throw_exception(boost::lock_error());
  194. }
  195. }
  196. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  197. if(current_state==old_state)
  198. {
  199. break;
  200. }
  201. old_state=current_state;
  202. }
  203. if(!(old_state.exclusive| old_state.exclusive_waiting_blocked))
  204. {
  205. return true;
  206. }
  207. // If the clock is the system clock, it may jump while this function
  208. // is waiting. To compensate for this and time out near the correct
  209. // time, we call WaitForSingleObjectEx() in a loop with a short
  210. // timeout and recheck the time remaining each time through the loop.
  211. unsigned long res=0;
  212. for(;;)
  213. {
  214. Duration d(t - Clock::now());
  215. if(d <= Duration::zero()) // timeout occurred
  216. {
  217. res=detail::win32::timeout;
  218. break;
  219. }
  220. if(max != Duration::zero())
  221. {
  222. d = (std::min)(d, max);
  223. }
  224. res=winapi::WaitForSingleObjectEx(semaphores[unlock_sem],getMs(d),0);
  225. if(res!=detail::win32::timeout) // semaphore released
  226. {
  227. break;
  228. }
  229. }
  230. if(res==detail::win32::timeout)
  231. {
  232. for(;;)
  233. {
  234. state_data new_state=old_state;
  235. if(new_state.exclusive || new_state.exclusive_waiting_blocked)
  236. {
  237. if(new_state.shared_waiting)
  238. {
  239. --new_state.shared_waiting;
  240. }
  241. }
  242. else
  243. {
  244. ++new_state.shared_count;
  245. if(!new_state.shared_count)
  246. {
  247. return false;
  248. }
  249. }
  250. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  251. if(current_state==old_state)
  252. {
  253. break;
  254. }
  255. old_state=current_state;
  256. }
  257. if(!(old_state.exclusive| old_state.exclusive_waiting_blocked))
  258. {
  259. return true;
  260. }
  261. return false;
  262. }
  263. BOOST_ASSERT(res==0);
  264. }
  265. }
  266. public:
  267. #if defined BOOST_THREAD_USES_DATETIME
  268. template<typename TimeDuration>
  269. bool timed_lock_shared(TimeDuration const & relative_time)
  270. {
  271. const detail::mono_platform_timepoint t(detail::mono_platform_clock::now() + detail::platform_duration(relative_time));
  272. // The reference clock is steady and so no need to poll periodically, thus 0 ms max (i.e. no max)
  273. return do_lock_shared_until<detail::mono_platform_clock>(t, detail::platform_duration::zero());
  274. }
  275. bool timed_lock_shared(boost::system_time const& wait_until)
  276. {
  277. const detail::real_platform_timepoint t(wait_until);
  278. return do_lock_shared_until<detail::real_platform_clock>(t, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  279. }
  280. #endif
  281. #ifdef BOOST_THREAD_USES_CHRONO
  282. template <class Rep, class Period>
  283. bool try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time)
  284. {
  285. const chrono::steady_clock::time_point t(chrono::steady_clock::now() + rel_time);
  286. typedef typename chrono::duration<Rep, Period> Duration;
  287. typedef typename common_type<Duration, typename chrono::steady_clock::duration>::type common_duration;
  288. // The reference clock is steady and so no need to poll periodically, thus 0 ms max (i.e. no max)
  289. return do_lock_shared_until<chrono::steady_clock>(t, common_duration::zero());
  290. }
  291. template <class Duration>
  292. bool try_lock_shared_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
  293. {
  294. typedef typename common_type<Duration, typename chrono::steady_clock::duration>::type common_duration;
  295. // The reference clock is steady and so no need to poll periodically, thus 0 ms max (i.e. no max)
  296. return do_lock_shared_until<chrono::steady_clock>(t, common_duration::zero());
  297. }
  298. template <class Clock, class Duration>
  299. bool try_lock_shared_until(const chrono::time_point<Clock, Duration>& t)
  300. {
  301. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  302. return do_lock_shared_until<Clock>(t, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  303. }
  304. #endif
  305. void unlock_shared()
  306. {
  307. state_data old_state=state;
  308. for(;;)
  309. {
  310. state_data new_state=old_state;
  311. bool const last_reader=!--new_state.shared_count;
  312. if(last_reader)
  313. {
  314. if(new_state.upgrade)
  315. {
  316. new_state.upgrade=false;
  317. new_state.exclusive=true;
  318. }
  319. else
  320. {
  321. if(new_state.exclusive_waiting)
  322. {
  323. --new_state.exclusive_waiting;
  324. new_state.exclusive_waiting_blocked=false;
  325. }
  326. new_state.shared_waiting=0;
  327. }
  328. }
  329. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  330. if(current_state==old_state)
  331. {
  332. if(last_reader)
  333. {
  334. if(old_state.upgrade)
  335. {
  336. BOOST_VERIFY(winapi::ReleaseSemaphore(upgrade_sem,1,0)!=0);
  337. }
  338. else
  339. {
  340. release_waiters(old_state);
  341. }
  342. }
  343. break;
  344. }
  345. old_state=current_state;
  346. }
  347. }
  348. bool try_lock()
  349. {
  350. state_data old_state=state;
  351. for(;;)
  352. {
  353. state_data new_state=old_state;
  354. if(new_state.shared_count || new_state.exclusive)
  355. {
  356. return false;
  357. }
  358. else
  359. {
  360. new_state.exclusive=true;
  361. }
  362. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  363. if(current_state==old_state)
  364. {
  365. break;
  366. }
  367. old_state=current_state;
  368. }
  369. return true;
  370. }
  371. void lock()
  372. {
  373. for(;;)
  374. {
  375. state_data old_state=state;
  376. for(;;)
  377. {
  378. state_data new_state=old_state;
  379. if(new_state.shared_count || new_state.exclusive)
  380. {
  381. ++new_state.exclusive_waiting;
  382. if(!new_state.exclusive_waiting)
  383. {
  384. boost::throw_exception(boost::lock_error());
  385. }
  386. new_state.exclusive_waiting_blocked=true;
  387. }
  388. else
  389. {
  390. new_state.exclusive=true;
  391. }
  392. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  393. if(current_state==old_state)
  394. {
  395. break;
  396. }
  397. old_state=current_state;
  398. }
  399. if(!old_state.shared_count && !old_state.exclusive)
  400. {
  401. return;
  402. }
  403. #ifndef UNDER_CE
  404. const bool wait_all = true;
  405. #else
  406. const bool wait_all = false;
  407. #endif
  408. BOOST_VERIFY(winapi::WaitForMultipleObjectsEx(2,semaphores,wait_all,::boost::detail::win32::infinite,0)<2);
  409. }
  410. }
  411. private:
  412. template <typename Clock, typename Timepoint, typename Duration>
  413. bool do_lock_until(Timepoint const& t, Duration const& max)
  414. {
  415. for(;;)
  416. {
  417. state_data old_state=state;
  418. for(;;)
  419. {
  420. state_data new_state=old_state;
  421. if(new_state.shared_count || new_state.exclusive)
  422. {
  423. ++new_state.exclusive_waiting;
  424. if(!new_state.exclusive_waiting)
  425. {
  426. boost::throw_exception(boost::lock_error());
  427. }
  428. new_state.exclusive_waiting_blocked=true;
  429. }
  430. else
  431. {
  432. new_state.exclusive=true;
  433. }
  434. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  435. if(current_state==old_state)
  436. {
  437. break;
  438. }
  439. old_state=current_state;
  440. }
  441. if(!old_state.shared_count && !old_state.exclusive)
  442. {
  443. return true;
  444. }
  445. // If the clock is the system clock, it may jump while this function
  446. // is waiting. To compensate for this and time out near the correct
  447. // time, we call WaitForMultipleObjectsEx() in a loop with a short
  448. // timeout and recheck the time remaining each time through the loop.
  449. unsigned long wait_res=0;
  450. for(;;)
  451. {
  452. Duration d(t - Clock::now());
  453. if(d <= Duration::zero()) // timeout occurred
  454. {
  455. wait_res=detail::win32::timeout;
  456. break;
  457. }
  458. if(max != Duration::zero())
  459. {
  460. d = (std::min)(d, max);
  461. }
  462. #ifndef UNDER_CE
  463. wait_res=winapi::WaitForMultipleObjectsEx(2,semaphores,true,getMs(d),0);
  464. #else
  465. wait_res=winapi::WaitForMultipleObjectsEx(2,semaphores,false,getMs(d),0);
  466. #endif
  467. //wait_res=winapi::WaitForMultipleObjectsEx(2,semaphores,wait_all,getMs(d), 0);
  468. if(wait_res!=detail::win32::timeout) // semaphore released
  469. {
  470. break;
  471. }
  472. }
  473. if(wait_res==detail::win32::timeout)
  474. {
  475. for(;;)
  476. {
  477. bool must_notify = false;
  478. state_data new_state=old_state;
  479. if(new_state.shared_count || new_state.exclusive)
  480. {
  481. if(new_state.exclusive_waiting)
  482. {
  483. if(!--new_state.exclusive_waiting)
  484. {
  485. new_state.exclusive_waiting_blocked=false;
  486. must_notify = true;
  487. }
  488. }
  489. }
  490. else
  491. {
  492. new_state.exclusive=true;
  493. }
  494. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  495. if (must_notify)
  496. {
  497. BOOST_VERIFY(winapi::ReleaseSemaphore(semaphores[unlock_sem],1,0)!=0);
  498. }
  499. if(current_state==old_state)
  500. {
  501. break;
  502. }
  503. old_state=current_state;
  504. }
  505. if(!old_state.shared_count && !old_state.exclusive)
  506. {
  507. return true;
  508. }
  509. return false;
  510. }
  511. BOOST_ASSERT(wait_res<2);
  512. }
  513. }
  514. public:
  515. #if defined BOOST_THREAD_USES_DATETIME
  516. bool timed_lock(boost::system_time const& wait_until)
  517. {
  518. const detail::real_platform_timepoint t(wait_until);
  519. return do_lock_until<detail::real_platform_clock>(t, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  520. }
  521. template<typename TimeDuration>
  522. bool timed_lock(TimeDuration const & relative_time)
  523. {
  524. const detail::mono_platform_timepoint t(detail::mono_platform_clock::now() + detail::platform_duration(relative_time));
  525. // The reference clock is steady and so no need to poll periodically, thus 0 ms max (i.e. no max)
  526. return do_lock_until<detail::mono_platform_clock>(t, detail::platform_duration::zero());
  527. }
  528. #endif
  529. #ifdef BOOST_THREAD_USES_CHRONO
  530. template <class Rep, class Period>
  531. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
  532. {
  533. const chrono::steady_clock::time_point t(chrono::steady_clock::now() + rel_time);
  534. typedef typename chrono::duration<Rep, Period> Duration;
  535. typedef typename common_type<Duration, typename chrono::steady_clock::duration>::type common_duration;
  536. // The reference clock is steady and so no need to poll periodically, thus 0 ms max (i.e. no max)
  537. return do_lock_until<chrono::steady_clock>(t, common_duration::zero());
  538. }
  539. template <class Duration>
  540. bool try_lock_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
  541. {
  542. typedef typename common_type<Duration, typename chrono::steady_clock::duration>::type common_duration;
  543. // The reference clock is steady and so no need to poll periodically, thus 0 ms max (i.e. no max)
  544. return do_lock_until<chrono::steady_clock>(t, common_duration::zero());
  545. }
  546. template <class Clock, class Duration>
  547. bool try_lock_until(const chrono::time_point<Clock, Duration>& t)
  548. {
  549. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  550. return do_lock_until<Clock>(t, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  551. }
  552. #endif
  553. void unlock()
  554. {
  555. state_data old_state=state;
  556. for(;;)
  557. {
  558. state_data new_state=old_state;
  559. new_state.exclusive=false;
  560. if(new_state.exclusive_waiting)
  561. {
  562. --new_state.exclusive_waiting;
  563. new_state.exclusive_waiting_blocked=false;
  564. }
  565. new_state.shared_waiting=0;
  566. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  567. if(current_state==old_state)
  568. {
  569. break;
  570. }
  571. old_state=current_state;
  572. }
  573. release_waiters(old_state);
  574. }
  575. void lock_upgrade()
  576. {
  577. for(;;)
  578. {
  579. state_data old_state=state;
  580. for(;;)
  581. {
  582. state_data new_state=old_state;
  583. if(new_state.exclusive || new_state.exclusive_waiting_blocked || new_state.upgrade)
  584. {
  585. ++new_state.shared_waiting;
  586. if(!new_state.shared_waiting)
  587. {
  588. boost::throw_exception(boost::lock_error());
  589. }
  590. }
  591. else
  592. {
  593. ++new_state.shared_count;
  594. if(!new_state.shared_count)
  595. {
  596. boost::throw_exception(boost::lock_error());
  597. }
  598. new_state.upgrade=true;
  599. }
  600. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  601. if(current_state==old_state)
  602. {
  603. break;
  604. }
  605. old_state=current_state;
  606. }
  607. if(!(old_state.exclusive|| old_state.exclusive_waiting_blocked|| old_state.upgrade))
  608. {
  609. return;
  610. }
  611. BOOST_VERIFY(winapi::WaitForSingleObjectEx(semaphores[unlock_sem],winapi::infinite,0)==0);
  612. }
  613. }
  614. bool try_lock_upgrade()
  615. {
  616. state_data old_state=state;
  617. for(;;)
  618. {
  619. state_data new_state=old_state;
  620. if(new_state.exclusive || new_state.exclusive_waiting_blocked || new_state.upgrade)
  621. {
  622. return false;
  623. }
  624. else
  625. {
  626. ++new_state.shared_count;
  627. if(!new_state.shared_count)
  628. {
  629. return false;
  630. }
  631. new_state.upgrade=true;
  632. }
  633. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  634. if(current_state==old_state)
  635. {
  636. break;
  637. }
  638. old_state=current_state;
  639. }
  640. return true;
  641. }
  642. void unlock_upgrade()
  643. {
  644. state_data old_state=state;
  645. for(;;)
  646. {
  647. state_data new_state=old_state;
  648. new_state.upgrade=false;
  649. bool const last_reader=!--new_state.shared_count;
  650. new_state.shared_waiting=0;
  651. if(last_reader)
  652. {
  653. if(new_state.exclusive_waiting)
  654. {
  655. --new_state.exclusive_waiting;
  656. new_state.exclusive_waiting_blocked=false;
  657. }
  658. }
  659. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  660. if(current_state==old_state)
  661. {
  662. if(last_reader)
  663. {
  664. release_waiters(old_state);
  665. }
  666. else {
  667. release_shared_waiters(old_state);
  668. }
  669. // #7720
  670. //else {
  671. // release_waiters(old_state);
  672. //}
  673. break;
  674. }
  675. old_state=current_state;
  676. }
  677. }
  678. void unlock_upgrade_and_lock()
  679. {
  680. state_data old_state=state;
  681. for(;;)
  682. {
  683. state_data new_state=old_state;
  684. bool const last_reader=!--new_state.shared_count;
  685. if(last_reader)
  686. {
  687. new_state.upgrade=false;
  688. new_state.exclusive=true;
  689. }
  690. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  691. if(current_state==old_state)
  692. {
  693. if(!last_reader)
  694. {
  695. BOOST_VERIFY(winapi::WaitForSingleObjectEx(upgrade_sem,detail::win32::infinite,0)==0);
  696. }
  697. break;
  698. }
  699. old_state=current_state;
  700. }
  701. }
  702. void unlock_and_lock_upgrade()
  703. {
  704. state_data old_state=state;
  705. for(;;)
  706. {
  707. state_data new_state=old_state;
  708. new_state.exclusive=false;
  709. new_state.upgrade=true;
  710. ++new_state.shared_count;
  711. if(new_state.exclusive_waiting)
  712. {
  713. --new_state.exclusive_waiting;
  714. new_state.exclusive_waiting_blocked=false;
  715. }
  716. new_state.shared_waiting=0;
  717. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  718. if(current_state==old_state)
  719. {
  720. break;
  721. }
  722. old_state=current_state;
  723. }
  724. release_waiters(old_state);
  725. }
  726. void unlock_and_lock_shared()
  727. {
  728. state_data old_state=state;
  729. for(;;)
  730. {
  731. state_data new_state=old_state;
  732. new_state.exclusive=false;
  733. ++new_state.shared_count;
  734. if(new_state.exclusive_waiting)
  735. {
  736. --new_state.exclusive_waiting;
  737. new_state.exclusive_waiting_blocked=false;
  738. }
  739. new_state.shared_waiting=0;
  740. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  741. if(current_state==old_state)
  742. {
  743. break;
  744. }
  745. old_state=current_state;
  746. }
  747. release_waiters(old_state);
  748. }
  749. void unlock_upgrade_and_lock_shared()
  750. {
  751. state_data old_state=state;
  752. for(;;)
  753. {
  754. state_data new_state=old_state;
  755. new_state.upgrade=false;
  756. if(new_state.exclusive_waiting)
  757. {
  758. --new_state.exclusive_waiting;
  759. new_state.exclusive_waiting_blocked=false;
  760. }
  761. new_state.shared_waiting=0;
  762. state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
  763. if(current_state==old_state)
  764. {
  765. break;
  766. }
  767. old_state=current_state;
  768. }
  769. release_waiters(old_state);
  770. }
  771. };
  772. typedef shared_mutex upgrade_mutex;
  773. }
  774. #include <boost/config/abi_suffix.hpp>
  775. #endif