nsprpub/pr/tests/monref.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/monref.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,74 @@
     1.4 +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * This test program demonstrates that PR_ExitMonitor needs to add a
    1.11 + * reference to the PRMonitor object before unlocking the internal
    1.12 + * mutex.
    1.13 + */
    1.14 +
    1.15 +#include "prlog.h"
    1.16 +#include "prmon.h"
    1.17 +#include "prthread.h"
    1.18 +
    1.19 +#include <stdio.h>
    1.20 +#include <stdlib.h>
    1.21 +
    1.22 +/* Protected by the PRMonitor 'mon' in the main function. */
    1.23 +static PRBool done = PR_FALSE;
    1.24 +
    1.25 +static void ThreadFunc(void *arg)
    1.26 +{
    1.27 +    PRMonitor *mon = (PRMonitor *)arg;
    1.28 +    PRStatus rv;
    1.29 +
    1.30 +    PR_EnterMonitor(mon);
    1.31 +    done = PR_TRUE;
    1.32 +    rv = PR_Notify(mon);
    1.33 +    PR_ASSERT(rv == PR_SUCCESS);
    1.34 +    rv = PR_ExitMonitor(mon);
    1.35 +    PR_ASSERT(rv == PR_SUCCESS);
    1.36 +}
    1.37 +
    1.38 +int main()
    1.39 +{
    1.40 +    PRMonitor *mon;
    1.41 +    PRThread *thread;
    1.42 +    PRStatus rv;
    1.43 +
    1.44 +    mon = PR_NewMonitor();
    1.45 +    if (!mon) {
    1.46 +        fprintf(stderr, "PR_NewMonitor failed\n");
    1.47 +        exit(1);
    1.48 +    }
    1.49 +
    1.50 +    thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, mon,
    1.51 +                             PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
    1.52 +                             PR_JOINABLE_THREAD, 0);
    1.53 +    if (!thread) {
    1.54 +        fprintf(stderr, "PR_CreateThread failed\n");
    1.55 +        exit(1);
    1.56 +    }
    1.57 +
    1.58 +    PR_EnterMonitor(mon);
    1.59 +    while (!done) {
    1.60 +        rv = PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
    1.61 +        PR_ASSERT(rv == PR_SUCCESS);
    1.62 +    }
    1.63 +    rv = PR_ExitMonitor(mon);
    1.64 +    PR_ASSERT(rv == PR_SUCCESS);
    1.65 +
    1.66 +    /*
    1.67 +     * Do you agree it should be safe to destroy 'mon' now?
    1.68 +     * See bug 844784 comment 27.
    1.69 +     */
    1.70 +    PR_DestroyMonitor(mon);
    1.71 +
    1.72 +    rv = PR_JoinThread(thread);
    1.73 +    PR_ASSERT(rv == PR_SUCCESS);
    1.74 +
    1.75 +    printf("PASS\n");
    1.76 +    return 0;
    1.77 +}

mercurial