summaryrefslogtreecommitdiffstats
path: root/contrib/tar/lib/msleep.c
blob: 38dfed2a280e62fccbf3f0088239c592843c48c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* Sleep a given number of milliseconds.
   Copyright (C) 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
   François Pinard <pinard@iro.umontreal.ca>, 1992.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

/* This code is heavily borrowed from Taylor UUCP 1.03.	 Ian picks one of
   usleep, nap, napms, poll, select and sleep, in decreasing order of
   preference.	The sleep function is always available.	 */

/* In many cases, we will sleep if the wanted number of milliseconds
   is higher than this value.  */
#define THRESHOLD_FOR_SLEEP 30000

/* Include some header files.  */

#if HAVE_UNISTD_H
# include <unistd.h>
#endif

#if HAVE_POLL
# if HAVE_STROPTS_H
#  include <stropts.h>
# endif
# if HAVE_POLL_H
#  include <sys/types.h>
#  include <poll.h>
# endif
# if !HAVE_STROPTS_H && !HAVE_POLL_H
/* We need a definition for struct pollfd, although it doesn't matter
   what it contains.  */
struct pollfd
{
  int idummy;
};
# endif
#else
# if HAVE_SELECT
#  include <sys/time.h>
# endif
#endif

/*---------------------------------------.
| Sleep a given number of milliseconds.	 |
`---------------------------------------*/

void
msleep (milliseconds)
     int milliseconds;
{
#if HAVE_USLEEP

  if (milliseconds > 0)
    usleep (milliseconds * (long) 1000);

#else
# if HAVE_NAP

  if (milliseconds > 0)
    nap ((long) milliseconds);

# else
#  if HAVE_NAPMS

  if (milliseconds >= THRESHOLD_FOR_SLEEP)
    {
      sleep (milliseconds / 1000);
      milliseconds %= 1000;
    }
  if (milliseconds > 0)
    napms (milliseconds);

#  else
#   if HAVE_POLL

  struct pollfd sdummy;		/* poll(2) checks this address */

  if (milliseconds >= THRESHOLD_FOR_SLEEP)
    {
      sleep (milliseconds / 1000);
      milliseconds %= 1000;
    }
  if (milliseconds > 0)
    poll (&sdummy, 0, milliseconds);

#   else
#    if HAVE_SELECT

  struct timeval s;

  if (milliseconds >= THRESHOLD_FOR_SLEEP)
    {
      sleep (milliseconds / 1000);
      milliseconds %= 1000;
    }
  if (milliseconds > 0)
    {
      s.tv_sec = milliseconds / 1000;
      s.tv_usec = (milliseconds % 1000) * (long) 1000;
      select (0, NULL, NULL, NULL, &s);
    }

#    else

  /* Round the time up to the next full second.  */

  if (milliseconds > 0)
    sleep ((milliseconds + 999) / 1000);

#    endif
#   endif
#  endif
# endif
#endif
}
OpenPOWER on IntegriCloud