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_LOCKABLE_CONCEPTS_HPP
7 #define BOOST_THREAD_LOCKABLE_CONCEPTS_HPP
9 #include <boost/chrono/chrono.hpp>
10 #include <boost/concept_check.hpp>
16 * BasicLockable object supports the basic features
17 * required to delimit a critical region
18 * Supports the basic lock and unlock functions.
22 template <typename Mutex>
26 BOOST_CONCEPT_USAGE(BasicLockable)
31 BasicLockable() : l(*static_cast<Mutex*>(0)) {}
33 BasicLockable operator=(BasicLockable const&);
40 * Lockable extends BasicLockable
41 * with try_lock functions.
45 template <typename Mutex>
48 BOOST_CONCEPT_ASSERT(( BasicLockable<Mutex> ));
50 BOOST_CONCEPT_USAGE(Lockable)
52 if (l.try_lock()) return;
54 Lockable() : l(*static_cast<Mutex*>(0)) {}
56 Lockable operator=(Lockable const&);
62 * TimedLockable object extends Lockable
63 * with timed lock functions: try_lock_until and try_lock_for and the exception based lock_until and lock_for
67 template <typename Mutex>
70 BOOST_CONCEPT_ASSERT(( Lockable<Mutex> ));
72 BOOST_CONCEPT_USAGE(TimedLockable)
74 if (l.try_lock_until(t)) return;
75 if (l.try_lock_for(d)) return;
77 TimedLockable() : l(*static_cast<Mutex*>(0)) {}
79 TimedLockable operator=(TimedLockable const&);
81 chrono::system_clock::time_point t;
82 chrono::system_clock::duration d;
87 * SharedLockable object extends TimedLockable
88 * with the lock_shared, lock_shared_until, lock_shared_for, try_lock_shared_until, try_lock_shared
89 * and unlock_shared functions
92 template <typename Mutex>
95 BOOST_CONCEPT_ASSERT(( TimedLockable<Mutex> ));
97 BOOST_CONCEPT_USAGE(SharedLockable)
101 if (l.try_lock_shared()) return;
102 if (l.try_lock_shared_until(t)) return;
103 if (l.try_lock_shared_for(d)) return;
105 SharedLockable() : l(*static_cast<Mutex*>(0)) {}
107 SharedLockable operator=(SharedLockable const&);
109 chrono::system_clock::time_point t;
110 chrono::system_clock::duration d;
115 * UpgradeLockable object extends SharedLockable
116 * with the lock_upgrade, lock_upgrade_until, unlock_upgrade_and_lock,
117 * unlock_and_lock_shared and unlock_upgrade_and_lock_shared functions
121 template <typename Mutex>
122 struct UpgradeLockable
124 BOOST_CONCEPT_ASSERT(( SharedLockable<Mutex> ));
126 BOOST_CONCEPT_USAGE(UpgradeLockable)
130 if (l.try_lock_upgrade()) return;
131 if (l.try_lock_upgrade_until(t)) return;
132 if (l.try_lock_upgrade_for(d)) return;
133 if (l.try_unlock_shared_and_lock()) return;
134 if (l.try_unlock_shared_and_lock_until(t)) return;
135 if (l.try_unlock_shared_and_lock_for(d)) return;
136 l.unlock_and_lock_shared();
137 if (l.try_unlock_shared_and_lock_upgrade()) return;
138 if (l.try_unlock_shared_and_lock_upgrade_until(t)) return;
139 if (l.try_unlock_shared_and_lock_upgrade_for(d)) return;
140 l.unlock_and_lock_upgrade();
141 l.unlock_upgrade_and_lock();
142 if (l.try_unlock_upgrade_and_lock()) return;
143 if (l.try_unlock_upgrade_and_lock_until(t)) return;
144 if (l.try_unlock_upgrade_and_lock_for(d)) return;
145 l.unlock_upgrade_and_lock_shared();
147 UpgradeLockable() : l(*static_cast<Mutex*>(0)) {}
149 UpgradeLockable operator=(UpgradeLockable const&);
151 chrono::system_clock::time_point t;
152 chrono::system_clock::duration d;