nsprpub/pr/tests/sleep.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial