Blob


1 #include <u.h>
2 #include <signal.h>
3 #include <libc.h>
5 #define DBG 0
7 static void
8 ign(int x)
9 {
10 USED(x);
11 }
13 void /*__attribute__((constructor))*/
14 ignusr1(int restart)
15 {
16 struct sigaction sa;
18 memset(&sa, 0, sizeof sa);
19 sa.sa_handler = ign;
20 sigemptyset(&sa.sa_mask);
21 sigaddset(&sa.sa_mask, SIGUSR1);
22 if(restart)
23 sa.sa_flags = SA_RESTART;
24 sigaction(SIGUSR1, &sa, nil);
25 }
27 void
28 _procsleep(_Procrend *r)
29 {
30 sigset_t mask;
32 /*
33 * Go to sleep.
34 *
35 * Block USR1, set the handler to interrupt system calls,
36 * unlock the vouslock so our waker can wake us,
37 * and then suspend.
38 */
39 r->asleep = 1;
40 r->pid = getpid();
42 sigprocmask(SIG_SETMASK, nil, &mask);
43 sigaddset(&mask, SIGUSR1);
44 sigprocmask(SIG_SETMASK, &mask, nil);
45 ignusr1(0);
46 unlock(r->l);
47 sigdelset(&mask, SIGUSR1);
48 sigsuspend(&mask);
50 /*
51 * We're awake. Make USR1 not interrupt system calls.
52 */
53 ignusr1(1);
54 assert(r->asleep == 0);
55 lock(r->l);
56 }
58 void
59 _procwakeup(_Procrend *r)
60 {
61 r->asleep = 0;
62 assert(r->pid >= 1);
63 kill(r->pid, SIGUSR1);
64 }