gbasys

view src/signal.c @ 0:875ef6085efc

gbasys mercurial repository
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 04 Mar 2012 04:04:25 +0200
parents
children e3dc7705ad9c
line source
1 /*
2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
4 This file is part of libgba, a library for GameBoy Advance development.
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
19 */
21 #include "signal.h"
22 #include "intr.h"
24 static void sig_invalid_handler(int signum) {
25 panic("signal error");
26 }
28 static sighandler_t signal_handler[SIG_MAX];
29 static sighandler_t default_sig_handler[SIG_MAX];
30 static volatile int wait_for_signal;
31 static sighandler_t saved_signal;
33 void sig_init(void) {
34 int i;
36 for(i=0; i<SIG_MAX; i++) {
37 default_sig_handler[i] = sig_invalid_handler;
38 }
40 default_sig_handler[SIGALRM] = SIG_IGN;
41 default_sig_handler[SIGUSR1] = SIG_IGN;
42 default_sig_handler[SIGUSR2] = SIG_IGN;
43 default_sig_handler[SIGIO] = SIG_IGN;
44 for(i=0; i<SIG_MAX; i++) {
45 signal_handler[i] = default_sig_handler[i];
46 }
47 }
49 sighandler_t signal(int signum, sighandler_t handler) {
50 sighandler_t prev = signal_handler[signum];
52 signal_handler[signum] = handler == SIG_IGN ? 0 : (handler == SIG_DFL ? default_sig_handler[signum] : handler);
54 return prev;
55 }
57 int raise(int signum) {
58 if(signal_handler[signum] != SIG_IGN) {
59 signal_handler[signum](signum);
60 wait_for_signal = 0;
61 }
62 return 0;
63 }
65 int pause(void) {
66 clr_int();
67 wait_for_signal = 1;
68 set_int();
70 while(wait_for_signal);
72 /*errno = EINTR;*/
73 return -1;
74 }
76 void save_signal(int signum) {
77 saved_signal = signal_handler[signum];
78 }
80 void restore_signal(int signum) {
81 signal_handler[signum] = saved_signal;
82 }