00001 // BLAlarm.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 BLALARM_HH 00020 #define BLALARM_HH 00021 00022 #ifdef WIN32 00023 00024 class BLAlarm { 00025 public: 00026 static void clear() { } 00027 static void set(int seconds) { } 00028 static void init() { } 00029 }; 00030 00031 #else 00032 00033 #include <stdio.h> 00034 #include <unistd.h> 00035 #include <signal.h> 00036 00037 #include "globals.hh" 00038 00039 /** class BLAlarm implements an alarm clock. 00040 * used to prevent an infinite tracking loop from hanging an entire job. 00041 * 00042 * NOTE: all functions are inline, so no .cc file is needed. 00043 **/ 00044 class BLAlarm { 00045 public: 00046 /// clear will clear any alarm. 00047 static void clear() { alarm(0); } 00048 00049 /// set() sets an alarm in the future. 00050 /// Implicitly clears any previous alarm. 00051 static void set(int seconds) { alarm(seconds); } 00052 00053 /// init() will setup the signal handler 00054 /// Must be called before set(). 00055 static void init() { signal(SIGALRM,sighandler); } 00056 00057 private: 00058 /// sighandler() handles the alarm signal 00059 static void sighandler(int sig) { 00060 G4Exception("BLAlarm","Alarm Signal",FatalException, 00061 "SIGALRM fired, cannot be recovered"); 00062 } 00063 }; 00064 00065 #endif // ! WIN32 00066 #endif // BLALARM_HH