1 // (C) Copyright 2012 Vicente Botet
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_THREAD_LOCK_CONCEPTS_HPP
7 #define BOOST_THREAD_LOCK_CONCEPTS_HPP
9 #include <boost/thread/lock_traits.hpp>
10 #include <boost/thread/lock_options.hpp>
11 #include <boost/thread/lockable_concepts.hpp>
12 #include <boost/thread/exceptions.hpp>
13 #include <boost/thread/detail/move.hpp>
15 #include <boost/chrono/chrono.hpp>
16 #include <boost/concept_check.hpp>
17 #include <boost/static_assert.hpp>
23 * BasicLock object supports the basic features
24 * required to delimit a critical region
25 * Supports the basic lock, unlock and try_lock functions and
26 * defines the lock traits
29 template <typename Lk>
32 typedef typename Lk::mutex_type mutex_type;
33 void cvt_mutex_ptr(mutex_type*) {}
34 BOOST_CONCEPT_ASSERT(( BasicLockable<mutex_type> ));
36 BOOST_CONCEPT_USAGE(BasicLock)
39 Lk l2(mtx, defer_lock);
40 Lk l3(mtx, adopt_lock);
42 Lk l5(( boost::move(l2)));
43 cvt_mutex_ptr(l1.mutex());
44 if (l1.owns_lock()) return;
54 mtx(*static_cast<mutex_type*>(0))
57 BasicLock operator=(BasicLock const&);
62 template <typename Lk>
65 BOOST_CONCEPT_ASSERT(( BasicLock<Lk> ));
66 typedef typename Lk::mutex_type mutex_type;
67 BOOST_CONCEPT_ASSERT(( Lockable<mutex_type> ));
69 BOOST_CONCEPT_USAGE(Lock)
71 Lk l1(mtx, try_to_lock);
72 if (l1.try_lock()) return;
75 mtx(*static_cast<mutex_type*>(0))
78 Lock operator=(Lock const&);
82 template <typename Lk>
85 BOOST_CONCEPT_ASSERT(( Lock<Lk> ));
86 typedef typename Lk::mutex_type mutex_type;
87 BOOST_CONCEPT_ASSERT(( TimedLockable<mutex_type> ));
89 BOOST_CONCEPT_USAGE(TimedLock)
93 if (l1.try_lock_until(t)) return;
94 if (l1.try_lock_for(d)) return;
97 mtx(*static_cast<mutex_type*>(0))
100 TimedLock operator=(TimedLock const&);
102 boost::chrono::system_clock::time_point t;
103 boost::chrono::system_clock::duration d;
106 template <typename Lk>
109 BOOST_CONCEPT_ASSERT(( TimedLock<Lk> ));
110 typedef typename Lk::mutex_type mutex_type;
112 BOOST_CONCEPT_USAGE(UniqueLock)
117 mtx(*static_cast<mutex_type*>(0))
120 UniqueLock operator=(UniqueLock const&);
124 template <typename Lk>
127 BOOST_CONCEPT_ASSERT(( TimedLock<Lk> ));
128 typedef typename Lk::mutex_type mutex_type;
130 BOOST_CONCEPT_USAGE(SharedLock)
134 mtx(*static_cast<mutex_type*>(0))
137 SharedLock operator=(SharedLock const&);
142 template <typename Lk>
145 BOOST_CONCEPT_ASSERT(( SharedLock<Lk> ));
146 typedef typename Lk::mutex_type mutex_type;
148 BOOST_CONCEPT_USAGE(UpgradeLock)
152 mtx(*static_cast<mutex_type*>(0))
155 UpgradeLock operator=(UpgradeLock const&);
160 * An StrictLock is a scoped lock guard ensuring the mutex is locked on the
161 * scope of the lock, by locking the mutex on construction and unlocking it on
164 * Essentially, a StrictLock's role is only to live on the stack as an
165 * automatic variable. strict_lock must adhere to a non-copy and non-alias
166 * policy. StrictLock disables copying by making the copy constructor and the
167 * assignment operator private. While we're at it, let's disable operator new
168 * and operator delete; strict locks are not intended to be allocated on the
169 * heap. StrictLock avoids aliasing by using a slightly less orthodox and
170 * less well-known technique: disable address taking.
173 template <typename Lk>
176 typedef typename Lk::mutex_type mutex_type;
177 BOOST_CONCEPT_ASSERT(( BasicLockable<mutex_type> ));
178 BOOST_STATIC_ASSERT(( is_strict_lock<Lk>::value ));
180 BOOST_CONCEPT_USAGE( StrictLock)
182 if (l1.owns_lock(&mtx)) return;
185 l1(*static_cast<Lk*>(0)),
186 mtx(*static_cast<mutex_type*>(0))
189 StrictLock operator=(StrictLock const&);
192 mutex_type const& mtx;