diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-12-26 01:46:48 +0100 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-12-26 01:46:48 +0100 |
commit | 2ab1fbd763ad8692ea3ca29705a4fe821ccb1309 (patch) | |
tree | e05b4e7925e610e68230e418c99c7b5f520a2d93 /src | |
parent | 5e8b56644fa132287431cb06717cdfe0ea05dfbc (diff) | |
download | mad-2ab1fbd763ad8692ea3ca29705a4fe821ccb1309.tar mad-2ab1fbd763ad8692ea3ca29705a4fe821ccb1309.zip |
ActionManager fuer bessere Behandlung von Signalen (und Threads) hinzugefuegt
Diffstat (limited to 'src')
-rw-r--r-- | src/Common/ActionManager.cpp | 85 | ||||
-rw-r--r-- | src/Common/ActionManager.h | 61 | ||||
-rw-r--r-- | src/Common/Makefile.am | 12 | ||||
-rw-r--r-- | src/Common/Makefile.in | 20 | ||||
-rw-r--r-- | src/modules/SystemBackendPosix.cpp | 7 | ||||
-rw-r--r-- | src/modules/SystemBackendProc.cpp | 10 |
6 files changed, 175 insertions, 20 deletions
diff --git a/src/Common/ActionManager.cpp b/src/Common/ActionManager.cpp new file mode 100644 index 0000000..c6c4e41 --- /dev/null +++ b/src/Common/ActionManager.cpp @@ -0,0 +1,85 @@ +/* + * ActionManager.cpp + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 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 General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "ActionManager.h" +#include <Net/FdManager.h> + +#include <fcntl.h> +#include <signal.h> + +#include <sigc++/hide.h> + +namespace Mad { +namespace Common { + +ActionManager ActionManager::actionManager; + + +void ActionManager::doInit() { + // TODO Error handling + + pipe(notifyPipe); + + fcntl(notifyPipe[0], F_SETFL, fcntl(notifyPipe[0], F_GETFL) | O_NONBLOCK); + fcntl(notifyPipe[1], F_SETFL, fcntl(notifyPipe[1], F_GETFL) | O_NONBLOCK); + + Net::FdManager::get()->registerFd(notifyPipe[0], sigc::hide(sigc::mem_fun(this, &ActionManager::run)), POLLIN); +} + +void ActionManager::doDeinit() { + Net::FdManager::get()->unregisterFd(notifyPipe[0]); + + close(notifyPipe[0]); + close(notifyPipe[1]); +} + + +void ActionManager::run() { + while(true) { + sigset_t set, oldset; + sigfillset(&set); + sigprocmask(SIG_SETMASK, &set, &oldset); + + if(actions.empty()) { + sigprocmask(SIG_SETMASK, &oldset, 0); + return; + } + + sigc::slot<void> action = actions.front(); + actions.pop(); + + sigprocmask(SIG_SETMASK, &oldset, 0); + + action(); + } +} + +void ActionManager::add(const sigc::slot<void> &action) { + sigset_t set, oldset; + sigfillset(&set); + sigprocmask(SIG_SETMASK, &set, &oldset); + + actions.push(action); + write(notifyPipe[1], "", 1); + + sigprocmask(SIG_SETMASK, &oldset, 0); +} + +} +} diff --git a/src/Common/ActionManager.h b/src/Common/ActionManager.h new file mode 100644 index 0000000..e47652e --- /dev/null +++ b/src/Common/ActionManager.h @@ -0,0 +1,61 @@ +/* + * ActionManager.h + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 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 General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MAD_COMMON_ACTIONMANAGER_H_ +#define MAD_COMMON_ACTIONMANAGER_H_ + +#include "Initializable.h" + +#include <queue> +#include <unistd.h> + +#include <sigc++/slot.h> + +namespace Mad { +namespace Common { + +class ActionManager : public Initializable { + private: + std::queue<sigc::slot<void> > actions; + int notifyPipe[2]; + + static ActionManager actionManager; + + ActionManager() {} + + protected: + void doInit(); + void doDeinit(); + + public: + void run(); + void add(const sigc::slot<void> &action); + + static ActionManager *get() { + if(!actionManager.isInitialized()) + actionManager.init(); + + return &actionManager; + } +}; + +} +} + +#endif /* MAD_COMMON_ACTIONMANAGER_H_ */ diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am index e41b63b..0f2fb39 100644 --- a/src/Common/Makefile.am +++ b/src/Common/Makefile.am @@ -1,10 +1,12 @@ SUBDIRS = Requests RequestHandlers noinst_LTLIBRARIES = libcommon.la -libcommon_la_SOURCES = ConfigEntry.cpp ConfigManager.cpp Exception.cpp Initializable.cpp Logger.cpp \ - LogManager.cpp ModuleManager.cpp RequestManager.cpp SystemBackend.cpp Tokenizer.cpp +libcommon_la_SOURCES = ActionManager.cpp ConfigEntry.cpp ConfigManager.cpp Exception.cpp Initializable.cpp \ + Logger.cpp LogManager.cpp ModuleManager.cpp RequestManager.cpp \ + SystemBackend.cpp Tokenizer.cpp libcommon_la_LIBADD = Requests/librequests.la RequestHandlers/librequesthandlers.la -noinst_HEADERS = ConfigEntry.h ConfigManager.h Configurable.h Exception.h HostInfo.h Initializable.h \ - Logger.h LoggerBase.h LogManager.h ModuleManager.h RemoteLogger.h Request.h \ - RequestBase.h RequestHandler.h RequestManager.h SystemBackend.h Tokenizer.h +noinst_HEADERS = ActionManager.h ConfigEntry.h ConfigManager.h Configurable.h Exception.h HostInfo.h \ + Initializable.h Logger.h LoggerBase.h LogManager.h ModuleManager.h \ + RemoteLogger.h Request.h RequestBase.h RequestHandler.h RequestManager.h \ + SystemBackend.h Tokenizer.h diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in index 665d78a..e787215 100644 --- a/src/Common/Makefile.in +++ b/src/Common/Makefile.in @@ -51,9 +51,10 @@ CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libcommon_la_DEPENDENCIES = Requests/librequests.la \ RequestHandlers/librequesthandlers.la -am_libcommon_la_OBJECTS = ConfigEntry.lo ConfigManager.lo Exception.lo \ - Initializable.lo Logger.lo LogManager.lo ModuleManager.lo \ - RequestManager.lo SystemBackend.lo Tokenizer.lo +am_libcommon_la_OBJECTS = ActionManager.lo ConfigEntry.lo \ + ConfigManager.lo Exception.lo Initializable.lo Logger.lo \ + LogManager.lo ModuleManager.lo RequestManager.lo \ + SystemBackend.lo Tokenizer.lo libcommon_la_OBJECTS = $(am_libcommon_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) depcomp = $(SHELL) $(top_srcdir)/config/depcomp @@ -227,13 +228,15 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ SUBDIRS = Requests RequestHandlers noinst_LTLIBRARIES = libcommon.la -libcommon_la_SOURCES = ConfigEntry.cpp ConfigManager.cpp Exception.cpp Initializable.cpp Logger.cpp \ - LogManager.cpp ModuleManager.cpp RequestManager.cpp SystemBackend.cpp Tokenizer.cpp +libcommon_la_SOURCES = ActionManager.cpp ConfigEntry.cpp ConfigManager.cpp Exception.cpp Initializable.cpp \ + Logger.cpp LogManager.cpp ModuleManager.cpp RequestManager.cpp \ + SystemBackend.cpp Tokenizer.cpp libcommon_la_LIBADD = Requests/librequests.la RequestHandlers/librequesthandlers.la -noinst_HEADERS = ConfigEntry.h ConfigManager.h Configurable.h Exception.h HostInfo.h Initializable.h \ - Logger.h LoggerBase.h LogManager.h ModuleManager.h RemoteLogger.h Request.h \ - RequestBase.h RequestHandler.h RequestManager.h SystemBackend.h Tokenizer.h +noinst_HEADERS = ActionManager.h ConfigEntry.h ConfigManager.h Configurable.h Exception.h HostInfo.h \ + Initializable.h Logger.h LoggerBase.h LogManager.h ModuleManager.h \ + RemoteLogger.h Request.h RequestBase.h RequestHandler.h RequestManager.h \ + SystemBackend.h Tokenizer.h all: all-recursive @@ -286,6 +289,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ActionManager.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConfigEntry.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConfigManager.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Exception.Plo@am__quote@ diff --git a/src/modules/SystemBackendPosix.cpp b/src/modules/SystemBackendPosix.cpp index d34b2c8..c8135a3 100644 --- a/src/modules/SystemBackendPosix.cpp +++ b/src/modules/SystemBackendPosix.cpp @@ -20,6 +20,7 @@ #include "SystemBackendPosix.h" #include <Net/FdManager.h> +#include <Common/ActionManager.h> #include <cstdio> #include <cstdlib> @@ -113,13 +114,11 @@ void SystemBackendPosix::childHandler(int) { int status; pid_t pid; - // TODO Don't call callbacks directly - while((pid = waitpid(-1, &status, WNOHANG)) > 0) { std::map<pid_t, sigc::slot<void, int> >::iterator it = processes.find(pid); if(it != processes.end()) { - it->second(status); + Common::ActionManager::get()->add(sigc::bind(it->second, status)); processes.erase(it); } else { @@ -137,7 +136,7 @@ void SystemBackendPosix::childHandler(int) { Net::FdManager::get()->unregisterFd(handle); close(handle); - it2->second(status, output); + Common::ActionManager::get()->add(sigc::bind(it2->second, status, output)); processesWithOutput.erase(it2); processesWOHandles.erase(pid); processesWOOutput.erase(pid); diff --git a/src/modules/SystemBackendProc.cpp b/src/modules/SystemBackendProc.cpp index 1af2f28..3a0d33d 100644 --- a/src/modules/SystemBackendProc.cpp +++ b/src/modules/SystemBackendProc.cpp @@ -19,9 +19,13 @@ #include "SystemBackendProc.h" +#include <Common/ActionManager.h> + #include <cstdio> #include <cstring> +#include <sigc++/bind.h> + #define init SystemBackendProc_LTX_init #define deinit SystemBackendProc_LTX_deinit @@ -49,7 +53,7 @@ bool SystemBackendProc::uptimeInfo(const sigc::slot<void, unsigned long, unsigne if(uptimeFile.good()) idleTime = (unsigned long)f; - callback(uptime, idleTime); + Common::ActionManager::get()->add(sigc::bind(callback, uptime, idleTime)); return true; } @@ -86,7 +90,7 @@ bool SystemBackendProc::memoryInfo(const sigc::slot<void, unsigned long, unsigne break; } - callback(totalMem, freeMem, totalSwap, freeSwap); + Common::ActionManager::get()->add(sigc::bind(callback, totalMem, freeMem, totalSwap, freeSwap)); return true; } @@ -108,7 +112,7 @@ bool SystemBackendProc::loadInfo(const sigc::slot<void, unsigned long, unsigned std::sscanf(line.c_str(), "%f %f %f %lu/%lu", &loadAvg1, &loadAvg5, &loadAvg15, ¤tLoad, &nProcesses); - callback(currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15); + Common::ActionManager::get()->add(sigc::bind(callback, currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15)); return true; } |