00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 1999-2009 Soeren Sonnenburg 00008 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #ifndef __SIGNAL__H_ 00012 #define __SIGNAL__H_ 00013 00014 #ifndef DISABLE_CANCEL_CALLBACK 00015 namespace shogun 00016 { 00017 extern void (*sg_cancel_computations)(bool &delayed, bool &immediately); 00018 } 00019 #endif 00020 00021 #include <shogun/lib/config.h> 00022 #include <shogun/lib/ShogunException.h> 00023 #include <shogun/base/SGObject.h> 00024 #include <shogun/base/init.h> 00025 00026 #ifndef WIN32 00027 #include <signal.h> 00028 #define NUMTRAPPEDSIGS 2 00029 00030 namespace shogun 00031 { 00047 class CSignal : public CSGObject 00048 { 00049 public: 00051 CSignal(); 00052 virtual ~CSignal(); 00053 00058 static void handler(int signal); 00059 00064 static bool set_handler(); 00065 00070 static bool unset_handler(); 00071 00073 static void clear(); 00074 00076 static void clear_cancel(); 00077 00079 static void set_cancel(bool immediately=false); 00080 00085 static inline bool cancel_computations() 00086 { 00087 #ifndef DISABLE_CANCEL_CALLBACK 00088 if (sg_cancel_computations) 00089 sg_cancel_computations(cancel_computation, cancel_immediately); 00090 #endif 00091 if (cancel_immediately) 00092 throw ShogunException("Computations have been cancelled immediately"); 00093 00094 return cancel_computation; 00095 } 00096 00098 inline virtual const char* get_name() const { return "Signal"; } 00099 00100 protected: 00102 static int signals[NUMTRAPPEDSIGS]; 00103 00105 static struct sigaction oldsigaction[NUMTRAPPEDSIGS]; 00106 00108 static bool active; 00109 00111 static bool cancel_computation; 00112 00114 static bool cancel_immediately; 00115 }; 00116 #endif // WIN32 00117 } 00118 #endif // __SIGNAL__H_