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

52 lines
827 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
2018-06-21 20:33:47 +00:00
#include <signal.h>
#include <setjmp.h>
1989-12-18 14:40:54 +00:00
int _alarm(int n);
void _pause(void);
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;
}
2018-06-21 20:33:47 +00:00
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
}
}