drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
barrier.h
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2010 Brian Aker
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of the <organization> nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31#include <boost/thread/mutex.hpp>
32#include <boost/thread/condition_variable.hpp>
33#include <memory>
34
35#include "observer.h"
36
37#pragma once
38
39/*
40 Barrier was designed with the following concepts.
41
42 1) A barrier can be set with an initial limit which can be used such that if the limit is met, it releases all waiters.
43 2) A barrier can be released at any time, even if the limit is not met by an outside caller.
44 3) An observer can register itself to the barrier, it will wait until some predicate action releases it.
45 4) Observers are always released by limit, or in the case where the barrier is released or destroyed.
46 5) Observers should be held by copy, not by reference in order to allow for correct deletion.
47
48 @todo while we do pass an owner type to a barrier currently, we make no usage of it, and instead we currently protect
49 poor use, namely the owner of a barrier calling wait() via the layer above. It may be a good idea to change this.
50*/
51
52namespace user_locks {
53namespace barriers {
54
55// Barrier starts in a blocking posistion
56class Barrier {
57public:
58 typedef std::shared_ptr<Barrier> shared_ptr;
59
60 Barrier(drizzled::session_id_t owner_arg) :
61 owner(owner_arg),
62 limit(0),
63 current_wait(0),
64 generation(0)
65 { }
66
67 Barrier(drizzled::session_id_t owner_arg, int64_t limit_arg) :
68 owner(owner_arg),
69 limit(limit_arg),
70 current_wait(limit),
71 generation(0)
72 {
73 }
74
75 ~Barrier()
76 {
77 wakeAll();
78 }
79
80 // Signal all of the observers to start
81 void signal()
82 {
83 boost::mutex::scoped_lock scopedBarrier(sleeper_mutex);
84 wakeAll();
85 }
86
87 drizzled::session_id_t getOwner() const
88 {
89 return owner;
90 }
91
92 void wait()
93 {
94 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
95 int64_t my_generation= generation;
96
97 --current_wait;
98 if (limit)
99 {
100 if (not current_wait)
101 {
102 wakeAll();
103
104 return;
105 }
106
107 }
108 checkObservers();
109
110 // If we are interrupted we remove ourself from the list, and check on
111 // the observers.
112 try
113 {
114 while (my_generation == generation)
115 {
116 sleep_threshhold.wait(sleeper_mutex);
117 }
118 }
119 catch(boost::thread_interrupted const& error)
120 {
121 current_wait++;
122 checkObservers();
123 }
124 }
125
126 // A call to either signal or a release will cause wait_for() to continue
127 void wait_until(int64_t wait_until_arg)
128 {
129 Observer::shared_ptr observer;
130 {
131 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
132
133 if (wait_until_arg <= count())
134 return;
135
136 observer.reset(new Observer(wait_until_arg));
137 observers.push_back(observer);
138 }
139
140 try {
141 observer->sleep();
142 }
143 catch(boost::thread_interrupted const& error)
144 {
145 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
146 // Someone has interrupted us, we now try to remove ourself from the
147 // observer chain ourself
148
149 observers.remove(observer);
150
151 throw error;
152 }
153 }
154
155 void wait(int64_t generation_arg)
156 {
157 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
158 int64_t my_generation= generation;
159
160 // If the generation is newer then we just return immediatly
161 if (my_generation > generation_arg)
162 return;
163
164 --current_wait;
165
166 if (limit)
167 {
168 if (not current_wait)
169 {
170 wakeAll();
171 return;
172 }
173
174 }
175
176 while (my_generation == generation)
177 {
178 sleep_threshhold.wait(sleeper_mutex);
179 }
180 }
181
182 int64_t getGeneration()
183 {
184 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
185 return generation;
186 }
187
188 int64_t sizeObservers()
189 {
190 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
191 return static_cast<int64_t>(observers.size());
192 }
193
194 int64_t sizeWaiters()
195 {
196 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
197 return count();
198 }
199
200 int64_t getLimit() const
201 {
202 return limit;
203 }
204
205private:
206 void wakeAll()
207 {
208 generation++;
209 current_wait= limit;
210 sleep_threshhold.notify_all();
211
212 checkObservers();
213 }
214
215 struct isReady
216 {
217 const int64_t count;
218
219 isReady(int64_t arg) :
220 count(arg)
221 { }
222
223 bool operator() (Observer::list::const_reference observer)
224 {
225 if (observer->getLimit() <= count or count == 0)
226 {
227 observer->wake();
228 return true;
229 }
230
231 return false;
232 }
233 };
234
235 void checkObservers()
236 {
237 observers.remove_if(isReady(count()));
238 }
239
240 int64_t count() const
241 {
242 if (limit)
243 {
244 return limit - current_wait;
245 }
246 return std::abs(static_cast<long int>(current_wait));
247 }
248
249
250 drizzled::session_id_t owner;
251
252 const int64_t limit;
253 int64_t current_wait;
254 int64_t generation;
255
256 Observer::list observers;
257
258 boost::mutex sleeper_mutex;
259 boost::condition_variable_any sleep_threshhold;
260
261};
262
263} // namespace barriers
264} // namespace user_locks
265