| 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/Sync.h> | 
|---|
| 18 | #include <JTC/Exception.h> | 
|---|
| 19 |  | 
|---|
| 20 | #include <errno.h> | 
|---|
| 21 |  | 
|---|
| 22 | #ifdef HAVE_STD_IOSTREAM | 
|---|
| 23 | using namespace std; | 
|---|
| 24 | #endif | 
|---|
| 25 |  | 
|---|
| 26 | // | 
|---|
| 27 | // This class encapsulates the functionality of a WIN32 event | 
|---|
| 28 | // semaphore. | 
|---|
| 29 | // | 
|---|
| 30 |  | 
|---|
| 31 | // ---------------------------------------------------------------------- | 
|---|
| 32 | // JTCEvent constructor and destructor | 
|---|
| 33 | // ---------------------------------------------------------------------- | 
|---|
| 34 |  | 
|---|
| 35 | JTCEvent::JTCEvent() | 
|---|
| 36 |  | 
|---|
| 37 | #ifndef WIN32 | 
|---|
| 38 | : posted_(false) | 
|---|
| 39 | #endif | 
|---|
| 40 |  | 
|---|
| 41 | { | 
|---|
| 42 | #if defined(HAVE_POSIX_THREADS) | 
|---|
| 43 | JTC_SYSCALL_2(pthread_cond_init, &cond_, 0, != 0) | 
|---|
| 44 | #endif | 
|---|
| 45 |  | 
|---|
| 46 | #if defined(HAVE_DCE_THREADS) | 
|---|
| 47 | JTC_SYSCALL_2(pthread_cond_init, &cond_, pthread_condattr_default, != 0) | 
|---|
| 48 | #endif | 
|---|
| 49 |  | 
|---|
| 50 | #if defined(HAVE_WIN32_THREADS) | 
|---|
| 51 | JTC_SYSCALL_4(event_ = CreateEvent, 0, 1, 0, 0, == INVALID_HANDLE_VALUE) | 
|---|
| 52 | #endif | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | JTCEvent::~JTCEvent() | 
|---|
| 56 | { | 
|---|
| 57 | #if defined(HAVE_POSIX_THREADS) || defined(HAVE_DCE_THREADS) | 
|---|
| 58 | pthread_cond_destroy(&cond_); | 
|---|
| 59 | #endif | 
|---|
| 60 |  | 
|---|
| 61 | #if defined(HAVE_WIN32_THREADS) | 
|---|
| 62 | CloseHandle(event_); | 
|---|
| 63 | #endif | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | // ---------------------------------------------------------------------- | 
|---|
| 67 | // JTCEvent public member implementation | 
|---|
| 68 | // ---------------------------------------------------------------------- | 
|---|
| 69 |  | 
|---|
| 70 | // | 
|---|
| 71 | // Set the state of the event object to signalled. Wake any waiting | 
|---|
| 72 | // threads. | 
|---|
| 73 | // | 
|---|
| 74 | void | 
|---|
| 75 | JTCEvent::post() | 
|---|
| 76 | { | 
|---|
| 77 | #if defined(HAVE_POSIX_THREADS) || defined(HAVE_DCE_THREADS) | 
|---|
| 78 | JTCSynchronized sync(mut_); | 
|---|
| 79 | posted_ = true; | 
|---|
| 80 |  | 
|---|
| 81 | // | 
|---|
| 82 | // Wake up all waiters. | 
|---|
| 83 | // | 
|---|
| 84 | JTC_SYSCALL_1(pthread_cond_broadcast, &cond_, != 0) | 
|---|
| 85 | #endif | 
|---|
| 86 |  | 
|---|
| 87 | #if defined(HAVE_WIN32_THREADS) | 
|---|
| 88 | JTC_SYSCALL_1(SetEvent, event_, == 0) | 
|---|
| 89 | #endif | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | // | 
|---|
| 93 | // Wake any waiting threads. | 
|---|
| 94 | // | 
|---|
| 95 | void | 
|---|
| 96 | JTCEvent::pulse() | 
|---|
| 97 | { | 
|---|
| 98 | #if defined(HAVE_POSIX_THREADS) || defined(HAVE_DCE_THREADS) | 
|---|
| 99 | JTCSynchronized sync(mut_); | 
|---|
| 100 |  | 
|---|
| 101 | posted_ = false; | 
|---|
| 102 |  | 
|---|
| 103 | // | 
|---|
| 104 | // Wake up one waiter. | 
|---|
| 105 | // | 
|---|
| 106 | JTC_SYSCALL_1(pthread_cond_signal, &cond_, != 0) | 
|---|
| 107 | #endif | 
|---|
| 108 | #if defined(HAVE_WIN32_THREADS) | 
|---|
| 109 | JTC_SYSCALL_1(PulseEvent, event_, == 0) | 
|---|
| 110 | #endif | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | // | 
|---|
| 114 | // Wait for the event object to become signalled. | 
|---|
| 115 | // | 
|---|
| 116 | void | 
|---|
| 117 | JTCEvent::wait() | 
|---|
| 118 | { | 
|---|
| 119 | #if defined(HAVE_POSIX_THREADS) || defined(HAVE_DCE_THREADS) | 
|---|
| 120 | // | 
|---|
| 121 | // We want to lock the internal mutex. If we're not currently | 
|---|
| 122 | // posted then wait on the condition variable. next unlock the | 
|---|
| 123 | // internal mutex, | 
|---|
| 124 | // | 
|---|
| 125 | JTCSynchronized sync(mut_); | 
|---|
| 126 | if(!posted_) | 
|---|
| 127 | { | 
|---|
| 128 | for(;;) | 
|---|
| 129 | { | 
|---|
| 130 | try | 
|---|
| 131 | { | 
|---|
| 132 | JTC_SYSCALL_2(pthread_cond_wait, &cond_, &mut_.lock_, < 0) | 
|---|
| 133 | } | 
|---|
| 134 | catch(const JTCSystemCallException& e) | 
|---|
| 135 | { | 
|---|
| 136 | if(e.getError() == EINTR) | 
|---|
| 137 | continue; | 
|---|
| 138 | throw; | 
|---|
| 139 | } | 
|---|
| 140 | break; | 
|---|
| 141 | } | 
|---|
| 142 | } | 
|---|
| 143 | #endif | 
|---|
| 144 |  | 
|---|
| 145 | #if defined(HAVE_WIN32_THREADS) | 
|---|
| 146 | JTC_SYSCALL_2(WaitForSingleObject,event_, INFINITE, != WAIT_OBJECT_0) | 
|---|
| 147 | #endif | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | // | 
|---|
| 151 | // Set the state of the event object to unsignalled. | 
|---|
| 152 | // | 
|---|
| 153 | void | 
|---|
| 154 | JTCEvent::reset() | 
|---|
| 155 | { | 
|---|
| 156 | #if defined(HAVE_POSIX_THREADS) || defined(HAVE_DCE_THREADS) | 
|---|
| 157 | JTCSynchronized sync(mut_); | 
|---|
| 158 | posted_ = false; | 
|---|
| 159 | #endif | 
|---|
| 160 |  | 
|---|
| 161 | #if defined(HAVE_WIN32_THREADS) | 
|---|
| 162 | JTC_SYSCALL_1(ResetEvent, event_, == 0) | 
|---|
| 163 | #endif | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | // | 
|---|
| 167 | // Determine if the event object is signalled. | 
|---|
| 168 | // | 
|---|
| 169 | bool | 
|---|
| 170 | JTCEvent::posted() | 
|---|
| 171 | { | 
|---|
| 172 | #if defined(HAVE_POSIX_THREADS) || defined(HAVE_DCE_THREADS) | 
|---|
| 173 | JTCSynchronized sync(mut_); | 
|---|
| 174 | return posted_; | 
|---|
| 175 | #endif | 
|---|
| 176 |  | 
|---|
| 177 | #if defined(HAVE_WIN32_THREADS) | 
|---|
| 178 | long rc; | 
|---|
| 179 | JTC_SYSCALL_2(rc = WaitForSingleObject, event_, 0, == WAIT_FAILED) | 
|---|
| 180 | return rc != WAIT_TIMEOUT; | 
|---|
| 181 | #endif | 
|---|
| 182 | } | 
|---|