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.

     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/. */
     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  */
    12 #include "prlog.h"
    13 #include "prmon.h"
    14 #include "prthread.h"
    16 #include <stdio.h>
    17 #include <stdlib.h>
    19 /* Protected by the PRMonitor 'mon' in the main function. */
    20 static PRBool done = PR_FALSE;
    22 static void ThreadFunc(void *arg)
    23 {
    24     PRMonitor *mon = (PRMonitor *)arg;
    25     PRStatus rv;
    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 }
    35 int main()
    36 {
    37     PRMonitor *mon;
    38     PRThread *thread;
    39     PRStatus rv;
    41     mon = PR_NewMonitor();
    42     if (!mon) {
    43         fprintf(stderr, "PR_NewMonitor failed\n");
    44         exit(1);
    45     }
    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     }
    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);
    63     /*
    64      * Do you agree it should be safe to destroy 'mon' now?
    65      * See bug 844784 comment 27.
    66      */
    67     PR_DestroyMonitor(mon);
    69     rv = PR_JoinThread(thread);
    70     PR_ASSERT(rv == PR_SUCCESS);
    72     printf("PASS\n");
    73     return 0;
    74 }

mercurial