Debian Codegraph @

nsprpub/pr/tests/monref.c

raw
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 PRMonitor* mon = (PRMonitor*)arg;
24 PRStatus rv;
25
26 PR_EnterMonitor(mon);
27 done = PR_TRUE;
28 rv = PR_Notify(mon);
29 PR_ASSERT(rv == PR_SUCCESS);
30 rv = PR_ExitMonitor(mon);
31 PR_ASSERT(rv == PR_SUCCESS);
32 }
33
34 int main() {
35 PRMonitor* mon;
36 PRThread* thread;
37 PRStatus rv;
38
39 mon = PR_NewMonitor();
40 if (!mon) {
41 fprintf(stderr, "PR_NewMonitor failed\n");
42 exit(1);
43 }
44
45 thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, mon, PR_PRIORITY_NORMAL,
46 PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
47 if (!thread) {
48 fprintf(stderr, "PR_CreateThread failed\n");
49 exit(1);
50 }
51
52 PR_EnterMonitor(mon);
53 while (!done) {
54 rv = PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
55 PR_ASSERT(rv == PR_SUCCESS);
56 }
57 rv = PR_ExitMonitor(mon);
58 PR_ASSERT(rv == PR_SUCCESS);
59
60 /*
61 * Do you agree it should be safe to destroy 'mon' now?
62 * See bug 844784 comment 27.
63 */
64 PR_DestroyMonitor(mon);
65
66 rv = PR_JoinThread(thread);
67 PR_ASSERT(rv == PR_SUCCESS);
68
69 printf("PASS\n");
70 return 0;
71 }