Main Page | Namespace List | Class List | File List | Namespace Members | Class Members | File Members

monitor.h

Go to the documentation of this file.
00001 #ifndef DV_THREAD_MONITOR_H
00002 #define DV_THREAD_MONITOR_H
00003 // $Id: monitor.h,v 1.11 2006/06/20 17:50:45 dvermeir Exp $
00004 
00005 #include <string>
00006 #include <iostream>
00007 #include <stdexcept>
00008 extern "C" {
00009 #include <pthread.h> // posix thread library
00010 }
00011 #include <sys/time.h>
00012 
00013 namespace Dv {
00014   namespace Thread {
00015     /**
00016      * A simple Monitor class using posix threads.
00017      *
00018      * The following defines a Buffer monitor with protected
00019      * get() and put() access.
00020      * @include buffer.h
00021      * 
00022      * @see Dv::Thread::Thread
00023      */
00024     class Monitor {
00025     public:
00026       /** 
00027        * Constructor.
00028        * @param name of this monitor.
00029        * @param n_conditions number of conditions in this monitor.
00030        * @param dbg if non-zero, debug output will be written to @a *dbg.
00031        */
00032       Monitor(const std::string& name, unsigned int n_conditions=0, std::ostream* dbg = 0);
00033       /**
00034        * Virtual destructor.
00035        */
00036       virtual ~Monitor();
00037       /**
00038        * Enter the monitor. 
00039        * @exception runtime_error if this thread already owns the monitor
00040        */
00041       void enter() throw (std::runtime_error);
00042       /**
00043        * Exit the monitor.
00044        * @exception runtime_error if this thread is not the current owner
00045        *   of the monitor
00046        */
00047       void exit() throw (std::runtime_error);
00048       /**
00049        * Unconditionally wait for a given condition.
00050        * @param condition number of the condition to wait for.
00051        * @exception runtime_error if this thread is not the current owner
00052        *   of the monitor
00053        */
00054       void wait(unsigned int condition=0) throw (std::runtime_error); 
00055       /**
00056        * Wait for a given condition or until timeout millisecs elapsed.
00057        *
00058        * @param condition number of the condition to wait for.
00059        * @param timeout number of milliseconds we are prepared to wait.
00060        * @return false if a timeout occured.
00061        * @exception runtime_error if this thread is not the current owner
00062        *   of the monitor
00063        */
00064       bool wait(unsigned int condition, time_t timeout) throw (std::runtime_error); 
00065       /**
00066        * Signal a given condition. This function unblocks at least one
00067        * thread blocked on the condition. The unblocked threads contend
00068        * for the monitor.
00069        *
00070        * @param condition to signal.
00071        * @exception runtime_error if this thread is not the current owner
00072        *   of the monitor
00073        */
00074       void signal(unsigned int condition=0) const throw (std::runtime_error);
00075       /**
00076        * Signal all threads waiting on this condition.
00077        * This function unblocks all the threads blocked on the condition.
00078        * The unblocked threads contend for the monitor.
00079        *
00080        * @param condition to signal.
00081        * @exception runtime_error if this thread is not the current owner
00082        *   of the monitor
00083        */
00084       void broadcast(unsigned int condition=0) const throw (std::runtime_error); 
00085       /**
00086        * @return name of this monitor.
00087        */
00088       const std::string& name() const { return name_; }
00089     private:
00090       /**
00091        * Name of the monitor.
00092        */
00093       std::string name_; 
00094       /**
00095        * Thread that currently owns the monitor.
00096        */
00097       pthread_t owner_; 
00098       /**
00099        * Entry to the monitor is controlled by this mutex semaphore.
00100        */
00101       pthread_mutex_t mutex_;
00102       /**
00103        * Attributes of mutex_.
00104        */
00105       pthread_mutexattr_t mutex_attributes_;
00106       /**
00107        * Number of conditions of the monitor.
00108        */
00109       unsigned int n_conditions_; 
00110       /**
00111        * Array of n_conditions_ posix conditions of this monitor.
00112        */
00113       pthread_cond_t*  conditions_;
00114       pthread_condattr_t  condition_attributes_;
00115       /**
00116        * This functions throws a runtime_error if the current thread
00117        * is not the current owner of the monitor.
00118        *
00119        * @param message used as part of runtime_error.what()
00120        * @exception runtime_error if this thread is not the current owner
00121        *   of the monitor
00122        */
00123       void check(const std::string& message) const throw (std::runtime_error); 
00124       /**
00125        * This functions throws a runtime_error if n is not a valid
00126        * condition of this monitor or if the current thread does not
00127        * own the monitor.
00128        * @param msg used as part of runtime_error.what()
00129        * @param n number of condition to check
00130        * @exception runtime_error if this thread is not the current owner
00131        *   of the monitor
00132        */
00133       void check_cond(const std::string& msg,unsigned int n) const 
00134         throw (std::runtime_error); 
00135       /**
00136        * No copy ctor.
00137        */
00138       Monitor(const Monitor&); 
00139       /**
00140        * No assignment operator.
00141        */
00142       Monitor& operator=(const Monitor&); 
00143       /** Write debug output to stream if not 0 */
00144       std::ostream* dbg_;
00145     };
00146   }
00147 }
00148   
00149 #endif

dvthread-0.5.0 [22 June, 2006]