00001 // BLAlarm.hh 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 BLALARM_HH 00020 #define BLALARM_HH 00021 00022 /** class BLAlarm implements an alarm clock. 00023 * used to prevent an infinite tracking loop from hanging an entire job. 00024 * 00025 * If BLSignal is also used, BLSignal::init() must be called first. 00026 * 00027 * This class is 100% static. 00028 **/ 00029 class BLAlarm { 00030 static void (*customHandler)(); 00031 public: 00032 /// clear will clear any alarm. 00033 static void clear(); 00034 00035 /// set() sets an alarm in the future. 00036 /// Implicitly clears any previous alarm. 00037 /// seconds <= 0 does a clear(). 00038 static void set(int seconds); 00039 00040 /// init() will setup the signal handler. 00041 /// Must be called before set(). 00042 static void init(); 00043 00044 /// setCustomhandler() sets a custom alarm handler. 00045 /// returns the previous handler, or 0 if none. 00046 static void (*setCustomHandler(void (*handler)()))(); 00047 00048 /// timeRemaining() returns the time remaining, in seconds. 00049 /// returns -1 if no alarm is set. 00050 int timeRemaining(); 00051 00052 private: 00053 /// sighandler() handles the alarm signal 00054 static void sighandler(int sig); 00055 }; 00056 00057 #endif // BLALARM_HH