xref: /onnv-gate/usr/src/lib/libast/common/tm/tvsleep.c (revision 4887:feebf9260c2e)
1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #pragma prototyped
23*4887Schin 
24*4887Schin #include <tv.h>
25*4887Schin #include <tm.h>
26*4887Schin 
27*4887Schin #include "FEATURE/tvlib"
28*4887Schin 
29*4887Schin #if !_lib_nanosleep
30*4887Schin # if _lib_select
31*4887Schin #  if _sys_select
32*4887Schin #   include <sys/select.h>
33*4887Schin #  else
34*4887Schin #   include <sys/socket.h>
35*4887Schin #  endif
36*4887Schin # else
37*4887Schin #  if !_lib_usleep
38*4887Schin #   if _lib_poll_notimer
39*4887Schin #    undef _lib_poll
40*4887Schin #   endif
41*4887Schin #   if _lib_poll
42*4887Schin #    include <poll.h>
43*4887Schin #   endif
44*4887Schin #  endif
45*4887Schin # endif
46*4887Schin #endif
47*4887Schin 
48*4887Schin /*
49*4887Schin  * sleep for tv
50*4887Schin  * non-zero exit if sleep did not complete
51*4887Schin  * with remaining time in rv
52*4887Schin  */
53*4887Schin 
54*4887Schin int
55*4887Schin tvsleep(register const Tv_t* tv, register Tv_t* rv)
56*4887Schin {
57*4887Schin 
58*4887Schin #if _lib_nanosleep
59*4887Schin 
60*4887Schin 	struct timespec	stv;
61*4887Schin 	struct timespec	srv;
62*4887Schin 	int		r;
63*4887Schin 
64*4887Schin 	stv.tv_sec = tv->tv_sec;
65*4887Schin 	stv.tv_nsec = tv->tv_nsec;
66*4887Schin 	if ((r = nanosleep(&stv, &srv)) && rv)
67*4887Schin 	{
68*4887Schin 		rv->tv_sec = srv.tv_sec;
69*4887Schin 		rv->tv_nsec = srv.tv_nsec;
70*4887Schin 	}
71*4887Schin 	return r;
72*4887Schin 
73*4887Schin #else
74*4887Schin 
75*4887Schin #if _lib_select
76*4887Schin 
77*4887Schin 	struct timeval	stv;
78*4887Schin 
79*4887Schin 	stv.tv_sec = tv->tv_sec;
80*4887Schin 	stv.tv_usec = tv->tv_nsec / 1000;
81*4887Schin 	if (select(0, NiL, NiL, NiL, &stv) < 0)
82*4887Schin 	{
83*4887Schin 		if (rv)
84*4887Schin 			*rv = *tv;
85*4887Schin 		return -1;
86*4887Schin 	}
87*4887Schin 	if (rv)
88*4887Schin 	{
89*4887Schin 		rv->tv_sec = stv.tv_sec;
90*4887Schin 		rv->tv_nsec = stv.tv_usec * 1000;
91*4887Schin 	}
92*4887Schin 	return 0;
93*4887Schin 
94*4887Schin #else
95*4887Schin 
96*4887Schin 	unsigned int		s = tv->tv_sec;
97*4887Schin 	uint32_t		n = tv->tv_nsec;
98*4887Schin 
99*4887Schin #if _lib_usleep
100*4887Schin 
101*4887Schin 
102*4887Schin 	unsigned long		t;
103*4887Schin 
104*4887Schin 	if (t = (n + 999L) / 1000L)
105*4887Schin 	{
106*4887Schin 		usleep(t);
107*4887Schin 		s -= t / 1000000L;
108*4887Schin 		n = 0;
109*4887Schin 	}
110*4887Schin 
111*4887Schin #else
112*4887Schin 
113*4887Schin #if _lib_poll
114*4887Schin 
115*4887Schin 	struct pollfd		pfd;
116*4887Schin 	int			t;
117*4887Schin 
118*4887Schin 	if ((t = (n + 999999L) / 1000000L) > 0)
119*4887Schin 	{
120*4887Schin 		poll(&pfd, 0, t);
121*4887Schin 		s -= t / 1000L;
122*4887Schin 		n = 0;
123*4887Schin 	}
124*4887Schin 
125*4887Schin #endif
126*4887Schin 
127*4887Schin #endif
128*4887Schin 
129*4887Schin 	if ((s += (n + 999999999L) / 1000000000L) && (s = sleep(s)))
130*4887Schin 	{
131*4887Schin 		if (rv)
132*4887Schin 		{
133*4887Schin 			rv->tv_sec = s;
134*4887Schin 			rv->tv_nsec = 0;
135*4887Schin 		}
136*4887Schin 		return -1;
137*4887Schin 	}
138*4887Schin 	return 0;
139*4887Schin 
140*4887Schin #endif
141*4887Schin 
142*4887Schin #endif
143*4887Schin 
144*4887Schin }
145