Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / src / zthread / win32 / Monitor.h
blob7073343b7f8830b312fb080e15bf1138bec273cf
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 #ifndef __ZTMONITOR_H__
24 #define __ZTMONITOR_H__
26 #include "../Status.h"
27 #include "../FastLock.h"
29 namespace ZThread {
31 /**
32 * @class Monitor
33 * @author Eric Crahen <http://www.code-foo.com>
34 * @date <2003-07-16T23:33:10-0400>
35 * @version 2.2.11
37 class Monitor : public Status, private NonCopyable {
39 //! Serialize access to external objects
40 FastLock _lock;
42 //! Event used to block thread
43 HANDLE _handle;
45 //! Serialize access to the internal state of the monitor
46 FastLock _waitLock;
48 //! Owning thread
49 DWORD _owner;
51 //! Waiting flag, to avoid uneccessary signals
52 volatile bool _waiting;
54 //! State of the monitor
55 volatile int _state;
57 public:
59 //! Create a new monitor.
60 Monitor();
62 //! Destroy the monitor.
63 ~Monitor();
65 //! Acquire the external lock for this monitor.
66 inline void acquire() {
67 _lock.acquire();
70 //! Try to acquire the external lock for this monitor.
71 inline bool tryAcquire() {
72 return _lock.tryAcquire();
75 //! Release the external lock for this monitor.
76 inline void release() {
77 _lock.release();
80 /**
81 * Wait for a state change and atomically unlock the external lock.
82 * Blocks for an indefinent amount of time.
84 * @return INTERRUPTED if the wait was ended by a interrupt()
85 * or SIGNALED if the wait was ended by a notify()
87 * @post the external lock is always acquired before this function returns
89 inline STATE wait() {
90 return wait(0);
93 /**
94 * Wait for a state change and atomically unlock the external lock.
95 * May blocks for an indefinent amount of time.
97 * @param timeout - maximum time to block (milliseconds) or 0 to
98 * block indefinently
100 * @return INTERRUPTED if the wait was ended by a interrupt()
101 * or TIMEDOUT if the maximum wait time expired.
102 * or SIGNALED if the wait was ended by a notify()
104 * @post the external lock is always acquired before this function returns
106 STATE wait(unsigned long timeout);
109 * Interrupt this monitor. If there is a thread blocked on this monitor object
110 * it will be signaled and released. If there is no waiter, a flag is set and
111 * the next attempt to wait() will return INTERRUPTED w/o blocking.
113 * @return false if the thread was previously INTERRUPTED.
115 bool interrupt();
118 * Notify this monitor. If there is a thread blocked on this monitor object
119 * it will be signaled and released. If there is no waiter, a flag is set and
120 * the next attempt to wait() will return SIGNALED w/o blocking, if no other
121 * flag is set.
123 * @return false if the thread was previously INTERRUPTED.
125 bool notify();
128 * Check the state of this monitor, clearing the INTERRUPTED status if set.
130 * @return bool true if the monitor was INTERRUPTED.
131 * @post INTERRUPTED flag cleared if the calling thread owns the monitor.
133 bool isInterrupted();
136 * Mark the Status CANCELED, and INTERRUPT the montor.
138 * @see interrupt()
140 bool cancel();
143 * Test the CANCELED Status, clearing the INTERRUPTED status if set.
145 * @return bool
147 bool isCanceled();
153 #endif