]> git.pond.sub.org Git - empserver/blob - src/lib/lwp/sig.c
04fb9f97741dba4d645c893673d3a8aa4e029afe
[empserver] / src / lib / lwp / sig.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1994-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  sig.c: Wait for signals
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2006-2020
31  */
32
33 #include <config.h>
34
35 #include <errno.h>
36 #include <stddef.h>
37 #include <signal.h>
38 #include "lwp.h"
39 #include "lwpint.h"
40
41 /*
42  * Signals caught so far.
43  * Access only with signals blocked!
44  */
45 static sigset_t LwpSigCaught;
46
47 /*
48  * LwpSigCaught 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(&LwpSigCaught);
67
68     act.sa_flags = 0;
69     act.sa_mask = *set;
70     act.sa_handler = lwpCatchAwaitedSig;
71     for (i = 0; i < NSIG; i++) {
72         if (sigismember(set, i) > 0)
73             sigaction(i, &act, NULL);
74     }
75 }
76
77 static void
78 lwpCatchAwaitedSig(int sig)
79 {
80     sigaddset(&LwpSigCaught, sig);
81     LwpSigCheck = 1;
82 }
83
84 /*
85  * Test whether a signal from @set has been caught.
86  * If yes, delete that signal from the set of caught signals, and
87  * return its number.
88  * Else return 0.
89  */
90 static int
91 lwpGetSig(sigset_t *set)
92 {
93     sigset_t save;
94     int i;
95
96     sigprocmask(SIG_BLOCK, set, &save);
97     for (i = NSIG - 1; i > 0; i--) {
98         if (sigismember(set, i) > 0 && sigismember(&LwpSigCaught, i) > 0) {
99             lwpStatus(LwpCurrent, "Got awaited signal %d", i);
100             sigdelset(&LwpSigCaught, i);
101             break;
102         }
103     }
104     sigprocmask(SIG_SETMASK, &save, NULL);
105     return i;
106 }
107
108 /*
109  * Wait until a signal from @set arrives.
110  * Assign its number to *@sig and return 0.
111  * If another thread is already waiting for signals, return EBUSY
112  * without waiting.
113  */
114 int
115 lwpSigWait(sigset_t *set, int *sig)
116 {
117     int res;
118
119     if (LwpSigWaiter)
120         return EBUSY;
121     for (;;) {
122         LwpSigCheck = 0;
123         res = lwpGetSig(set);
124         if (res > 0)
125             break;
126         lwpStatus(LwpCurrent, "Waiting for signals");
127         LwpSigWaiter = LwpCurrent;
128         lwpReschedule();
129     }
130     *sig = res;
131     return 0;
132 }
133
134 /*
135  * Wake up the thread awaiting signals if one arrived.
136  * To be called from lwpReschedule().
137  */
138 void
139 lwpSigWakeup(void)
140 {
141     if (LwpSigWaiter && LwpSigCheck) {
142         lwpReady(LwpSigWaiter);
143         LwpSigWaiter = NULL;
144     }
145 }