nsprpub/pr/tests/initclk.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/initclk.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     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 is a regression test for the bug that the interval timer
    1.11 + * is not initialized when _PR_CreateCPU calls PR_IntervalNow.
    1.12 + * The bug would make this test program finish prematurely,
    1.13 + * when the SHORT_TIMEOUT period expires.  The correct behavior
    1.14 + * is for the test to finish when the LONG_TIMEOUT period expires.
    1.15 + */
    1.16 +
    1.17 +#include "prlock.h"
    1.18 +#include "prcvar.h"
    1.19 +#include "prthread.h"
    1.20 +#include "prinrval.h"
    1.21 +#include "prlog.h"
    1.22 +#include <stdio.h>
    1.23 +#include <stdlib.h>
    1.24 +
    1.25 +/* The timeouts, in milliseconds */
    1.26 +#define SHORT_TIMEOUT 1000
    1.27 +#define LONG_TIMEOUT 3000
    1.28 +
    1.29 +PRLock *lock1, *lock2;
    1.30 +PRCondVar *cv1, *cv2;
    1.31 +
    1.32 +void ThreadFunc(void *arg)
    1.33 +{
    1.34 +    PR_Lock(lock1);
    1.35 +    PR_WaitCondVar(cv1, PR_MillisecondsToInterval(SHORT_TIMEOUT));
    1.36 +    PR_Unlock(lock1);
    1.37 +}
    1.38 +
    1.39 +int main(int argc, char **argv)
    1.40 +{
    1.41 +    PRThread *thread;
    1.42 +    PRIntervalTime start, end;
    1.43 +    PRUint32 elapsed_ms;
    1.44 +
    1.45 +    lock1 = PR_NewLock();
    1.46 +    PR_ASSERT(NULL != lock1);
    1.47 +    cv1 = PR_NewCondVar(lock1);
    1.48 +    PR_ASSERT(NULL != cv1);
    1.49 +    lock2 = PR_NewLock();
    1.50 +    PR_ASSERT(NULL != lock2);
    1.51 +    cv2 = PR_NewCondVar(lock2);
    1.52 +    PR_ASSERT(NULL != cv2);
    1.53 +    start = PR_IntervalNow();
    1.54 +    thread = PR_CreateThread(
    1.55 +            PR_USER_THREAD,
    1.56 +            ThreadFunc,
    1.57 +            NULL,
    1.58 +            PR_PRIORITY_NORMAL,
    1.59 +            PR_LOCAL_THREAD,
    1.60 +            PR_JOINABLE_THREAD,
    1.61 +            0);
    1.62 +    PR_ASSERT(NULL != thread);
    1.63 +    PR_Lock(lock2);
    1.64 +    PR_WaitCondVar(cv2, PR_MillisecondsToInterval(LONG_TIMEOUT));
    1.65 +    PR_Unlock(lock2);
    1.66 +    PR_JoinThread(thread);
    1.67 +    end = PR_IntervalNow();
    1.68 +    elapsed_ms = PR_IntervalToMilliseconds((PRIntervalTime)(end - start));
    1.69 +    /* Allow 100ms imprecision */
    1.70 +    if (elapsed_ms < LONG_TIMEOUT - 100 || elapsed_ms > LONG_TIMEOUT + 100) {
    1.71 +        printf("Elapsed time should be %u ms but is %u ms\n",
    1.72 +                LONG_TIMEOUT, elapsed_ms);
    1.73 +        printf("FAIL\n");
    1.74 +        exit(1);
    1.75 +    }
    1.76 +	printf("Elapsed time: %u ms, expected time: %u ms\n",
    1.77 +               LONG_TIMEOUT, elapsed_ms);
    1.78 +    printf("PASS\n");
    1.79 +    return 0;
    1.80 +}

mercurial