Back to index

lightning-sunbird  0.9+nobinonly
sleep.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nspr.h"
00039 
00040 #if defined(XP_UNIX) || defined(XP_OS2)
00041 
00042 #include <stdio.h>
00043 
00044 #ifndef XP_OS2
00045 #include <unistd.h>
00046 #endif
00047 #include <sys/time.h>
00048 
00049 #if defined(HAVE_SVID_GETTOD)
00050 #define GTOD(_a) gettimeofday(_a)
00051 #else
00052 #define GTOD(_a) gettimeofday((_a), NULL)
00053 #endif
00054 
00055 #if defined (XP_OS2_VACPP)
00056 #define INCL_DOSPROCESS
00057 #include <os2.h>
00058 #endif
00059 
00060 static PRIntn rv = 0;
00061 
00062 static void Other(void *unused)
00063 {
00064     PRIntn didit = 0;
00065     while (PR_SUCCESS == PR_Sleep(PR_MillisecondsToInterval(250)))
00066     {
00067         fprintf(stderr, ".");
00068         didit += 1;
00069     }
00070     if (didit < 5) rv = 1;
00071 }
00072 
00073 PRIntn main ()
00074 {
00075     PRUint32 elapsed;
00076     PRThread *thread;
00077        struct timeval timein, timeout;
00078     PRInt32 onePercent = 3000000UL / 100UL;
00079 
00080        fprintf (stderr, "First sleep will sleep 3 seconds.\n");
00081        fprintf (stderr, "   sleep 1 begin\n");
00082     (void)GTOD(&timein);
00083        sleep (3);
00084     (void)GTOD(&timeout);
00085        fprintf (stderr, "   sleep 1 end\n");
00086     elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
00087     elapsed += (timeout.tv_usec - timein.tv_usec);
00088     fprintf(stderr, "elapsed %u usecs\n", elapsed);
00089     if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
00090 
00091        PR_Init (PR_USER_THREAD, PR_PRIORITY_NORMAL, 100);
00092     PR_STDIO_INIT();
00093 
00094        fprintf (stderr, "Second sleep should do the same (does it?).\n");
00095        fprintf (stderr, "   sleep 2 begin\n");
00096     (void)GTOD(&timein);
00097        sleep (3);
00098     (void)GTOD(&timeout);
00099        fprintf (stderr, "   sleep 2 end\n");
00100     elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
00101     elapsed += (timeout.tv_usec - timein.tv_usec);
00102     fprintf(stderr, "elapsed %u usecs\n", elapsed);
00103     if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
00104 
00105        fprintf (stderr, "What happens to other threads?\n");
00106        fprintf (stderr, "You should see dots every quarter second.\n");
00107        fprintf (stderr, "If you don't, you're probably running on classic NSPR.\n");
00108     thread = PR_CreateThread(
00109         PR_USER_THREAD, Other, NULL, PR_PRIORITY_NORMAL,
00110         PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
00111        fprintf (stderr, "   sleep 2 begin\n");
00112     (void)GTOD(&timein);
00113        sleep (3);
00114     (void)GTOD(&timeout);
00115        fprintf (stderr, "   sleep 2 end\n");
00116     PR_Interrupt(thread);
00117     PR_JoinThread(thread);
00118     elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
00119     elapsed += (timeout.tv_usec - timein.tv_usec);
00120     fprintf(stderr, "elapsed %u usecs\n", elapsed);
00121     if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
00122     fprintf(stderr, "%s\n", (0 == rv) ? "PASSED" : "FAILED");
00123     return rv;
00124 }
00125 
00126 #else /* defined(XP_UNIX) */
00127 
00128 PRIntn main()
00129 {
00130        return 2;
00131 }
00132 
00133 #endif /*  defined(XP_UNIX) */
00134