Upload Tizen:Base source
[toolchains/nspr.git] / mozilla / nsprpub / pr / src / threads / prmon.c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 "primpl.h"
39
40 /************************************************************************/
41
42 /*
43 ** Create a new monitor.
44 */
45 PR_IMPLEMENT(PRMonitor*) PR_NewMonitor()
46 {
47     PRMonitor *mon;
48         PRCondVar *cvar;
49         PRLock *lock;
50
51     mon = PR_NEWZAP(PRMonitor);
52     if (mon) {
53                 lock = PR_NewLock();
54             if (!lock) {
55                         PR_DELETE(mon);
56                         return 0;
57         }
58
59             cvar = PR_NewCondVar(lock);
60             if (!cvar) {
61                 PR_DestroyLock(lock);
62                         PR_DELETE(mon);
63                         return 0;
64         }
65         mon->cvar = cvar;
66         mon->name = NULL;
67     }
68     return mon;
69 }
70
71 PR_IMPLEMENT(PRMonitor*) PR_NewNamedMonitor(const char* name)
72 {
73     PRMonitor* mon = PR_NewMonitor();
74     if (mon)
75         mon->name = name;
76     return mon;
77 }
78
79 /*
80 ** Destroy a monitor. There must be no thread waiting on the monitor's
81 ** condition variable. The caller is responsible for guaranteeing that the
82 ** monitor is no longer in use.
83 */
84 PR_IMPLEMENT(void) PR_DestroyMonitor(PRMonitor *mon)
85 {
86         PR_DestroyLock(mon->cvar->lock);
87     PR_DestroyCondVar(mon->cvar);
88     PR_DELETE(mon);
89 }
90
91 /*
92 ** Enter the lock associated with the monitor.
93 */
94 PR_IMPLEMENT(void) PR_EnterMonitor(PRMonitor *mon)
95 {
96     if (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) {
97                 mon->entryCount++;
98     } else {
99                 PR_Lock(mon->cvar->lock);
100                 mon->entryCount = 1;
101     }
102 }
103
104 /*
105 ** Test and then enter the lock associated with the monitor if it's not
106 ** already entered by some other thread. Return PR_FALSE if some other
107 ** thread owned the lock at the time of the call.
108 */
109 PR_IMPLEMENT(PRBool) PR_TestAndEnterMonitor(PRMonitor *mon)
110 {
111     if (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) {
112                 mon->entryCount++;
113                 return PR_TRUE;
114     } else {
115                 if (PR_TestAndLock(mon->cvar->lock)) {
116                 mon->entryCount = 1;
117                         return PR_TRUE;
118                 }
119     }
120     return PR_FALSE;
121 }
122
123 /*
124 ** Exit the lock associated with the monitor once.
125 */
126 PR_IMPLEMENT(PRStatus) PR_ExitMonitor(PRMonitor *mon)
127 {
128     if (mon->cvar->lock->owner != _PR_MD_CURRENT_THREAD()) {
129         return PR_FAILURE;
130     }
131     if (--mon->entryCount == 0) {
132                 return PR_Unlock(mon->cvar->lock);
133     }
134     return PR_SUCCESS;
135 }
136
137 /*
138 ** Return the number of times that the current thread has entered the
139 ** lock. Returns zero if the current thread has not entered the lock.
140 */
141 PR_IMPLEMENT(PRIntn) PR_GetMonitorEntryCount(PRMonitor *mon)
142 {
143     return (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) ?
144         mon->entryCount : 0;
145 }
146
147 /*
148 ** If the current thread is in |mon|, this assertion is guaranteed to
149 ** succeed.  Otherwise, the behavior of this function is undefined.
150 */
151 PR_IMPLEMENT(void) PR_AssertCurrentThreadInMonitor(PRMonitor *mon)
152 {
153     PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(mon->cvar->lock);
154 }
155
156 /*
157 ** Wait for a notify on the condition variable. Sleep for "ticks" amount
158 ** of time (if "tick" is 0 then the sleep is indefinite). While
159 ** the thread is waiting it exits the monitors lock (as if it called
160 ** PR_ExitMonitor as many times as it had called PR_EnterMonitor).  When
161 ** the wait has finished the thread regains control of the monitors lock
162 ** with the same entry count as before the wait began.
163 **
164 ** The thread waiting on the monitor will be resumed when the monitor is
165 ** notified (assuming the thread is the next in line to receive the
166 ** notify) or when the "ticks" elapses.
167 **
168 ** Returns PR_FAILURE if the caller has not locked the lock associated
169 ** with the condition variable.
170 ** This routine can return PR_PENDING_INTERRUPT if the waiting thread 
171 ** has been interrupted.
172 */
173 PR_IMPLEMENT(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks)
174 {
175     PRUintn entryCount;
176         PRStatus status;
177     PRThread *me = _PR_MD_CURRENT_THREAD();
178
179     if (mon->cvar->lock->owner != me) return PR_FAILURE;
180
181     entryCount = mon->entryCount;
182     mon->entryCount = 0;
183
184         status = _PR_WaitCondVar(me, mon->cvar, mon->cvar->lock, ticks);
185
186     mon->entryCount = entryCount;
187
188     return status;
189 }
190
191 /*
192 ** Notify the highest priority thread waiting on the condition
193 ** variable. If a thread is waiting on the condition variable (using
194 ** PR_Wait) then it is awakened and begins waiting on the monitor's lock.
195 */
196 PR_IMPLEMENT(PRStatus) PR_Notify(PRMonitor *mon)
197 {
198     PRThread *me = _PR_MD_CURRENT_THREAD();
199     if (mon->cvar->lock->owner != me) return PR_FAILURE;
200     PR_NotifyCondVar(mon->cvar);
201     return PR_SUCCESS;
202 }
203
204 /*
205 ** Notify all of the threads waiting on the condition variable. All of
206 ** threads are notified in turn. The highest priority thread will
207 ** probably acquire the monitor first when the monitor is exited.
208 */
209 PR_IMPLEMENT(PRStatus) PR_NotifyAll(PRMonitor *mon)
210 {
211     PRThread *me = _PR_MD_CURRENT_THREAD();
212     if (mon->cvar->lock->owner != me) return PR_FAILURE;
213     PR_NotifyAllCondVar(mon->cvar);
214     return PR_SUCCESS;
215 }
216
217 /************************************************************************/
218
219 PRUint32 _PR_MonitorToString(PRMonitor *mon, char *buf, PRUint32 buflen)
220 {
221     PRUint32 nb;
222
223     if (mon->cvar->lock->owner) {
224         nb = PR_snprintf(buf, buflen, "[%p] owner=%d[%p] count=%ld",
225                          mon, mon->cvar->lock->owner->id,
226                          mon->cvar->lock->owner, mon->entryCount);
227     } else {
228         nb = PR_snprintf(buf, buflen, "[%p]", mon);
229     }
230     return nb;
231 }