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-2008 Soeren Sonnenburg
00008  * Copyright (C) 1999-2008 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 "base/SGObject.h"
00016 
00017 #ifndef WIN32
00018 #include <signal.h>
00019 #define NUMTRAPPEDSIGS 2
00020 
00021 #include "lib/python.h"
00022 
00036 class CSignal : public CSGObject
00037 {
00038     public:
00040         CSignal();
00041         ~CSignal();
00042 
00047         static void handler(int signal);
00048 
00053         static bool set_handler();
00054         
00059         static bool unset_handler();
00060 
00062         static void clear();
00063 
00065         static void clear_cancel();
00066 
00071         static inline bool cancel_computations()
00072         {
00073 #ifdef HAVE_PYTHON
00074             if (PyErr_CheckSignals())
00075             {
00076                 SG_SPRINT("\nImmediately return to matlab prompt / Prematurely finish computations / Do nothing (I/P/D)? ");
00077                 char answer=fgetc(stdin);
00078 
00079                 if (answer == 'I')
00080                     SG_SERROR("shogun stopped by SIGINT\n");
00081                 else if (answer == 'P')
00082                 {
00083                     PyErr_Clear();
00084                     cancel_computation=true;
00085                 }
00086                 else
00087                     SG_SPRINT("\n");
00088             }
00089 #endif
00090             return cancel_computation;
00091         }
00092 
00093     protected:
00095         static int signals[NUMTRAPPEDSIGS];
00096 
00098         static struct sigaction oldsigaction[NUMTRAPPEDSIGS];
00099 
00101         static bool active;
00102 
00104         static bool cancel_computation;
00105 };
00106 #endif // WIN32
00107 #endif // __SIGNAL__H_

SHOGUN Machine Learning Toolbox - Documentation