initclk.c 2.3 KB

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