nsprpub/pr/tests/monref.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * This test program demonstrates that PR_ExitMonitor needs to add a
michael@0 8 * reference to the PRMonitor object before unlocking the internal
michael@0 9 * mutex.
michael@0 10 */
michael@0 11
michael@0 12 #include "prlog.h"
michael@0 13 #include "prmon.h"
michael@0 14 #include "prthread.h"
michael@0 15
michael@0 16 #include <stdio.h>
michael@0 17 #include <stdlib.h>
michael@0 18
michael@0 19 /* Protected by the PRMonitor 'mon' in the main function. */
michael@0 20 static PRBool done = PR_FALSE;
michael@0 21
michael@0 22 static void ThreadFunc(void *arg)
michael@0 23 {
michael@0 24 PRMonitor *mon = (PRMonitor *)arg;
michael@0 25 PRStatus rv;
michael@0 26
michael@0 27 PR_EnterMonitor(mon);
michael@0 28 done = PR_TRUE;
michael@0 29 rv = PR_Notify(mon);
michael@0 30 PR_ASSERT(rv == PR_SUCCESS);
michael@0 31 rv = PR_ExitMonitor(mon);
michael@0 32 PR_ASSERT(rv == PR_SUCCESS);
michael@0 33 }
michael@0 34
michael@0 35 int main()
michael@0 36 {
michael@0 37 PRMonitor *mon;
michael@0 38 PRThread *thread;
michael@0 39 PRStatus rv;
michael@0 40
michael@0 41 mon = PR_NewMonitor();
michael@0 42 if (!mon) {
michael@0 43 fprintf(stderr, "PR_NewMonitor failed\n");
michael@0 44 exit(1);
michael@0 45 }
michael@0 46
michael@0 47 thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, mon,
michael@0 48 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
michael@0 49 PR_JOINABLE_THREAD, 0);
michael@0 50 if (!thread) {
michael@0 51 fprintf(stderr, "PR_CreateThread failed\n");
michael@0 52 exit(1);
michael@0 53 }
michael@0 54
michael@0 55 PR_EnterMonitor(mon);
michael@0 56 while (!done) {
michael@0 57 rv = PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
michael@0 58 PR_ASSERT(rv == PR_SUCCESS);
michael@0 59 }
michael@0 60 rv = PR_ExitMonitor(mon);
michael@0 61 PR_ASSERT(rv == PR_SUCCESS);
michael@0 62
michael@0 63 /*
michael@0 64 * Do you agree it should be safe to destroy 'mon' now?
michael@0 65 * See bug 844784 comment 27.
michael@0 66 */
michael@0 67 PR_DestroyMonitor(mon);
michael@0 68
michael@0 69 rv = PR_JoinThread(thread);
michael@0 70 PR_ASSERT(rv == PR_SUCCESS);
michael@0 71
michael@0 72 printf("PASS\n");
michael@0 73 return 0;
michael@0 74 }

mercurial