sleep.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include "nspr.h"
  6. #if defined(XP_UNIX) || defined(XP_OS2)
  7. #include <stdio.h>
  8. #ifndef XP_OS2
  9. #include <unistd.h>
  10. #endif
  11. #include <sys/time.h>
  12. #if defined(HAVE_SVID_GETTOD)
  13. #define GTOD(_a) gettimeofday(_a)
  14. #else
  15. #define GTOD(_a) gettimeofday((_a), NULL)
  16. #endif
  17. static PRIntn rv = 0;
  18. static void Other(void *unused)
  19. {
  20. PRIntn didit = 0;
  21. while (PR_SUCCESS == PR_Sleep(PR_MillisecondsToInterval(250)))
  22. {
  23. fprintf(stderr, ".");
  24. didit += 1;
  25. }
  26. if (didit < 5) {
  27. rv = 1;
  28. }
  29. }
  30. int main(int argc, char **argv)
  31. {
  32. PRUint32 elapsed;
  33. PRThread *thread;
  34. struct timeval timein, timeout;
  35. PRInt32 onePercent = 3000000UL / 100UL;
  36. fprintf (stderr, "First sleep will sleep 3 seconds.\n");
  37. fprintf (stderr, " sleep 1 begin\n");
  38. (void)GTOD(&timein);
  39. sleep (3);
  40. (void)GTOD(&timeout);
  41. fprintf (stderr, " sleep 1 end\n");
  42. elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
  43. elapsed += (timeout.tv_usec - timein.tv_usec);
  44. fprintf(stderr, "elapsed %u usecs\n", elapsed);
  45. if (labs(elapsed - 3000000UL) > onePercent) {
  46. rv = 1;
  47. }
  48. PR_Init (PR_USER_THREAD, PR_PRIORITY_NORMAL, 100);
  49. PR_STDIO_INIT();
  50. fprintf (stderr, "Second sleep should do the same (does it?).\n");
  51. fprintf (stderr, " sleep 2 begin\n");
  52. (void)GTOD(&timein);
  53. sleep (3);
  54. (void)GTOD(&timeout);
  55. fprintf (stderr, " sleep 2 end\n");
  56. elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
  57. elapsed += (timeout.tv_usec - timein.tv_usec);
  58. fprintf(stderr, "elapsed %u usecs\n", elapsed);
  59. if (labs(elapsed - 3000000UL) > onePercent) {
  60. rv = 1;
  61. }
  62. fprintf (stderr, "What happens to other threads?\n");
  63. fprintf (stderr, "You should see dots every quarter second.\n");
  64. fprintf (stderr, "If you don't, you're probably running on classic NSPR.\n");
  65. thread = PR_CreateThread(
  66. PR_USER_THREAD, Other, NULL, PR_PRIORITY_NORMAL,
  67. PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
  68. fprintf (stderr, " sleep 2 begin\n");
  69. (void)GTOD(&timein);
  70. sleep (3);
  71. (void)GTOD(&timeout);
  72. fprintf (stderr, " sleep 2 end\n");
  73. PR_Interrupt(thread);
  74. PR_JoinThread(thread);
  75. elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
  76. elapsed += (timeout.tv_usec - timein.tv_usec);
  77. fprintf(stderr, "elapsed %u usecs\n", elapsed);
  78. if (labs(elapsed - 3000000UL) > onePercent) {
  79. rv = 1;
  80. }
  81. fprintf(stderr, "%s\n", (0 == rv) ? "PASSED" : "FAILED");
  82. return rv;
  83. }
  84. #else /* defined(XP_UNIX) */
  85. PRIntn main()
  86. {
  87. return 2;
  88. }
  89. #endif /* defined(XP_UNIX) */