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
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.
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.
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/>.
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.
27 * sig.c: Wait for signals
29 * Known contributors to this file:
30 * Markus Armbruster, 2006-2020
42 * Signals caught so far.
43 * Access only with signals blocked!
45 static sigset_t LwpSigCaught;
48 * LwpSigCaught changed since last
50 static sig_atomic_t LwpSigCheck;
52 /* The thread waiting for signals in lwpSigWait() */
53 static struct lwpProc *LwpSigWaiter;
55 static void lwpCatchAwaitedSig(int);
58 * Initialize waiting for signals in @set.
61 lwpInitSigWait(sigset_t *set)
66 sigemptyset(&LwpSigCaught);
70 act.sa_handler = lwpCatchAwaitedSig;
71 for (i = 0; i < NSIG; i++) {
72 if (sigismember(set, i) > 0)
73 sigaction(i, &act, NULL);
78 lwpCatchAwaitedSig(int sig)
80 sigaddset(&LwpSigCaught, sig);
85 * Test whether a signal from @set has been caught.
86 * If yes, delete that signal from the set of caught signals, and
91 lwpGetSig(sigset_t *set)
96 sigprocmask(SIG_BLOCK, set, &save);
98 for (i = NSIG - 1; i > 0; i--) {
99 if (sigismember(set, i) > 0 && sigismember(&LwpSigCaught, i) > 0) {
100 lwpStatus(LwpCurrent, "Got awaited signal %d", i);
101 sigdelset(&LwpSigCaught, i);
107 sigismember(set, i) > 0 && sigismember(&LwpSigCaught, i) > 0;
113 sigprocmask(SIG_SETMASK, &save, NULL);
118 * Wait until a signal from @set arrives.
119 * Assign its number to *@sig and return 0.
120 * If another thread is already waiting for signals, return EBUSY
124 lwpSigWait(sigset_t *set, int *sig)
131 res = lwpGetSig(set);
134 lwpStatus(LwpCurrent, "Waiting for signals");
135 LwpSigWaiter = LwpCurrent;
143 * Wake up the thread awaiting signals if one arrived.
144 * To be called from lwpReschedule().
149 if (LwpSigWaiter && LwpSigCheck) {
150 lwpReady(LwpSigWaiter);