From: Christian P. <cp...@us...> - 2005-02-17 14:15:28
|
Update of /cvsroot/pclasses/pclasses2/src/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20972/src/System Added Files: SignalListener.h SignalListener.posix.cpp Log Message: Added signal listener. --- NEW FILE: SignalListener.posix.cpp --- /*************************************************************************** * Copyright (C) 2005 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * This program 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 General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "pclasses/System/EventQueue.h" #include "SignalListener.h" #include "FdListener.h" #include <signal.h> #include <unistd.h> #include <fcntl.h> namespace P { namespace System { class SignalFdListener: public FdListener { public: SignalFdListener(int handle) : FdListener(handle, FdListener::Read) { for(int i = 0; i < sizeof(_listeners) / sizeof(SignalListener*); i++) _listeners[i] = 0; System::FdListenerList& lst = System::FdListenerList::instance(EventQueue::instance()); lst.addListener(this); } ~SignalFdListener() { System::FdListenerList& lst = System::FdListenerList::instance(EventQueue::instance()); lst.removeListener(this); } static SignalFdListener& instance() { if(!_instance) { int ret = ::pipe(_signalPipe); // make pipe nonblocking ... ::fcntl(_signalPipe[0], F_SETFL, O_NONBLOCK); ::fcntl(_signalPipe[1], F_SETFL, O_NONBLOCK); _instance = new SignalFdListener(_signalPipe[0]); } return *_instance; } void addListener(int sig, SignalListener& l) { _listeners[sig] = &l; } void removeListener(SignalListener& l) { } void writeSignal(int sig) { // read current pending signals ... sigset_t sigSet; int ret = ::read(_signalPipe[0], &sigSet, sizeof(sigset_t)); if(ret != sizeof(sigset_t)) sigemptyset(&sigSet); // add pending signal to set and write back to pipe sigaddset(&sigSet, sig); ::write(_signalPipe[1], &sigSet, sizeof(sigset_t)); } protected: void onRead() { sigset_t blockedSet, oldSet; sigfillset(&blockedSet); sigprocmask(SIG_SETMASK, &blockedSet, &oldSet); sigset_t sigSet; int ret = ::read(fd(), &sigSet, sizeof(sigset_t)); if(ret == sizeof(sigset_t)) { for(int i = 0; i < 64; i++) { if(sigismember(&sigSet, i) == 1 && _listeners[i]) _listeners[i]->onSignal(); } } sigprocmask(SIG_SETMASK, &oldSet, 0); } private: SignalListener* _listeners[64]; static int _signalPipe[2]; static SignalFdListener* _instance; }; int SignalFdListener::_signalPipe[2]; SignalFdListener* SignalFdListener::_instance = 0; void signal_handler(int sig) { SignalFdListener& l = SignalFdListener::instance(); l.writeSignal(sig); } SignalListener::SignalListener(int sig) { struct sigaction sa; sa.sa_handler = &signal_handler; sigfillset(&sa.sa_mask); // block all signals during signal handler's execution sa.sa_flags = SA_RESTART; // restart system calls SignalFdListener& l = SignalFdListener::instance(); l.addListener(sig, *this); sigaction(sig, &sa, 0); } SignalListener::~SignalListener() { SignalFdListener& l = SignalFdListener::instance(); l.removeListener(*this); } } } --- NEW FILE: SignalListener.h --- /*************************************************************************** * Copyright (C) 2005 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * This program 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 General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifndef P_System_SignalListener #define P_System_SignalListener #include "pclasses/Export.h" namespace P { namespace System { class PSYSTEM_EXPORT SignalListener { public: SignalListener(int sig); virtual ~SignalListener(); virtual void onSignal() = 0; }; } // !namespace System } // !namespace P #endif |