Chris@16
|
1 // (C) Copyright 2012 Vicente Botet
|
Chris@16
|
2 //
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 #ifndef BOOST_THREAD_LOCKABLE_CONCEPTS_HPP
|
Chris@16
|
7 #define BOOST_THREAD_LOCKABLE_CONCEPTS_HPP
|
Chris@16
|
8
|
Chris@16
|
9 #include <boost/chrono/chrono.hpp>
|
Chris@16
|
10 #include <boost/concept_check.hpp>
|
Chris@16
|
11
|
Chris@16
|
12 namespace boost
|
Chris@16
|
13 {
|
Chris@16
|
14
|
Chris@16
|
15 /**
|
Chris@16
|
16 * BasicLockable object supports the basic features
|
Chris@16
|
17 * required to delimit a critical region
|
Chris@16
|
18 * Supports the basic lock and unlock functions.
|
Chris@16
|
19 */
|
Chris@16
|
20
|
Chris@16
|
21 //[BasicLockable
|
Chris@16
|
22 template <typename Mutex>
|
Chris@16
|
23 struct BasicLockable
|
Chris@16
|
24 {
|
Chris@16
|
25
|
Chris@16
|
26 BOOST_CONCEPT_USAGE(BasicLockable)
|
Chris@16
|
27 {
|
Chris@16
|
28 l.lock();
|
Chris@16
|
29 l.unlock();
|
Chris@16
|
30 }
|
Chris@16
|
31 BasicLockable() : l(*static_cast<Mutex*>(0)) {}
|
Chris@16
|
32 private:
|
Chris@16
|
33 BasicLockable operator=(BasicLockable const&);
|
Chris@16
|
34
|
Chris@16
|
35 Mutex& l;
|
Chris@16
|
36 }
|
Chris@16
|
37 ;
|
Chris@16
|
38 //]
|
Chris@16
|
39 /**
|
Chris@16
|
40 * Lockable extends BasicLockable
|
Chris@16
|
41 * with try_lock functions.
|
Chris@16
|
42 */
|
Chris@16
|
43
|
Chris@16
|
44 //[Lockable
|
Chris@16
|
45 template <typename Mutex>
|
Chris@16
|
46 struct Lockable
|
Chris@16
|
47 {
|
Chris@16
|
48 BOOST_CONCEPT_ASSERT(( BasicLockable<Mutex> ));
|
Chris@16
|
49
|
Chris@16
|
50 BOOST_CONCEPT_USAGE(Lockable)
|
Chris@16
|
51 {
|
Chris@16
|
52 if (l.try_lock()) return;
|
Chris@16
|
53 }
|
Chris@16
|
54 Lockable() : l(*static_cast<Mutex*>(0)) {}
|
Chris@16
|
55 private:
|
Chris@16
|
56 Lockable operator=(Lockable const&);
|
Chris@16
|
57 Mutex& l;
|
Chris@16
|
58 };
|
Chris@16
|
59 //]
|
Chris@16
|
60
|
Chris@16
|
61 /**
|
Chris@16
|
62 * TimedLockable object extends Lockable
|
Chris@16
|
63 * with timed lock functions: try_lock_until and try_lock_for and the exception based lock_until and lock_for
|
Chris@16
|
64 */
|
Chris@16
|
65
|
Chris@16
|
66 //[TimedLockable
|
Chris@16
|
67 template <typename Mutex>
|
Chris@16
|
68 struct TimedLockable
|
Chris@16
|
69 {
|
Chris@16
|
70 BOOST_CONCEPT_ASSERT(( Lockable<Mutex> ));
|
Chris@16
|
71
|
Chris@16
|
72 BOOST_CONCEPT_USAGE(TimedLockable)
|
Chris@16
|
73 {
|
Chris@16
|
74 if (l.try_lock_until(t)) return;
|
Chris@16
|
75 if (l.try_lock_for(d)) return;
|
Chris@16
|
76 }
|
Chris@16
|
77 TimedLockable() : l(*static_cast<Mutex*>(0)) {}
|
Chris@16
|
78 private:
|
Chris@16
|
79 TimedLockable operator=(TimedLockable const&);
|
Chris@16
|
80 Mutex& l;
|
Chris@16
|
81 chrono::system_clock::time_point t;
|
Chris@16
|
82 chrono::system_clock::duration d;
|
Chris@16
|
83 };
|
Chris@16
|
84 //]
|
Chris@16
|
85
|
Chris@16
|
86 /**
|
Chris@16
|
87 * SharedLockable object extends TimedLockable
|
Chris@16
|
88 * with the lock_shared, lock_shared_until, lock_shared_for, try_lock_shared_until, try_lock_shared
|
Chris@16
|
89 * and unlock_shared functions
|
Chris@16
|
90 */
|
Chris@16
|
91 //[SharedLockable
|
Chris@16
|
92 template <typename Mutex>
|
Chris@16
|
93 struct SharedLockable
|
Chris@16
|
94 {
|
Chris@16
|
95 BOOST_CONCEPT_ASSERT(( TimedLockable<Mutex> ));
|
Chris@16
|
96
|
Chris@16
|
97 BOOST_CONCEPT_USAGE(SharedLockable)
|
Chris@16
|
98 {
|
Chris@16
|
99 l.lock_shared();
|
Chris@16
|
100 l.unlock_shared();
|
Chris@16
|
101 if (l.try_lock_shared()) return;
|
Chris@16
|
102 if (l.try_lock_shared_until(t)) return;
|
Chris@16
|
103 if (l.try_lock_shared_for(d)) return;
|
Chris@16
|
104 }
|
Chris@16
|
105 SharedLockable() : l(*static_cast<Mutex*>(0)) {}
|
Chris@16
|
106 private:
|
Chris@16
|
107 SharedLockable operator=(SharedLockable const&);
|
Chris@16
|
108 Mutex& l;
|
Chris@16
|
109 chrono::system_clock::time_point t;
|
Chris@16
|
110 chrono::system_clock::duration d;
|
Chris@16
|
111 };
|
Chris@16
|
112 //]
|
Chris@16
|
113
|
Chris@16
|
114 /**
|
Chris@16
|
115 * UpgradeLockable object extends SharedLockable
|
Chris@16
|
116 * with the lock_upgrade, lock_upgrade_until, unlock_upgrade_and_lock,
|
Chris@16
|
117 * unlock_and_lock_shared and unlock_upgrade_and_lock_shared functions
|
Chris@16
|
118 */
|
Chris@16
|
119
|
Chris@16
|
120 //[UpgradeLockable
|
Chris@16
|
121 template <typename Mutex>
|
Chris@16
|
122 struct UpgradeLockable
|
Chris@16
|
123 {
|
Chris@16
|
124 BOOST_CONCEPT_ASSERT(( SharedLockable<Mutex> ));
|
Chris@16
|
125
|
Chris@16
|
126 BOOST_CONCEPT_USAGE(UpgradeLockable)
|
Chris@16
|
127 {
|
Chris@16
|
128 l.lock_upgrade();
|
Chris@16
|
129 l.unlock_upgrade();
|
Chris@16
|
130 if (l.try_lock_upgrade()) return;
|
Chris@16
|
131 if (l.try_lock_upgrade_until(t)) return;
|
Chris@16
|
132 if (l.try_lock_upgrade_for(d)) return;
|
Chris@16
|
133 if (l.try_unlock_shared_and_lock()) return;
|
Chris@16
|
134 if (l.try_unlock_shared_and_lock_until(t)) return;
|
Chris@16
|
135 if (l.try_unlock_shared_and_lock_for(d)) return;
|
Chris@16
|
136 l.unlock_and_lock_shared();
|
Chris@16
|
137 if (l.try_unlock_shared_and_lock_upgrade()) return;
|
Chris@16
|
138 if (l.try_unlock_shared_and_lock_upgrade_until(t)) return;
|
Chris@16
|
139 if (l.try_unlock_shared_and_lock_upgrade_for(d)) return;
|
Chris@16
|
140 l.unlock_and_lock_upgrade();
|
Chris@16
|
141 l.unlock_upgrade_and_lock();
|
Chris@16
|
142 if (l.try_unlock_upgrade_and_lock()) return;
|
Chris@16
|
143 if (l.try_unlock_upgrade_and_lock_until(t)) return;
|
Chris@16
|
144 if (l.try_unlock_upgrade_and_lock_for(d)) return;
|
Chris@16
|
145 l.unlock_upgrade_and_lock_shared();
|
Chris@16
|
146 }
|
Chris@16
|
147 UpgradeLockable() : l(*static_cast<Mutex*>(0)) {}
|
Chris@16
|
148 private:
|
Chris@16
|
149 UpgradeLockable operator=(UpgradeLockable const&);
|
Chris@16
|
150 Mutex& l;
|
Chris@16
|
151 chrono::system_clock::time_point t;
|
Chris@16
|
152 chrono::system_clock::duration d;
|
Chris@16
|
153 };
|
Chris@16
|
154 //]
|
Chris@16
|
155
|
Chris@16
|
156 }
|
Chris@16
|
157 #endif
|