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 | #ifndef __ZTCONDITION_H__ |
---|
24 | #define __ZTCONDITION_H__ |
---|
25 | |
---|
26 | #include "zthread/Lockable.h" |
---|
27 | #include "zthread/NonCopyable.h" |
---|
28 | #include "zthread/Waitable.h" |
---|
29 | |
---|
30 | namespace ZThread { |
---|
31 | |
---|
32 | class FifoConditionImpl; |
---|
33 | |
---|
34 | /** |
---|
35 | * @class Condition |
---|
36 | * @author Eric Crahen <http://www.code-foo.com> |
---|
37 | * @date <2003-07-16T14:38:59-0400> |
---|
38 | * @version 2.2.1 |
---|
39 | * |
---|
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. |
---|
43 | * |
---|
44 | * Condition objects are reminiscent of POSIX condition variables in several ways but |
---|
45 | * are slightly different. |
---|
46 | * |
---|
47 | * A Condition is <i>not</i> subject to spurious wakeup. |
---|
48 | * |
---|
49 | * Like all Waitable objects, Conditions are sensitive to Thread::interupt() which can |
---|
50 | * be used to prematurely end a wait(). |
---|
51 | * |
---|
52 | * @see Thread::interupt() |
---|
53 | * |
---|
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). |
---|
57 | * |
---|
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. |
---|
61 | * |
---|
62 | * <b>Scheduling</b> |
---|
63 | * |
---|
64 | * Threads blocked on a Condition are resumed in FIFO order. |
---|
65 | */ |
---|
66 | class ZTHREAD_API Condition : public Waitable, private NonCopyable { |
---|
67 | |
---|
68 | FifoConditionImpl* _impl; |
---|
69 | |
---|
70 | public: |
---|
71 | |
---|
72 | /** |
---|
73 | * Create a Condition associated with the given Lockable object. |
---|
74 | * |
---|
75 | * @param l Lockable object to associate with this Condition object. |
---|
76 | */ |
---|
77 | Condition(Lockable& l); |
---|
78 | |
---|
79 | //! Destroy Condition object |
---|
80 | virtual ~Condition(); |
---|
81 | |
---|
82 | /** |
---|
83 | * Wake <em>one</em> thread waiting on this Condition. |
---|
84 | * |
---|
85 | * The associated Lockable need not have been acquire when this function is |
---|
86 | * invoked. |
---|
87 | * |
---|
88 | * @post a waiting thread, if any exists, will be awakened. |
---|
89 | */ |
---|
90 | void signal(); |
---|
91 | |
---|
92 | /** |
---|
93 | * Wake <em>all</em> threads wait()ing on this Condition. |
---|
94 | * |
---|
95 | * The associated Lockable need not have been acquire when this function is |
---|
96 | * invoked. |
---|
97 | * |
---|
98 | * @post all wait()ing threads, if any exist, will be awakened. |
---|
99 | */ |
---|
100 | void broadcast(); |
---|
101 | |
---|
102 | /** |
---|
103 | * Wait for this Condition, blocking the calling thread until a signal or broadcast |
---|
104 | * is received. |
---|
105 | * |
---|
106 | * This operation atomically releases the associated Lockable and blocks the calling thread. |
---|
107 | * |
---|
108 | * @exception Interrupted_Exception thrown when the calling thread is interrupted. |
---|
109 | * A thread may be interrupted at any time, prematurely ending any wait. |
---|
110 | * |
---|
111 | * @pre The thread calling this method must have first acquired the associated |
---|
112 | * Lockable object. |
---|
113 | * |
---|
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(). |
---|
117 | * |
---|
118 | * @see Waitable::wait() |
---|
119 | */ |
---|
120 | virtual void wait(); |
---|
121 | |
---|
122 | /** |
---|
123 | * Wait for this Condition, blocking the calling thread until a signal or broadcast |
---|
124 | * is received. |
---|
125 | * |
---|
126 | * This operation atomically releases the associated Lockable and blocks the calling thread. |
---|
127 | * |
---|
128 | * @param timeout maximum amount of time (milliseconds) this method could block |
---|
129 | * |
---|
130 | * @return |
---|
131 | * - <em>true</em> if the Condition receives a signal or broadcast before |
---|
132 | * <i>timeout</i> milliseconds elapse. |
---|
133 | * - <em>false</em> otherwise. |
---|
134 | * |
---|
135 | * @exception Interrupted_Exception thrown when the calling thread is interrupted. |
---|
136 | * A thread may be interrupted at any time, prematurely ending any wait. |
---|
137 | * |
---|
138 | * @pre The thread calling this method must have first acquired the associated |
---|
139 | * Lockable object. |
---|
140 | * |
---|
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(). |
---|
144 | * |
---|
145 | * @see Waitable::wait(unsigned long timeout) |
---|
146 | */ |
---|
147 | virtual bool wait(unsigned long timeout); |
---|
148 | |
---|
149 | |
---|
150 | }; |
---|
151 | |
---|
152 | } // namespace ZThread |
---|
153 | |
---|
154 | #endif // __ZTCONDITION_H__ |
---|