1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 is a regression test for the bug that the interval timer
8  * is not initialized when _PR_CreateCPU calls PR_IntervalNow.
9  * The bug would make this test program finish prematurely,
10  * when the SHORT_TIMEOUT period expires.  The correct behavior
11  * is for the test to finish when the LONG_TIMEOUT period expires.
12  */
13 
14 #include "prlock.h"
15 #include "prcvar.h"
16 #include "prthread.h"
17 #include "prinrval.h"
18 #include "prlog.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 /* The timeouts, in milliseconds */
23 #define SHORT_TIMEOUT 1000
24 #define LONG_TIMEOUT 3000
25 
26 PRLock *lock1, *lock2;
27 PRCondVar *cv1, *cv2;
28 
ThreadFunc(void * arg)29 void ThreadFunc(void *arg)
30 {
31     PR_Lock(lock1);
32     PR_WaitCondVar(cv1, PR_MillisecondsToInterval(SHORT_TIMEOUT));
33     PR_Unlock(lock1);
34 }
35 
main(int argc,char ** argv)36 int main(int argc, char **argv)
37 {
38     PRThread *thread;
39     PRIntervalTime start, end;
40     PRUint32 elapsed_ms;
41 
42     lock1 = PR_NewLock();
43     PR_ASSERT(NULL != lock1);
44     cv1 = PR_NewCondVar(lock1);
45     PR_ASSERT(NULL != cv1);
46     lock2 = PR_NewLock();
47     PR_ASSERT(NULL != lock2);
48     cv2 = PR_NewCondVar(lock2);
49     PR_ASSERT(NULL != cv2);
50     start = PR_IntervalNow();
51     thread = PR_CreateThread(
52                  PR_USER_THREAD,
53                  ThreadFunc,
54                  NULL,
55                  PR_PRIORITY_NORMAL,
56                  PR_LOCAL_THREAD,
57                  PR_JOINABLE_THREAD,
58                  0);
59     PR_ASSERT(NULL != thread);
60     PR_Lock(lock2);
61     PR_WaitCondVar(cv2, PR_MillisecondsToInterval(LONG_TIMEOUT));
62     PR_Unlock(lock2);
63     PR_JoinThread(thread);
64     end = PR_IntervalNow();
65     elapsed_ms = PR_IntervalToMilliseconds((PRIntervalTime)(end - start));
66     /* Allow 100ms imprecision */
67     if (elapsed_ms < LONG_TIMEOUT - 100 || elapsed_ms > LONG_TIMEOUT + 100) {
68         printf("Elapsed time should be %u ms but is %u ms\n",
69                LONG_TIMEOUT, elapsed_ms);
70         printf("FAIL\n");
71         exit(1);
72     }
73     printf("Elapsed time: %u ms, expected time: %u ms\n",
74            LONG_TIMEOUT, elapsed_ms);
75     printf("PASS\n");
76     return 0;
77 }
78