nspr: import 3.0 RC1 cutoff from CVS
[mozilla-nspr.git] / nsprpub / pr / tests / initclk.c
blob6540519e6a90791b88eb3d8dfc71a645fcf9b1cb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * This is a regression test for the bug that the interval timer
40 * is not initialized when _PR_CreateCPU calls PR_IntervalNow.
41 * The bug would make this test program finish prematurely,
42 * when the SHORT_TIMEOUT period expires. The correct behavior
43 * is for the test to finish when the LONG_TIMEOUT period expires.
46 #include "prlock.h"
47 #include "prcvar.h"
48 #include "prthread.h"
49 #include "prinrval.h"
50 #include "prlog.h"
51 #include <stdio.h>
52 #include <stdlib.h>
54 /* The timeouts, in milliseconds */
55 #define SHORT_TIMEOUT 1000
56 #define LONG_TIMEOUT 3000
58 PRLock *lock1, *lock2;
59 PRCondVar *cv1, *cv2;
61 void ThreadFunc(void *arg)
63 PR_Lock(lock1);
64 PR_WaitCondVar(cv1, PR_MillisecondsToInterval(SHORT_TIMEOUT));
65 PR_Unlock(lock1);
68 int main()
70 PRThread *thread;
71 PRIntervalTime start, end;
72 PRUint32 elapsed_ms;
74 lock1 = PR_NewLock();
75 PR_ASSERT(NULL != lock1);
76 cv1 = PR_NewCondVar(lock1);
77 PR_ASSERT(NULL != cv1);
78 lock2 = PR_NewLock();
79 PR_ASSERT(NULL != lock2);
80 cv2 = PR_NewCondVar(lock2);
81 PR_ASSERT(NULL != cv2);
82 start = PR_IntervalNow();
83 thread = PR_CreateThread(
84 PR_USER_THREAD,
85 ThreadFunc,
86 NULL,
87 PR_PRIORITY_NORMAL,
88 PR_LOCAL_THREAD,
89 PR_JOINABLE_THREAD,
90 0);
91 PR_ASSERT(NULL != thread);
92 PR_Lock(lock2);
93 PR_WaitCondVar(cv2, PR_MillisecondsToInterval(LONG_TIMEOUT));
94 PR_Unlock(lock2);
95 PR_JoinThread(thread);
96 end = PR_IntervalNow();
97 elapsed_ms = PR_IntervalToMilliseconds((PRIntervalTime)(end - start));
98 /* Allow 100ms imprecision */
99 if (elapsed_ms < LONG_TIMEOUT - 100 || elapsed_ms > LONG_TIMEOUT + 100) {
100 printf("Elapsed time should be %u ms but is %u ms\n",
101 LONG_TIMEOUT, elapsed_ms);
102 printf("FAIL\n");
103 exit(1);
105 printf("Elapsed time: %u ms, expected time: %u ms\n",
106 LONG_TIMEOUT, elapsed_ms);
107 printf("PASS\n");
108 return 0;