nsprpub/pr/tests/monref.c

branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
equal deleted inserted replaced
-1:000000000000 0:cee23db5e868
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 /*
7 * This test program demonstrates that PR_ExitMonitor needs to add a
8 * reference to the PRMonitor object before unlocking the internal
9 * mutex.
10 */
11
12 #include "prlog.h"
13 #include "prmon.h"
14 #include "prthread.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 /* Protected by the PRMonitor 'mon' in the main function. */
20 static PRBool done = PR_FALSE;
21
22 static void ThreadFunc(void *arg)
23 {
24 PRMonitor *mon = (PRMonitor *)arg;
25 PRStatus rv;
26
27 PR_EnterMonitor(mon);
28 done = PR_TRUE;
29 rv = PR_Notify(mon);
30 PR_ASSERT(rv == PR_SUCCESS);
31 rv = PR_ExitMonitor(mon);
32 PR_ASSERT(rv == PR_SUCCESS);
33 }
34
35 int main()
36 {
37 PRMonitor *mon;
38 PRThread *thread;
39 PRStatus rv;
40
41 mon = PR_NewMonitor();
42 if (!mon) {
43 fprintf(stderr, "PR_NewMonitor failed\n");
44 exit(1);
45 }
46
47 thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, mon,
48 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
49 PR_JOINABLE_THREAD, 0);
50 if (!thread) {
51 fprintf(stderr, "PR_CreateThread failed\n");
52 exit(1);
53 }
54
55 PR_EnterMonitor(mon);
56 while (!done) {
57 rv = PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
58 PR_ASSERT(rv == PR_SUCCESS);
59 }
60 rv = PR_ExitMonitor(mon);
61 PR_ASSERT(rv == PR_SUCCESS);
62
63 /*
64 * Do you agree it should be safe to destroy 'mon' now?
65 * See bug 844784 comment 27.
66 */
67 PR_DestroyMonitor(mon);
68
69 rv = PR_JoinThread(thread);
70 PR_ASSERT(rv == PR_SUCCESS);
71
72 printf("PASS\n");
73 return 0;
74 }

mercurial