home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / sleep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.2 KB  |  109 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "nspr.h"
  20.  
  21. #if defined(XP_UNIX)
  22.  
  23. #include <stdio.h>
  24.  
  25. #include <unistd.h>
  26. #include <sys/time.h>
  27.  
  28. #if defined(SOLARIS)
  29. #define GTOD(_a) gettimeofday(_a)
  30. #else
  31. #define GTOD(_a) gettimeofday(_a, NULL)
  32. #endif
  33.  
  34. static PRIntn rv = 0;
  35.  
  36. static void Other(void *unused)
  37. {
  38.     PRIntn didit = 0;
  39.     while (PR_SUCCESS == PR_Sleep(PR_MillisecondsToInterval(250)))
  40.     {
  41.         fprintf(stderr, ".");
  42.         didit += 1;
  43.     }
  44.     if (didit < 5) rv = 1;
  45. }
  46.  
  47. PRIntn main ()
  48. {
  49.     PRUint32 elapsed;
  50.     PRThread *thread;
  51.     struct timeval timein, timeout;
  52.     PRInt32 onePercent = 3000000UL / 100UL;
  53.  
  54.     fprintf (stderr, "First sleep will sleep 3 seconds.\n");
  55.     fprintf (stderr, "   sleep 1 begin\n");
  56.     (void)GTOD(&timein);
  57.     sleep (3);
  58.     (void)GTOD(&timeout);
  59.     fprintf (stderr, "   sleep 1 end\n");
  60.     elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
  61.     elapsed += (timeout.tv_usec - timein.tv_usec);
  62.     fprintf(stderr, "elapsed %u usecs\n", elapsed);
  63.     if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
  64.  
  65.     PR_Init (PR_USER_THREAD, PR_PRIORITY_NORMAL, 100);
  66.     PR_STDIO_INIT();
  67.  
  68.     fprintf (stderr, "Second sleep should do the same (does it?).\n");
  69.     fprintf (stderr, "   sleep 2 begin\n");
  70.     (void)GTOD(&timein);
  71.     sleep (3);
  72.     (void)GTOD(&timeout);
  73.     fprintf (stderr, "   sleep 2 end\n");
  74.     elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
  75.     elapsed += (timeout.tv_usec - timein.tv_usec);
  76.     fprintf(stderr, "elapsed %u usecs\n", elapsed);
  77.     if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
  78.  
  79.     fprintf (stderr, "What happens to other threads?\n");
  80.     fprintf (stderr, "You should see dots every quarter second.\n");
  81.     fprintf (stderr, "If you don't, you're probably running on classic NSPR.\n");
  82.     thread = PR_CreateThread(
  83.         PR_USER_THREAD, Other, NULL, PR_PRIORITY_NORMAL,
  84.         PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
  85.     fprintf (stderr, "   sleep 2 begin\n");
  86.     (void)GTOD(&timein);
  87.     sleep (3);
  88.     (void)GTOD(&timeout);
  89.     fprintf (stderr, "   sleep 2 end\n");
  90.     PR_Interrupt(thread);
  91.     PR_JoinThread(thread);
  92.     elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
  93.     elapsed += (timeout.tv_usec - timein.tv_usec);
  94.     fprintf(stderr, "elapsed %u usecs\n", elapsed);
  95.     if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
  96.     fprintf(stderr, "%s\n", (0 == rv) ? "PASSED" : "FAILED");
  97.     return rv;
  98. }
  99.  
  100. #else /* defined(XP_UNIX) */
  101.  
  102. PRIntn main()
  103. {
  104.     return 2;
  105. }
  106.  
  107. #endif /*  defined(XP_UNIX) */
  108.  
  109.