From afc0bb6f9ba1cc1f22db9c3f0fa4b53e9c0e1846 Mon Sep 17 00:00:00 2001 From: Caner Candan Date: Fri, 22 Jun 2012 15:37:51 +0200 Subject: [PATCH] + eoSignal: to handle signal with eoCheckpoint instances --- eo/src/utils/CMakeLists.txt | 1 + eo/src/utils/eoSignal.cpp | 36 ++++++++++++++ eo/src/utils/eoSignal.h | 96 +++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 eo/src/utils/eoSignal.cpp create mode 100644 eo/src/utils/eoSignal.h diff --git a/eo/src/utils/CMakeLists.txt b/eo/src/utils/CMakeLists.txt index 010c6d41..5224f652 100644 --- a/eo/src/utils/CMakeLists.txt +++ b/eo/src/utils/CMakeLists.txt @@ -29,6 +29,7 @@ SET(EOUTILS_SOURCES pipecom.cpp eoLogger.cpp eoParallel.cpp + eoSignal.cpp ) ADD_LIBRARY(eoutils STATIC ${EOUTILS_SOURCES}) diff --git a/eo/src/utils/eoSignal.cpp b/eo/src/utils/eoSignal.cpp new file mode 100644 index 00000000..745d21bd --- /dev/null +++ b/eo/src/utils/eoSignal.cpp @@ -0,0 +1,36 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +/** + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact: http://eodev.sourceforge.net + + Autors: todos@geneura.ugr.es, http://geneura.ugr.es + Marc.Schoenauer@polytechnique.fr + mak@dhi.dk + Caner.Candan@univ-angers.fr +*/ + +#include + +/** + * @addtogroup Continuators + * @{ + */ + +// --- Global variables - but don't know what else to do - MS --- +std::map< int, bool > signals_called; + +/** @} */ diff --git a/eo/src/utils/eoSignal.h b/eo/src/utils/eoSignal.h new file mode 100644 index 00000000..7eeafe3c --- /dev/null +++ b/eo/src/utils/eoSignal.h @@ -0,0 +1,96 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +/** + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact: http://eodev.sourceforge.net + + Authors: todos@geneura.ugr.es, http://geneura.ugr.es + Marc.Schoenauer@polytechnique.fr + mak@dhi.dk + Caner.Candan@univ-angers.fr +*/ + +#ifndef _eoSignal_h +#define _eoSignal_h + +#include +#include +#include + +#include +#include + +/** + * @addtogroup Continuators + * @{ + */ + +extern std::map< int, bool > signals_called; + +/** eoSignal inherits from eoCheckPoint including signals handling (see signal(7)) + * + * @ingroup Utilities + */ +template +class eoSignal : public eoCheckPoint +{ +public : + + eoSignal( int sig = SIGINT ) : eoCheckPoint( _dummyContinue ), _sig( sig ) + { + ::signals_called[_sig] = false; + ::signal( _sig, handler ); + } + + eoSignal( eoContinue& _cont, int sig = SIGINT ) : eoCheckPoint( _cont ), _sig( sig ) + { + ::signals_called[_sig] = false; + ::signal( _sig, handler ); + } + + bool operator()( const eoPop& _pop ) + { + bool& called = ::signals_called[_sig]; + if ( called ) + { + eo::log << eo::logging << "Signal granted…" << std::endl ; + called = false; + return this->eoCheckPoint::operator()( _pop ); + } + return true; + } + + virtual std::string className(void) const { return "eoSignal"; } + + static void handler( int sig ) + { + ::signals_called[sig] = true; + eo::log << eo::logging << "Signal wished…" << std::endl ; + } + +private: + class DummyContinue : public eoContinue + { + public: + bool operator() ( const eoPop& ) { return true; } + } _dummyContinue; + + int _sig; +}; + +/** @} */ + +#endif // !_eoSignal_h