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 __ZTCONDITION_H__
24 #define __ZTCONDITION_H__
26 #include "zthread/Lockable.h"
27 #include "zthread/NonCopyable.h"
28 #include "zthread/Waitable.h"
32 class FifoConditionImpl
;
36 * @author Eric Crahen <http://www.code-foo.com>
37 * @date <2003-07-16T14:38:59-0400>
40 * A Condition is a Waitable object used to block a thread until a particular
41 * condition is met. A Condition object is always used in conjunction with Lockable
42 * object. This object should be a FastMutex, Mutex, PriorityMutex or PriorityInheritanceMutex.
44 * Condition objects are reminiscent of POSIX condition variables in several ways but
45 * are slightly different.
47 * A Condition is <i>not</i> subject to spurious wakeup.
49 * Like all Waitable objects, Conditions are sensitive to Thread::interupt() which can
50 * be used to prematurely end a wait().
52 * @see Thread::interupt()
54 * Before a wait() is performed on a Condition, the associated Lockable object should
55 * have been acquire()ed. When the wait() begins, that Lockable object is release()d
56 * (wait() will atomically begin the wait and unlock the Lockable).
58 * A thread blocked by wait() will remain so until an exception occurs, or until
59 * the thread awakened by a signal() or broadcast(). When the thread resumes execution,
60 * the associated Lockable is acquire()d before wait() returns.
64 * Threads blocked on a Condition are resumed in FIFO order.
66 class ZTHREAD_API Condition
: public Waitable
, private NonCopyable
{
68 FifoConditionImpl
* _impl
;
73 * Create a Condition associated with the given Lockable object.
75 * @param l Lockable object to associate with this Condition object.
77 Condition(Lockable
& l
);
79 //! Destroy Condition object
83 * Wake <em>one</em> thread waiting on this Condition.
85 * The associated Lockable need not have been acquire when this function is
88 * @post a waiting thread, if any exists, will be awakened.
93 * Wake <em>all</em> threads wait()ing on this Condition.
95 * The associated Lockable need not have been acquire when this function is
98 * @post all wait()ing threads, if any exist, will be awakened.
103 * Wait for this Condition, blocking the calling thread until a signal or broadcast
106 * This operation atomically releases the associated Lockable and blocks the calling thread.
108 * @exception Interrupted_Exception thrown when the calling thread is interrupted.
109 * A thread may be interrupted at any time, prematurely ending any wait.
111 * @pre The thread calling this method must have first acquired the associated
114 * @post A thread that has resumed execution without exception (because of a signal(),
115 * broadcast() or exception) will have acquire()d the associated Lockable object
116 * before returning from a wait().
118 * @see Waitable::wait()
123 * Wait for this Condition, blocking the calling thread until a signal or broadcast
126 * This operation atomically releases the associated Lockable and blocks the calling thread.
128 * @param timeout maximum amount of time (milliseconds) this method could block
131 * - <em>true</em> if the Condition receives a signal or broadcast before
132 * <i>timeout</i> milliseconds elapse.
133 * - <em>false</em> otherwise.
135 * @exception Interrupted_Exception thrown when the calling thread is interrupted.
136 * A thread may be interrupted at any time, prematurely ending any wait.
138 * @pre The thread calling this method must have first acquired the associated
141 * @post A thread that has resumed execution without exception (because of a signal(),
142 * broadcast() or exception) will have acquire()d the associated Lockable object
143 * before returning from a wait().
145 * @see Waitable::wait(unsigned long timeout)
147 virtual bool wait(unsigned long timeout
);
152 } // namespace ZThread
154 #endif // __ZTCONDITION_H__