|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include <kernel/OS.h> |
|
7 |
|
8 #include "primpl.h" |
|
9 |
|
10 /* |
|
11 ** Create a new condition variable. |
|
12 ** |
|
13 ** "lock" is the lock used to protect the condition variable. |
|
14 ** |
|
15 ** Condition variables are synchronization objects that threads can use |
|
16 ** to wait for some condition to occur. |
|
17 ** |
|
18 ** This may fail if memory is tight or if some operating system resource |
|
19 ** is low. In such cases, a NULL will be returned. |
|
20 */ |
|
21 PR_IMPLEMENT(PRCondVar*) |
|
22 PR_NewCondVar (PRLock *lock) |
|
23 { |
|
24 PRCondVar *cv = PR_NEW( PRCondVar ); |
|
25 PR_ASSERT( NULL != lock ); |
|
26 if( NULL != cv ) |
|
27 { |
|
28 cv->lock = lock; |
|
29 cv->sem = create_sem(0, "CVSem"); |
|
30 cv->handshakeSem = create_sem(0, "CVHandshake"); |
|
31 cv->signalSem = create_sem( 0, "CVSignal"); |
|
32 cv->signalBenCount = 0; |
|
33 cv->ns = cv->nw = 0; |
|
34 PR_ASSERT( cv->sem >= B_NO_ERROR ); |
|
35 PR_ASSERT( cv->handshakeSem >= B_NO_ERROR ); |
|
36 PR_ASSERT( cv->signalSem >= B_NO_ERROR ); |
|
37 } |
|
38 return cv; |
|
39 } /* PR_NewCondVar */ |
|
40 |
|
41 /* |
|
42 ** Destroy a condition variable. There must be no thread |
|
43 ** waiting on the condvar. The caller is responsible for guaranteeing |
|
44 ** that the condvar is no longer in use. |
|
45 ** |
|
46 */ |
|
47 PR_IMPLEMENT(void) |
|
48 PR_DestroyCondVar (PRCondVar *cvar) |
|
49 { |
|
50 status_t result = delete_sem( cvar->sem ); |
|
51 PR_ASSERT( result == B_NO_ERROR ); |
|
52 |
|
53 result = delete_sem( cvar->handshakeSem ); |
|
54 PR_ASSERT( result == B_NO_ERROR ); |
|
55 |
|
56 result = delete_sem( cvar->signalSem ); |
|
57 PR_ASSERT( result == B_NO_ERROR ); |
|
58 |
|
59 PR_DELETE( cvar ); |
|
60 } |
|
61 |
|
62 /* |
|
63 ** The thread that waits on a condition is blocked in a "waiting on |
|
64 ** condition" state until another thread notifies the condition or a |
|
65 ** caller specified amount of time expires. The lock associated with |
|
66 ** the condition variable will be released, which must have be held |
|
67 ** prior to the call to wait. |
|
68 ** |
|
69 ** Logically a notified thread is moved from the "waiting on condition" |
|
70 ** state and made "ready." When scheduled, it will attempt to reacquire |
|
71 ** the lock that it held when wait was called. |
|
72 ** |
|
73 ** The timeout has two well known values, PR_INTERVAL_NO_TIMEOUT and |
|
74 ** PR_INTERVAL_NO_WAIT. The former value requires that a condition be |
|
75 ** notified (or the thread interrupted) before it will resume from the |
|
76 ** wait. If the timeout has a value of PR_INTERVAL_NO_WAIT, the effect |
|
77 ** is to release the lock, possibly causing a rescheduling within the |
|
78 ** runtime, then immediately attempting to reacquire the lock and resume. |
|
79 ** |
|
80 ** Any other value for timeout will cause the thread to be rescheduled |
|
81 ** either due to explicit notification or an expired interval. The latter |
|
82 ** must be determined by treating time as one part of the monitored data |
|
83 ** being protected by the lock and tested explicitly for an expired |
|
84 ** interval. |
|
85 ** |
|
86 ** Returns PR_FAILURE if the caller has not locked the lock associated |
|
87 ** with the condition variable or the thread was interrupted (PR_Interrupt()). |
|
88 ** The particular reason can be extracted with PR_GetError(). |
|
89 */ |
|
90 PR_IMPLEMENT(PRStatus) |
|
91 PR_WaitCondVar (PRCondVar *cvar, PRIntervalTime timeout) |
|
92 { |
|
93 status_t err; |
|
94 if( timeout == PR_INTERVAL_NO_WAIT ) |
|
95 { |
|
96 PR_Unlock( cvar->lock ); |
|
97 PR_Lock( cvar->lock ); |
|
98 return PR_SUCCESS; |
|
99 } |
|
100 |
|
101 if( atomic_add( &cvar->signalBenCount, 1 ) > 0 ) |
|
102 { |
|
103 if (acquire_sem(cvar->signalSem) == B_INTERRUPTED) |
|
104 { |
|
105 atomic_add( &cvar->signalBenCount, -1 ); |
|
106 return PR_FAILURE; |
|
107 } |
|
108 } |
|
109 cvar->nw += 1; |
|
110 if( atomic_add( &cvar->signalBenCount, -1 ) > 1 ) |
|
111 { |
|
112 release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE); |
|
113 } |
|
114 |
|
115 PR_Unlock( cvar->lock ); |
|
116 if( timeout==PR_INTERVAL_NO_TIMEOUT ) |
|
117 { |
|
118 err = acquire_sem(cvar->sem); |
|
119 } |
|
120 else |
|
121 { |
|
122 err = acquire_sem_etc(cvar->sem, 1, B_RELATIVE_TIMEOUT, PR_IntervalToMicroseconds(timeout) ); |
|
123 } |
|
124 |
|
125 if( atomic_add( &cvar->signalBenCount, 1 ) > 0 ) |
|
126 { |
|
127 while (acquire_sem(cvar->signalSem) == B_INTERRUPTED); |
|
128 } |
|
129 |
|
130 if (cvar->ns > 0) |
|
131 { |
|
132 release_sem_etc(cvar->handshakeSem, 1, B_DO_NOT_RESCHEDULE); |
|
133 cvar->ns -= 1; |
|
134 } |
|
135 cvar->nw -= 1; |
|
136 if( atomic_add( &cvar->signalBenCount, -1 ) > 1 ) |
|
137 { |
|
138 release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE); |
|
139 } |
|
140 |
|
141 PR_Lock( cvar->lock ); |
|
142 if(err!=B_NO_ERROR) |
|
143 { |
|
144 return PR_FAILURE; |
|
145 } |
|
146 return PR_SUCCESS; |
|
147 } |
|
148 |
|
149 /* |
|
150 ** Notify ONE thread that is currently waiting on 'cvar'. Which thread is |
|
151 ** dependent on the implementation of the runtime. Common sense would dictate |
|
152 ** that all threads waiting on a single condition have identical semantics, |
|
153 ** therefore which one gets notified is not significant. |
|
154 ** |
|
155 ** The calling thead must hold the lock that protects the condition, as |
|
156 ** well as the invariants that are tightly bound to the condition, when |
|
157 ** notify is called. |
|
158 ** |
|
159 ** Returns PR_FAILURE if the caller has not locked the lock associated |
|
160 ** with the condition variable. |
|
161 */ |
|
162 PR_IMPLEMENT(PRStatus) |
|
163 PR_NotifyCondVar (PRCondVar *cvar) |
|
164 { |
|
165 status_t err ; |
|
166 if( atomic_add( &cvar->signalBenCount, 1 ) > 0 ) |
|
167 { |
|
168 if (acquire_sem(cvar->signalSem) == B_INTERRUPTED) |
|
169 { |
|
170 atomic_add( &cvar->signalBenCount, -1 ); |
|
171 return PR_FAILURE; |
|
172 } |
|
173 } |
|
174 if (cvar->nw > cvar->ns) |
|
175 { |
|
176 cvar->ns += 1; |
|
177 release_sem_etc(cvar->sem, 1, B_DO_NOT_RESCHEDULE); |
|
178 if( atomic_add( &cvar->signalBenCount, -1 ) > 1 ) |
|
179 { |
|
180 release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE); |
|
181 } |
|
182 |
|
183 while (acquire_sem(cvar->handshakeSem) == B_INTERRUPTED) |
|
184 { |
|
185 err = B_INTERRUPTED; |
|
186 } |
|
187 } |
|
188 else |
|
189 { |
|
190 if( atomic_add( &cvar->signalBenCount, -1 ) > 1 ) |
|
191 { |
|
192 release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE); |
|
193 } |
|
194 } |
|
195 return PR_SUCCESS; |
|
196 } |
|
197 |
|
198 /* |
|
199 ** Notify all of the threads waiting on the condition variable. The order |
|
200 ** that the threads are notified is indeterminant. The lock that protects |
|
201 ** the condition must be held. |
|
202 ** |
|
203 ** Returns PR_FAILURE if the caller has not locked the lock associated |
|
204 ** with the condition variable. |
|
205 */ |
|
206 PR_IMPLEMENT(PRStatus) |
|
207 PR_NotifyAllCondVar (PRCondVar *cvar) |
|
208 { |
|
209 int32 handshakes; |
|
210 status_t err = B_OK; |
|
211 |
|
212 if( atomic_add( &cvar->signalBenCount, 1 ) > 0 ) |
|
213 { |
|
214 if (acquire_sem(cvar->signalSem) == B_INTERRUPTED) |
|
215 { |
|
216 atomic_add( &cvar->signalBenCount, -1 ); |
|
217 return PR_FAILURE; |
|
218 } |
|
219 } |
|
220 |
|
221 if (cvar->nw > cvar->ns) |
|
222 { |
|
223 handshakes = cvar->nw - cvar->ns; |
|
224 cvar->ns = cvar->nw; |
|
225 release_sem_etc(cvar->sem, handshakes, B_DO_NOT_RESCHEDULE); |
|
226 if( atomic_add( &cvar->signalBenCount, -1 ) > 1 ) |
|
227 { |
|
228 release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE); |
|
229 } |
|
230 |
|
231 while (acquire_sem_etc(cvar->handshakeSem, handshakes, 0, 0) == B_INTERRUPTED) |
|
232 { |
|
233 err = B_INTERRUPTED; |
|
234 } |
|
235 } |
|
236 else |
|
237 { |
|
238 if( atomic_add( &cvar->signalBenCount, -1 ) > 1 ) |
|
239 { |
|
240 release_sem_etc(cvar->signalSem, 1, B_DO_NOT_RESCHEDULE); |
|
241 } |
|
242 } |
|
243 return PR_SUCCESS; |
|
244 } |