synchronized_value.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. // (C) Copyright 2010 Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
  2. // (C) Copyright 2012 Vicente J. Botet Escriba
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_THREAD_SYNCHRONIZED_VALUE_HPP
  7. #define BOOST_THREAD_SYNCHRONIZED_VALUE_HPP
  8. #include <boost/thread/detail/config.hpp>
  9. #include <boost/thread/detail/move.hpp>
  10. #include <boost/thread/mutex.hpp>
  11. #include <boost/thread/lock_types.hpp>
  12. #include <boost/thread/lock_guard.hpp>
  13. #include <boost/thread/lock_algorithms.hpp>
  14. #include <boost/thread/lock_factories.hpp>
  15. #include <boost/thread/strict_lock.hpp>
  16. #include <boost/core/swap.hpp>
  17. #include <boost/utility/declval.hpp>
  18. //#include <boost/type_traits.hpp>
  19. //#include <boost/thread/detail/is_nothrow_default_constructible.hpp>
  20. //#if ! defined BOOST_NO_CXX11_HDR_TYPE_TRAITS
  21. //#include <type_traits>
  22. //#endif
  23. #if ! defined(BOOST_THREAD_NO_SYNCHRONIZE)
  24. #include <tuple> // todo change to <boost/tuple.hpp> once Boost.Tuple or Boost.Fusion provides Move semantics on C++98 compilers.
  25. #include <functional>
  26. #endif
  27. #include <boost/utility/result_of.hpp>
  28. #include <boost/config/abi_prefix.hpp>
  29. namespace boost
  30. {
  31. /**
  32. * strict lock providing a const pointer access to the synchronized value type.
  33. *
  34. * @param T the value type.
  35. * @param Lockable the mutex type protecting the value type.
  36. */
  37. template <typename T, typename Lockable = mutex>
  38. class const_strict_lock_ptr
  39. {
  40. public:
  41. typedef T value_type;
  42. typedef Lockable mutex_type;
  43. protected:
  44. // this should be a strict_lock, but unique_lock is needed to be able to return it.
  45. boost::unique_lock<mutex_type> lk_;
  46. T const& value_;
  47. public:
  48. BOOST_THREAD_MOVABLE_ONLY( const_strict_lock_ptr )
  49. /**
  50. * @param value constant reference of the value to protect.
  51. * @param mtx reference to the mutex used to protect the value.
  52. * @effects locks the mutex @c mtx, stores a reference to it and to the value type @c value.
  53. */
  54. const_strict_lock_ptr(T const& val, Lockable & mtx) :
  55. lk_(mtx), value_(val)
  56. {
  57. }
  58. const_strict_lock_ptr(T const& val, Lockable & mtx, adopt_lock_t tag) BOOST_NOEXCEPT :
  59. lk_(mtx, tag), value_(val)
  60. {
  61. }
  62. /**
  63. * Move constructor.
  64. * @effects takes ownership of the mutex owned by @c other, stores a reference to the mutex and the value type of @c other.
  65. */
  66. const_strict_lock_ptr(BOOST_THREAD_RV_REF(const_strict_lock_ptr) other) BOOST_NOEXCEPT
  67. : lk_(boost::move(BOOST_THREAD_RV(other).lk_)),value_(BOOST_THREAD_RV(other).value_)
  68. {
  69. }
  70. ~const_strict_lock_ptr()
  71. {
  72. }
  73. /**
  74. * @return a constant pointer to the protected value
  75. */
  76. const T* operator->() const
  77. {
  78. return &value_;
  79. }
  80. /**
  81. * @return a constant reference to the protected value
  82. */
  83. const T& operator*() const
  84. {
  85. return value_;
  86. }
  87. };
  88. /**
  89. * strict lock providing a pointer access to the synchronized value type.
  90. *
  91. * @param T the value type.
  92. * @param Lockable the mutex type protecting the value type.
  93. */
  94. template <typename T, typename Lockable = mutex>
  95. class strict_lock_ptr : public const_strict_lock_ptr<T,Lockable>
  96. {
  97. typedef const_strict_lock_ptr<T,Lockable> base_type;
  98. public:
  99. BOOST_THREAD_MOVABLE_ONLY( strict_lock_ptr )
  100. /**
  101. * @param value reference of the value to protect.
  102. * @param mtx reference to the mutex used to protect the value.
  103. * @effects locks the mutex @c mtx, stores a reference to it and to the value type @c value.
  104. */
  105. strict_lock_ptr(T & val, Lockable & mtx) :
  106. base_type(val, mtx)
  107. {
  108. }
  109. strict_lock_ptr(T & val, Lockable & mtx, adopt_lock_t tag) :
  110. base_type(val, mtx, tag)
  111. {
  112. }
  113. /**
  114. * Move constructor.
  115. * @effects takes ownership of the mutex owned by @c other, stores a reference to the mutex and the value type of @c other.
  116. */
  117. strict_lock_ptr(BOOST_THREAD_RV_REF(strict_lock_ptr) other)
  118. : base_type(boost::move(static_cast<base_type&>(other)))
  119. {
  120. }
  121. ~strict_lock_ptr()
  122. {
  123. }
  124. /**
  125. * @return a pointer to the protected value
  126. */
  127. T* operator->()
  128. {
  129. return const_cast<T*>(&this->value_);
  130. }
  131. /**
  132. * @return a reference to the protected value
  133. */
  134. T& operator*()
  135. {
  136. return const_cast<T&>(this->value_);
  137. }
  138. };
  139. template <typename SV>
  140. struct synchronized_value_strict_lock_ptr
  141. {
  142. typedef strict_lock_ptr<typename SV::value_type, typename SV::mutex_type> type;
  143. };
  144. template <typename SV>
  145. struct synchronized_value_strict_lock_ptr<const SV>
  146. {
  147. typedef const_strict_lock_ptr<typename SV::value_type, typename SV::mutex_type> type;
  148. };
  149. /**
  150. * unique_lock providing a const pointer access to the synchronized value type.
  151. *
  152. * An object of type const_unique_lock_ptr is a unique_lock that provides a const pointer access to the synchronized value type.
  153. * As unique_lock controls the ownership of a lockable object within a scope.
  154. * Ownership of the lockable object may be acquired at construction or after construction,
  155. * and may be transferred, after acquisition, to another const_unique_lock_ptr object.
  156. * Objects of type const_unique_lock_ptr are not copyable but are movable.
  157. * The behavior of a program is undefined if the mutex and the value type
  158. * pointed do not exist for the entire remaining lifetime of the const_unique_lock_ptr object.
  159. * The supplied Mutex type shall meet the BasicLockable requirements.
  160. *
  161. * @note const_unique_lock_ptr<T, Lockable> meets the Lockable requirements.
  162. * If Lockable meets the TimedLockable requirements, const_unique_lock_ptr<T,Lockable>
  163. * also meets the TimedLockable requirements.
  164. *
  165. * @param T the value type.
  166. * @param Lockable the mutex type protecting the value type.
  167. */
  168. template <typename T, typename Lockable = mutex>
  169. class const_unique_lock_ptr : public unique_lock<Lockable>
  170. {
  171. typedef unique_lock<Lockable> base_type;
  172. public:
  173. typedef T value_type;
  174. typedef Lockable mutex_type;
  175. protected:
  176. T const& value_;
  177. public:
  178. BOOST_THREAD_MOVABLE_ONLY(const_unique_lock_ptr)
  179. /**
  180. * @param value reference of the value to protect.
  181. * @param mtx reference to the mutex used to protect the value.
  182. *
  183. * @requires If mutex_type is not a recursive mutex the calling thread does not own the mutex.
  184. *
  185. * @effects locks the mutex @c mtx, stores a reference to it and to the value type @c value.
  186. */
  187. const_unique_lock_ptr(T const& val, Lockable & mtx)
  188. : base_type(mtx), value_(val)
  189. {
  190. }
  191. /**
  192. * @param value reference of the value to protect.
  193. * @param mtx reference to the mutex used to protect the value.
  194. * @param tag of type adopt_lock_t used to differentiate the constructor.
  195. * @requires The calling thread own the mutex.
  196. * @effects stores a reference to it and to the value type @c value taking ownership.
  197. */
  198. const_unique_lock_ptr(T const& val, Lockable & mtx, adopt_lock_t) BOOST_NOEXCEPT
  199. : base_type(mtx, adopt_lock), value_(val)
  200. {
  201. }
  202. /**
  203. * @param value reference of the value to protect.
  204. * @param mtx reference to the mutex used to protect the value.
  205. * @param tag of type defer_lock_t used to differentiate the constructor.
  206. * @effects stores a reference to it and to the value type @c value c.
  207. */
  208. const_unique_lock_ptr(T const& val, Lockable & mtx, defer_lock_t) BOOST_NOEXCEPT
  209. : base_type(mtx, defer_lock), value_(val)
  210. {
  211. }
  212. /**
  213. * @param value reference of the value to protect.
  214. * @param mtx reference to the mutex used to protect the value.
  215. * @param tag of type try_to_lock_t used to differentiate the constructor.
  216. * @requires If mutex_type is not a recursive mutex the calling thread does not own the mutex.
  217. * @effects try to lock the mutex @c mtx, stores a reference to it and to the value type @c value.
  218. */
  219. const_unique_lock_ptr(T const& val, Lockable & mtx, try_to_lock_t) BOOST_NOEXCEPT
  220. : base_type(mtx, try_to_lock), value_(val)
  221. {
  222. }
  223. /**
  224. * Move constructor.
  225. * @effects takes ownership of the mutex owned by @c other, stores a reference to the mutex and the value type of @c other.
  226. */
  227. const_unique_lock_ptr(BOOST_THREAD_RV_REF(const_unique_lock_ptr) other) BOOST_NOEXCEPT
  228. : base_type(boost::move(static_cast<base_type&>(other))), value_(BOOST_THREAD_RV(other).value_)
  229. {
  230. }
  231. /**
  232. * @effects If owns calls unlock() on the owned mutex.
  233. */
  234. ~const_unique_lock_ptr()
  235. {
  236. }
  237. /**
  238. * @return a constant pointer to the protected value
  239. */
  240. const T* operator->() const
  241. {
  242. BOOST_ASSERT (this->owns_lock());
  243. return &value_;
  244. }
  245. /**
  246. * @return a constant reference to the protected value
  247. */
  248. const T& operator*() const
  249. {
  250. BOOST_ASSERT (this->owns_lock());
  251. return value_;
  252. }
  253. };
  254. /**
  255. * unique lock providing a pointer access to the synchronized value type.
  256. *
  257. * @param T the value type.
  258. * @param Lockable the mutex type protecting the value type.
  259. */
  260. template <typename T, typename Lockable = mutex>
  261. class unique_lock_ptr : public const_unique_lock_ptr<T, Lockable>
  262. {
  263. typedef const_unique_lock_ptr<T, Lockable> base_type;
  264. public:
  265. typedef T value_type;
  266. typedef Lockable mutex_type;
  267. BOOST_THREAD_MOVABLE_ONLY(unique_lock_ptr)
  268. /**
  269. * @param value reference of the value to protect.
  270. * @param mtx reference to the mutex used to protect the value.
  271. * @effects locks the mutex @c mtx, stores a reference to it and to the value type @c value.
  272. */
  273. unique_lock_ptr(T & val, Lockable & mtx)
  274. : base_type(val, mtx)
  275. {
  276. }
  277. /**
  278. * @param value reference of the value to protect.
  279. * @param mtx reference to the mutex used to protect the value.
  280. * @param tag of type adopt_lock_t used to differentiate the constructor.
  281. * @effects stores a reference to it and to the value type @c value taking ownership.
  282. */
  283. unique_lock_ptr(T & value, Lockable & mtx, adopt_lock_t) BOOST_NOEXCEPT
  284. : base_type(value, mtx, adopt_lock)
  285. {
  286. }
  287. /**
  288. * @param value reference of the value to protect.
  289. * @param mtx reference to the mutex used to protect the value.
  290. * @param tag of type defer_lock_t used to differentiate the constructor.
  291. * @effects stores a reference to it and to the value type @c value c.
  292. */
  293. unique_lock_ptr(T & value, Lockable & mtx, defer_lock_t) BOOST_NOEXCEPT
  294. : base_type(value, mtx, defer_lock)
  295. {
  296. }
  297. /**
  298. * @param value reference of the value to protect.
  299. * @param mtx reference to the mutex used to protect the value.
  300. * @param tag of type try_to_lock_t used to differentiate the constructor.
  301. * @effects try to lock the mutex @c mtx, stores a reference to it and to the value type @c value.
  302. */
  303. unique_lock_ptr(T & value, Lockable & mtx, try_to_lock_t) BOOST_NOEXCEPT
  304. : base_type(value, mtx, try_to_lock)
  305. {
  306. }
  307. /**
  308. * Move constructor.
  309. * @effects takes ownership of the mutex owned by @c other, stores a reference to the mutex and the value type of @c other.
  310. */
  311. unique_lock_ptr(BOOST_THREAD_RV_REF(unique_lock_ptr) other) BOOST_NOEXCEPT
  312. : base_type(boost::move(static_cast<base_type&>(other)))
  313. {
  314. }
  315. ~unique_lock_ptr()
  316. {
  317. }
  318. /**
  319. * @return a pointer to the protected value
  320. */
  321. T* operator->()
  322. {
  323. BOOST_ASSERT (this->owns_lock());
  324. return const_cast<T*>(&this->value_);
  325. }
  326. /**
  327. * @return a reference to the protected value
  328. */
  329. T& operator*()
  330. {
  331. BOOST_ASSERT (this->owns_lock());
  332. return const_cast<T&>(this->value_);
  333. }
  334. };
  335. template <typename SV>
  336. struct synchronized_value_unique_lock_ptr
  337. {
  338. typedef unique_lock_ptr<typename SV::value_type, typename SV::mutex_type> type;
  339. };
  340. template <typename SV>
  341. struct synchronized_value_unique_lock_ptr<const SV>
  342. {
  343. typedef const_unique_lock_ptr<typename SV::value_type, typename SV::mutex_type> type;
  344. };
  345. /**
  346. * cloaks a value type and the mutex used to protect it together.
  347. * @param T the value type.
  348. * @param Lockable the mutex type protecting the value type.
  349. */
  350. template <typename T, typename Lockable = mutex>
  351. class synchronized_value
  352. {
  353. #if ! defined(BOOST_THREAD_NO_MAKE_UNIQUE_LOCKS)
  354. #if ! defined BOOST_NO_CXX11_VARIADIC_TEMPLATES
  355. template <typename ...SV>
  356. friend std::tuple<typename synchronized_value_strict_lock_ptr<SV>::type ...> synchronize(SV& ...sv);
  357. #else
  358. template <typename SV1, typename SV2>
  359. friend std::tuple<
  360. typename synchronized_value_strict_lock_ptr<SV1>::type,
  361. typename synchronized_value_strict_lock_ptr<SV2>::type
  362. >
  363. synchronize(SV1& sv1, SV2& sv2);
  364. template <typename SV1, typename SV2, typename SV3>
  365. friend std::tuple<
  366. typename synchronized_value_strict_lock_ptr<SV1>::type,
  367. typename synchronized_value_strict_lock_ptr<SV2>::type,
  368. typename synchronized_value_strict_lock_ptr<SV3>::type
  369. >
  370. synchronize(SV1& sv1, SV2& sv2, SV3& sv3);
  371. #endif
  372. #endif
  373. public:
  374. typedef T value_type;
  375. typedef Lockable mutex_type;
  376. private:
  377. T value_;
  378. mutable mutex_type mtx_;
  379. public:
  380. // construction/destruction
  381. /**
  382. * Default constructor.
  383. *
  384. * @Requires: T is DefaultConstructible
  385. */
  386. synchronized_value()
  387. //BOOST_NOEXCEPT_IF(is_nothrow_default_constructible<T>::value)
  388. : value_()
  389. {
  390. }
  391. /**
  392. * Constructor from copy constructible value.
  393. *
  394. * Requires: T is CopyConstructible
  395. */
  396. synchronized_value(T const& other)
  397. //BOOST_NOEXCEPT_IF(is_nothrow_copy_constructible<T>::value)
  398. : value_(other)
  399. {
  400. }
  401. /**
  402. * Move Constructor.
  403. *
  404. * Requires: T is CopyMovable
  405. */
  406. synchronized_value(BOOST_THREAD_RV_REF(T) other)
  407. //BOOST_NOEXCEPT_IF(is_nothrow_move_constructible<T>::value)
  408. : value_(boost::move(other))
  409. {
  410. }
  411. /**
  412. * Constructor from value type.
  413. *
  414. * Requires: T is DefaultConstructible and Assignable
  415. * Effects: Assigns the value on a scope protected by the mutex of the rhs. The mutex is not copied.
  416. */
  417. synchronized_value(synchronized_value const& rhs)
  418. {
  419. strict_lock<mutex_type> lk(rhs.mtx_);
  420. value_ = rhs.value_;
  421. }
  422. /**
  423. * Move Constructor from movable value type
  424. *
  425. */
  426. synchronized_value(BOOST_THREAD_RV_REF(synchronized_value) other)
  427. {
  428. strict_lock<mutex_type> lk(BOOST_THREAD_RV(other).mtx_);
  429. value_= boost::move(BOOST_THREAD_RV(other).value_);
  430. }
  431. // mutation
  432. /**
  433. * Assignment operator.
  434. *
  435. * Effects: Copies the underlying value on a scope protected by the two mutexes.
  436. * The mutex is not copied. The locks are acquired using lock, so deadlock is avoided.
  437. * For example, there is no problem if one thread assigns a = b and the other assigns b = a.
  438. *
  439. * Return: *this
  440. */
  441. synchronized_value& operator=(synchronized_value const& rhs)
  442. {
  443. if(&rhs != this)
  444. {
  445. // auto _ = make_unique_locks(mtx_, rhs.mtx_);
  446. unique_lock<mutex_type> lk1(mtx_, defer_lock);
  447. unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
  448. lock(lk1,lk2);
  449. value_ = rhs.value_;
  450. }
  451. return *this;
  452. }
  453. /**
  454. * Assignment operator from a T const&.
  455. * Effects: The operator copies the value on a scope protected by the mutex.
  456. * Return: *this
  457. */
  458. synchronized_value& operator=(value_type const& val)
  459. {
  460. {
  461. strict_lock<mutex_type> lk(mtx_);
  462. value_ = val;
  463. }
  464. return *this;
  465. }
  466. //observers
  467. /**
  468. * Explicit conversion to value type.
  469. *
  470. * Requires: T is CopyConstructible
  471. * Return: A copy of the protected value obtained on a scope protected by the mutex.
  472. *
  473. */
  474. T get() const
  475. {
  476. strict_lock<mutex_type> lk(mtx_);
  477. return value_;
  478. }
  479. /**
  480. * Explicit conversion to value type.
  481. *
  482. * Requires: T is CopyConstructible
  483. * Return: A copy of the protected value obtained on a scope protected by the mutex.
  484. *
  485. */
  486. #if ! defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  487. explicit operator T() const
  488. {
  489. return get();
  490. }
  491. #endif
  492. /**
  493. * value type getter.
  494. *
  495. * Return: A constant reference to the protected value.
  496. *
  497. * Note: Not thread safe
  498. *
  499. */
  500. T const& value() const
  501. {
  502. return value_;
  503. }
  504. /**
  505. * mutex getter.
  506. *
  507. * Return: A constant reference to the protecting mutex.
  508. *
  509. * Note: Not thread safe
  510. *
  511. */
  512. mutex_type const& mutex() const
  513. {
  514. return mtx_;
  515. }
  516. /**
  517. * Swap
  518. *
  519. * Effects: Swaps the data. Again, locks are acquired using lock(). The mutexes are not swapped.
  520. * A swap method accepts a T& and swaps the data inside a critical section.
  521. * This is by far the preferred method of changing the guarded datum wholesale because it keeps the lock only
  522. * for a short time, thus lowering the pressure on the mutex.
  523. */
  524. void swap(synchronized_value & rhs)
  525. {
  526. if (this == &rhs) {
  527. return;
  528. }
  529. // auto _ = make_unique_locks(mtx_, rhs.mtx_);
  530. unique_lock<mutex_type> lk1(mtx_, defer_lock);
  531. unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
  532. lock(lk1,lk2);
  533. boost::swap(value_, rhs.value_);
  534. }
  535. /**
  536. * Swap with the underlying value type
  537. *
  538. * Effects: Swaps the data on a scope protected by the mutex.
  539. */
  540. void swap(value_type & rhs)
  541. {
  542. strict_lock<mutex_type> lk(mtx_);
  543. boost::swap(value_, rhs);
  544. }
  545. /**
  546. * Essentially calling a method obj->foo(x, y, z) calls the method foo(x, y, z) inside a critical section as
  547. * long-lived as the call itself.
  548. */
  549. strict_lock_ptr<T,Lockable> operator->()
  550. {
  551. return BOOST_THREAD_MAKE_RV_REF((strict_lock_ptr<T,Lockable>(value_, mtx_)));
  552. }
  553. /**
  554. * If the synchronized_value object involved is const-qualified, then you'll only be able to call const methods
  555. * through operator->. So, for example, vec->push_back("xyz") won't work if vec were const-qualified.
  556. * The locking mechanism capitalizes on the assumption that const methods don't modify their underlying data.
  557. */
  558. const_strict_lock_ptr<T,Lockable> operator->() const
  559. {
  560. return BOOST_THREAD_MAKE_RV_REF((const_strict_lock_ptr<T,Lockable>(value_, mtx_)));
  561. }
  562. /**
  563. * Call function on a locked block.
  564. *
  565. * @requires fct(value_) is well formed.
  566. *
  567. * Example
  568. * void fun(synchronized_value<vector<int>> & v) {
  569. * v ( [](vector<int>> & vec)
  570. * {
  571. * vec.push_back(42);
  572. * assert(vec.back() == 42);
  573. * } );
  574. * }
  575. */
  576. template <typename F>
  577. inline
  578. typename boost::result_of<F(value_type&)>::type
  579. operator()(BOOST_THREAD_RV_REF(F) fct)
  580. {
  581. strict_lock<mutex_type> lk(mtx_);
  582. return fct(value_);
  583. }
  584. template <typename F>
  585. inline
  586. typename boost::result_of<F(value_type const&)>::type
  587. operator()(BOOST_THREAD_RV_REF(F) fct) const
  588. {
  589. strict_lock<mutex_type> lk(mtx_);
  590. return fct(value_);
  591. }
  592. #if defined BOOST_NO_CXX11_RVALUE_REFERENCES
  593. template <typename F>
  594. inline
  595. typename boost::result_of<F(value_type&)>::type
  596. operator()(F const & fct)
  597. {
  598. strict_lock<mutex_type> lk(mtx_);
  599. return fct(value_);
  600. }
  601. template <typename F>
  602. inline
  603. typename boost::result_of<F(value_type const&)>::type
  604. operator()(F const & fct) const
  605. {
  606. strict_lock<mutex_type> lk(mtx_);
  607. return fct(value_);
  608. }
  609. template <typename R>
  610. inline
  611. R operator()(R(*fct)(value_type&))
  612. {
  613. strict_lock<mutex_type> lk(mtx_);
  614. return fct(value_);
  615. }
  616. template <typename R>
  617. inline
  618. R operator()(R(*fct)(value_type const&)) const
  619. {
  620. strict_lock<mutex_type> lk(mtx_);
  621. return fct(value_);
  622. }
  623. #endif
  624. /**
  625. * The synchronize() factory make easier to lock on a scope.
  626. * As discussed, operator-> can only lock over the duration of a call, so it is insufficient for complex operations.
  627. * With synchronize() you get to lock the object in a scoped and to directly access the object inside that scope.
  628. *
  629. * Example
  630. * void fun(synchronized_value<vector<int>> & v) {
  631. * auto&& vec=v.synchronize();
  632. * vec.push_back(42);
  633. * assert(vec.back() == 42);
  634. * }
  635. */
  636. strict_lock_ptr<T,Lockable> synchronize()
  637. {
  638. return BOOST_THREAD_MAKE_RV_REF((strict_lock_ptr<T,Lockable>(value_, mtx_)));
  639. }
  640. const_strict_lock_ptr<T,Lockable> synchronize() const
  641. {
  642. return BOOST_THREAD_MAKE_RV_REF((const_strict_lock_ptr<T,Lockable>(value_, mtx_)));
  643. }
  644. unique_lock_ptr<T,Lockable> unique_synchronize()
  645. {
  646. return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_)));
  647. }
  648. const_unique_lock_ptr<T,Lockable> unique_synchronize() const
  649. {
  650. return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_)));
  651. }
  652. unique_lock_ptr<T,Lockable> unique_synchronize(defer_lock_t tag)
  653. {
  654. return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_, tag)));
  655. }
  656. const_unique_lock_ptr<T,Lockable> unique_synchronize(defer_lock_t tag) const
  657. {
  658. return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_, tag)));
  659. }
  660. unique_lock_ptr<T,Lockable> defer_synchronize() BOOST_NOEXCEPT
  661. {
  662. return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_, defer_lock)));
  663. }
  664. const_unique_lock_ptr<T,Lockable> defer_synchronize() const BOOST_NOEXCEPT
  665. {
  666. return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_, defer_lock)));
  667. }
  668. unique_lock_ptr<T,Lockable> try_to_synchronize() BOOST_NOEXCEPT
  669. {
  670. return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_, try_to_lock)));
  671. }
  672. const_unique_lock_ptr<T,Lockable> try_to_synchronize() const BOOST_NOEXCEPT
  673. {
  674. return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_, try_to_lock)));
  675. }
  676. unique_lock_ptr<T,Lockable> adopt_synchronize() BOOST_NOEXCEPT
  677. {
  678. return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_, adopt_lock)));
  679. }
  680. const_unique_lock_ptr<T,Lockable> adopt_synchronize() const BOOST_NOEXCEPT
  681. {
  682. return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_, adopt_lock)));
  683. }
  684. #if ! defined __IBMCPP__
  685. private:
  686. #endif
  687. class deref_value
  688. {
  689. private:
  690. friend class synchronized_value;
  691. boost::unique_lock<mutex_type> lk_;
  692. T& value_;
  693. explicit deref_value(synchronized_value& outer):
  694. lk_(outer.mtx_),value_(outer.value_)
  695. {}
  696. public:
  697. BOOST_THREAD_MOVABLE_ONLY(deref_value)
  698. deref_value(BOOST_THREAD_RV_REF(deref_value) other):
  699. lk_(boost::move(BOOST_THREAD_RV(other).lk_)),value_(BOOST_THREAD_RV(other).value_)
  700. {}
  701. operator T&()
  702. {
  703. return value_;
  704. }
  705. deref_value& operator=(T const& newVal)
  706. {
  707. value_=newVal;
  708. return *this;
  709. }
  710. };
  711. class const_deref_value
  712. {
  713. private:
  714. friend class synchronized_value;
  715. boost::unique_lock<mutex_type> lk_;
  716. const T& value_;
  717. explicit const_deref_value(synchronized_value const& outer):
  718. lk_(outer.mtx_), value_(outer.value_)
  719. {}
  720. public:
  721. BOOST_THREAD_MOVABLE_ONLY(const_deref_value)
  722. const_deref_value(BOOST_THREAD_RV_REF(const_deref_value) other):
  723. lk_(boost::move(BOOST_THREAD_RV(other).lk_)), value_(BOOST_THREAD_RV(other).value_)
  724. {}
  725. operator const T&()
  726. {
  727. return value_;
  728. }
  729. };
  730. public:
  731. deref_value operator*()
  732. {
  733. return BOOST_THREAD_MAKE_RV_REF(deref_value(*this));
  734. }
  735. const_deref_value operator*() const
  736. {
  737. return BOOST_THREAD_MAKE_RV_REF(const_deref_value(*this));
  738. }
  739. // io functions
  740. /**
  741. * @requires T is OutputStreamable
  742. * @effects saves the value type on the output stream @c os.
  743. */
  744. template <typename OStream>
  745. void save(OStream& os) const
  746. {
  747. strict_lock<mutex_type> lk(mtx_);
  748. os << value_;
  749. }
  750. /**
  751. * @requires T is InputStreamable
  752. * @effects loads the value type from the input stream @c is.
  753. */
  754. template <typename IStream>
  755. void load(IStream& is)
  756. {
  757. strict_lock<mutex_type> lk(mtx_);
  758. is >> value_;
  759. }
  760. // relational operators
  761. /**
  762. * @requires T is EqualityComparable
  763. *
  764. */
  765. bool operator==(synchronized_value const& rhs) const
  766. {
  767. unique_lock<mutex_type> lk1(mtx_, defer_lock);
  768. unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
  769. lock(lk1,lk2);
  770. return value_ == rhs.value_;
  771. }
  772. /**
  773. * @requires T is LessThanComparable
  774. *
  775. */
  776. bool operator<(synchronized_value const& rhs) const
  777. {
  778. unique_lock<mutex_type> lk1(mtx_, defer_lock);
  779. unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
  780. lock(lk1,lk2);
  781. return value_ < rhs.value_;
  782. }
  783. /**
  784. * @requires T is GreaterThanComparable
  785. *
  786. */
  787. bool operator>(synchronized_value const& rhs) const
  788. {
  789. unique_lock<mutex_type> lk1(mtx_, defer_lock);
  790. unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
  791. lock(lk1,lk2);
  792. return value_ > rhs.value_;
  793. }
  794. bool operator<=(synchronized_value const& rhs) const
  795. {
  796. unique_lock<mutex_type> lk1(mtx_, defer_lock);
  797. unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
  798. lock(lk1,lk2);
  799. return value_ <= rhs.value_;
  800. }
  801. bool operator>=(synchronized_value const& rhs) const
  802. {
  803. unique_lock<mutex_type> lk1(mtx_, defer_lock);
  804. unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
  805. lock(lk1,lk2);
  806. return value_ >= rhs.value_;
  807. }
  808. bool operator==(value_type const& rhs) const
  809. {
  810. unique_lock<mutex_type> lk1(mtx_);
  811. return value_ == rhs;
  812. }
  813. bool operator!=(value_type const& rhs) const
  814. {
  815. unique_lock<mutex_type> lk1(mtx_);
  816. return value_ != rhs;
  817. }
  818. bool operator<(value_type const& rhs) const
  819. {
  820. unique_lock<mutex_type> lk1(mtx_);
  821. return value_ < rhs;
  822. }
  823. bool operator<=(value_type const& rhs) const
  824. {
  825. unique_lock<mutex_type> lk1(mtx_);
  826. return value_ <= rhs;
  827. }
  828. bool operator>(value_type const& rhs) const
  829. {
  830. unique_lock<mutex_type> lk1(mtx_);
  831. return value_ > rhs;
  832. }
  833. bool operator>=(value_type const& rhs) const
  834. {
  835. unique_lock<mutex_type> lk1(mtx_);
  836. return value_ >= rhs;
  837. }
  838. };
  839. // Specialized algorithms
  840. /**
  841. *
  842. */
  843. template <typename T, typename L>
  844. inline void swap(synchronized_value<T,L> & lhs, synchronized_value<T,L> & rhs)
  845. {
  846. lhs.swap(rhs);
  847. }
  848. template <typename T, typename L>
  849. inline void swap(synchronized_value<T,L> & lhs, T & rhs)
  850. {
  851. lhs.swap(rhs);
  852. }
  853. template <typename T, typename L>
  854. inline void swap(T & lhs, synchronized_value<T,L> & rhs)
  855. {
  856. rhs.swap(lhs);
  857. }
  858. //Hash support
  859. // template <class T> struct hash;
  860. // template <typename T, typename L>
  861. // struct hash<synchronized_value<T,L> >;
  862. // Comparison with T
  863. template <typename T, typename L>
  864. bool operator!=(synchronized_value<T,L> const&lhs, synchronized_value<T,L> const& rhs)
  865. {
  866. return ! (lhs==rhs);
  867. }
  868. template <typename T, typename L>
  869. bool operator==(T const& lhs, synchronized_value<T,L> const&rhs)
  870. {
  871. return rhs==lhs;
  872. }
  873. template <typename T, typename L>
  874. bool operator!=(T const& lhs, synchronized_value<T,L> const&rhs)
  875. {
  876. return rhs!=lhs;
  877. }
  878. template <typename T, typename L>
  879. bool operator<(T const& lhs, synchronized_value<T,L> const&rhs)
  880. {
  881. return rhs>lhs;
  882. }
  883. template <typename T, typename L>
  884. bool operator<=(T const& lhs, synchronized_value<T,L> const&rhs)
  885. {
  886. return rhs>=lhs;
  887. }
  888. template <typename T, typename L>
  889. bool operator>(T const& lhs, synchronized_value<T,L> const&rhs)
  890. {
  891. return rhs<lhs;
  892. }
  893. template <typename T, typename L>
  894. bool operator>=(T const& lhs, synchronized_value<T,L> const&rhs)
  895. {
  896. return rhs<=lhs;
  897. }
  898. /**
  899. *
  900. */
  901. template <typename OStream, typename T, typename L>
  902. inline OStream& operator<<(OStream& os, synchronized_value<T,L> const& rhs)
  903. {
  904. rhs.save(os);
  905. return os;
  906. }
  907. template <typename IStream, typename T, typename L>
  908. inline IStream& operator>>(IStream& is, synchronized_value<T,L>& rhs)
  909. {
  910. rhs.load(is);
  911. return is;
  912. }
  913. #if ! defined(BOOST_THREAD_NO_SYNCHRONIZE)
  914. #if ! defined BOOST_NO_CXX11_VARIADIC_TEMPLATES
  915. template <typename ...SV>
  916. std::tuple<typename synchronized_value_strict_lock_ptr<SV>::type ...> synchronize(SV& ...sv)
  917. {
  918. boost::lock(sv.mtx_ ...);
  919. typedef std::tuple<typename synchronized_value_strict_lock_ptr<SV>::type ...> t_type;
  920. return t_type(typename synchronized_value_strict_lock_ptr<SV>::type(sv.value_, sv.mtx_, adopt_lock) ...);
  921. }
  922. #else
  923. template <typename SV1, typename SV2>
  924. std::tuple<
  925. typename synchronized_value_strict_lock_ptr<SV1>::type,
  926. typename synchronized_value_strict_lock_ptr<SV2>::type
  927. >
  928. synchronize(SV1& sv1, SV2& sv2)
  929. {
  930. boost::lock(sv1.mtx_, sv2.mtx_);
  931. typedef std::tuple<
  932. typename synchronized_value_strict_lock_ptr<SV1>::type,
  933. typename synchronized_value_strict_lock_ptr<SV2>::type
  934. > t_type;
  935. return t_type(
  936. typename synchronized_value_strict_lock_ptr<SV1>::type(sv1.value_, sv1.mtx_, adopt_lock),
  937. typename synchronized_value_strict_lock_ptr<SV2>::type(sv2.value_, sv2.mtx_, adopt_lock)
  938. );
  939. }
  940. template <typename SV1, typename SV2, typename SV3>
  941. std::tuple<
  942. typename synchronized_value_strict_lock_ptr<SV1>::type,
  943. typename synchronized_value_strict_lock_ptr<SV2>::type,
  944. typename synchronized_value_strict_lock_ptr<SV3>::type
  945. >
  946. synchronize(SV1& sv1, SV2& sv2, SV3& sv3)
  947. {
  948. boost::lock(sv1.mtx_, sv2.mtx_);
  949. typedef std::tuple<
  950. typename synchronized_value_strict_lock_ptr<SV1>::type,
  951. typename synchronized_value_strict_lock_ptr<SV2>::type,
  952. typename synchronized_value_strict_lock_ptr<SV3>::type
  953. > t_type;
  954. return t_type(
  955. typename synchronized_value_strict_lock_ptr<SV1>::type(sv1.value_, sv1.mtx_, adopt_lock),
  956. typename synchronized_value_strict_lock_ptr<SV2>::type(sv2.value_, sv2.mtx_, adopt_lock),
  957. typename synchronized_value_strict_lock_ptr<SV3>::type(sv3.value_, sv3.mtx_, adopt_lock)
  958. );
  959. }
  960. #endif
  961. #endif
  962. }
  963. #include <boost/config/abi_suffix.hpp>
  964. #endif // header