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"
33 * @author Eric Crahen <http://www.code-foo.com>
34 * @date <2003-07-16T23:33:10-0400>
37 class Monitor
: public Status
, private NonCopyable
{
39 //! Serialize access to external objects
42 //! Event used to block thread
45 //! Serialize access to the internal state of the monitor
51 //! Waiting flag, to avoid uneccessary signals
52 volatile bool _waiting
;
54 //! State of the monitor
59 //! Create a new monitor.
62 //! Destroy the monitor.
65 //! Acquire the external lock for this monitor.
66 inline void 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() {
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
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
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.
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
123 * @return false if the thread was previously INTERRUPTED.
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.
143 * Test the CANCELED Status, clearing the INTERRUPTED status if set.