ack/lang/cem/libcc.ansi/sys/misc/sleep.c

55 lines
864 B
C
Raw Normal View History

1989-12-18 14:40:54 +00:00
/*
* sleep - suspend current process for a number of seconds
*/
1994-06-24 14:02:31 +00:00
/* $Id$ */
1989-12-18 14:40:54 +00:00
#include <stdio.h>
2018-06-21 20:33:47 +00:00
#include <signal.h>
#include <unistd.h>
2018-06-21 20:33:47 +00:00
#include <setjmp.h>
1989-12-18 14:40:54 +00:00
#if ACKCONF_WANT_EMULATED_SLEEP
1989-12-18 14:40:54 +00:00
2018-06-21 20:33:47 +00:00
static jmp_buf setjmpbuf;
1989-12-18 14:40:54 +00:00
static void
alfun(int sig)
{
longjmp(setjmpbuf, 1);
2018-06-21 20:33:47 +00:00
} /* used with sleep() below */
1989-12-18 14:40:54 +00:00
2018-06-21 20:33:47 +00:00
void sleep(int n)
1989-12-18 14:40:54 +00:00
{
2018-06-21 20:33:47 +00:00
/* sleep(n) pauses for 'n' seconds by scheduling an alarm interrupt. */
1991-09-30 16:24:45 +00:00
unsigned oldalarm = 0;
void (*oldsig)(int) = 0;
1989-12-18 14:40:54 +00:00
2018-06-21 20:33:47 +00:00
if (n <= 0)
return;
if (setjmp(setjmpbuf))
{
1989-12-18 14:40:54 +00:00
signal(SIGALRM, oldsig);
alarm(oldalarm);
1989-12-18 14:40:54 +00:00
return;
}
oldalarm = alarm(5000); /* Who cares how long, as long
1989-12-18 14:40:54 +00:00
* as it is long enough
*/
2018-06-21 20:33:47 +00:00
if (oldalarm > n)
oldalarm -= n;
else if (oldalarm)
{
1989-12-18 14:40:54 +00:00
n = oldalarm;
oldalarm = 1;
}
oldsig = signal(SIGALRM, alfun);
alarm(n);
2018-06-21 20:33:47 +00:00
for (;;)
{
1989-12-18 14:40:54 +00:00
/* allow for other handlers ... */
pause();
1989-12-18 14:40:54 +00:00
}
}
#endif