2.9
[glibc/nacl-glibc.git] / sysdeps / mach / hurd / sigwait.c
blob9794076be6a5d3b47dc0aca1c4e0e12534e4dd22
1 /* Copyright (C) 1996,97,2001,02 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <hurd.h>
21 #include <hurd/signal.h>
22 #include <hurd/msg.h>
23 #include <hurd/sigpreempt.h>
24 #include <assert.h>
26 /* Select any of pending signals from SET or wait for any to arrive. */
27 int
28 __sigwait (const sigset_t *set, int *sig)
30 struct hurd_sigstate *ss;
31 sigset_t mask, ready;
32 int signo = 0;
33 struct hurd_signal_preemptor preemptor;
34 jmp_buf buf;
35 mach_port_t wait;
36 mach_msg_header_t msg;
38 sighandler_t
39 preempt_fun (struct hurd_signal_preemptor *pe,
40 struct hurd_sigstate *ss,
41 int *sigp,
42 struct hurd_signal_detail *detail)
44 if (signo)
45 /* We've already been run; don't interfere. */
46 return SIG_ERR;
48 signo = *sigp;
50 /* Make sure this is all kosher */
51 assert (__sigismember (&mask, signo));
53 /* Make sure this signal is unblocked */
54 __sigdelset (&ss->blocked, signo);
56 return pe->handler;
59 void
60 handler (int sig)
62 assert (sig == signo);
63 longjmp (buf, 1);
66 wait = __mach_reply_port ();
68 if (set != NULL)
69 /* Crash before locking */
70 mask = *set;
71 else
72 __sigemptyset (&mask);
74 ss = _hurd_self_sigstate ();
75 __spin_lock (&ss->lock);
77 /* See if one of these signals is currently pending. */
78 __sigandset (&ready, &ss->pending, &mask);
79 if (! __sigisemptyset (&ready))
81 for (signo = 1; signo < NSIG; signo++)
82 if (__sigismember (&ready, signo))
84 __sigdelset (&ready, signo);
85 goto all_done;
87 /* Huh? Where'd it go? */
88 abort ();
91 /* Wait for one of them to show up. */
93 if (!setjmp (buf))
95 /* Make the preemptor */
96 preemptor.signals = mask;
97 preemptor.first = 0;
98 preemptor.last = -1;
99 preemptor.preemptor = preempt_fun;
100 preemptor.handler = handler;
102 /* Install this preemptor */
103 preemptor.next = ss->preemptors;
104 ss->preemptors = &preemptor;
106 __spin_unlock (&ss->lock);
108 /* Wait. */
109 __mach_msg (&msg, MACH_RCV_MSG, 0, sizeof (msg), wait,
110 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
111 abort ();
113 else
115 assert (signo);
117 __spin_lock (&ss->lock);
119 /* Delete our preemptor. */
120 assert (ss->preemptors == &preemptor);
121 ss->preemptors = preemptor.next;
125 all_done:
126 spin_unlock (&ss->lock);
128 __mach_port_destroy (__mach_task_self (), wait);
129 *sig = signo;
130 return 0;
133 weak_alias (__sigwait, sigwait)