1989-12-18 14:40:54 +00:00
|
|
|
/*
|
|
|
|
* sleep - suspend current process for a number of seconds
|
|
|
|
*/
|
|
|
|
/* $Header$ */
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
1990-01-22 11:44:21 +00:00
|
|
|
int _alarm(int n);
|
|
|
|
void _pause(void);
|
1989-12-18 14:40:54 +00:00
|
|
|
|
|
|
|
static jmp_buf setjmpbuf;
|
|
|
|
|
|
|
|
static void
|
|
|
|
alfun(int sig)
|
|
|
|
{
|
|
|
|
longjmp(setjmpbuf, 1);
|
|
|
|
} /* used with sleep() below */
|
|
|
|
|
|
|
|
void
|
|
|
|
sleep(int n)
|
|
|
|
{
|
|
|
|
/* sleep(n) pauses for 'n' seconds by scheduling an alarm interrupt. */
|
|
|
|
unsigned oldalarm;
|
|
|
|
void (*oldsig)(int);
|
|
|
|
|
|
|
|
if (n <= 0) return;
|
|
|
|
if (setjmp(setjmpbuf)) {
|
|
|
|
signal(SIGALRM, oldsig);
|
1990-01-22 11:44:21 +00:00
|
|
|
_alarm(oldalarm);
|
1989-12-18 14:40:54 +00:00
|
|
|
return;
|
|
|
|
}
|
1990-01-22 11:44:21 +00:00
|
|
|
oldalarm = _alarm(5000); /* Who cares how long, as long
|
1989-12-18 14:40:54 +00:00
|
|
|
* as it is long enough
|
|
|
|
*/
|
|
|
|
if (oldalarm > n) oldalarm -= n;
|
|
|
|
else if (oldalarm) {
|
|
|
|
n = oldalarm;
|
|
|
|
oldalarm = 1;
|
|
|
|
}
|
|
|
|
oldsig = signal(SIGALRM, alfun);
|
1990-01-22 11:44:21 +00:00
|
|
|
_alarm(n);
|
1989-12-18 14:40:54 +00:00
|
|
|
for (;;) {
|
|
|
|
/* allow for other handlers ... */
|
1990-01-22 11:44:21 +00:00
|
|
|
_pause();
|
1989-12-18 14:40:54 +00:00
|
|
|
}
|
|
|
|
}
|