| 1 | // ********************************************************************** | 
|---|
| 2 | // | 
|---|
| 3 | // Copyright (c) 2000 | 
|---|
| 4 | // Object Oriented Concepts, Inc. | 
|---|
| 5 | // Billerica, MA, USA | 
|---|
| 6 | // | 
|---|
| 7 | // All Rights Reserved | 
|---|
| 8 | // | 
|---|
| 9 | // ********************************************************************** | 
|---|
| 10 |  | 
|---|
| 11 | #include <JTC/Types.h> | 
|---|
| 12 | #include <JTC/Syscall.h> | 
|---|
| 13 | #include <JTC/Mutex.h> | 
|---|
| 14 | #include <JTC/Event.h> | 
|---|
| 15 | #include <JTC/Cond.h> | 
|---|
| 16 | #include <JTC/Monitor.h> | 
|---|
| 17 | #include <JTC/Exception.h> | 
|---|
| 18 | #include <JTC/Handle.h> | 
|---|
| 19 | #include <JTC/HandleI.h> | 
|---|
| 20 | #include <JTC/Thread.h> | 
|---|
| 21 | #include <JTC/ThreadGroup.h> | 
|---|
| 22 | #include <JTC/Runnable.h> | 
|---|
| 23 | #include <JTC/Sync.h> | 
|---|
| 24 |  | 
|---|
| 25 | #if defined(HAVE_STRSTREAM) | 
|---|
| 26 | #   include <strstream> | 
|---|
| 27 | #else | 
|---|
| 28 | #   if defined(HAVE_STRSTREA_H) | 
|---|
| 29 | #       include <strstrea.h> | 
|---|
| 30 | #   else | 
|---|
| 31 | #       include <strstream.h> | 
|---|
| 32 | #   endif | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | #ifdef HAVE_STD_IOSTREAM | 
|---|
| 36 | using namespace std; | 
|---|
| 37 | #endif | 
|---|
| 38 |  | 
|---|
| 39 | #include <stdlib.h> | 
|---|
| 40 |  | 
|---|
| 41 | // | 
|---|
| 42 | // Inlining the methods causes problems with CC 7.1 under SGI | 
|---|
| 43 | // | 
|---|
| 44 | #if defined(__sgi) | 
|---|
| 45 | JTCSynchronized::JTCSynchronized(const JTCMonitor& mon) | 
|---|
| 46 | : mon_(&mon), lockType_(LOCKED_MONITOR) | 
|---|
| 47 | { | 
|---|
| 48 | mon_ -> lock(false); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | JTCSynchronized::JTCSynchronized(const JTCMonitor& mon, bool internal) | 
|---|
| 52 | : mon_(&mon), lockType_(LOCKED_MONITOR) | 
|---|
| 53 | { | 
|---|
| 54 | mon_ -> lock(internal); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | JTCSynchronized::JTCSynchronized(const JTCMutex& m) | 
|---|
| 58 | : mut_(&m), lockType_(LOCKED_MUTEX) | 
|---|
| 59 | { | 
|---|
| 60 | mut_ -> lock(); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | JTCSynchronized::JTCSynchronized(const JTCRecursiveMutex& m) | 
|---|
| 64 | : nrmut_(&m), lockType_(LOCKED_RECURSIVE_MUTEX) | 
|---|
| 65 | { | 
|---|
| 66 | nrmut_ -> lock(); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | JTCSynchronized::~JTCSynchronized() | 
|---|
| 70 | { | 
|---|
| 71 | switch(lockType_) | 
|---|
| 72 | { | 
|---|
| 73 | case LOCKED_MONITOR: | 
|---|
| 74 | mon_ -> unlock(); | 
|---|
| 75 | break; | 
|---|
| 76 | case LOCKED_MUTEX: | 
|---|
| 77 | mut_ -> unlock(); | 
|---|
| 78 | break; | 
|---|
| 79 | case LOCKED_RECURSIVE_MUTEX: | 
|---|
| 80 | nrmut_ -> unlock(); | 
|---|
| 81 | break; | 
|---|
| 82 | } | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | #endif | 
|---|
| 86 |  | 
|---|
| 87 | // ---------------------------------------------------------------------- | 
|---|
| 88 | // JTCMonitor private member implementation | 
|---|
| 89 | // ---------------------------------------------------------------------- | 
|---|
| 90 |  | 
|---|
| 91 | // | 
|---|
| 92 | // Verify that the mutex is locked by the calling thread. If the mutex | 
|---|
| 93 | // is not locked by the calling thread then throw | 
|---|
| 94 | // JTCIllegalMonitorStateException. | 
|---|
| 95 | // | 
|---|
| 96 | void | 
|---|
| 97 | JTCMonitor::validateMutexOwner(const char* caller) const | 
|---|
| 98 | { | 
|---|
| 99 | if(monMutex_._JTC_getId() != JTCThreadId::self()) | 
|---|
| 100 | { | 
|---|
| 101 | char buf[1024]; | 
|---|
| 102 | ostrstream os(buf, 1024); | 
|---|
| 103 | os << "JTCMonitor::" | 
|---|
| 104 | << caller | 
|---|
| 105 | << " Thread: " | 
|---|
| 106 | << JTCThreadId::self() | 
|---|
| 107 | << " doesn't own mutex.  Owner: " | 
|---|
| 108 | << monMutex_._JTC_getId() << ends; | 
|---|
| 109 | throw JTCIllegalMonitorStateException(buf); | 
|---|
| 110 | } | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | // | 
|---|
| 114 | // Lock the monitors mutex. If internal is set then do not check the | 
|---|
| 115 | // running status of the current thread. | 
|---|
| 116 | // | 
|---|
| 117 | void | 
|---|
| 118 | JTCMonitor::lock(bool internal) const | 
|---|
| 119 | { | 
|---|
| 120 | #ifdef HAVE_JTC_STOP | 
|---|
| 121 | if (!internal) | 
|---|
| 122 | { | 
|---|
| 123 | JTCThread::_JTC_checkRunningStatus(); | 
|---|
| 124 | } | 
|---|
| 125 | ((JTCMonitor*)this) -> internalLock_ = internal; | 
|---|
| 126 | #endif | 
|---|
| 127 | monMutex_.lock(); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | // | 
|---|
| 131 | // Unlock the monitors mutex. If this was a lock placed by an internal | 
|---|
| 132 | // method of the JTC library then do not check the current threads | 
|---|
| 133 | // running status. | 
|---|
| 134 | // | 
|---|
| 135 | void | 
|---|
| 136 | JTCMonitor::unlock() const | 
|---|
| 137 | { | 
|---|
| 138 | #ifdef HAVE_JTC_STOP | 
|---|
| 139 | bool internalLock = internalLock_; | 
|---|
| 140 |  | 
|---|
| 141 | monMutex_.unlock(); | 
|---|
| 142 |  | 
|---|
| 143 | if (!internalLock) | 
|---|
| 144 | { | 
|---|
| 145 | JTCThread::_JTC_checkRunningStatus(); | 
|---|
| 146 | } | 
|---|
| 147 | #else | 
|---|
| 148 |  | 
|---|
| 149 | monMutex_.unlock(); | 
|---|
| 150 |  | 
|---|
| 151 | #endif | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | // ---------------------------------------------------------------------- | 
|---|
| 155 | // JTCMonitor constructor and destructor | 
|---|
| 156 | // ---------------------------------------------------------------------- | 
|---|
| 157 |  | 
|---|
| 158 | JTCMonitor::JTCMonitor() | 
|---|
| 159 | : monCond_(monMutex_) | 
|---|
| 160 | #ifdef HAVE_JTC_STOP | 
|---|
| 161 | , internalLock_(false) | 
|---|
| 162 | #endif | 
|---|
| 163 | { | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | JTCMonitor::~JTCMonitor() | 
|---|
| 167 | { | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | // ---------------------------------------------------------------------- | 
|---|
| 171 | // JTCMonitor public member implementation | 
|---|
| 172 | // ---------------------------------------------------------------------- | 
|---|
| 173 |  | 
|---|
| 174 | // | 
|---|
| 175 | // Wait for the monitor to be signalled. | 
|---|
| 176 | // | 
|---|
| 177 | void | 
|---|
| 178 | JTCMonitor::wait() | 
|---|
| 179 | { | 
|---|
| 180 | #ifdef HAVE_JTC_STOP | 
|---|
| 181 | // | 
|---|
| 182 | // Check the threads running status. | 
|---|
| 183 | // | 
|---|
| 184 | JTCThread::_JTC_checkRunningStatus(); | 
|---|
| 185 |  | 
|---|
| 186 | // | 
|---|
| 187 | // Verify that the monitor is locked by the calling thread. | 
|---|
| 188 | // | 
|---|
| 189 | validateMutexOwner("wait"); | 
|---|
| 190 |  | 
|---|
| 191 | // | 
|---|
| 192 | // Tell the current thread that it is waiting in a monitor. | 
|---|
| 193 | // | 
|---|
| 194 | JTCThread::_JTC_setMonitor(this); | 
|---|
| 195 |  | 
|---|
| 196 | try | 
|---|
| 197 | { | 
|---|
| 198 | monCond_.wait(); | 
|---|
| 199 | } | 
|---|
| 200 | catch(...) | 
|---|
| 201 | { | 
|---|
| 202 | // | 
|---|
| 203 | // The current thread is no longer in a monitor. Check the | 
|---|
| 204 | // running status. | 
|---|
| 205 | // | 
|---|
| 206 | JTCThread::_JTC_setMonitor(0); | 
|---|
| 207 | JTCThread::_JTC_checkRunningStatus(); | 
|---|
| 208 | throw; | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | // | 
|---|
| 212 | // The current thread is no longer in a monitor. Check the running | 
|---|
| 213 | // status. | 
|---|
| 214 | // | 
|---|
| 215 | JTCThread::_JTC_setMonitor(0); | 
|---|
| 216 | JTCThread::_JTC_checkRunningStatus(); | 
|---|
| 217 | #else | 
|---|
| 218 |  | 
|---|
| 219 | // | 
|---|
| 220 | // Verify that the monitor is locked by the calling thread. | 
|---|
| 221 | // | 
|---|
| 222 | validateMutexOwner("wait(long)"); | 
|---|
| 223 |  | 
|---|
| 224 | monCond_.wait(); | 
|---|
| 225 |  | 
|---|
| 226 | #endif | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 | // | 
|---|
| 230 | // Wait to be signalled for timeout milliseconds. | 
|---|
| 231 | // | 
|---|
| 232 | void | 
|---|
| 233 | JTCMonitor::wait(long timeout) | 
|---|
| 234 | { | 
|---|
| 235 | #ifdef HAVE_JTC_STOP | 
|---|
| 236 | // | 
|---|
| 237 | // Check the threads running status. | 
|---|
| 238 | // | 
|---|
| 239 | JTCThread::_JTC_checkRunningStatus(); | 
|---|
| 240 |  | 
|---|
| 241 | // | 
|---|
| 242 | // Verify that the monitor is locked by the calling thread. | 
|---|
| 243 | // | 
|---|
| 244 | validateMutexOwner("wait(long)"); | 
|---|
| 245 |  | 
|---|
| 246 | // | 
|---|
| 247 | // Tell the current thread that it is waiting in a monitor. | 
|---|
| 248 | // | 
|---|
| 249 | JTCThread::_JTC_setMonitor(this); | 
|---|
| 250 |  | 
|---|
| 251 | try | 
|---|
| 252 | { | 
|---|
| 253 | monCond_.wait(timeout); | 
|---|
| 254 | } | 
|---|
| 255 | catch(...) | 
|---|
| 256 | { | 
|---|
| 257 | // | 
|---|
| 258 | // The current thread is no longer in a monitor. Check the | 
|---|
| 259 | // running status. | 
|---|
| 260 | // | 
|---|
| 261 | JTCThread::_JTC_setMonitor(0); | 
|---|
| 262 | JTCThread::_JTC_checkRunningStatus(); | 
|---|
| 263 | throw; | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | // | 
|---|
| 267 | // The current thread is no longer in a monitor. Check the running | 
|---|
| 268 | // status. | 
|---|
| 269 | // | 
|---|
| 270 | // | 
|---|
| 271 | JTCThread::_JTC_setMonitor(0); | 
|---|
| 272 | JTCThread::_JTC_checkRunningStatus(); | 
|---|
| 273 | #else | 
|---|
| 274 |  | 
|---|
| 275 | // | 
|---|
| 276 | // Verify that the monitor is locked by the calling thread. | 
|---|
| 277 | // | 
|---|
| 278 | validateMutexOwner("wait(long)"); | 
|---|
| 279 |  | 
|---|
| 280 | monCond_.wait(timeout); | 
|---|
| 281 |  | 
|---|
| 282 | #endif | 
|---|
| 283 | } | 
|---|
| 284 |  | 
|---|
| 285 | // | 
|---|
| 286 | // Wake one waiting thread. | 
|---|
| 287 | // | 
|---|
| 288 | void | 
|---|
| 289 | JTCMonitor::notify() | 
|---|
| 290 | { | 
|---|
| 291 | // | 
|---|
| 292 | // Ensure that the monitors mutex is locked by the calling thread. | 
|---|
| 293 | // | 
|---|
| 294 | validateMutexOwner("notify"); | 
|---|
| 295 | monCond_.signal(); | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | // | 
|---|
| 299 | // Wake all waiting threads. | 
|---|
| 300 | // | 
|---|
| 301 | void | 
|---|
| 302 | JTCMonitor::notifyAll() | 
|---|
| 303 | { | 
|---|
| 304 | // | 
|---|
| 305 | // Ensure that the monitors mutex is locked by the calling thread. | 
|---|
| 306 | // | 
|---|
| 307 | validateMutexOwner("notifyAll"); | 
|---|
| 308 | monCond_.broadcast(); | 
|---|
| 309 | } | 
|---|
| 310 |  | 
|---|
| 311 |  | 
|---|