gbasys

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/signal.c	Sun Mar 04 04:04:25 2012 +0200
     1.3 @@ -0,0 +1,82 @@
     1.4 +/*
     1.5 +Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
     1.6 +
     1.7 +This file is part of libgba, a library for GameBoy Advance development.
     1.8 +
     1.9 +This program is free software; you can redistribute it and/or modify
    1.10 +it under the terms of the GNU General Public License as published by
    1.11 +the Free Software Foundation; either version 2 of the License, or
    1.12 +(at your option) any later version.
    1.13 +
    1.14 +This program is distributed in the hope that it will be useful,
    1.15 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 +GNU General Public License for more details.
    1.18 +
    1.19 +You should have received a copy of the GNU General Public License
    1.20 +along with this program; if not, write to the Free Software
    1.21 +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.22 +*/
    1.23 +
    1.24 +#include "signal.h"
    1.25 +#include "intr.h"
    1.26 +
    1.27 +static void sig_invalid_handler(int signum) {
    1.28 +	panic("signal error");
    1.29 +}
    1.30 +
    1.31 +static sighandler_t signal_handler[SIG_MAX];
    1.32 +static sighandler_t default_sig_handler[SIG_MAX];
    1.33 +static volatile int wait_for_signal;
    1.34 +static sighandler_t saved_signal;
    1.35 +
    1.36 +void sig_init(void) {
    1.37 +	int i;
    1.38 +	
    1.39 +	for(i=0; i<SIG_MAX; i++) {
    1.40 +		default_sig_handler[i] = sig_invalid_handler;
    1.41 +	}
    1.42 +	
    1.43 +	default_sig_handler[SIGALRM] = SIG_IGN;
    1.44 +	default_sig_handler[SIGUSR1] = SIG_IGN;
    1.45 +	default_sig_handler[SIGUSR2] = SIG_IGN;
    1.46 +	default_sig_handler[SIGIO] = SIG_IGN;
    1.47 +	for(i=0; i<SIG_MAX; i++) {
    1.48 +		signal_handler[i] = default_sig_handler[i];
    1.49 +	}
    1.50 +}
    1.51 +
    1.52 +sighandler_t signal(int signum, sighandler_t handler) {
    1.53 +	sighandler_t prev = signal_handler[signum];
    1.54 +	
    1.55 +	signal_handler[signum] = handler == SIG_IGN ? 0 : (handler == SIG_DFL ? default_sig_handler[signum] : handler);
    1.56 +	
    1.57 +	return prev;
    1.58 +}
    1.59 +
    1.60 +int raise(int signum) {
    1.61 +	if(signal_handler[signum] != SIG_IGN) {
    1.62 +		signal_handler[signum](signum);
    1.63 +		wait_for_signal = 0;
    1.64 +	}
    1.65 +	return 0;
    1.66 +}
    1.67 +
    1.68 +int pause(void) {
    1.69 +	clr_int();
    1.70 +	wait_for_signal = 1;
    1.71 +	set_int();
    1.72 +
    1.73 +	while(wait_for_signal);
    1.74 +	
    1.75 +	/*errno = EINTR;*/
    1.76 +	return -1;
    1.77 +}
    1.78 +
    1.79 +void save_signal(int signum) {
    1.80 +	saved_signal = signal_handler[signum];
    1.81 +}
    1.82 +
    1.83 +void restore_signal(int signum) {
    1.84 +	signal_handler[signum] = saved_signal;
    1.85 +}