VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/sleep.c@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#include "nspr.h"
39
40#if defined(XP_UNIX) || defined(XP_OS2)
41
42#include <stdio.h>
43
44#ifndef XP_OS2
45#include <unistd.h>
46#endif
47#include <sys/time.h>
48
49#if defined(HAVE_SVID_GETTOD)
50#define GTOD(_a) gettimeofday(_a)
51#else
52#define GTOD(_a) gettimeofday((_a), NULL)
53#endif
54
55#if defined (XP_OS2_VACPP)
56#define INCL_DOSPROCESS
57#include <os2.h>
58#endif
59
60static PRIntn rv = 0;
61
62static void Other(void *unused)
63{
64 PRIntn didit = 0;
65 while (PR_SUCCESS == PR_Sleep(PR_MillisecondsToInterval(250)))
66 {
67 fprintf(stderr, ".");
68 didit += 1;
69 }
70 if (didit < 5) rv = 1;
71}
72
73PRIntn main ()
74{
75 PRUint32 elapsed;
76 PRThread *thread;
77 struct timeval timein, timeout;
78 PRInt32 onePercent = 3000000UL / 100UL;
79
80 fprintf (stderr, "First sleep will sleep 3 seconds.\n");
81 fprintf (stderr, " sleep 1 begin\n");
82 (void)GTOD(&timein);
83 sleep (3);
84 (void)GTOD(&timeout);
85 fprintf (stderr, " sleep 1 end\n");
86 elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
87 elapsed += (timeout.tv_usec - timein.tv_usec);
88 fprintf(stderr, "elapsed %u usecs\n", elapsed);
89 if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
90
91 PR_Init (PR_USER_THREAD, PR_PRIORITY_NORMAL, 100);
92 PR_STDIO_INIT();
93
94 fprintf (stderr, "Second sleep should do the same (does it?).\n");
95 fprintf (stderr, " sleep 2 begin\n");
96 (void)GTOD(&timein);
97 sleep (3);
98 (void)GTOD(&timeout);
99 fprintf (stderr, " sleep 2 end\n");
100 elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
101 elapsed += (timeout.tv_usec - timein.tv_usec);
102 fprintf(stderr, "elapsed %u usecs\n", elapsed);
103 if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
104
105 fprintf (stderr, "What happens to other threads?\n");
106 fprintf (stderr, "You should see dots every quarter second.\n");
107 fprintf (stderr, "If you don't, you're probably running on classic NSPR.\n");
108 thread = PR_CreateThread(
109 PR_USER_THREAD, Other, NULL, PR_PRIORITY_NORMAL,
110 PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
111 fprintf (stderr, " sleep 2 begin\n");
112 (void)GTOD(&timein);
113 sleep (3);
114 (void)GTOD(&timeout);
115 fprintf (stderr, " sleep 2 end\n");
116 PR_Interrupt(thread);
117 PR_JoinThread(thread);
118 elapsed = 1000000UL * (timeout.tv_sec - timein.tv_sec);
119 elapsed += (timeout.tv_usec - timein.tv_usec);
120 fprintf(stderr, "elapsed %u usecs\n", elapsed);
121 if (labs(elapsed - 3000000UL) > onePercent) rv = 1;
122 fprintf(stderr, "%s\n", (0 == rv) ? "PASSED" : "FAILED");
123 return rv;
124}
125
126#else /* defined(XP_UNIX) */
127
128PRIntn main()
129{
130 return 2;
131}
132
133#endif /* defined(XP_UNIX) */
134
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette