Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / libcc.ansi / misc / sleep.c
1 /*
2  * sleep - suspend current process for a number of seconds
3  */
4 /* $Id: sleep.c,v 1.4 1994/06/24 11:45:44 ceriel Exp $ */
5
6 #include        <signal.h>
7 #include        <setjmp.h>
8
9 int _alarm(int n);
10 void _pause(void);
11
12 static jmp_buf  setjmpbuf;
13
14 static void
15 alfun(int sig)
16 {
17         longjmp(setjmpbuf, 1);
18 }               /* used with sleep() below */
19
20 void
21 sleep(int n)
22 {
23 /* sleep(n) pauses for 'n' seconds by scheduling an alarm interrupt. */
24         unsigned oldalarm = 0;
25         void (*oldsig)(int) = 0;
26
27         if (n <= 0) return;
28         if (setjmp(setjmpbuf)) {
29                 signal(SIGALRM, oldsig);
30                 _alarm(oldalarm);
31                 return;
32         }
33         oldalarm = _alarm(5000);        /* Who cares how long, as long
34                                          * as it is long enough
35                                          */
36         if (oldalarm > n) oldalarm -= n;
37         else if (oldalarm) {
38                 n = oldalarm;
39                 oldalarm = 1;
40         }
41         oldsig = signal(SIGALRM, alfun);
42         _alarm(n);
43         for (;;)  {
44                 /* allow for other handlers ... */
45                 _pause();
46         }
47 }