00001 // BLSignal.cc 00002 /* 00003 This source file is part of G4beamline, http://g4beamline.muonsinc.com 00004 Copyright (C) 2003,2004,2005,2006 by Tom Roberts, all rights reserved. 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 http://www.gnu.org/copyleft/gpl.html 00017 */ 00018 00019 #ifndef BLSIGNAL_HH 00020 #define BLSIGNAL_HH 00021 00022 /** class BLSignal implements a signal handler to permit G4beamline 00023 * to exit cleanly when a UNIX signal is received. 00024 * 00025 * SIGUSR1 and SIGUSR2 are special, and do not exit the program; 00026 * they are merely reported via functions sigusr1() and sigusr2(). 00027 * 00028 * Note that a second received signal causes an immediate call to 00029 * g4bl_exit() from the signal handler. 00030 * 00031 * If BLAlarm is also used, BLSignal::init() must be called first. 00032 * 00033 * Note that BLManager checks received() in UserSteppingAction(), and 00034 * issues a fatal exception when any signal is received.. 00035 **/ 00036 class BLSignal { 00037 public: 00038 /// init() will setup the signal handler 00039 static void init(); 00040 00041 /// received() returns true if a signal has been received. 00042 /// Does not include SIGUSR1 or SIGUSR2. Does not include SIGALRM 00043 /// if BLAlarm is used. 00044 static bool received() { return signalReceived; }; 00045 00046 /// value() returns the signal # of the last sginal received. 00047 /// Does not include SIGUSR1 or SIGUSR2. Does not include SIGALRM 00048 /// if BLAlarm is used. 00049 static int value() { return prev; } 00050 00051 /// clear() will clear the signalReceived flag. 00052 void clear() { signalReceived = false; prev = 0; } 00053 00054 /// setSignalReceived() will set the signalReceived flag. 00055 static void setSignalReceived(bool v) { signalReceived = v; } 00056 00057 /// sigusr1() returns true if one or more SIGUSR1 signals was 00058 /// received since the last call; 00059 static bool sigusr1() { bool v=usr1; usr1=false; return v; } 00060 00061 /// sigusr2() returns true if one or more SIGUSR2 signals was 00062 /// received since the last call; 00063 static bool sigusr2() { bool v=usr2; usr2=false; return v; } 00064 00065 private: 00066 static bool signalReceived; 00067 static bool usr1, usr2; 00068 static int prev; 00069 00070 /// sighandler() handles the alarm signal 00071 static void sighandler(int sig); 00072 }; 00073 00074 #endif // BLSIGNAL_HH