]> git.pond.sub.org Git - empserver/blob - src/lib/lwp/sig.c
Simplify. Should also be more efficient.
[empserver] / src / lib / lwp / sig.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1994-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  sig.c: Wait for signals
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2006-2007
32  */
33
34 #include <config.h>
35
36 #include <errno.h>
37 #include <signal.h>
38 #include "lwp.h"
39 #include "lwpint.h"
40
41 /*
42  * Signals catched so far.
43  * Access only with signals blocked!
44  */
45 static sigset_t LwpSigCatched;
46
47 /*
48  * LwpSigCatched changed since last 
49  */
50 static sig_atomic_t LwpSigCheck;
51
52 /* The thread waiting for signals in lwpSigWait() */
53 static struct lwpProc *LwpSigWaiter;
54
55 static void lwpCatchAwaitedSig(int);
56
57 /*
58  * Initialize waiting for signals in SET.
59  */
60 void
61 lwpInitSigWait(sigset_t *set)
62 {
63     struct sigaction act;
64     int i;
65
66     sigemptyset(&LwpSigCatched);
67
68     act.sa_flags = 0;
69     act.sa_mask = *set;
70     sigemptyset(&act.sa_mask);
71     act.sa_handler = lwpCatchAwaitedSig;
72     for (i = 0; i < NSIG; i++) {
73         if (sigismember(set, i))
74             sigaction(i, &act, NULL);
75     }
76 }
77
78 static void
79 lwpCatchAwaitedSig(int sig)
80 {
81     sigaddset(&LwpSigCatched, sig);
82     LwpSigCheck = 1;
83 }
84
85 /*
86  * Test whether a signal from SET has been catched.
87  * If yes, delete that signal from the set of catched signals, and
88  * return its number.
89  * Else return 0.
90  */
91 static int
92 lwpGetSig(sigset_t *set)
93 {
94     sigset_t save;
95     int i;
96
97     sigprocmask(SIG_BLOCK, set, &save);
98     for (i = NSIG - 1; i > 0; i--) {
99         if (sigismember(set, i) && sigismember(&LwpSigCatched, i)) {
100             lwpStatus(LwpCurrent, "Got awaited signal %d", i);
101             sigdelset(&LwpSigCatched, i);
102             break;
103         }
104     }
105     sigprocmask(SIG_SETMASK, &save, NULL);
106     return i;
107 }
108
109 /*
110  * Wait until a signal from SET arrives.
111  * Assign its number to *SIG and return 0.
112  * If another thread is already waiting for signals, return EBUSY
113  * without waiting.
114  */
115 int
116 lwpSigWait(sigset_t *set, int *sig)
117 {
118     int res;
119
120     if (CANT_HAPPEN(LwpSigWaiter))
121         return EBUSY;
122     for (;;) {
123         LwpSigCheck = 0;
124         res = lwpGetSig(set);
125         if (res > 0)
126             break;
127         lwpStatus(LwpCurrent, "Waiting for signals");
128         LwpSigWaiter = LwpCurrent;
129         lwpReschedule();
130     }
131     *sig = res;
132     return 0;
133 }
134
135 /*
136  * Wake up the thread awaiting signals if one arrived.
137  * To be called from lwpReschedule().
138  */
139 void
140 lwpSigWakeup(void)
141 {
142     if (LwpSigWaiter && LwpSigCheck) {
143         lwpReady(LwpSigWaiter);
144         LwpSigWaiter = NULL;
145     }
146 }