nsprpub/pr/tests/sleep.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/sleep.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,97 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nspr.h"
    1.10 +
    1.11 +#if defined(XP_UNIX) || defined(XP_OS2)
    1.12 +
    1.13 +#include <stdio.h>
    1.14 +
    1.15 +#ifndef XP_OS2
    1.16 +#include <unistd.h>
    1.17 +#endif
    1.18 +#include <sys/time.h>
    1.19 +
    1.20 +#if defined(HAVE_SVID_GETTOD)
    1.21 +#define GTOD(_a) gettimeofday(_a)
    1.22 +#else
    1.23 +#define GTOD(_a) gettimeofday((_a), NULL)
    1.24 +#endif
    1.25 +
    1.26 +static PRIntn rv = 0;
    1.27 +
    1.28 +static void Other(void *unused)
    1.29 +{
    1.30 +    PRIntn didit = 0;
    1.31 +    while (PR_SUCCESS == PR_Sleep(PR_MillisecondsToInterval(250)))
    1.32 +    {
    1.33 +        fprintf(stderr, ".");
    1.34 +        didit += 1;
    1.35 +    }
    1.36 +    if (didit < 5) rv = 1;
    1.37 +}
    1.38 +
    1.39 +int main(int argc, char **argv)
    1.40 +{
    1.41 +    PRUint32 elapsed;
    1.42 +    PRThread *thread;
    1.43 +	struct timeval timein, timeout;
    1.44 +    PRInt32 onePercent = 3000000UL / 100UL;
    1.45 +
    1.46 +	fprintf (stderr, "First sleep will sleep 3 seconds.\n");
    1.47 +	fprintf (stderr, "   sleep 1 begin\n");
    1.48 +    (void)GTOD(&timein);
    1.49 +	sleep (3);
    1.50 +    (void)GTOD(&timeout);
    1.51 +	fprintf (stderr, "   sleep 1 end\n");
    1.52 +    elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
    1.53 +    elapsed += (timeout.tv_usec - timein.tv_usec);
    1.54 +    fprintf(stderr, "elapsed %u usecs\n", elapsed);
    1.55 +    if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
    1.56 +
    1.57 +	PR_Init (PR_USER_THREAD, PR_PRIORITY_NORMAL, 100);
    1.58 +    PR_STDIO_INIT();
    1.59 +
    1.60 +	fprintf (stderr, "Second sleep should do the same (does it?).\n");
    1.61 +	fprintf (stderr, "   sleep 2 begin\n");
    1.62 +    (void)GTOD(&timein);
    1.63 +	sleep (3);
    1.64 +    (void)GTOD(&timeout);
    1.65 +	fprintf (stderr, "   sleep 2 end\n");
    1.66 +    elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
    1.67 +    elapsed += (timeout.tv_usec - timein.tv_usec);
    1.68 +    fprintf(stderr, "elapsed %u usecs\n", elapsed);
    1.69 +    if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
    1.70 +
    1.71 +	fprintf (stderr, "What happens to other threads?\n");
    1.72 +	fprintf (stderr, "You should see dots every quarter second.\n");
    1.73 +	fprintf (stderr, "If you don't, you're probably running on classic NSPR.\n");
    1.74 +    thread = PR_CreateThread(
    1.75 +        PR_USER_THREAD, Other, NULL, PR_PRIORITY_NORMAL,
    1.76 +        PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
    1.77 +	fprintf (stderr, "   sleep 2 begin\n");
    1.78 +    (void)GTOD(&timein);
    1.79 +	sleep (3);
    1.80 +    (void)GTOD(&timeout);
    1.81 +	fprintf (stderr, "   sleep 2 end\n");
    1.82 +    PR_Interrupt(thread);
    1.83 +    PR_JoinThread(thread);
    1.84 +    elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
    1.85 +    elapsed += (timeout.tv_usec - timein.tv_usec);
    1.86 +    fprintf(stderr, "elapsed %u usecs\n", elapsed);
    1.87 +    if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
    1.88 +    fprintf(stderr, "%s\n", (0 == rv) ? "PASSED" : "FAILED");
    1.89 +    return rv;
    1.90 +}
    1.91 +
    1.92 +#else /* defined(XP_UNIX) */
    1.93 +
    1.94 +PRIntn main()
    1.95 +{
    1.96 +	return 2;
    1.97 +}
    1.98 +
    1.99 +#endif /*  defined(XP_UNIX) */
   1.100 +

mercurial