Upload Tizen:Base source
[external/eglibc.git] / sysdeps / mach / hurd / sigwait.c
1 /* Copyright (C) 1996,97,2001,02 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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.
8
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.
13
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.  */
18
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>
25
26 /* Select any of pending signals from SET or wait for any to arrive.  */
27 int
28 __sigwait (const sigset_t *set, int *sig)
29 {
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;
37
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)
43     {
44       if (signo)
45         /* We've already been run; don't interfere. */
46         return SIG_ERR;
47
48       signo = *sigp;
49
50       /* Make sure this is all kosher */
51       assert (__sigismember (&mask, signo));
52
53       /* Make sure this signal is unblocked */
54       __sigdelset (&ss->blocked, signo);
55
56       return pe->handler;
57     }
58
59   void
60     handler (int sig)
61     {
62       assert (sig == signo);
63       longjmp (buf, 1);
64     }
65
66   wait = __mach_reply_port ();
67
68   if (set != NULL)
69     /* Crash before locking */
70     mask = *set;
71   else
72     __sigemptyset (&mask);
73
74   ss = _hurd_self_sigstate ();
75   __spin_lock (&ss->lock);
76
77   /* See if one of these signals is currently pending.  */
78   __sigandset (&ready, &ss->pending, &mask);
79   if (! __sigisemptyset (&ready))
80     {
81       for (signo = 1; signo < NSIG; signo++)
82         if (__sigismember (&ready, signo))
83           {
84             __sigdelset (&ready, signo);
85             goto all_done;
86           }
87       /* Huh?  Where'd it go? */
88       abort ();
89     }
90
91   /* Wait for one of them to show up.  */
92
93   if (!setjmp (buf))
94     {
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;
101
102       /* Install this preemptor */
103       preemptor.next = ss->preemptors;
104       ss->preemptors = &preemptor;
105
106       __spin_unlock (&ss->lock);
107
108       /* Wait. */
109       __mach_msg (&msg, MACH_RCV_MSG, 0, sizeof (msg), wait,
110                   MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
111       abort ();
112     }
113   else
114     {
115       assert (signo);
116
117       __spin_lock (&ss->lock);
118
119       /* Delete our preemptor. */
120       assert (ss->preemptors == &preemptor);
121       ss->preemptors = preemptor.next;
122     }
123
124
125 all_done:
126   spin_unlock (&ss->lock);
127
128   __mach_port_destroy (__mach_task_self (), wait);
129   *sig = signo;
130   return 0;
131 }
132
133 weak_alias (__sigwait, sigwait)