1 /*
2 * Copyright (c) 2005, Eric Crahen
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is furnished
9 * to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22
23 #include "Monitor.h"
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 using namespace ZThread;
30
Monitor()31 Monitor::Monitor() : _owner(0), _waiting(false) {
32
33 _handle = ::CreateEvent(0, TRUE, FALSE, 0);
34 if(_handle == NULL) {
35 assert(0);
36 }
37
38 }
39
~Monitor()40 Monitor::~Monitor() {
41
42 assert(!_waiting);
43
44 ::CloseHandle(_handle);
45
46 }
47
wait(unsigned long ms)48 Monitor::STATE Monitor::wait(unsigned long ms) {
49
50 // Update the owner on first use. The owner will not change, each
51 // thread waits only on a single Monitor and a Monitor is never
52 // shared
53 if(_owner == 0)
54 _owner = ::GetCurrentThreadId();
55
56 STATE state; //(INVALID);
57
58 // Serialize access to the state of the Monitor
59 // and test the state to determine if a wait is needed.
60 _waitLock.acquire();
61
62 if(pending(ANYTHING)) {
63
64 // Return without waiting when possible
65 state = next();
66
67 _waitLock.release();
68 return state;
69
70 }
71 // Unlock the external lock if a wait() is probably needed.
72 // Access to the state is still serial.
73 _lock.release();
74
75 // Wait for a transition in the state that is of interest, this
76 // allows waits to exclude certain flags (e.g. INTERRUPTED)
77 // for a single wait() w/o actually discarding those flags -
78 // they will remain set until a wait interested in those flags
79 // occurs.
80 // if(!currentState(interest)) {
81
82 // Wait, ignoring signals
83 _waiting = true;
84
85 // Block until the event is set.
86 _waitLock.release();
87
88 // The event is manual reset so this lack of atmoicity will not
89 // be an issue
90
91 DWORD dwResult =
92 ::WaitForSingleObject(_handle, ((ms == 0) ? INFINITE : (DWORD)ms));
93
94 // Reacquire serialized access to the state
95 _waitLock.acquire();
96
97 // Awaken only when the event is set or the timeout expired
98 assert(dwResult == WAIT_OBJECT_0 || dwResult == WAIT_TIMEOUT);
99
100 if(dwResult == WAIT_TIMEOUT)
101 push(TIMEDOUT);
102
103 // Get the next available STATE
104 state = next();
105 _waiting = false;
106
107 ::ResetEvent(_handle);
108
109 // Acquire the internal lock & release the external lock
110 _waitLock.release();
111
112 // Reaquire the external lock, keep from deadlocking threads calling
113 // notify(), interrupt(), etc.
114 _lock.acquire();
115
116 return state;
117
118 }
119
120
interrupt()121 bool Monitor::interrupt() {
122
123 // Serialize access to the state
124 _waitLock.acquire();
125
126 bool wasInterruptable = !pending(INTERRUPTED);
127 bool hadWaiter = _waiting;
128
129 if(wasInterruptable) {
130
131 // Update the state & wake the waiter if there is one
132 push(INTERRUPTED);
133
134 wasInterruptable = false;
135
136 if(hadWaiter && !masked(Monitor::INTERRUPTED)) {
137
138 // Blocked on a synchronization object
139 if(::SetEvent(_handle) == FALSE) {
140 assert(0);
141 }
142
143 } else
144 wasInterruptable = !(_owner == ::GetCurrentThreadId());
145
146 }
147
148 _waitLock.release();
149
150 // Only returns true when an interrupted thread is not currently blocked
151 return wasInterruptable;
152
153 }
154
isInterrupted()155 bool Monitor::isInterrupted() {
156
157 // Serialize access to the state
158 _waitLock.acquire();
159
160 bool wasInterrupted = pending(INTERRUPTED);
161 clear(INTERRUPTED);
162
163 _waitLock.release();
164
165 return wasInterrupted;
166
167 }
168
169
notify()170 bool Monitor::notify() {
171
172 // Serialize access to the state
173 _waitLock.acquire();
174
175 bool wasNotifyable = !pending(INTERRUPTED);
176
177 if(wasNotifyable) {
178
179 // Set the flag and wake the waiter if there
180 // is one
181 push(SIGNALED);
182
183 // If there is a waiter then send the signal.
184 if(_waiting)
185 if(::SetEvent(_handle) == FALSE) {
186 assert(0);
187 }
188
189 }
190
191 _waitLock.release();
192
193 return wasNotifyable;
194
195 }
196
197
cancel()198 bool Monitor::cancel() {
199
200 // Serialize access to the state
201 _waitLock.acquire();
202
203 bool wasInterrupted = !pending(INTERRUPTED);
204 bool hadWaiter = _waiting;
205
206 push(CANCELED);
207
208 if(wasInterrupted) {
209
210 // Update the state & wake the waiter if there is one
211 push(INTERRUPTED);
212
213 // If there is a waiter then send the signal.
214 if(hadWaiter && !masked(Monitor::INTERRUPTED))
215 if(::SetEvent(_handle) == FALSE) {
216 assert(0);
217 }
218
219 }
220
221 _waitLock.release();
222
223 return wasInterrupted;
224
225 }
226
isCanceled()227 bool Monitor::isCanceled() {
228
229 // Serialize access to the state
230 _waitLock.acquire();
231
232 bool wasCanceled = examine(CANCELED);
233
234 if(_owner == ::GetCurrentThreadId())
235 clear(INTERRUPTED);
236
237 _waitLock.release();
238
239 return wasCanceled;
240
241 }
242
243