Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: nspr/pr/src/threads/prmon.c

Issue 200653003: Update to NSPR 4.10.4. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/nss/
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « nspr/pr/src/threads/combined/prulock.c ('k') | patches/nspr-darwin.patch » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public 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 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/. */ 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 5
6 #include "primpl.h" 6 #include "primpl.h"
7 7
8 /************************************************************************/ 8 /************************************************************************/
9 9
10 /* 10 /*
11 * Notifies just get posted to the monitor. The actual notification is done
12 * when the monitor is fully exited so that MP systems don't contend for a
13 * monitor that they can't enter.
14 */
15 static void _PR_PostNotifyToMonitor(PRMonitor *mon, PRBool broadcast)
16 {
17 PR_ASSERT(mon != NULL);
18 PR_ASSERT_CURRENT_THREAD_IN_MONITOR(mon);
19
20 /* mon->notifyTimes is protected by the monitor, so we don't need to
21 * acquire mon->lock.
22 */
23 if (broadcast)
24 mon->notifyTimes = -1;
25 else if (mon->notifyTimes != -1)
26 mon->notifyTimes += 1;
27 }
28
29 static void _PR_PostNotifiesFromMonitor(PRCondVar *cv, PRIntn times)
30 {
31 PRStatus rv;
32
33 /*
34 * Time to actually notify any waits that were affected while the monitor
35 * was entered.
36 */
37 PR_ASSERT(cv != NULL);
38 PR_ASSERT(times != 0);
39 if (times == -1) {
40 rv = PR_NotifyAllCondVar(cv);
41 PR_ASSERT(rv == PR_SUCCESS);
42 } else {
43 while (times-- > 0) {
44 rv = PR_NotifyCondVar(cv);
45 PR_ASSERT(rv == PR_SUCCESS);
46 }
47 }
48 }
49
50 /*
11 ** Create a new monitor. 51 ** Create a new monitor.
12 */ 52 */
13 PR_IMPLEMENT(PRMonitor*) PR_NewMonitor() 53 PR_IMPLEMENT(PRMonitor*) PR_NewMonitor()
14 { 54 {
15 PRMonitor *mon; 55 PRMonitor *mon;
16 » PRCondVar *cvar; 56 PRStatus rv;
17 » PRLock *lock; 57
58 if (!_pr_initialized) _PR_ImplicitInitialization();
18 59
19 mon = PR_NEWZAP(PRMonitor); 60 mon = PR_NEWZAP(PRMonitor);
20 if (mon) { 61 if (mon == NULL) {
21 » » lock = PR_NewLock(); 62 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
22 » if (!lock) { 63 return NULL;
23 » » » PR_DELETE(mon); 64 }
24 » » » return 0;
25 » }
26 65
27 » cvar = PR_NewCondVar(lock); 66 rv = _PR_InitLock(&mon->lock);
28 » if (!cvar) { 67 PR_ASSERT(rv == PR_SUCCESS);
29 » » PR_DestroyLock(lock); 68 if (rv != PR_SUCCESS)
30 » » » PR_DELETE(mon); 69 goto error1;
31 » » » return 0; 70
32 » } 71 mon->owner = NULL;
33 » mon->cvar = cvar; 72
34 » mon->name = NULL; 73 rv = _PR_InitCondVar(&mon->entryCV, &mon->lock);
35 } 74 PR_ASSERT(rv == PR_SUCCESS);
75 if (rv != PR_SUCCESS)
76 goto error2;
77
78 rv = _PR_InitCondVar(&mon->waitCV, &mon->lock);
79 PR_ASSERT(rv == PR_SUCCESS);
80 if (rv != PR_SUCCESS)
81 goto error3;
82
83 mon->notifyTimes = 0;
84 mon->entryCount = 0;
85 mon->name = NULL;
36 return mon; 86 return mon;
87
88 error3:
89 _PR_FreeCondVar(&mon->entryCV);
90 error2:
91 _PR_FreeLock(&mon->lock);
92 error1:
93 PR_Free(mon);
94 return NULL;
37 } 95 }
38 96
39 PR_IMPLEMENT(PRMonitor*) PR_NewNamedMonitor(const char* name) 97 PR_IMPLEMENT(PRMonitor*) PR_NewNamedMonitor(const char* name)
40 { 98 {
41 PRMonitor* mon = PR_NewMonitor(); 99 PRMonitor* mon = PR_NewMonitor();
42 if (mon) 100 if (mon)
43 mon->name = name; 101 mon->name = name;
44 return mon; 102 return mon;
45 } 103 }
46 104
47 /* 105 /*
48 ** Destroy a monitor. There must be no thread waiting on the monitor's 106 ** Destroy a monitor. There must be no thread waiting on the monitor's
49 ** condition variable. The caller is responsible for guaranteeing that the 107 ** condition variable. The caller is responsible for guaranteeing that the
50 ** monitor is no longer in use. 108 ** monitor is no longer in use.
51 */ 109 */
52 PR_IMPLEMENT(void) PR_DestroyMonitor(PRMonitor *mon) 110 PR_IMPLEMENT(void) PR_DestroyMonitor(PRMonitor *mon)
53 { 111 {
54 » PR_DestroyLock(mon->cvar->lock); 112 PR_ASSERT(mon != NULL);
55 PR_DestroyCondVar(mon->cvar); 113 _PR_FreeCondVar(&mon->waitCV);
56 PR_DELETE(mon); 114 _PR_FreeCondVar(&mon->entryCV);
115 _PR_FreeLock(&mon->lock);
116 #if defined(DEBUG)
117 memset(mon, 0xaf, sizeof(PRMonitor));
118 #endif
119 PR_Free(mon);
57 } 120 }
58 121
59 /* 122 /*
60 ** Enter the lock associated with the monitor. 123 ** Enter the lock associated with the monitor.
61 */ 124 */
62 PR_IMPLEMENT(void) PR_EnterMonitor(PRMonitor *mon) 125 PR_IMPLEMENT(void) PR_EnterMonitor(PRMonitor *mon)
63 { 126 {
64 if (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) { 127 PRThread *me = _PR_MD_CURRENT_THREAD();
65 » » mon->entryCount++; 128 PRStatus rv;
66 } else { 129
67 » » PR_Lock(mon->cvar->lock); 130 PR_ASSERT(mon != NULL);
68 » » mon->entryCount = 1; 131 PR_Lock(&mon->lock);
132 if (mon->entryCount != 0) {
133 if (mon->owner == me)
134 goto done;
135 while (mon->entryCount != 0) {
136 rv = PR_WaitCondVar(&mon->entryCV, PR_INTERVAL_NO_TIMEOUT);
137 PR_ASSERT(rv == PR_SUCCESS);
138 }
69 } 139 }
140 /* and now I have the monitor */
141 PR_ASSERT(mon->notifyTimes == 0);
142 PR_ASSERT(mon->owner == NULL);
143 mon->owner = me;
144
145 done:
146 mon->entryCount += 1;
147 rv = PR_Unlock(&mon->lock);
148 PR_ASSERT(rv == PR_SUCCESS);
70 } 149 }
71 150
72 /* 151 /*
73 ** Test and then enter the lock associated with the monitor if it's not 152 ** Test and then enter the lock associated with the monitor if it's not
74 ** already entered by some other thread. Return PR_FALSE if some other 153 ** already entered by some other thread. Return PR_FALSE if some other
75 ** thread owned the lock at the time of the call. 154 ** thread owned the lock at the time of the call.
76 */ 155 */
77 PR_IMPLEMENT(PRBool) PR_TestAndEnterMonitor(PRMonitor *mon) 156 PR_IMPLEMENT(PRBool) PR_TestAndEnterMonitor(PRMonitor *mon)
78 { 157 {
79 if (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) { 158 PRThread *me = _PR_MD_CURRENT_THREAD();
80 » » mon->entryCount++; 159 PRStatus rv;
81 » » return PR_TRUE; 160
82 } else { 161 PR_ASSERT(mon != NULL);
83 » » if (PR_TestAndLock(mon->cvar->lock)) { 162 PR_Lock(&mon->lock);
84 » » mon->entryCount = 1; 163 if (mon->entryCount != 0) {
85 » » » return PR_TRUE; 164 if (mon->owner == me)
86 » » } 165 goto done;
166 rv = PR_Unlock(&mon->lock);
167 PR_ASSERT(rv == PR_SUCCESS);
168 return PR_FALSE;
87 } 169 }
88 return PR_FALSE; 170 /* and now I have the monitor */
171 PR_ASSERT(mon->notifyTimes == 0);
172 PR_ASSERT(mon->owner == NULL);
173 mon->owner = me;
174
175 done:
176 mon->entryCount += 1;
177 rv = PR_Unlock(&mon->lock);
178 PR_ASSERT(rv == PR_SUCCESS);
179 return PR_TRUE;
89 } 180 }
90 181
91 /* 182 /*
92 ** Exit the lock associated with the monitor once. 183 ** Exit the lock associated with the monitor once.
93 */ 184 */
94 PR_IMPLEMENT(PRStatus) PR_ExitMonitor(PRMonitor *mon) 185 PR_IMPLEMENT(PRStatus) PR_ExitMonitor(PRMonitor *mon)
95 { 186 {
96 if (mon->cvar->lock->owner != _PR_MD_CURRENT_THREAD()) { 187 PRThread *me = _PR_MD_CURRENT_THREAD();
188 PRStatus rv;
189
190 PR_ASSERT(mon != NULL);
191 PR_Lock(&mon->lock);
192 /* the entries should be > 0 and we'd better be the owner */
193 PR_ASSERT(mon->entryCount > 0);
194 PR_ASSERT(mon->owner == me);
195 if (mon->entryCount == 0 || mon->owner != me)
196 {
197 rv = PR_Unlock(&mon->lock);
198 PR_ASSERT(rv == PR_SUCCESS);
97 return PR_FAILURE; 199 return PR_FAILURE;
98 } 200 }
99 if (--mon->entryCount == 0) { 201
100 » » return PR_Unlock(mon->cvar->lock); 202 mon->entryCount -= 1; /* reduce by one */
203 if (mon->entryCount == 0)
204 {
205 /* and if it transitioned to zero - notify an entry waiter */
206 /* make the owner unknown */
207 mon->owner = NULL;
208 if (mon->notifyTimes != 0) {
209 _PR_PostNotifiesFromMonitor(&mon->waitCV, mon->notifyTimes);
210 mon->notifyTimes = 0;
211 }
212 rv = PR_NotifyCondVar(&mon->entryCV);
213 PR_ASSERT(rv == PR_SUCCESS);
101 } 214 }
215 rv = PR_Unlock(&mon->lock);
216 PR_ASSERT(rv == PR_SUCCESS);
102 return PR_SUCCESS; 217 return PR_SUCCESS;
103 } 218 }
104 219
105 /* 220 /*
106 ** Return the number of times that the current thread has entered the 221 ** Return the number of times that the current thread has entered the
107 ** lock. Returns zero if the current thread has not entered the lock. 222 ** lock. Returns zero if the current thread has not entered the lock.
108 */ 223 */
109 PR_IMPLEMENT(PRIntn) PR_GetMonitorEntryCount(PRMonitor *mon) 224 PR_IMPLEMENT(PRIntn) PR_GetMonitorEntryCount(PRMonitor *mon)
110 { 225 {
111 return (mon->cvar->lock->owner == _PR_MD_CURRENT_THREAD()) ? 226 PRThread *me = _PR_MD_CURRENT_THREAD();
112 mon->entryCount : 0; 227 PRStatus rv;
228 PRIntn count = 0;
229
230 PR_Lock(&mon->lock);
231 if (mon->owner == me)
232 count = mon->entryCount;
233 rv = PR_Unlock(&mon->lock);
234 PR_ASSERT(rv == PR_SUCCESS);
235 return count;
236 }
237
238 PR_IMPLEMENT(void) PR_AssertCurrentThreadInMonitor(PRMonitor *mon)
239 {
240 #if defined(DEBUG) || defined(FORCE_PR_ASSERT)
241 PRStatus rv;
242
243 PR_Lock(&mon->lock);
244 PR_ASSERT(mon->entryCount != 0 &&
245 mon->owner == _PR_MD_CURRENT_THREAD());
246 rv = PR_Unlock(&mon->lock);
247 PR_ASSERT(rv == PR_SUCCESS);
248 #endif
113 } 249 }
114 250
115 /* 251 /*
116 ** If the current thread is in |mon|, this assertion is guaranteed to
117 ** succeed. Otherwise, the behavior of this function is undefined.
118 */
119 PR_IMPLEMENT(void) PR_AssertCurrentThreadInMonitor(PRMonitor *mon)
120 {
121 PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(mon->cvar->lock);
122 }
123
124 /*
125 ** Wait for a notify on the condition variable. Sleep for "ticks" amount 252 ** Wait for a notify on the condition variable. Sleep for "ticks" amount
126 ** of time (if "tick" is 0 then the sleep is indefinite). While 253 ** of time (if "tick" is 0 then the sleep is indefinite). While
127 ** the thread is waiting it exits the monitors lock (as if it called 254 ** the thread is waiting it exits the monitors lock (as if it called
128 ** PR_ExitMonitor as many times as it had called PR_EnterMonitor). When 255 ** PR_ExitMonitor as many times as it had called PR_EnterMonitor). When
129 ** the wait has finished the thread regains control of the monitors lock 256 ** the wait has finished the thread regains control of the monitors lock
130 ** with the same entry count as before the wait began. 257 ** with the same entry count as before the wait began.
131 ** 258 **
132 ** The thread waiting on the monitor will be resumed when the monitor is 259 ** The thread waiting on the monitor will be resumed when the monitor is
133 ** notified (assuming the thread is the next in line to receive the 260 ** notified (assuming the thread is the next in line to receive the
134 ** notify) or when the "ticks" elapses. 261 ** notify) or when the "ticks" elapses.
135 ** 262 **
136 ** Returns PR_FAILURE if the caller has not locked the lock associated 263 ** Returns PR_FAILURE if the caller has not locked the lock associated
137 ** with the condition variable. 264 ** with the condition variable.
138 ** This routine can return PR_PENDING_INTERRUPT if the waiting thread 265 ** This routine can return PR_PENDING_INTERRUPT_ERROR if the waiting thread
139 ** has been interrupted. 266 ** has been interrupted.
140 */ 267 */
141 PR_IMPLEMENT(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks) 268 PR_IMPLEMENT(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks)
142 { 269 {
143 PRUintn entryCount; 270 PRStatus rv;
144 » PRStatus status; 271 PRUint32 saved_entries;
145 PRThread *me = _PR_MD_CURRENT_THREAD(); 272 PRThread *saved_owner;
146 273
147 if (mon->cvar->lock->owner != me) return PR_FAILURE; 274 PR_ASSERT(mon != NULL);
275 PR_Lock(&mon->lock);
276 /* the entries better be positive */
277 PR_ASSERT(mon->entryCount > 0);
278 /* and it better be owned by us */
279 PR_ASSERT(mon->owner == _PR_MD_CURRENT_THREAD()); /* XXX return failure */
148 280
149 entryCount = mon->entryCount; 281 /* tuck these away 'till later */
282 saved_entries = mon->entryCount;
150 mon->entryCount = 0; 283 mon->entryCount = 0;
284 saved_owner = mon->owner;
285 mon->owner = NULL;
286 /* If we have pending notifies, post them now. */
287 if (mon->notifyTimes != 0) {
288 _PR_PostNotifiesFromMonitor(&mon->waitCV, mon->notifyTimes);
289 mon->notifyTimes = 0;
290 }
291 rv = PR_NotifyCondVar(&mon->entryCV);
292 PR_ASSERT(rv == PR_SUCCESS);
151 293
152 » status = _PR_WaitCondVar(me, mon->cvar, mon->cvar->lock, ticks); 294 rv = PR_WaitCondVar(&mon->waitCV, ticks);
295 PR_ASSERT(rv == PR_SUCCESS);
153 296
154 mon->entryCount = entryCount; 297 while (mon->entryCount != 0) {
298 rv = PR_WaitCondVar(&mon->entryCV, PR_INTERVAL_NO_TIMEOUT);
299 PR_ASSERT(rv == PR_SUCCESS);
300 }
301 PR_ASSERT(mon->notifyTimes == 0);
302 /* reinstate the interesting information */
303 mon->entryCount = saved_entries;
304 mon->owner = saved_owner;
155 305
156 return status; 306 rv = PR_Unlock(&mon->lock);
307 PR_ASSERT(rv == PR_SUCCESS);
308 return rv;
157 } 309 }
158 310
159 /* 311 /*
160 ** Notify the highest priority thread waiting on the condition 312 ** Notify the highest priority thread waiting on the condition
161 ** variable. If a thread is waiting on the condition variable (using 313 ** variable. If a thread is waiting on the condition variable (using
162 ** PR_Wait) then it is awakened and begins waiting on the monitor's lock. 314 ** PR_Wait) then it is awakened and begins waiting on the monitor's lock.
163 */ 315 */
164 PR_IMPLEMENT(PRStatus) PR_Notify(PRMonitor *mon) 316 PR_IMPLEMENT(PRStatus) PR_Notify(PRMonitor *mon)
165 { 317 {
166 PRThread *me = _PR_MD_CURRENT_THREAD(); 318 _PR_PostNotifyToMonitor(mon, PR_FALSE);
167 if (mon->cvar->lock->owner != me) return PR_FAILURE;
168 PR_NotifyCondVar(mon->cvar);
169 return PR_SUCCESS; 319 return PR_SUCCESS;
170 } 320 }
171 321
172 /* 322 /*
173 ** Notify all of the threads waiting on the condition variable. All of 323 ** Notify all of the threads waiting on the condition variable. All of
174 ** threads are notified in turn. The highest priority thread will 324 ** threads are notified in turn. The highest priority thread will
175 ** probably acquire the monitor first when the monitor is exited. 325 ** probably acquire the monitor first when the monitor is exited.
176 */ 326 */
177 PR_IMPLEMENT(PRStatus) PR_NotifyAll(PRMonitor *mon) 327 PR_IMPLEMENT(PRStatus) PR_NotifyAll(PRMonitor *mon)
178 { 328 {
179 PRThread *me = _PR_MD_CURRENT_THREAD(); 329 _PR_PostNotifyToMonitor(mon, PR_TRUE);
180 if (mon->cvar->lock->owner != me) return PR_FAILURE;
181 PR_NotifyAllCondVar(mon->cvar);
182 return PR_SUCCESS; 330 return PR_SUCCESS;
183 } 331 }
184 332
185 /************************************************************************/ 333 /************************************************************************/
186 334
187 PRUint32 _PR_MonitorToString(PRMonitor *mon, char *buf, PRUint32 buflen) 335 PRUint32 _PR_MonitorToString(PRMonitor *mon, char *buf, PRUint32 buflen)
188 { 336 {
189 PRUint32 nb; 337 PRUint32 nb;
190 338
191 if (mon->cvar->lock->owner) { 339 if (mon->owner) {
192 nb = PR_snprintf(buf, buflen, "[%p] owner=%d[%p] count=%ld", 340 nb = PR_snprintf(buf, buflen, "[%p] owner=%d[%p] count=%ld",
193 » » » mon, mon->cvar->lock->owner->id, 341 » » » mon, mon->owner->id, mon->owner, mon->entryCount);
194 » » » mon->cvar->lock->owner, mon->entryCount);
195 } else { 342 } else {
196 nb = PR_snprintf(buf, buflen, "[%p]", mon); 343 nb = PR_snprintf(buf, buflen, "[%p]", mon);
197 } 344 }
198 return nb; 345 return nb;
199 } 346 }
OLDNEW
« no previous file with comments | « nspr/pr/src/threads/combined/prulock.c ('k') | patches/nspr-darwin.patch » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698