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 
00024 class CSignal : public CSGObject
00025 {
00026     public:
00028         CSignal();
00029         ~CSignal();
00030 
00035         static void handler(int signal);
00036 
00041         static bool set_handler();
00042         
00047         static bool unset_handler();
00048 
00050         static void clear();
00051 
00056         static inline bool cancel_computations()
00057         {
00058 #ifdef HAVE_PYTHON
00059             if (PyErr_CheckSignals())
00060             {
00061                 SG_SPRINT("\nImmediately return to matlab prompt / Prematurely finish computations / Do nothing (I/P/D)? ");
00062                 char answer=fgetc(stdin);
00063 
00064                 if (answer == 'I')
00065                     SG_SERROR("shogun stopped by SIGINT\n");
00066                 else if (answer == 'P')
00067                 {
00068                     PyErr_Clear();
00069                     cancel_computation=true;
00070                 }
00071                 else
00072                     SG_SPRINT("\n");
00073             }
00074 #endif
00075             return cancel_computation;
00076         }
00077 
00078     protected:
00080         static int signals[NUMTRAPPEDSIGS];
00081 
00083         static struct sigaction oldsigaction[NUMTRAPPEDSIGS];
00084 
00086         static bool active;
00087 
00089         static bool cancel_computation;
00090 };
00091 #endif // WIN32
00092 #endif // __SIGNAL__H_

SHOGUN Machine Learning Toolbox - Documentation