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