A barrier is a synchronization tool which blocks until a given number of threads have reached the barrier. More...
#include <core/threading/barrier.h>
Public Member Functions | |
InterruptibleBarrier (unsigned int count) | |
Constructor. | |
InterruptibleBarrier (Mutex *mutex, unsigned int count) | |
Constructor with custom mutex. | |
virtual | ~InterruptibleBarrier () |
Destructor. | |
bool | wait (unsigned int timeout_sec, unsigned int timeout_nanosec) |
Wait for other threads. | |
virtual void | wait () |
Wait for other threads. | |
void | interrupt () throw () |
Interrupt the barrier. | |
void | reset () throw () |
Clears the barrier. | |
RefPtr< ThreadList > | passed_threads () |
Get a list of threads that passed the barrier. |
A barrier is a synchronization tool which blocks until a given number of threads have reached the barrier.
This particular implementations allows for giving a timeout after which the waiting is aborted.
For general information when a barrier is useful see the Barrier class.
Additionally to the general barrier features the InterruptibleBarrier::wait() can be given a timeout after which the waiting is aborted. Since the POSIX standard does not provide a timed wait for barriers this implementation uses a Mutex and WaitCondition internally to achieve the desired result.
Definition at line 38 of file interruptible_barrier.h.
fawkes::InterruptibleBarrier::InterruptibleBarrier | ( | unsigned int | count | ) |
Constructor.
count | the number of threads to wait for |
Definition at line 90 of file interruptible_barrier.cpp.
References fawkes::Barrier::_count.
fawkes::InterruptibleBarrier::InterruptibleBarrier | ( | Mutex * | mutex, | |
unsigned int | count | |||
) |
Constructor with custom mutex.
Use this constructor only if you really know what you are doing. This constructor allows to pass a mutex that is used internally for the barrier. Note that in this case it is your duty to lock the mutex before the wait() and unlock afterwards! It combines features of a barrier and a wait condition.
mutex | Mutex to use | |
count | the number of threads to wait for |
Definition at line 114 of file interruptible_barrier.cpp.
References fawkes::Barrier::_count.
fawkes::InterruptibleBarrier::~InterruptibleBarrier | ( | ) | [virtual] |
Destructor.
Definition at line 130 of file interruptible_barrier.cpp.
void fawkes::InterruptibleBarrier::interrupt | ( | ) | throw () |
Interrupt the barrier.
This will cause all threads currently waiting on the barrier to throw an exception and no further thread will wait. You have to call reset() the before you use this barrier the next time.
Definition at line 156 of file interruptible_barrier.cpp.
Referenced by FawkesMainThread::set_mainloop_thread().
RefPtr< ThreadList > fawkes::InterruptibleBarrier::passed_threads | ( | ) |
Get a list of threads that passed the barrier.
The list contains the threads that passed the barrier. With some book keeping outside of the barrier you can determine which threads you expected at the barrier but did not pass it.
Definition at line 143 of file interruptible_barrier.cpp.
Referenced by fawkes::ThreadList::wakeup_and_wait().
void fawkes::InterruptibleBarrier::reset | ( | void | ) | throw () |
Clears the barrier.
Call this method when you want to use the barrier the next time after an interrupt or timeout occured. Make sure all threads that should have passed the barrier the last time did pass it.
Definition at line 171 of file interruptible_barrier.cpp.
References fawkes::Barrier::_count, and fawkes::RefPtr< T_CppObject >::clear().
virtual void fawkes::InterruptibleBarrier::wait | ( | ) | [inline, virtual] |
Wait for other threads.
This method will block until as many threads have called wait as you have given count to the constructor.
Reimplemented from fawkes::Barrier.
Definition at line 46 of file interruptible_barrier.h.
References wait().
Referenced by wait().
bool fawkes::InterruptibleBarrier::wait | ( | unsigned int | timeout_sec, | |
unsigned int | timeout_nanosec | |||
) |
Wait for other threads.
This method will block until as many threads have called wait as you have given count to the constructor. Note that if the barrier is interrupted or times out you need to call reset() to get the barrier into a re-usable state. It is your duty to make sure that all threads using the barrier are in a cohesive state.
timeout_sec | relative timeout in seconds, added to timeout_nanosec | |
timeout_nanosec | timeout in nanoseconds |
InterruptedException | thrown if the barrier was forcefully interrupted by calling interrupt(). |
Definition at line 196 of file interruptible_barrier.cpp.
References fawkes::Barrier::_count, fawkes::RefPtr< T_CppObject >::clear(), fawkes::Thread::current_thread(), and fawkes::Exception::print_trace().
Referenced by FawkesMainThread::loop(), fawkes::NetworkConfiguration::set_mirror_mode(), and fawkes::ThreadList::wakeup_and_wait().