monitor.h

Go to the documentation of this file.
00001 
00014 #ifndef _DLR_THREAD_MONITOR_H_
00015 #define _DLR_THREAD_MONITOR_H_
00016 
00017 #include <vector>
00018 #include <boost/thread.hpp>
00019 #include <dlrCommon/referenceCount.h>
00020 
00021 namespace dlr {
00022 
00023   namespace thread {
00024 
00025     // Forward declarations.
00026     class Monitor;
00027     class Condition;
00028 
00029     
00037     class Token {
00038       friend class Monitor;
00039       friend class Condition;
00040       
00041     public:
00042       Token();
00043       Token(const Token& other);
00044       ~Token();
00045       operator bool();
00046 
00047     private:
00048       Token(boost::timed_mutex& mutex);
00049       Token(boost::timed_mutex& mutex, double timeout);
00050       boost::timed_mutex::scoped_lock& getLock();
00051       boost::timed_mutex::scoped_timed_lock& getTimedLock();
00052       bool isTimed();
00053       Token& operator=(const Token& other);
00054 
00055       bool m_isTimed;
00056       boost::timed_mutex::scoped_lock* m_lockPtr;
00057       boost::timed_mutex::scoped_timed_lock* m_timedLockPtr;
00058 
00059       // We use ReferenceCount directly, rather than through a
00060       // pointer, because Token objects are never expected to be
00061       // shared between threads, so thread isn't an issue.
00062       ReferenceCount m_referenceCount;
00063     };
00064 
00065     
00072     class Condition {
00073       friend class Monitor;
00074     public:
00075       Condition(const Condition& other);
00076       ~Condition();
00077       Condition& operator=(const Condition& other);
00078 
00079     private:
00080       Condition(boost::condition* conditionPtr);
00081       void signalOne();
00082       void signalAll();
00083       void wait(Token& token);
00084       bool wait(Token& token, double timeout);
00085 
00086       boost::condition* m_conditionPtr;
00087     };
00088 
00089     
00090 
00223     class Monitor
00224     {
00225     public:
00226 
00230       Monitor();
00231 
00232 
00241       Monitor(const Monitor& other);
00242 
00243 
00247       virtual
00248       ~Monitor();
00249 
00250 
00260       Monitor&
00261       operator=(const Monitor& other);
00262 
00263 
00274       void
00275       makeCopyable(bool copyableFlag=true) {m_isCopyable = copyableFlag;}
00276 
00277     protected:
00278 
00287       void
00288       copyOther(const Monitor& other);
00289 
00290       
00315       Condition
00316       createCondition(bool doInitialize = true);
00317 
00318 
00328       Token
00329       getToken() {return Token(*m_tokenMutexPtr);}
00330 
00331       
00341       Token
00342       getToken(double timeout) {return Token(*m_tokenMutexPtr, timeout);}
00343 
00344 
00349       void
00350       releaseResources();
00351 
00352       
00368       void
00369       signalOne(Condition& condition) {condition.signalOne();}
00370 
00371       
00382       void
00383       signalAll(Condition& condition) {condition.signalAll();}
00384 
00385       
00399       void
00400       wait(Condition& condition, Token& token) {condition.wait(token);}
00401         
00402 
00423       bool
00424       wait(Condition& condition, Token& token, double timeout) {
00425         return condition.wait(token, timeout);
00426       }
00427 
00428 
00429     private:
00430 
00431       // We need a mutex in order to implement the virtual token.
00432       boost::timed_mutex* m_tokenMutexPtr;
00433 
00434       // This vector lets us manage deletion of condition objects
00435       // explicitly, rather than relying on the Condition class to do
00436       // so.  This is important because the Condition class destructor
00437       // is not always called in a protected section of code.
00438       std::vector<boost::condition*>* m_boostConditionVectorPtr;
00439       
00440       // This bool indicates whether or not copying should be allowed.
00441       bool m_isCopyable;
00442       
00443       // Note that thread-safety requires us to use a pointer here.
00444       size_t* m_referenceCountPtr;
00445     };
00446 
00447   } // namespace thread
00448 
00449 } // namespace dlr
00450 
00451 #endif /* #ifndef _DLR_THREAD_MONITOR_H_ */

Generated on Wed Jun 25 13:47:22 2008 for dlrUtilities Utility Library by  doxygen 1.5.5