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

FairMutexWithCancel.h

Go to the documentation of this file.
00001 #ifndef FAIRMUTEXWITHCANCEL_H 00002 #define FAIRMUTEXWITHCANCEL_H 00003 00004 #include <PtMutex.h> 00005 #include <PtCondition.h> 00006 #include <list> 00007 #include <exception> 00008 00009 BOREALIS_NAMESPACE_BEGIN; 00010 00011 /*** NOTE: THIS CODE BUILDS, BUT HAS NOT BEEN UNIT TESTED!!! -CJC ***/ 00012 00013 /* 00014 Like a regular mutex, but it can also be cancelled. Cancellation is a 00015 permanent state. 00016 00017 A thread waiting on this mutex can be awakened because it was granted 00018 the mutex, or because the mutex was cancelled. 00019 00020 Waiters are granted the mutex first-come-first-serve. 00021 00022 This is actually a lot like a cancellable version of FifoCriticalSection, 00023 but it doesn't implement ILockable (since lock() can fail under normal 00024 circumstances). 00025 */ 00026 00027 class FairMutexWithCancel 00028 { 00029 public: 00030 FairMutexWithCancel(); 00031 virtual ~FairMutexWithCancel(); 00032 00033 // Acquires the mutex. Returns true if the mutex was granted, false if 00034 // the mutex has been cancelled. 00035 bool lock() 00036 throw (exception); 00037 00038 // A thread holding the lock must call this exactly once to release it, 00039 // if/when it's OK for other threads to acquire the lock. 00040 void unlock() 00041 throw (exception); 00042 00043 // Forever marks the mutex as cancelled, and causes all pending and future 00044 // invocations of lock() to return 'false'. 00045 void cancel() 00046 throw (exception); 00047 00048 private: 00049 PtMutex _mtx; 00050 bool _cancelled; 00051 00052 // Owner of the condition at the front of the list is the thread that 00053 // currently holds this lock. 00054 list<PtCondition*> _alarmClocks; 00055 00056 // Iff (! _alarmClocks.empty()), then either: 00057 // (A) this is the thread that's currently holding the lock, or 00058 // (B) this is the thread that previously held the lock, and the thread 00059 // that is to next hold the lock is now at the front of _alarmClocks. 00060 // We just use it for double-checking that the thread calling the "unlock()" 00061 // method is the thread holding the lock. 00062 pthread_t _currentHolder; 00063 }; 00064 00065 BOREALIS_NAMESPACE_END; 00066 00067 #endif

Generated on Fri Nov 12 15:15:21 2004 for Borealis by doxygen 1.3.8