Dv::Thread::Monitor Class Reference

#include <monitor.h>

Inheritance diagram for Dv::Thread::Monitor:
Inheritance graph
[legend]
Collaboration diagram for Dv::Thread::Monitor:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Monitor (const std::string &name, unsigned int n_conditions=0, unsigned int min_debug_level=0, Debugable *debug_master=0)
 Constructor.
virtual ~Monitor ()
 Virtual destructor.
void enter (const std::string *msg=0)
 Enter the monitor.
void exit (const std::string *msg=0)
 Exit the monitor.
void wait (unsigned int condition=0)
 Unconditionally wait for a given condition.
bool wait (unsigned int condition, time_t timeout)
 Wait for a given condition or until timeout millisecs elapsed.
void signal (unsigned int condition=0) const
 Signal a given condition.
void broadcast (unsigned int condition=0) const
 Signal all threads waiting on this condition.
const std::string & name () const
pthread_t owner () const
bool owned_by_self () const

Private Member Functions

void check (const std::string &message) const
 This functions throws a std::runtime_error if the current thread is not the current owner of the monitor.
void check_cond (const std::string &msg, unsigned int n) const
 This functions throws a runtime_error if n is not a valid condition of this monitor or if the current thread does not own the monitor.
 Monitor (const Monitor &)
 No copy ctor.
Monitoroperator= (const Monitor &)
 No assignment operator.

Private Attributes

std::string name_
 Name of the monitor.
pthread_t owner_
 Thread that currently owns the monitor.
pthread_mutex_t mutex_
 Entry to the monitor is controlled by this mutex semaphore.
pthread_mutexattr_t mutex_attributes_
 Attributes of mutex_.
unsigned int n_conditions_
 Number of conditions of the monitor.
pthread_cond_t * conditions_
 Array of n_conditions_ posix conditions of this monitor.
pthread_condattr_t condition_attributes_

Detailed Description

Examples:

test-pool.C.


Constructor & Destructor Documentation

Dv::Thread::Monitor::Monitor ( const std::string &  name,
unsigned int  n_conditions = 0,
unsigned int  min_debug_level = 0,
Debugable debug_master = 0 
)

Constructor.

Parameters:
name of this monitor.
n_conditions number of conditions in this monitor.
min_debug_level if a debug_master is connected, logging info will only be written if the master's level is at least min_debug_level
debug_master from where debug info will be taken
See also:
Dv::DebugSlave
Examples:
test-buffer.h.
virtual Dv::Thread::Monitor::~Monitor (  )  [virtual]

Virtual destructor.

Dv::Thread::Monitor::Monitor ( const Monitor  )  [private]

No copy ctor.


Member Function Documentation

void Dv::Thread::Monitor::enter ( const std::string *  msg = 0  ) 

Enter the monitor.

Exceptions:
std::runtime_error if this thread already owns the monitor
void Dv::Thread::Monitor::exit ( const std::string *  msg = 0  ) 

Exit the monitor.

Exceptions:
std::runtime_error if this thread is not the current owner of the monitor
void Dv::Thread::Monitor::wait ( unsigned int  condition = 0  ) 

Unconditionally wait for a given condition.

Parameters:
condition number of the condition to wait for.
Exceptions:
std::runtime_error if this thread is not the current owner of the monitor
Examples:
test-buffer.h.

Referenced by Buffer::get(), Dv::Thread::MailBox< MessageReply >::get(), Buffer::put(), and Dv::Thread::Pool::Thread::wait().

bool Dv::Thread::Monitor::wait ( unsigned int  condition,
time_t  timeout 
)

Wait for a given condition or until timeout millisecs elapsed.

Parameters:
condition number of the condition to wait for.
timeout number of milliseconds we are prepared to wait.
Returns:
false if a timeout occured.
Exceptions:
std::runtime_error if this thread is not the current owner of the monitor
void Dv::Thread::Monitor::signal ( unsigned int  condition = 0  )  const

Signal a given condition.

This function unblocks at least one thread blocked on the condition. The unblocked threads contend for the monitor.

Parameters:
condition to signal.
Exceptions:
runtime_error if this thread is not the current owner of the monitor
Examples:
test-buffer.h.

Referenced by Buffer::get(), Buffer::put(), Dv::Thread::MailBox< MessageReply >::put(), and Dv::Thread::Pool::Thread::signal().

void Dv::Thread::Monitor::broadcast ( unsigned int  condition = 0  )  const

Signal all threads waiting on this condition.

This function unblocks all the threads blocked on the condition. The unblocked threads compete for the monitor.

Parameters:
condition to signal.
Exceptions:
runtime_error if this thread is not the current owner of the monitor
const std::string& Dv::Thread::Monitor::name (  )  const [inline]
Returns:
name of this monitor.

Reimplemented in Dv::Thread::Pool.

References name_.

Referenced by Dv::Thread::MailBox< MessageReply >::get(), and Dv::Thread::MailBox< MessageReply >::put().

pthread_t Dv::Thread::Monitor::owner (  )  const [inline]
Returns:
id of thread currently owning the monitor

References owner_.

Referenced by owned_by_self().

bool Dv::Thread::Monitor::owned_by_self (  )  const [inline]
Returns:
true iff the current thread is the owner of the monitor.

References owner().

void Dv::Thread::Monitor::check ( const std::string &  message  )  const [private]

This functions throws a std::runtime_error if the current thread is not the current owner of the monitor.

Parameters:
message used as part of runtime_error.what()
Exceptions:
std::runtime_error if this thread is not the current owner of the monitor
void Dv::Thread::Monitor::check_cond ( const std::string &  msg,
unsigned int  n 
) const [private]

This functions throws a runtime_error if n is not a valid condition of this monitor or if the current thread does not own the monitor.

Parameters:
msg used as part of runtime_error.what()
n number of condition to check
Exceptions:
std::runtime_error if this thread is not the current owner of the monitor
Monitor& Dv::Thread::Monitor::operator= ( const Monitor  )  [private]

No assignment operator.

Reimplemented in Dv::Thread::Pool.


Member Data Documentation

std::string Dv::Thread::Monitor::name_ [private]

Name of the monitor.

Reimplemented in Dv::Thread::Pool.

Referenced by name().

pthread_t Dv::Thread::Monitor::owner_ [private]

Thread that currently owns the monitor.

Reimplemented in Dv::Thread::logstream::LogFilter.

Referenced by owner().

pthread_mutex_t Dv::Thread::Monitor::mutex_ [private]

Entry to the monitor is controlled by this mutex semaphore.

pthread_mutexattr_t Dv::Thread::Monitor::mutex_attributes_ [private]

Attributes of mutex_.

unsigned int Dv::Thread::Monitor::n_conditions_ [private]

Number of conditions of the monitor.

pthread_cond_t* Dv::Thread::Monitor::conditions_ [private]

Array of n_conditions_ posix conditions of this monitor.

pthread_condattr_t Dv::Thread::Monitor::condition_attributes_ [private]

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

dvthread-0.13.4 [11 December, 2009]