1 /* Sleep a given number of milliseconds.
2 Copyright (C) 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
3 François Pinard <pinard@iro.umontreal.ca>, 1992.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* This code is heavily borrowed from Taylor UUCP 1.03. Ian picks one of
24 usleep, nap, napms, poll, select and sleep, in decreasing order of
25 preference. The sleep function is always available. */
27 /* In many cases, we will sleep if the wanted number of milliseconds
28 is higher than this value. */
29 #define THRESHOLD_FOR_SLEEP 30000
31 /* Include some header files. */
42 # include <sys/types.h>
45 # if !HAVE_STROPTS_H && !HAVE_POLL_H
46 /* We need a definition for struct pollfd, although it doesn't matter
55 # include <sys/time.h>
59 /*---------------------------------------.
60 | Sleep a given number of milliseconds. |
61 `---------------------------------------*/
70 usleep (milliseconds * (long) 1000);
76 nap ((long) milliseconds);
81 if (milliseconds >= THRESHOLD_FOR_SLEEP)
83 sleep (milliseconds / 1000);
92 struct pollfd sdummy; /* poll(2) checks this address */
94 if (milliseconds >= THRESHOLD_FOR_SLEEP)
96 sleep (milliseconds / 1000);
100 poll (&sdummy, 0, milliseconds);
107 if (milliseconds >= THRESHOLD_FOR_SLEEP)
109 sleep (milliseconds / 1000);
110 milliseconds %= 1000;
112 if (milliseconds > 0)
114 s.tv_sec = milliseconds / 1000;
115 s.tv_usec = (milliseconds % 1000) * (long) 1000;
116 select (0, NULL, NULL, NULL, &s);
121 /* Round the time up to the next full second. */
123 if (milliseconds > 0)
124 sleep ((milliseconds + 999) / 1000);