gbasys

annotate src/signal.c @ 2:e3dc7705ad9c

communications stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 07 Mar 2012 06:11:51 +0200
parents 875ef6085efc
children 06726f0b8cd3
rev   line source
nuclear@0 1 /*
nuclear@0 2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
nuclear@0 3
nuclear@0 4 This file is part of libgba, a library for GameBoy Advance development.
nuclear@0 5
nuclear@0 6 This program is free software; you can redistribute it and/or modify
nuclear@0 7 it under the terms of the GNU General Public License as published by
nuclear@0 8 the Free Software Foundation; either version 2 of the License, or
nuclear@0 9 (at your option) any later version.
nuclear@0 10
nuclear@0 11 This program is distributed in the hope that it will be useful,
nuclear@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@0 14 GNU General Public License for more details.
nuclear@0 15
nuclear@0 16 You should have received a copy of the GNU General Public License
nuclear@0 17 along with this program; if not, write to the Free Software
nuclear@0 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
nuclear@0 19 */
nuclear@0 20
nuclear@0 21 #include "signal.h"
nuclear@0 22 #include "intr.h"
nuclear@0 23
nuclear@0 24 static void sig_invalid_handler(int signum) {
nuclear@0 25 panic("signal error");
nuclear@0 26 }
nuclear@0 27
nuclear@0 28 static sighandler_t signal_handler[SIG_MAX];
nuclear@0 29 static sighandler_t default_sig_handler[SIG_MAX];
nuclear@0 30 static volatile int wait_for_signal;
nuclear@0 31 static sighandler_t saved_signal;
nuclear@0 32
nuclear@0 33 void sig_init(void) {
nuclear@0 34 int i;
nuclear@2 35
nuclear@0 36 for(i=0; i<SIG_MAX; i++) {
nuclear@0 37 default_sig_handler[i] = sig_invalid_handler;
nuclear@0 38 }
nuclear@2 39
nuclear@0 40 default_sig_handler[SIGALRM] = SIG_IGN;
nuclear@0 41 default_sig_handler[SIGUSR1] = SIG_IGN;
nuclear@0 42 default_sig_handler[SIGUSR2] = SIG_IGN;
nuclear@0 43 default_sig_handler[SIGIO] = SIG_IGN;
nuclear@0 44 for(i=0; i<SIG_MAX; i++) {
nuclear@0 45 signal_handler[i] = default_sig_handler[i];
nuclear@0 46 }
nuclear@0 47 }
nuclear@0 48
nuclear@0 49 sighandler_t signal(int signum, sighandler_t handler) {
nuclear@0 50 sighandler_t prev = signal_handler[signum];
nuclear@2 51
nuclear@0 52 signal_handler[signum] = handler == SIG_IGN ? 0 : (handler == SIG_DFL ? default_sig_handler[signum] : handler);
nuclear@2 53
nuclear@0 54 return prev;
nuclear@0 55 }
nuclear@0 56
nuclear@0 57 int raise(int signum) {
nuclear@0 58 if(signal_handler[signum] != SIG_IGN) {
nuclear@0 59 signal_handler[signum](signum);
nuclear@0 60 wait_for_signal = 0;
nuclear@0 61 }
nuclear@0 62 return 0;
nuclear@0 63 }
nuclear@0 64
nuclear@0 65 int pause(void) {
nuclear@0 66 clr_int();
nuclear@0 67 wait_for_signal = 1;
nuclear@0 68 set_int();
nuclear@0 69
nuclear@0 70 while(wait_for_signal);
nuclear@2 71
nuclear@0 72 /*errno = EINTR;*/
nuclear@0 73 return -1;
nuclear@0 74 }
nuclear@0 75
nuclear@0 76 void save_signal(int signum) {
nuclear@0 77 saved_signal = signal_handler[signum];
nuclear@0 78 }
nuclear@0 79
nuclear@0 80 void restore_signal(int signum) {
nuclear@0 81 signal_handler[signum] = saved_signal;
nuclear@0 82 }
nuclear@2 83
nuclear@2 84 sighandler_t signal_func(int signum)
nuclear@2 85 {
nuclear@2 86 return signal_handler[signum];
nuclear@2 87 }