31 #include <boost/thread/mutex.hpp>
32 #include <boost/thread/condition_variable.hpp>
33 #include <boost/shared_ptr.hpp>
34 #include <boost/foreach.hpp>
59 typedef boost::shared_ptr<Barrier> shared_ptr;
61 Barrier(drizzled::session_id_t owner_arg) :
68 Barrier(drizzled::session_id_t owner_arg, int64_t limit_arg) :
84 boost::mutex::scoped_lock scopedBarrier(sleeper_mutex);
88 drizzled::session_id_t getOwner()
const
95 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
96 int64_t my_generation= generation;
101 if (not current_wait)
115 while (my_generation == generation)
117 sleep_threshhold.wait(sleeper_mutex);
120 catch(boost::thread_interrupted
const& error)
128 void wait_until(int64_t wait_until_arg)
130 Observer::shared_ptr observer;
132 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
134 if (wait_until_arg <= count())
137 observer.reset(
new Observer(wait_until_arg));
138 observers.push_back(observer);
144 catch(boost::thread_interrupted
const& error)
146 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
150 observers.remove(observer);
156 void wait(int64_t generation_arg)
158 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
159 int64_t my_generation= generation;
162 if (my_generation > generation_arg)
169 if (not current_wait)
177 while (my_generation == generation)
179 sleep_threshhold.wait(sleeper_mutex);
183 int64_t getGeneration()
185 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
189 int64_t sizeObservers()
191 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
192 return static_cast<int64_t
>(observers.size());
195 int64_t sizeWaiters()
197 boost::mutex::scoped_lock scopedLock(sleeper_mutex);
201 int64_t getLimit()
const
211 sleep_threshhold.notify_all();
216 struct isReady :
public std::unary_function<Observer::list::const_reference, bool>
224 result_type operator() (argument_type observer)
226 if (observer->getLimit() <= count or count == 0)
236 void checkObservers()
238 observers.remove_if(
isReady(count()));
241 int64_t count()
const
245 return limit - current_wait;
247 return std::abs(static_cast<long int>(current_wait));
251 drizzled::session_id_t owner;
254 int64_t current_wait;
257 Observer::list observers;
259 boost::mutex sleeper_mutex;
260 boost::condition_variable_any sleep_threshhold;