| 1 | // **********************************************************************
 | 
|---|
| 2 | //
 | 
|---|
| 3 | // Copyright (c) 2000
 | 
|---|
| 4 | // Object Oriented Concepts, Inc.
 | 
|---|
| 5 | // Billerica, MA, USA
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // All Rights Reserved
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // **********************************************************************
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #ifndef JTC_MONITOR_H
 | 
|---|
| 12 | #define JTC_MONITOR_H
 | 
|---|
| 13 | 
 | 
|---|
| 14 | //
 | 
|---|
| 15 | // A monitor is used to ensure that only one thread can have access to
 | 
|---|
| 16 | // the internals of an object at one time. Use the class
 | 
|---|
| 17 | // JTCSynchronized to automatically lock and unlock a monitor.
 | 
|---|
| 18 | //
 | 
|---|
| 19 | // The JTCMonitor class also provides an efficient way of notification
 | 
|---|
| 20 | // and suspension of threads. The wait method waits for a notification
 | 
|---|
| 21 | // by another thread. A thread may notify a waiting object with the
 | 
|---|
| 22 | // notify call. A thread may notify all waiting objects with the
 | 
|---|
| 23 | // notifyAll call.
 | 
|---|
| 24 | //
 | 
|---|
| 25 | class JTCMonitor
 | 
|---|
| 26 | {
 | 
|---|
| 27 |     //
 | 
|---|
| 28 |     // Hide copy constructor and assignment operator.
 | 
|---|
| 29 |     //
 | 
|---|
| 30 |     JTCMonitor(const JTCMonitor&);
 | 
|---|
| 31 |     void operator=(const JTCMonitor&);
 | 
|---|
| 32 | 
 | 
|---|
| 33 |     friend class JTCSynchronized;
 | 
|---|
| 34 | 
 | 
|---|
| 35 |     //
 | 
|---|
| 36 |     // Validate that the monitor is locked by the calling thread.
 | 
|---|
| 37 |     //
 | 
|---|
| 38 |     void validateMutexOwner(const char*) const;
 | 
|---|
| 39 | 
 | 
|---|
| 40 |     //
 | 
|---|
| 41 |     // Lock the monitors mutex. If internal is set the do not check
 | 
|---|
| 42 |     // the threads exit status.
 | 
|---|
| 43 |     //
 | 
|---|
| 44 |     void lock(bool) const;
 | 
|---|
| 45 | 
 | 
|---|
| 46 |     //
 | 
|---|
| 47 |     // Unlock the monitors mutex.
 | 
|---|
| 48 |     //
 | 
|---|
| 49 |     void unlock() const;
 | 
|---|
| 50 | 
 | 
|---|
| 51 |     JTCRecursiveMutex monMutex_; // Monitors mutex.
 | 
|---|
| 52 |     JTCCond monCond_; // Monitors condition variable.
 | 
|---|
| 53 | 
 | 
|---|
| 54 | #ifdef HAVE_JTC_STOP
 | 
|---|
| 55 |     //
 | 
|---|
| 56 |     // Was the mutex aquired by an internal JTC method?
 | 
|---|
| 57 |     //
 | 
|---|
| 58 |     bool internalLock_;
 | 
|---|
| 59 | #endif
 | 
|---|
| 60 | 
 | 
|---|
| 61 | public:
 | 
|---|
| 62 | 
 | 
|---|
| 63 |     JTCMonitor();
 | 
|---|
| 64 |     virtual ~JTCMonitor();
 | 
|---|
| 65 | 
 | 
|---|
| 66 |     //
 | 
|---|
| 67 |     // Wait for notification.
 | 
|---|
| 68 |     //
 | 
|---|
| 69 |     void wait();
 | 
|---|
| 70 | 
 | 
|---|
| 71 |     //
 | 
|---|
| 72 |     // Wait for notification, or timeout milliseconds.
 | 
|---|
| 73 |     //
 | 
|---|
| 74 |     void wait(long);
 | 
|---|
| 75 |         
 | 
|---|
| 76 |     //
 | 
|---|
| 77 |     // Wake one waiting object.
 | 
|---|
| 78 |     //
 | 
|---|
| 79 |     void notify();
 | 
|---|
| 80 | 
 | 
|---|
| 81 |     //
 | 
|---|
| 82 |     // Wake all waiting objects.
 | 
|---|
| 83 |     //
 | 
|---|
| 84 |     void notifyAll();
 | 
|---|
| 85 | };
 | 
|---|
| 86 | 
 | 
|---|
| 87 | #endif
 | 
|---|