1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Brian Aker
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
4
* Copyright (c) 2010, Brian Aker
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.
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.
21
31
#include <boost/thread/mutex.hpp>
45
57
// Barrier starts in a blocking posistion
48
typedef boost::shared_ptr<observer_st> shared_ptr;
49
typedef std::list <shared_ptr> list;
55
boost::condition_variable_any cond;
57
observer_st(int64_t wait_until_arg, int64_t generation_arg) :
59
waiting_for(wait_until_arg),
60
generation(generation_arg)
77
boost::mutex::scoped_lock mutex;
90
60
typedef boost::shared_ptr<Barrier> shared_ptr;
140
110
checkObservers();
142
while (my_generation == generation)
144
sleep_threshhold.wait(sleeper_mutex);
112
// If we are interrupted we remove ourself from the list, and check on
116
while (my_generation == generation)
118
sleep_threshhold.wait(sleeper_mutex);
121
catch(boost::thread_interrupted const& error)
148
128
// A call to either signal or a release will cause wait_for() to continue
149
129
void wait_until(int64_t wait_until_arg)
151
observer_st::shared_ptr observer;
131
Observer::shared_ptr observer;
153
133
boost::mutex::scoped_lock scopedLock(sleeper_mutex);
155
135
if (wait_until_arg <= count())
158
observer.reset(new observer_st(wait_until_arg, generation));
138
observer.reset(new Observer(wait_until_arg));
159
139
observers.push_back(observer);
145
catch(boost::thread_interrupted const& error)
147
boost::mutex::scoped_lock scopedLock(sleeper_mutex);
148
// Someone has interrupted us, we now try to remove ourself from the
149
// observer chain ourself
151
observers.remove(observer);
164
157
void wait(int64_t generation_arg)