1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/thread for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_THREAD_LOCKABLE_ADAPTER_HPP
12 #define BOOST_THREAD_LOCKABLE_ADAPTER_HPP
14 #include <boost/thread/detail/delete.hpp>
15 #include <boost/chrono/chrono.hpp>
20 //[basic_lockable_adapter
21 template <typename BasicLockable>
22 class basic_lockable_adapter
25 typedef BasicLockable mutex_type;
28 mutex_type& lockable() const
32 mutable mutex_type lockable_; /*< mutable so that it can be modified by const functions >*/
35 BOOST_THREAD_NO_COPYABLE( basic_lockable_adapter) /*< no copyable >*/
37 basic_lockable_adapter()
53 template <typename Lockable>
54 class lockable_adapter : public basic_lockable_adapter<Lockable>
57 typedef Lockable mutex_type;
61 return this->lockable().try_lock();
66 //[timed_lockable_adapter
67 template <typename TimedLock>
68 class timed_lockable_adapter: public lockable_adapter<TimedLock>
71 typedef TimedLock mutex_type;
73 template <typename Clock, typename Duration>
74 bool try_lock_until(chrono::time_point<Clock, Duration> const & abs_time) const
76 return this->lockable().try_lock_until(abs_time);
78 template <typename Rep, typename Period>
79 bool try_lock_for(chrono::duration<Rep, Period> const & rel_time) const
81 return this->lockable().try_lock_for(rel_time);
87 //[shared_lockable_adapter
88 template <typename SharableLock>
89 class shared_lockable_adapter: public timed_lockable_adapter<SharableLock>
92 typedef SharableLock mutex_type;
94 void lock_shared() const
96 this->lockable().lock_shared();
98 bool try_lock_shared() const
100 return this->lockable().try_lock_shared();
102 void unlock_shared() const
104 this->lockable().unlock_shared();
107 template <typename Clock, typename Duration>
108 bool try_lock_shared_until(chrono::time_point<Clock, Duration> const & abs_time) const
110 return this->lockable().try_lock_shared_until(abs_time);
112 template <typename Rep, typename Period>
113 bool try_lock_shared_for(chrono::duration<Rep, Period> const & rel_time) const
115 return this->lockable().try_lock_shared_for(rel_time);
122 //[upgrade_lockable_adapter
123 template <typename UpgradableLock>
124 class upgrade_lockable_adapter: public shared_lockable_adapter<UpgradableLock>
127 typedef UpgradableLock mutex_type;
129 void lock_upgrade() const
131 this->lockable().lock_upgrade();
134 bool try_lock_upgrade() const
136 return this->lockable().try_lock_upgrade();
139 void unlock_upgrade() const
141 this->lockable().unlock_upgrade();
144 template <typename Clock, typename Duration>
145 bool try_lock_upgrade_until(chrono::time_point<Clock, Duration> const & abs_time) const
147 return this->lockable().try_lock_upgrade_until(abs_time);
149 template <typename Rep, typename Period>
150 bool try_lock_upgrade_for(chrono::duration<Rep, Period> const & rel_time) const
152 return this->lockable().try_lock_upgrade_for(rel_time);
155 bool try_unlock_shared_and_lock() const
157 return this->lockable().try_unlock_shared_and_lock();
160 template <typename Clock, typename Duration>
161 bool try_unlock_shared_and_lock_until(chrono::time_point<Clock, Duration> const & abs_time) const
163 return this->lockable().try_unlock_shared_and_lock_until(abs_time);
165 template <typename Rep, typename Period>
166 bool try_unlock_shared_and_lock_for(chrono::duration<Rep, Period> const & rel_time) const
168 return this->lockable().try_unlock_shared_and_lock_for(rel_time);
171 void unlock_and_lock_shared() const
173 this->lockable().unlock_and_lock_shared();
176 bool try_unlock_shared_and_lock_upgrade() const
178 return this->lockable().try_unlock_shared_and_lock_upgrade();
181 template <typename Clock, typename Duration>
182 bool try_unlock_shared_and_lock_upgrade_until(chrono::time_point<Clock, Duration> const & abs_time) const
184 return this->lockable().try_unlock_shared_and_lock_upgrade_until(abs_time);
186 template <typename Rep, typename Period>
187 bool try_unlock_shared_and_lock_upgrade_for(chrono::duration<Rep, Period> const & rel_time) const
189 return this->lockable().try_unlock_shared_and_lock_upgrade_for(rel_time);
192 void unlock_and_lock_upgrade() const
194 this->lockable().unlock_and_lock_upgrade();
197 void unlock_upgrade_and_lock() const
199 this->lockable().unlock_upgrade_and_lock();
202 bool try_unlock_upgrade_and_lock() const
204 return this->lockable().try_unlock_upgrade_and_lock();
206 template <typename Clock, typename Duration>
207 bool try_unlock_upgrade_and_lock_until(chrono::time_point<Clock, Duration> const & abs_time) const
209 return this->lockable().try_unlock_upgrade_and_lock_until(abs_time);
211 template <typename Rep, typename Period>
212 bool try_unlock_upgrade_and_lock_for(chrono::duration<Rep, Period> const & rel_time) const
214 return this->lockable().try_unlock_upgrade_and_lock_for(rel_time);
217 void unlock_upgrade_and_lock_shared() const
219 this->lockable().unlock_upgrade_and_lock_shared();