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 "plerror.h"
|
---|
39 | #include "plgetopt.h"
|
---|
40 |
|
---|
41 | #include "prinit.h"
|
---|
42 | #include "prprf.h"
|
---|
43 | #include "prio.h"
|
---|
44 | #include "prcvar.h"
|
---|
45 | #include "prmon.h"
|
---|
46 | #include "prcmon.h"
|
---|
47 | #include "prlock.h"
|
---|
48 | #include "prerror.h"
|
---|
49 | #include "prinit.h"
|
---|
50 | #include "prinrval.h"
|
---|
51 | #include "prthread.h"
|
---|
52 |
|
---|
53 | static PRLock *ml = NULL;
|
---|
54 | static PRIntervalTime base;
|
---|
55 | static PRFileDesc *err = NULL;
|
---|
56 |
|
---|
57 | typedef struct CMonShared
|
---|
58 | {
|
---|
59 | PRInt32 o1, o2;
|
---|
60 | } CMonShared;
|
---|
61 |
|
---|
62 | typedef struct MonShared
|
---|
63 | {
|
---|
64 | PRMonitor *o1, *o2;
|
---|
65 | } MonShared;
|
---|
66 |
|
---|
67 | typedef struct LockShared
|
---|
68 | {
|
---|
69 | PRLock *o1, *o2;
|
---|
70 | PRCondVar *cv1, *cv2;
|
---|
71 | } LockShared;
|
---|
72 |
|
---|
73 | static void LogNow(const char *msg, PRStatus rv)
|
---|
74 | {
|
---|
75 | PRIntervalTime now = PR_IntervalNow();
|
---|
76 | PR_Lock(ml);
|
---|
77 | PR_fprintf(err, "%6ld: %s", (now - base), msg);
|
---|
78 | if (PR_FAILURE == rv) PL_FPrintError(err, " ");
|
---|
79 | else PR_fprintf(err, "\n");
|
---|
80 | PR_Unlock(ml);
|
---|
81 | } /* LogNow */
|
---|
82 |
|
---|
83 | static void Help(void)
|
---|
84 | {
|
---|
85 | PR_fprintf(err, "Usage: [-[d][l][m][c]] [-h]\n");
|
---|
86 | PR_fprintf(err, "\t-d debug mode (default: FALSE)\n");
|
---|
87 | PR_fprintf(err, "\t-l test with locks (default: FALSE)\n");
|
---|
88 | PR_fprintf(err, "\t-m tests with monitors (default: FALSE)\n");
|
---|
89 | PR_fprintf(err, "\t-c tests with cmonitors (default: FALSE)\n");
|
---|
90 | PR_fprintf(err, "\t-h help\n");
|
---|
91 | } /* Help */
|
---|
92 |
|
---|
93 | static void PR_CALLBACK T2CMon(void *arg)
|
---|
94 | {
|
---|
95 | PRStatus rv;
|
---|
96 | CMonShared *shared = (CMonShared*)arg;
|
---|
97 |
|
---|
98 | PR_CEnterMonitor(&shared->o1);
|
---|
99 | LogNow("T2 waiting 5 seconds on o1", PR_SUCCESS);
|
---|
100 | rv = PR_CWait(&shared->o1, PR_SecondsToInterval(5));
|
---|
101 | if (PR_SUCCESS == rv) LogNow("T2 resuming on o1", rv);
|
---|
102 | else LogNow("T2 wait failed on o1", rv);
|
---|
103 |
|
---|
104 | rv = PR_CNotify(&shared->o1);
|
---|
105 | if (PR_SUCCESS == rv) LogNow("T2 notified o1", rv);
|
---|
106 | else LogNow("T2 notify on o1 failed", rv);
|
---|
107 |
|
---|
108 | PR_CExitMonitor(&shared->o1);
|
---|
109 | } /* T2CMon */
|
---|
110 |
|
---|
111 | static void PR_CALLBACK T3CMon(void *arg)
|
---|
112 | {
|
---|
113 | PRStatus rv;
|
---|
114 | CMonShared *shared = (CMonShared*)arg;
|
---|
115 |
|
---|
116 | PR_CEnterMonitor(&shared->o2);
|
---|
117 | LogNow("T3 waiting 5 seconds on o2", PR_SUCCESS);
|
---|
118 | rv = PR_CWait(&shared->o2, PR_SecondsToInterval(5));
|
---|
119 | if (PR_SUCCESS == rv) LogNow("T3 resuming on o2", rv);
|
---|
120 | else LogNow("T3 wait failed on o2", rv);
|
---|
121 | rv = PR_CNotify(&shared->o2);
|
---|
122 | LogNow("T3 notify on o2", rv);
|
---|
123 | PR_CExitMonitor(&shared->o2);
|
---|
124 |
|
---|
125 | } /* T3CMon */
|
---|
126 |
|
---|
127 | static CMonShared sharedCM;
|
---|
128 |
|
---|
129 | static void T1CMon(void)
|
---|
130 | {
|
---|
131 | PRStatus rv;
|
---|
132 | PRThread *t2, *t3;
|
---|
133 |
|
---|
134 | PR_fprintf(err, "\n**********************************\n");
|
---|
135 | PR_fprintf(err, " CACHED MONITORS\n");
|
---|
136 | PR_fprintf(err, "**********************************\n");
|
---|
137 |
|
---|
138 | base = PR_IntervalNow();
|
---|
139 |
|
---|
140 | PR_CEnterMonitor(&sharedCM.o1);
|
---|
141 | LogNow("T1 waiting 3 seconds on o1", PR_SUCCESS);
|
---|
142 | rv = PR_CWait(&sharedCM.o1, PR_SecondsToInterval(3));
|
---|
143 | if (PR_SUCCESS == rv) LogNow("T1 resuming on o1", rv);
|
---|
144 | else LogNow("T1 wait on o1 failed", rv);
|
---|
145 | PR_CExitMonitor(&sharedCM.o1);
|
---|
146 |
|
---|
147 | LogNow("T1 creating T2", PR_SUCCESS);
|
---|
148 | t2 = PR_CreateThread(
|
---|
149 | PR_USER_THREAD, T2CMon, &sharedCM, PR_PRIORITY_NORMAL,
|
---|
150 | PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
151 |
|
---|
152 | LogNow("T1 creating T3", PR_SUCCESS);
|
---|
153 | t3 = PR_CreateThread(
|
---|
154 | PR_USER_THREAD, T3CMon, &sharedCM, PR_PRIORITY_NORMAL,
|
---|
155 | PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
156 |
|
---|
157 | PR_CEnterMonitor(&sharedCM.o2);
|
---|
158 | LogNow("T1 waiting forever on o2", PR_SUCCESS);
|
---|
159 | rv = PR_CWait(&sharedCM.o2, PR_INTERVAL_NO_TIMEOUT);
|
---|
160 | if (PR_SUCCESS == rv) LogNow("T1 resuming on o2", rv);
|
---|
161 | else LogNow("T1 wait on o2 failed", rv);
|
---|
162 | PR_CExitMonitor(&sharedCM.o2);
|
---|
163 |
|
---|
164 | (void)PR_JoinThread(t2);
|
---|
165 | (void)PR_JoinThread(t3);
|
---|
166 |
|
---|
167 | } /* T1CMon */
|
---|
168 |
|
---|
169 | static void PR_CALLBACK T2Mon(void *arg)
|
---|
170 | {
|
---|
171 | PRStatus rv;
|
---|
172 | MonShared *shared = (MonShared*)arg;
|
---|
173 |
|
---|
174 | PR_EnterMonitor(shared->o1);
|
---|
175 | LogNow("T2 waiting 5 seconds on o1", PR_SUCCESS);
|
---|
176 | rv = PR_Wait(shared->o1, PR_SecondsToInterval(5));
|
---|
177 | if (PR_SUCCESS == rv) LogNow("T2 resuming on o1", rv);
|
---|
178 | else LogNow("T2 wait failed on o1", rv);
|
---|
179 |
|
---|
180 | rv = PR_Notify(shared->o1);
|
---|
181 | if (PR_SUCCESS == rv) LogNow("T2 notified o1", rv);
|
---|
182 | else LogNow("T2 notify on o1 failed", rv);
|
---|
183 |
|
---|
184 | PR_ExitMonitor(shared->o1);
|
---|
185 | } /* T2Mon */
|
---|
186 |
|
---|
187 | static void PR_CALLBACK T3Mon(void *arg)
|
---|
188 | {
|
---|
189 | PRStatus rv;
|
---|
190 | MonShared *shared = (MonShared*)arg;
|
---|
191 |
|
---|
192 | PR_EnterMonitor(shared->o2);
|
---|
193 | LogNow("T3 waiting 5 seconds on o2", PR_SUCCESS);
|
---|
194 | rv = PR_Wait(shared->o2, PR_SecondsToInterval(5));
|
---|
195 | if (PR_SUCCESS == rv) LogNow("T3 resuming on o2", rv);
|
---|
196 | else LogNow("T3 wait failed on o2", rv);
|
---|
197 | rv = PR_Notify(shared->o2);
|
---|
198 | LogNow("T3 notify on o2", rv);
|
---|
199 | PR_ExitMonitor(shared->o2);
|
---|
200 |
|
---|
201 | } /* T3Mon */
|
---|
202 |
|
---|
203 | static MonShared sharedM;
|
---|
204 | static void T1Mon(void)
|
---|
205 | {
|
---|
206 | PRStatus rv;
|
---|
207 | PRThread *t2, *t3;
|
---|
208 |
|
---|
209 | PR_fprintf(err, "\n**********************************\n");
|
---|
210 | PR_fprintf(err, " MONITORS\n");
|
---|
211 | PR_fprintf(err, "**********************************\n");
|
---|
212 |
|
---|
213 | sharedM.o1 = PR_NewMonitor();
|
---|
214 | sharedM.o2 = PR_NewMonitor();
|
---|
215 |
|
---|
216 | base = PR_IntervalNow();
|
---|
217 |
|
---|
218 | PR_EnterMonitor(sharedM.o1);
|
---|
219 | LogNow("T1 waiting 3 seconds on o1", PR_SUCCESS);
|
---|
220 | rv = PR_Wait(sharedM.o1, PR_SecondsToInterval(3));
|
---|
221 | if (PR_SUCCESS == rv) LogNow("T1 resuming on o1", rv);
|
---|
222 | else LogNow("T1 wait on o1 failed", rv);
|
---|
223 | PR_ExitMonitor(sharedM.o1);
|
---|
224 |
|
---|
225 | LogNow("T1 creating T2", PR_SUCCESS);
|
---|
226 | t2 = PR_CreateThread(
|
---|
227 | PR_USER_THREAD, T2Mon, &sharedM, PR_PRIORITY_NORMAL,
|
---|
228 | PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
229 |
|
---|
230 | LogNow("T1 creating T3", PR_SUCCESS);
|
---|
231 | t3 = PR_CreateThread(
|
---|
232 | PR_USER_THREAD, T3Mon, &sharedM, PR_PRIORITY_NORMAL,
|
---|
233 | PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
234 |
|
---|
235 | PR_EnterMonitor(sharedM.o2);
|
---|
236 | LogNow("T1 waiting forever on o2", PR_SUCCESS);
|
---|
237 | rv = PR_Wait(sharedM.o2, PR_INTERVAL_NO_TIMEOUT);
|
---|
238 | if (PR_SUCCESS == rv) LogNow("T1 resuming on o2", rv);
|
---|
239 | else LogNow("T1 wait on o2 failed", rv);
|
---|
240 | PR_ExitMonitor(sharedM.o2);
|
---|
241 |
|
---|
242 | (void)PR_JoinThread(t2);
|
---|
243 | (void)PR_JoinThread(t3);
|
---|
244 |
|
---|
245 | PR_DestroyMonitor(sharedM.o1);
|
---|
246 | PR_DestroyMonitor(sharedM.o2);
|
---|
247 |
|
---|
248 | } /* T1Mon */
|
---|
249 |
|
---|
250 | static void PR_CALLBACK T2Lock(void *arg)
|
---|
251 | {
|
---|
252 | PRStatus rv;
|
---|
253 | LockShared *shared = (LockShared*)arg;
|
---|
254 |
|
---|
255 | PR_Lock(shared->o1);
|
---|
256 | LogNow("T2 waiting 5 seconds on o1", PR_SUCCESS);
|
---|
257 | rv = PR_WaitCondVar(shared->cv1, PR_SecondsToInterval(5));
|
---|
258 | if (PR_SUCCESS == rv) LogNow("T2 resuming on o1", rv);
|
---|
259 | else LogNow("T2 wait failed on o1", rv);
|
---|
260 |
|
---|
261 | rv = PR_NotifyCondVar(shared->cv1);
|
---|
262 | if (PR_SUCCESS == rv) LogNow("T2 notified o1", rv);
|
---|
263 | else LogNow("T2 notify on o1 failed", rv);
|
---|
264 |
|
---|
265 | PR_Unlock(shared->o1);
|
---|
266 | } /* T2Lock */
|
---|
267 |
|
---|
268 | static void PR_CALLBACK T3Lock(void *arg)
|
---|
269 | {
|
---|
270 | PRStatus rv;
|
---|
271 | LockShared *shared = (LockShared*)arg;
|
---|
272 |
|
---|
273 | PR_Lock(shared->o2);
|
---|
274 | LogNow("T3 waiting 5 seconds on o2", PR_SUCCESS);
|
---|
275 | rv = PR_WaitCondVar(shared->cv2, PR_SecondsToInterval(5));
|
---|
276 | if (PR_SUCCESS == rv) LogNow("T3 resuming on o2", rv);
|
---|
277 | else LogNow("T3 wait failed on o2", rv);
|
---|
278 | rv = PR_NotifyCondVar(shared->cv2);
|
---|
279 | LogNow("T3 notify on o2", rv);
|
---|
280 | PR_Unlock(shared->o2);
|
---|
281 |
|
---|
282 | } /* T3Lock */
|
---|
283 |
|
---|
284 | /*
|
---|
285 | ** Make shared' a static variable for Win16
|
---|
286 | */
|
---|
287 | static LockShared sharedL;
|
---|
288 |
|
---|
289 | static void T1Lock(void)
|
---|
290 | {
|
---|
291 | PRStatus rv;
|
---|
292 | PRThread *t2, *t3;
|
---|
293 | sharedL.o1 = PR_NewLock();
|
---|
294 | sharedL.o2 = PR_NewLock();
|
---|
295 | sharedL.cv1 = PR_NewCondVar(sharedL.o1);
|
---|
296 | sharedL.cv2 = PR_NewCondVar(sharedL.o2);
|
---|
297 |
|
---|
298 | PR_fprintf(err, "\n**********************************\n");
|
---|
299 | PR_fprintf(err, " LOCKS\n");
|
---|
300 | PR_fprintf(err, "**********************************\n");
|
---|
301 |
|
---|
302 | base = PR_IntervalNow();
|
---|
303 |
|
---|
304 | PR_Lock(sharedL.o1);
|
---|
305 | LogNow("T1 waiting 3 seconds on o1", PR_SUCCESS);
|
---|
306 | rv = PR_WaitCondVar(sharedL.cv1, PR_SecondsToInterval(3));
|
---|
307 | if (PR_SUCCESS == rv) LogNow("T1 resuming on o1", rv);
|
---|
308 | else LogNow("T1 wait on o1 failed", rv);
|
---|
309 | PR_Unlock(sharedL.o1);
|
---|
310 |
|
---|
311 | LogNow("T1 creating T2", PR_SUCCESS);
|
---|
312 | t2 = PR_CreateThread(
|
---|
313 | PR_USER_THREAD, T2Lock, &sharedL, PR_PRIORITY_NORMAL,
|
---|
314 | PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
315 |
|
---|
316 | LogNow("T1 creating T3", PR_SUCCESS);
|
---|
317 | t3 = PR_CreateThread(
|
---|
318 | PR_USER_THREAD, T3Lock, &sharedL, PR_PRIORITY_NORMAL,
|
---|
319 | PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
320 |
|
---|
321 | PR_Lock(sharedL.o2);
|
---|
322 | LogNow("T1 waiting forever on o2", PR_SUCCESS);
|
---|
323 | rv = PR_WaitCondVar(sharedL.cv2, PR_INTERVAL_NO_TIMEOUT);
|
---|
324 | if (PR_SUCCESS == rv) LogNow("T1 resuming on o2", rv);
|
---|
325 | else LogNow("T1 wait on o2 failed", rv);
|
---|
326 | PR_Unlock(sharedL.o2);
|
---|
327 |
|
---|
328 | (void)PR_JoinThread(t2);
|
---|
329 | (void)PR_JoinThread(t3);
|
---|
330 |
|
---|
331 | PR_DestroyLock(sharedL.o1);
|
---|
332 | PR_DestroyLock(sharedL.o2);
|
---|
333 | PR_DestroyCondVar(sharedL.cv1);
|
---|
334 | PR_DestroyCondVar(sharedL.cv2);
|
---|
335 | } /* T1Lock */
|
---|
336 |
|
---|
337 | static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv )
|
---|
338 | {
|
---|
339 | PLOptStatus os;
|
---|
340 | PLOptState *opt = PL_CreateOptState(argc, argv, "dhlmc");
|
---|
341 | PRBool locks = PR_FALSE, monitors = PR_FALSE, cmonitors = PR_FALSE;
|
---|
342 |
|
---|
343 | err = PR_GetSpecialFD(PR_StandardError);
|
---|
344 |
|
---|
345 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
346 | {
|
---|
347 | if (PL_OPT_BAD == os) continue;
|
---|
348 | switch (opt->option)
|
---|
349 | {
|
---|
350 | case 'd': /* debug mode (noop) */
|
---|
351 | break;
|
---|
352 | case 'l': /* locks */
|
---|
353 | locks = PR_TRUE;
|
---|
354 | break;
|
---|
355 | case 'm': /* monitors */
|
---|
356 | monitors = PR_TRUE;
|
---|
357 | break;
|
---|
358 | case 'c': /* cached monitors */
|
---|
359 | cmonitors = PR_TRUE;
|
---|
360 | break;
|
---|
361 | case 'h': /* needs guidance */
|
---|
362 | default:
|
---|
363 | Help();
|
---|
364 | return 2;
|
---|
365 | }
|
---|
366 | }
|
---|
367 | PL_DestroyOptState(opt);
|
---|
368 |
|
---|
369 | ml = PR_NewLock();
|
---|
370 | if (locks) T1Lock();
|
---|
371 | if (monitors) T1Mon();
|
---|
372 | if (cmonitors) T1CMon();
|
---|
373 |
|
---|
374 | PR_DestroyLock(ml);
|
---|
375 |
|
---|
376 | PR_fprintf(err, "Done!\n");
|
---|
377 | return 0;
|
---|
378 | } /* main */
|
---|
379 |
|
---|
380 |
|
---|
381 | PRIntn main(PRIntn argc, char **argv)
|
---|
382 | {
|
---|
383 | PRIntn rv;
|
---|
384 |
|
---|
385 | PR_STDIO_INIT();
|
---|
386 | rv = PR_Initialize(RealMain, argc, argv, 0);
|
---|
387 | return rv;
|
---|
388 | } /* main */
|
---|
389 | /* xnotify.c */
|
---|