michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * This is a regression test for the bug that the interval timer michael@0: * is not initialized when _PR_CreateCPU calls PR_IntervalNow. michael@0: * The bug would make this test program finish prematurely, michael@0: * when the SHORT_TIMEOUT period expires. The correct behavior michael@0: * is for the test to finish when the LONG_TIMEOUT period expires. michael@0: */ michael@0: michael@0: #include "prlock.h" michael@0: #include "prcvar.h" michael@0: #include "prthread.h" michael@0: #include "prinrval.h" michael@0: #include "prlog.h" michael@0: #include michael@0: #include michael@0: michael@0: /* The timeouts, in milliseconds */ michael@0: #define SHORT_TIMEOUT 1000 michael@0: #define LONG_TIMEOUT 3000 michael@0: michael@0: PRLock *lock1, *lock2; michael@0: PRCondVar *cv1, *cv2; michael@0: michael@0: void ThreadFunc(void *arg) michael@0: { michael@0: PR_Lock(lock1); michael@0: PR_WaitCondVar(cv1, PR_MillisecondsToInterval(SHORT_TIMEOUT)); michael@0: PR_Unlock(lock1); michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRThread *thread; michael@0: PRIntervalTime start, end; michael@0: PRUint32 elapsed_ms; michael@0: michael@0: lock1 = PR_NewLock(); michael@0: PR_ASSERT(NULL != lock1); michael@0: cv1 = PR_NewCondVar(lock1); michael@0: PR_ASSERT(NULL != cv1); michael@0: lock2 = PR_NewLock(); michael@0: PR_ASSERT(NULL != lock2); michael@0: cv2 = PR_NewCondVar(lock2); michael@0: PR_ASSERT(NULL != cv2); michael@0: start = PR_IntervalNow(); michael@0: thread = PR_CreateThread( michael@0: PR_USER_THREAD, michael@0: ThreadFunc, michael@0: NULL, michael@0: PR_PRIORITY_NORMAL, michael@0: PR_LOCAL_THREAD, michael@0: PR_JOINABLE_THREAD, michael@0: 0); michael@0: PR_ASSERT(NULL != thread); michael@0: PR_Lock(lock2); michael@0: PR_WaitCondVar(cv2, PR_MillisecondsToInterval(LONG_TIMEOUT)); michael@0: PR_Unlock(lock2); michael@0: PR_JoinThread(thread); michael@0: end = PR_IntervalNow(); michael@0: elapsed_ms = PR_IntervalToMilliseconds((PRIntervalTime)(end - start)); michael@0: /* Allow 100ms imprecision */ michael@0: if (elapsed_ms < LONG_TIMEOUT - 100 || elapsed_ms > LONG_TIMEOUT + 100) { michael@0: printf("Elapsed time should be %u ms but is %u ms\n", michael@0: LONG_TIMEOUT, elapsed_ms); michael@0: printf("FAIL\n"); michael@0: exit(1); michael@0: } michael@0: printf("Elapsed time: %u ms, expected time: %u ms\n", michael@0: LONG_TIMEOUT, elapsed_ms); michael@0: printf("PASS\n"); michael@0: return 0; michael@0: }