nsprpub/pr/tests/sleep.c

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nspr.h"
michael@0 7
michael@0 8 #if defined(XP_UNIX) || defined(XP_OS2)
michael@0 9
michael@0 10 #include <stdio.h>
michael@0 11
michael@0 12 #ifndef XP_OS2
michael@0 13 #include <unistd.h>
michael@0 14 #endif
michael@0 15 #include <sys/time.h>
michael@0 16
michael@0 17 #if defined(HAVE_SVID_GETTOD)
michael@0 18 #define GTOD(_a) gettimeofday(_a)
michael@0 19 #else
michael@0 20 #define GTOD(_a) gettimeofday((_a), NULL)
michael@0 21 #endif
michael@0 22
michael@0 23 static PRIntn rv = 0;
michael@0 24
michael@0 25 static void Other(void *unused)
michael@0 26 {
michael@0 27 PRIntn didit = 0;
michael@0 28 while (PR_SUCCESS == PR_Sleep(PR_MillisecondsToInterval(250)))
michael@0 29 {
michael@0 30 fprintf(stderr, ".");
michael@0 31 didit += 1;
michael@0 32 }
michael@0 33 if (didit < 5) rv = 1;
michael@0 34 }
michael@0 35
michael@0 36 int main(int argc, char **argv)
michael@0 37 {
michael@0 38 PRUint32 elapsed;
michael@0 39 PRThread *thread;
michael@0 40 struct timeval timein, timeout;
michael@0 41 PRInt32 onePercent = 3000000UL / 100UL;
michael@0 42
michael@0 43 fprintf (stderr, "First sleep will sleep 3 seconds.\n");
michael@0 44 fprintf (stderr, " sleep 1 begin\n");
michael@0 45 (void)GTOD(&timein);
michael@0 46 sleep (3);
michael@0 47 (void)GTOD(&timeout);
michael@0 48 fprintf (stderr, " sleep 1 end\n");
michael@0 49 elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
michael@0 50 elapsed += (timeout.tv_usec - timein.tv_usec);
michael@0 51 fprintf(stderr, "elapsed %u usecs\n", elapsed);
michael@0 52 if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
michael@0 53
michael@0 54 PR_Init (PR_USER_THREAD, PR_PRIORITY_NORMAL, 100);
michael@0 55 PR_STDIO_INIT();
michael@0 56
michael@0 57 fprintf (stderr, "Second sleep should do the same (does it?).\n");
michael@0 58 fprintf (stderr, " sleep 2 begin\n");
michael@0 59 (void)GTOD(&timein);
michael@0 60 sleep (3);
michael@0 61 (void)GTOD(&timeout);
michael@0 62 fprintf (stderr, " sleep 2 end\n");
michael@0 63 elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
michael@0 64 elapsed += (timeout.tv_usec - timein.tv_usec);
michael@0 65 fprintf(stderr, "elapsed %u usecs\n", elapsed);
michael@0 66 if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
michael@0 67
michael@0 68 fprintf (stderr, "What happens to other threads?\n");
michael@0 69 fprintf (stderr, "You should see dots every quarter second.\n");
michael@0 70 fprintf (stderr, "If you don't, you're probably running on classic NSPR.\n");
michael@0 71 thread = PR_CreateThread(
michael@0 72 PR_USER_THREAD, Other, NULL, PR_PRIORITY_NORMAL,
michael@0 73 PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
michael@0 74 fprintf (stderr, " sleep 2 begin\n");
michael@0 75 (void)GTOD(&timein);
michael@0 76 sleep (3);
michael@0 77 (void)GTOD(&timeout);
michael@0 78 fprintf (stderr, " sleep 2 end\n");
michael@0 79 PR_Interrupt(thread);
michael@0 80 PR_JoinThread(thread);
michael@0 81 elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
michael@0 82 elapsed += (timeout.tv_usec - timein.tv_usec);
michael@0 83 fprintf(stderr, "elapsed %u usecs\n", elapsed);
michael@0 84 if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
michael@0 85 fprintf(stderr, "%s\n", (0 == rv) ? "PASSED" : "FAILED");
michael@0 86 return rv;
michael@0 87 }
michael@0 88
michael@0 89 #else /* defined(XP_UNIX) */
michael@0 90
michael@0 91 PRIntn main()
michael@0 92 {
michael@0 93 return 2;
michael@0 94 }
michael@0 95
michael@0 96 #endif /* defined(XP_UNIX) */
michael@0 97

mercurial