sleep.c revision 26338
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if defined(LIBC_SCCS) && !defined(lint)
35static char sccsid[] = "@(#)sleep.c	8.1 (Berkeley) 6/4/93";
36#endif /* LIBC_SCCS and not lint */
37
38#include <sys/time.h>
39#include <signal.h>
40#include <unistd.h>
41#ifdef  _THREAD_SAFE
42#include <pthread.h>
43#include "pthread_private.h"
44#endif
45
46#ifndef _THREAD_SAFE
47#ifndef USE_NANOSLEEP
48#define	setvec(vec, a) \
49	vec.sv_handler = a; vec.sv_mask = vec.sv_onstack = 0
50
51static int ringring;
52#endif
53
54static void
55sleephandler()
56{
57#ifndef USE_NANOSLEEP
58	ringring = 1;
59#endif
60}
61#endif	/* _THREAD_SAFE */
62
63unsigned int
64sleep(seconds)
65	unsigned int seconds;
66{
67#ifdef _THREAD_SAFE
68	struct timespec time_to_sleep;
69	struct timespec time_remaining;
70
71	if (seconds != 0) {
72		time_to_sleep.tv_sec = seconds;
73		time_to_sleep.tv_nsec = 0;
74		nanosleep(&time_to_sleep, &time_remaining);
75		seconds = time_remaining.tv_sec;
76		if (time_remaining.tv_nsec > 0)
77			seconds++;	/* round up */
78	}
79	return (seconds);
80#else
81#if defined(USE_NANOSLEEP)
82	struct timespec time_to_sleep;
83	struct timespec time_remaining;
84	struct sigaction act, oact;
85	sigset_t mask, omask;
86	int alarm_blocked;
87
88	if (seconds != 0) {
89		time_to_sleep.tv_sec = seconds;
90		time_to_sleep.tv_nsec = 0;
91
92		/* Block SIGALRM while fiddling with it */
93		sigemptyset(&mask);
94		sigaddset(&mask, SIGALRM);
95		sigprocmask(SIG_BLOCK, &mask, &omask);
96
97		/* Was SIGALRM blocked already? */
98		alarm_blocked = sigismember(&omask, SIGALRM);
99
100		if (!alarm_blocked) {
101			/*
102			 * Set up handler to interrupt signanosleep only if
103			 * SIGALRM was unblocked. (Save some syscalls)
104			 */
105			memset(&act, 0, sizeof(act));
106			act.sa_handler = sleephandler;
107			sigaction(SIGALRM, &act, &oact);
108		}
109
110		/*
111		 * signanosleep() uses the given mask for the lifetime of
112		 * the syscall only - it resets on return.  Note that the
113		 * Old sleep explicitly unblocks SIGALRM during the sleep,
114		 * we don't do that now since we don't depend on SIGALRM
115		 * to end the timout.  If the process blocks SIGALRM, it
116		 * gets what it asks for.
117		 */
118		signanosleep(&time_to_sleep, &time_remaining, &omask);
119
120		if (!alarm_blocked) {
121			/* Unwind */
122			sigaction(SIGALRM, &oact, (struct sigaction *)0);
123			sigprocmask(SIG_SETMASK, &omask, (sigset_t *)0);
124		}
125
126		/* return how long is left */
127		seconds = time_remaining.tv_sec;
128		if (time_remaining.tv_nsec > 0)
129			seconds++;	/* round up */
130	}
131	return (seconds);
132#else
133	register struct itimerval *itp;
134	struct itimerval itv, oitv;
135	struct sigvec vec, ovec;
136	long omask;
137	static void sleephandler();
138
139	itp = &itv;
140	if (!seconds)
141		return 0;
142	timerclear(&itp->it_interval);
143	timerclear(&itp->it_value);
144	if (setitimer(ITIMER_REAL, itp, &oitv) < 0)
145		return seconds;
146	itp->it_value.tv_sec = seconds;
147	if (timerisset(&oitv.it_value)) {
148		if (timercmp(&oitv.it_value, &itp->it_value, >))
149			oitv.it_value.tv_sec -= itp->it_value.tv_sec;
150		else {
151			itp->it_value = oitv.it_value;
152			/*
153			 * This is a hack, but we must have time to return
154			 * from the setitimer after the alarm or else it'll
155			 * be restarted.  And, anyway, sleep never did
156			 * anything more than this before.
157			 */
158			oitv.it_value.tv_sec = 1;
159			oitv.it_value.tv_usec = 0;
160		}
161	}
162	setvec(vec, sleephandler);
163	(void) sigvec(SIGALRM, &vec, &ovec);
164	omask = sigblock(sigmask(SIGALRM));
165	ringring = 0;
166	(void) setitimer(ITIMER_REAL, itp, (struct itimerval *)0);
167	while (!ringring)
168		sigpause(omask &~ sigmask(SIGALRM));
169	(void) sigvec(SIGALRM, &ovec, (struct sigvec *)0);
170	(void) sigsetmask(omask);
171	(void) setitimer(ITIMER_REAL, &oitv, (struct itimerval *)0);
172	return 0;
173#endif	/* USE_NANOSLEEP */
174#endif	/* _THREAD_SAFE */
175}
176