Fawkes API  Fawkes Development Version
fawkes::InterruptibleBarrier Class Reference

A barrier is a synchronization tool which blocks until a given number of threads have reached the barrier. More...

#include <>>

Inheritance diagram for fawkes::InterruptibleBarrier:

Public Member Functions

 InterruptibleBarrier (unsigned int count)
 Constructor. More...
 
 InterruptibleBarrier (Mutex *mutex, unsigned int count)
 Constructor with custom mutex. More...
 
virtual ~InterruptibleBarrier ()
 Destructor. More...
 
bool wait (unsigned int timeout_sec, unsigned int timeout_nanosec)
 Wait for other threads. More...
 
virtual void wait ()
 Wait for other threads. More...
 
void interrupt () throw ()
 Interrupt the barrier. More...
 
void reset () throw ()
 Clears the barrier. More...
 
RefPtr< ThreadListpassed_threads ()
 Get a list of threads that passed the barrier. More...
 
bool no_threads_in_wait ()
 Checks if there are no more threads in the wait() function. More...
 
- Public Member Functions inherited from fawkes::Barrier
 Barrier (unsigned int count)
 Constructor. More...
 
virtual ~Barrier ()
 Destructor. More...
 
unsigned int count ()
 Get number of threads this barrier will wait for. More...
 

Additional Inherited Members

- Protected Member Functions inherited from fawkes::Barrier
 Barrier ()
 Protected Constructor. More...
 
- Protected Attributes inherited from fawkes::Barrier
unsigned int _count
 Number of threads that are expected to wait for the barrier. More...
 

Detailed Description

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.

See also
Barrier
Author
Tim Niemueller

Definition at line 38 of file interruptible_barrier.h.

Constructor & Destructor Documentation

◆ InterruptibleBarrier() [1/2]

fawkes::InterruptibleBarrier::InterruptibleBarrier ( unsigned int  count)

Constructor.

Parameters
countthe number of threads to wait for

Definition at line 90 of file interruptible_barrier.cpp.

References fawkes::Barrier::_count, and fawkes::Barrier::count().

Referenced by InterruptibleBarrier(), and wait().

◆ InterruptibleBarrier() [2/2]

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.

Parameters
mutexMutex to use
countthe number of threads to wait for

Definition at line 115 of file interruptible_barrier.cpp.

References fawkes::Barrier::_count, fawkes::Barrier::Barrier(), fawkes::Barrier::count(), and InterruptibleBarrier().

◆ ~InterruptibleBarrier()

fawkes::InterruptibleBarrier::~InterruptibleBarrier ( )
virtual

Destructor.

Definition at line 179 of file interruptible_barrier.cpp.

Member Function Documentation

◆ interrupt()

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 205 of file interruptible_barrier.cpp.

Referenced by fawkes::FawkesMainThread::set_mainloop_thread(), and wait().

◆ no_threads_in_wait()

bool fawkes::InterruptibleBarrier::no_threads_in_wait ( )

Checks if there are no more threads in the wait() function.

This method is used to prevent the destruction of the barrier while there are threads in wait().

Returns
true, if no thread currently is in wait()

Definition at line 327 of file interruptible_barrier.cpp.

Referenced by fawkes::ThreadList::erase(), fawkes::ThreadList::set_maintain_barrier(), and wait().

◆ passed_threads()

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.

Returns
refptr to list of threads that passed the barrier.

Definition at line 192 of file interruptible_barrier.cpp.

Referenced by wait(), and fawkes::ThreadList::wakeup_and_wait().

◆ reset()

void fawkes::InterruptibleBarrier::reset ( )
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 220 of file interruptible_barrier.cpp.

References fawkes::Barrier::_count.

Referenced by wait().

◆ wait() [1/2]

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.

Parameters
timeout_secrelative timeout in seconds, added to timeout_nanosec
timeout_nanosectimeout in nanoseconds
Returns
true, if the barrier was properly reached, false if the barrier timeout was reached and the wait did not finish properly.
Exceptions
InterruptedExceptionthrown if the barrier was forcefully interrupted by calling interrupt().

Definition at line 245 of file interruptible_barrier.cpp.

References fawkes::Barrier::_count, fawkes::Thread::current_thread(), fawkes::Exception::print_trace(), and fawkes::Barrier::wait().

Referenced by fawkes::FawkesMainThread::loop(), and fawkes::ThreadList::wakeup_and_wait().

◆ wait() [2/2]

virtual void fawkes::InterruptibleBarrier::wait ( )
inlinevirtual

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 interrupt(), InterruptibleBarrier(), no_threads_in_wait(), passed_threads(), reset(), and wait().

Referenced by wait().


The documentation for this class was generated from the following files: