2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1994-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
28 * sig.c: Wait for signals
30 * Known contributors to this file:
31 * Markus Armbruster, 2006-2007
43 * Signals catched so far.
44 * Access only with signals blocked!
46 static sigset_t LwpSigCatched;
49 * LwpSigCatched changed since last
51 static sig_atomic_t LwpSigCheck;
53 /* The thread waiting for signals in lwpSigWait() */
54 static struct lwpProc *LwpSigWaiter;
56 static void lwpCatchAwaitedSig(int);
59 * Initialize waiting for signals in SET.
62 lwpInitSigWait(sigset_t *set)
67 sigemptyset(&LwpSigCatched);
71 sigemptyset(&act.sa_mask);
72 act.sa_handler = lwpCatchAwaitedSig;
73 for (i = 0; i < NSIG; i++) {
74 if (sigismember(set, i))
75 sigaction(i, &act, NULL);
80 lwpCatchAwaitedSig(int sig)
82 sigaddset(&LwpSigCatched, sig);
87 * Test whether a signal from SET has been catched.
88 * If yes, delete that signal from the set of catched signals, and
93 lwpGetSig(sigset_t *set)
98 sigprocmask(SIG_BLOCK, set, &save);
99 for (i = NSIG - 1; i > 0; i--) {
100 if (sigismember(set, i) && sigismember(&LwpSigCatched, i)) {
101 lwpStatus(LwpCurrent, "Got awaited signal %d", i);
102 sigdelset(&LwpSigCatched, i);
106 sigprocmask(SIG_SETMASK, &save, NULL);
111 * Wait until a signal from SET arrives.
112 * Assign its number to *SIG and return 0.
113 * If another thread is already waiting for signals, return EBUSY
117 lwpSigWait(sigset_t *set, int *sig)
121 if (CANT_HAPPEN(LwpSigWaiter))
125 res = lwpGetSig(set);
128 lwpStatus(LwpCurrent, "Waiting for signals");
129 LwpSigWaiter = LwpCurrent;
137 * Wake up the thread awaiting signals if one arrived.
138 * To be called from lwpReschedule().
143 if (LwpSigWaiter && LwpSigCheck) {
144 lwpReady(LwpSigWaiter);