Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / src / zthread / win32 / Monitor.cxx
blob6e69487c054e21acb1bb02b25ab17092dc47a25c
1 /*
2 * Copyright (c) 2005, Eric Crahen
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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.
23 #include "Monitor.h"
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 using namespace ZThread;
31 Monitor::Monitor() : _owner(0), _waiting(false) {
33 _handle = ::CreateEvent(0, TRUE, FALSE, 0);
34 if(_handle == NULL) {
35 assert(0);
40 Monitor::~Monitor() {
42 assert(!_waiting);
44 ::CloseHandle(_handle);
48 Monitor::STATE Monitor::wait(unsigned long ms) {
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();
56 STATE state; //(INVALID);
58 // Serialize access to the state of the Monitor
59 // and test the state to determine if a wait is needed.
60 _waitLock.acquire();
62 if(pending(ANYTHING)) {
64 // Return without waiting when possible
65 state = next();
67 _waitLock.release();
68 return state;
71 // Unlock the external lock if a wait() is probably needed.
72 // Access to the state is still serial.
73 _lock.release();
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)) {
82 // Wait, ignoring signals
83 _waiting = true;
85 // Block until the event is set.
86 _waitLock.release();
88 // The event is manual reset so this lack of atmoicity will not
89 // be an issue
91 DWORD dwResult =
92 ::WaitForSingleObject(_handle, ((ms == 0) ? INFINITE : (DWORD)ms));
94 // Reacquire serialized access to the state
95 _waitLock.acquire();
97 // Awaken only when the event is set or the timeout expired
98 assert(dwResult == WAIT_OBJECT_0 || dwResult == WAIT_TIMEOUT);
100 if(dwResult == WAIT_TIMEOUT)
101 push(TIMEDOUT);
103 // Get the next available STATE
104 state = next();
105 _waiting = false;
107 ::ResetEvent(_handle);
109 // Acquire the internal lock & release the external lock
110 _waitLock.release();
112 // Reaquire the external lock, keep from deadlocking threads calling
113 // notify(), interrupt(), etc.
114 _lock.acquire();
116 return state;
121 bool Monitor::interrupt() {
123 // Serialize access to the state
124 _waitLock.acquire();
126 bool wasInterruptable = !pending(INTERRUPTED);
127 bool hadWaiter = _waiting;
129 if(wasInterruptable) {
131 // Update the state & wake the waiter if there is one
132 push(INTERRUPTED);
134 wasInterruptable = false;
136 if(hadWaiter && !masked(Monitor::INTERRUPTED)) {
138 // Blocked on a synchronization object
139 if(::SetEvent(_handle) == FALSE) {
140 assert(0);
143 } else
144 wasInterruptable = !(_owner == ::GetCurrentThreadId());
148 _waitLock.release();
150 // Only returns true when an interrupted thread is not currently blocked
151 return wasInterruptable;
155 bool Monitor::isInterrupted() {
157 // Serialize access to the state
158 _waitLock.acquire();
160 bool wasInterrupted = pending(INTERRUPTED);
161 clear(INTERRUPTED);
163 _waitLock.release();
165 return wasInterrupted;
170 bool Monitor::notify() {
172 // Serialize access to the state
173 _waitLock.acquire();
175 bool wasNotifyable = !pending(INTERRUPTED);
177 if(wasNotifyable) {
179 // Set the flag and wake the waiter if there
180 // is one
181 push(SIGNALED);
183 // If there is a waiter then send the signal.
184 if(_waiting)
185 if(::SetEvent(_handle) == FALSE) {
186 assert(0);
191 _waitLock.release();
193 return wasNotifyable;
198 bool Monitor::cancel() {
200 // Serialize access to the state
201 _waitLock.acquire();
203 bool wasInterrupted = !pending(INTERRUPTED);
204 bool hadWaiter = _waiting;
206 push(CANCELED);
208 if(wasInterrupted) {
210 // Update the state & wake the waiter if there is one
211 push(INTERRUPTED);
213 // If there is a waiter then send the signal.
214 if(hadWaiter && !masked(Monitor::INTERRUPTED))
215 if(::SetEvent(_handle) == FALSE) {
216 assert(0);
221 _waitLock.release();
223 return wasInterrupted;
227 bool Monitor::isCanceled() {
229 // Serialize access to the state
230 _waitLock.acquire();
232 bool wasCanceled = examine(CANCELED);
234 if(_owner == ::GetCurrentThreadId())
235 clear(INTERRUPTED);
237 _waitLock.release();
239 return wasCanceled;