lockable_adapter.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Vicente J. Botet Escriba 2008-2009,2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/thread for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_THREAD_LOCKABLE_ADAPTER_HPP
  11. #define BOOST_THREAD_LOCKABLE_ADAPTER_HPP
  12. #include <boost/thread/detail/delete.hpp>
  13. #include <boost/chrono/chrono.hpp>
  14. namespace boost
  15. {
  16. //[basic_lockable_adapter
  17. template <typename BasicLockable>
  18. class basic_lockable_adapter
  19. {
  20. public:
  21. typedef BasicLockable mutex_type;
  22. protected:
  23. mutex_type& lockable() const
  24. {
  25. return lockable_;
  26. }
  27. mutable mutex_type lockable_; /*< mutable so that it can be modified by const functions >*/
  28. public:
  29. BOOST_THREAD_NO_COPYABLE( basic_lockable_adapter) /*< no copyable >*/
  30. basic_lockable_adapter()
  31. {}
  32. void lock() const
  33. {
  34. lockable().lock();
  35. }
  36. void unlock() const
  37. {
  38. lockable().unlock();
  39. }
  40. };
  41. //]
  42. //[lockable_adapter
  43. template <typename Lockable>
  44. class lockable_adapter : public basic_lockable_adapter<Lockable>
  45. {
  46. public:
  47. typedef Lockable mutex_type;
  48. bool try_lock() const
  49. {
  50. return this->lockable().try_lock();
  51. }
  52. };
  53. //]
  54. //[timed_lockable_adapter
  55. template <typename TimedLock>
  56. class timed_lockable_adapter: public lockable_adapter<TimedLock>
  57. {
  58. public:
  59. typedef TimedLock mutex_type;
  60. template <typename Clock, typename Duration>
  61. bool try_lock_until(chrono::time_point<Clock, Duration> const & abs_time) const
  62. {
  63. return this->lockable().try_lock_until(abs_time);
  64. }
  65. template <typename Rep, typename Period>
  66. bool try_lock_for(chrono::duration<Rep, Period> const & rel_time) const
  67. {
  68. return this->lockable().try_lock_for(rel_time);
  69. }
  70. };
  71. //]
  72. //[shared_lockable_adapter
  73. template <typename SharableLock>
  74. class shared_lockable_adapter: public timed_lockable_adapter<SharableLock>
  75. {
  76. public:
  77. typedef SharableLock mutex_type;
  78. void lock_shared() const
  79. {
  80. this->lockable().lock_shared();
  81. }
  82. bool try_lock_shared() const
  83. {
  84. return this->lockable().try_lock_shared();
  85. }
  86. void unlock_shared() const
  87. {
  88. this->lockable().unlock_shared();
  89. }
  90. template <typename Clock, typename Duration>
  91. bool try_lock_shared_until(chrono::time_point<Clock, Duration> const & abs_time) const
  92. {
  93. return this->lockable().try_lock_shared_until(abs_time);
  94. }
  95. template <typename Rep, typename Period>
  96. bool try_lock_shared_for(chrono::duration<Rep, Period> const & rel_time) const
  97. {
  98. return this->lockable().try_lock_shared_for(rel_time);
  99. }
  100. };
  101. //]
  102. //[upgrade_lockable_adapter
  103. template <typename UpgradableLock>
  104. class upgrade_lockable_adapter: public shared_lockable_adapter<UpgradableLock>
  105. {
  106. public:
  107. typedef UpgradableLock mutex_type;
  108. void lock_upgrade() const
  109. {
  110. this->lockable().lock_upgrade();
  111. }
  112. bool try_lock_upgrade() const
  113. {
  114. return this->lockable().try_lock_upgrade();
  115. }
  116. void unlock_upgrade() const
  117. {
  118. this->lockable().unlock_upgrade();
  119. }
  120. template <typename Clock, typename Duration>
  121. bool try_lock_upgrade_until(chrono::time_point<Clock, Duration> const & abs_time) const
  122. {
  123. return this->lockable().try_lock_upgrade_until(abs_time);
  124. }
  125. template <typename Rep, typename Period>
  126. bool try_lock_upgrade_for(chrono::duration<Rep, Period> const & rel_time) const
  127. {
  128. return this->lockable().try_lock_upgrade_for(rel_time);
  129. }
  130. bool try_unlock_shared_and_lock() const
  131. {
  132. return this->lockable().try_unlock_shared_and_lock();
  133. }
  134. template <typename Clock, typename Duration>
  135. bool try_unlock_shared_and_lock_until(chrono::time_point<Clock, Duration> const & abs_time) const
  136. {
  137. return this->lockable().try_unlock_shared_and_lock_until(abs_time);
  138. }
  139. template <typename Rep, typename Period>
  140. bool try_unlock_shared_and_lock_for(chrono::duration<Rep, Period> const & rel_time) const
  141. {
  142. return this->lockable().try_unlock_shared_and_lock_for(rel_time);
  143. }
  144. void unlock_and_lock_shared() const
  145. {
  146. this->lockable().unlock_and_lock_shared();
  147. }
  148. bool try_unlock_shared_and_lock_upgrade() const
  149. {
  150. return this->lockable().try_unlock_shared_and_lock_upgrade();
  151. }
  152. template <typename Clock, typename Duration>
  153. bool try_unlock_shared_and_lock_upgrade_until(chrono::time_point<Clock, Duration> const & abs_time) const
  154. {
  155. return this->lockable().try_unlock_shared_and_lock_upgrade_until(abs_time);
  156. }
  157. template <typename Rep, typename Period>
  158. bool try_unlock_shared_and_lock_upgrade_for(chrono::duration<Rep, Period> const & rel_time) const
  159. {
  160. return this->lockable().try_unlock_shared_and_lock_upgrade_for(rel_time);
  161. }
  162. void unlock_and_lock_upgrade() const
  163. {
  164. this->lockable().unlock_and_lock_upgrade();
  165. }
  166. void unlock_upgrade_and_lock() const
  167. {
  168. this->lockable().unlock_upgrade_and_lock();
  169. }
  170. bool try_unlock_upgrade_and_lock() const
  171. {
  172. return this->lockable().try_unlock_upgrade_and_lock();
  173. }
  174. template <typename Clock, typename Duration>
  175. bool try_unlock_upgrade_and_lock_until(chrono::time_point<Clock, Duration> const & abs_time) const
  176. {
  177. return this->lockable().try_unlock_upgrade_and_lock_until(abs_time);
  178. }
  179. template <typename Rep, typename Period>
  180. bool try_unlock_upgrade_and_lock_for(chrono::duration<Rep, Period> const & rel_time) const
  181. {
  182. return this->lockable().try_unlock_upgrade_and_lock_for(rel_time);
  183. }
  184. void unlock_upgrade_and_lock_shared() const
  185. {
  186. this->lockable().unlock_upgrade_and_lock_shared();
  187. }
  188. };
  189. //]
  190. }
  191. #endif