Signal.h

Go to the documentation of this file.
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 #include "lib/config.h"
00015 #include "lib/ShogunException.h"
00016 #include "base/SGObject.h"
00017 
00018 #ifndef WIN32
00019 #include <signal.h>
00020 #define NUMTRAPPEDSIGS 2
00021 
00022 
00023 #ifndef DISABLE_CANCEL_CALLBACK
00024 extern void (*sg_cancel_computations)(bool &delayed, bool &immediately);
00025 #endif
00026 
00042 class CSignal : public CSGObject
00043 {
00044     public:
00046         CSignal();
00047         virtual ~CSignal();
00048 
00053         static void handler(int signal);
00054 
00059         static bool set_handler();
00060         
00065         static bool unset_handler();
00066 
00068         static void clear();
00069 
00071         static void clear_cancel();
00072 
00074         static void set_cancel(bool immediately=false);
00075 
00080         static inline bool cancel_computations()
00081         {
00082 #ifndef DISABLE_CANCEL_CALLBACK
00083             if (sg_cancel_computations)
00084                 sg_cancel_computations(cancel_computation, cancel_immediately);
00085 #endif
00086             if (cancel_immediately)
00087                 throw ShogunException("Computations have been cancelled immediately");
00088 
00089             return cancel_computation;
00090         }
00091 
00093         inline virtual const char* get_name() { return "Signal"; }
00094 
00095     protected:
00097         static int signals[NUMTRAPPEDSIGS];
00098 
00100         static struct sigaction oldsigaction[NUMTRAPPEDSIGS];
00101 
00103         static bool active;
00104 
00106         static bool cancel_computation;
00107 
00109         static bool cancel_immediately;
00110 };
00111 #endif // WIN32
00112 #endif // __SIGNAL__H_

SHOGUN Machine Learning Toolbox - Documentation