From 7234fe326d16d6bf9f4374a09ddc6ef790e6723f Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 18 Jun 2009 22:03:02 +0200 Subject: Globale Variablen durch Application-Klasse ersetzt --- src/Core/Application.cpp | 100 +++++++++++++++++++++++++++++++++++++++ src/Core/Application.h | 94 ++++++++++++++++++++++++++++++++++++ src/Core/CMakeLists.txt | 4 +- src/Core/ConfigManager.cpp | 16 +------ src/Core/ConfigManager.h | 12 ++--- src/Core/Initializable.cpp | 69 --------------------------- src/Core/Initializable.h | 54 --------------------- src/Core/LogManager.cpp | 16 +++++-- src/Core/LogManager.h | 11 ++--- src/Core/Logger.cpp | 89 ---------------------------------- src/Core/Logger.h | 26 +--------- src/Core/RemoteLogger.h | 1 + src/Core/Signals/GenericSignal.h | 2 + src/Core/Signals/Signal0.h | 5 +- src/Core/Signals/Signal1.h | 5 +- src/Core/Signals/Signal2.h | 5 +- src/Core/Signals/SignalBase.h | 11 ++++- src/Core/ThreadManager.cpp | 23 ++++----- src/Core/ThreadManager.h | 29 ++++-------- 19 files changed, 264 insertions(+), 308 deletions(-) create mode 100644 src/Core/Application.cpp create mode 100644 src/Core/Application.h delete mode 100644 src/Core/Initializable.cpp delete mode 100644 src/Core/Initializable.h delete mode 100644 src/Core/Logger.cpp (limited to 'src/Core') diff --git a/src/Core/Application.cpp b/src/Core/Application.cpp new file mode 100644 index 0000000..7c92ed6 --- /dev/null +++ b/src/Core/Application.cpp @@ -0,0 +1,100 @@ +/* + * Application.cpp + * + * Copyright (C) 2009 Matthias Schiffer + * + * 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 . + */ + +#include "Application.h" +#include "ConfigManager.h" +#include "LogManager.h" +#include "ThreadManager.h" + +#include + +namespace Mad { +namespace Core { + +Application::Application() : configManager(new ConfigManager(this)), logManager(new LogManager(this)), threadManager(new ThreadManager(this)) {} + +Application::~Application() { + delete threadManager; + delete logManager; + delete configManager; +} + + +void Application::logfv(LoggerBase::MessageCategory category, LoggerBase::MessageLevel level, const char *format, va_list ap) { + int size = 100; + char *buf = (char*)std::malloc(size); + + // If buffer is too small, try again with bigger buffer + while(true) { + va_list ap2; + + va_copy(ap2, ap); + int n = std::vsnprintf(buf, size, format, ap2); + va_end(ap2); + + if(n > -1 && n < size) { + logManager->log(category, level, std::time(0), buf); + std::free(buf); + return; + } + + if(n > -1) + size = n+1; + else + size *= 2; + + buf = (char*)std::realloc(buf, size); + } + +} + +void Application::log(LoggerBase::MessageCategory category, LoggerBase::MessageLevel level, const std::string &message) { + logManager->log(category, level, std::time(0), message); +} + +void Application::logf(LoggerBase::MessageCategory category, LoggerBase::MessageLevel level, const char *format, ...) { + va_list ap; + va_start(ap, format); + logfv(category, level, format, ap); + va_end(ap); +} + +void Application::logf(LoggerBase::MessageCategory category, const char *format, ...) { + va_list ap; + va_start(ap, format); + logfv(category, LoggerBase::DEFAULT, format, ap); + va_end(ap); +} + +void Application::logf(LoggerBase::MessageLevel level, const char *format, ...) { + va_list ap; + va_start(ap, format); + logfv(LoggerBase::GENERAL, level, format, ap); + va_end(ap); +} + +void Application::logf(const char *format, ...) { + va_list ap; + va_start(ap, format); + logfv(LoggerBase::GENERAL, LoggerBase::DEFAULT, format, ap); + va_end(ap); +} + +} +} diff --git a/src/Core/Application.h b/src/Core/Application.h new file mode 100644 index 0000000..bfc23db --- /dev/null +++ b/src/Core/Application.h @@ -0,0 +1,94 @@ +/* + * Application.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * 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 . + */ + +#ifndef MAD_CORE_APPLICATION_H_ +#define MAD_CORE_APPLICATION_H_ + +#include "LoggerBase.h" + +#include +#include + +#include +#include + +namespace Mad { +namespace Core { + +class ConfigManager; +class LogManager; +class ThreadManager; + +class Application : private boost::noncopyable { + private: + boost::asio::io_service ioService; + + ConfigManager *configManager; + LogManager *logManager; + ThreadManager *threadManager; + + void logfv(LoggerBase::MessageCategory category, LoggerBase::MessageLevel level, const char *format, va_list ap); + + protected: + Application(); + virtual ~Application(); + + public: + boost::asio::io_service& getIOService() { + return ioService; + } + + ConfigManager* getConfigManager() const { + return configManager; + } + + LogManager* getLogManager() const { + return logManager; + } + + ThreadManager* getThreadManager() const { + return threadManager; + } + + + void log(LoggerBase::MessageCategory category, LoggerBase::MessageLevel level, const std::string &message); + + void log(LoggerBase::MessageCategory category, const std::string &message) { + log(category, LoggerBase::DEFAULT, message); + } + + void log(LoggerBase::MessageLevel level, const std::string &message) { + log(LoggerBase::GENERAL, level, message); + } + + void log(const std::string &message) { + log(LoggerBase::GENERAL, LoggerBase::DEFAULT, message); + } + + + void logf(LoggerBase::MessageCategory category, LoggerBase::MessageLevel level, const char *format, ...); + void logf(LoggerBase::MessageCategory category, const char *format, ...); + void logf(LoggerBase::MessageLevel level, const char *format, ...); + void logf(const char *format, ...); +}; + +} +} + +#endif /* MAD_CORE_APPLICATION_H_ */ diff --git a/src/Core/CMakeLists.txt b/src/Core/CMakeLists.txt index 30947e3..9693460 100644 --- a/src/Core/CMakeLists.txt +++ b/src/Core/CMakeLists.txt @@ -9,12 +9,12 @@ add_library(Core Signals/Signal2.h Signals/SignalBase.h + Application.cpp Application.h ConfigEntry.cpp ConfigEntry.h ConfigManager.cpp ConfigManager.h Configurable.h Exception.cpp Exception.h - Initializable.cpp Initializable.h - Logger.cpp Logger.h + Logger.h LoggerBase.h LogManager.cpp LogManager.h RemoteLogger.h diff --git a/src/Core/ConfigManager.cpp b/src/Core/ConfigManager.cpp index 8b1ebd4..9e0dada 100644 --- a/src/Core/ConfigManager.cpp +++ b/src/Core/ConfigManager.cpp @@ -17,11 +17,10 @@ * with this program. If not, see . */ +#include "Application.h" #include "ConfigManager.h" #include "ConfigEntry.h" #include "Configurable.h" -#include "Logger.h" -#include "LogManager.h" #include "Tokenizer.h" #include @@ -30,9 +29,6 @@ namespace Mad { namespace Core { -ConfigManager ConfigManager::configManager; - - bool ConfigManager::Compare::operator() (const Configurable *c1, const Configurable *c2) { if(c1->getPriority() != c2->getPriority()) return c1->getPriority() > c2->getPriority(); @@ -50,7 +46,7 @@ void ConfigManager::handleConfigEntry(const ConfigEntry &entry) { } if(!handled) - Logger::logf(Logger::WARNING, "Invalid config option '%s'.", entry[entry.getSize()-1].getKey().c_str()); + application->logf(LoggerBase::WARNING, "Invalid config option '%s'.", entry[entry.getSize()-1].getKey().c_str()); } bool ConfigManager::loadFile(const std::string &filename) { @@ -129,13 +125,5 @@ void ConfigManager::finish() { finished = true; } -ConfigManager::ConfigManager() : finished(false) { - registerConfigurable(LogManager::get()); -} - -ConfigManager::~ConfigManager() { - unregisterConfigurable(LogManager::get()); -} - } } diff --git a/src/Core/ConfigManager.h b/src/Core/ConfigManager.h index 2d49a97..d7d7d01 100644 --- a/src/Core/ConfigManager.h +++ b/src/Core/ConfigManager.h @@ -27,6 +27,7 @@ namespace Mad { namespace Core { +class Application; class ConfigEntry; class Configurable; @@ -36,13 +37,14 @@ class ConfigManager { bool operator() (const Configurable *c1, const Configurable *c2); }; - static ConfigManager configManager; + friend class Application; + + Application *application; std::set configurables; bool finished; - ConfigManager(); - ~ConfigManager(); + ConfigManager(Application *application0) : application(application0), finished(false) {} void handleConfigEntry(const ConfigEntry &entry); @@ -57,10 +59,6 @@ class ConfigManager { void unregisterConfigurable(Configurable *c) { configurables.erase(c); } - - static ConfigManager *get() { - return &configManager; - } }; } diff --git a/src/Core/Initializable.cpp b/src/Core/Initializable.cpp deleted file mode 100644 index 95d527f..0000000 --- a/src/Core/Initializable.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Initializable.cpp - * - * Copyright (C) 2008 Matthias Schiffer - * - * 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 . - */ - -#include "Initializable.h" - -#include "ConfigManager.h" -#include "Configurable.h" -#include "Logger.h" - -namespace Mad { -namespace Core { - -std::stack Initializable::initializedObjects; - -void Initializable::init() { - if(initialized) - return; - - if(initializing) { - Logger::log(Logger::CRITICAL, "Fatal initialization error: cyclic dependencies."); - std::terminate(); - } - - initializing = true; - - doInit(); - - Configurable *c = dynamic_cast(this); - if(c) - ConfigManager::get()->registerConfigurable(c); - - initializing = false; - initialized = true; - initializedObjects.push(this); -} - -void Initializable::deinit() { - while(!initializedObjects.empty()) { - Initializable *in = initializedObjects.top(); - - Configurable *c = dynamic_cast(in); - if(c) - ConfigManager::get()->unregisterConfigurable(c); - - in->doDeinit(); - in->initialized = false; - - initializedObjects.pop(); - } -} - -} -} diff --git a/src/Core/Initializable.h b/src/Core/Initializable.h deleted file mode 100644 index e7d329d..0000000 --- a/src/Core/Initializable.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Initializable.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * 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 . - */ - -#ifndef MAD_CORE_INITIALIZABLE_H_ -#define MAD_CORE_INITIALIZABLE_H_ - -#include - -namespace Mad { -namespace Core { - -class Initializable { - private: - static std::stack initializedObjects; - - bool initializing; - bool initialized; - - protected: - Initializable() : initializing(false), initialized(false) {} - virtual void doInit() {} - virtual void doDeinit() {} - - public: - virtual ~Initializable() {} - - void init(); - - bool isInitialized() const {return initialized;} - bool isInitializing() const {return initializing;} - - static void deinit(); -}; - -} -} - -#endif /* MAD_CORE_INITIALIZABLE_H_ */ diff --git a/src/Core/LogManager.cpp b/src/Core/LogManager.cpp index 91149f9..28b1f6f 100644 --- a/src/Core/LogManager.cpp +++ b/src/Core/LogManager.cpp @@ -18,16 +18,15 @@ */ #include "LogManager.h" +#include "Application.h" #include "ConfigEntry.h" +#include "ConfigManager.h" #include namespace Mad { namespace Core { -LogManager LogManager::logManager; - - void LogManager::ConsoleLogger::logMessage(MessageCategory category _UNUSED_PARAMETER_, MessageLevel level, time_t timestamp _UNUSED_PARAMETER_, const std::string &message) { if(level != CRITICAL) {// Critical messages are printed to cerr directly, so don't print them a second time cerrLock.lock(); @@ -49,6 +48,15 @@ void LogManager::ConsoleLogger::logMessageDirect(MessageCategory category _UNUSE } +LogManager::LogManager(Application *application0) : application(application0), consoleLogger(new ConsoleLogger), configured(false), running(false) { + application->getConfigManager()->registerConfigurable(this); +} + +LogManager::~LogManager() { + application->getConfigManager()->unregisterConfigurable(this); +} + + bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) { if(handled) return false; @@ -61,7 +69,7 @@ bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) { } } else if(entry[1].empty()) { - Logger::logf(Logger::WARNING, "Unknown logger '%s'.", entry[0][0].c_str()); + application->logf(Logger::WARNING, "Unknown logger '%s'.", entry[0][0].c_str()); return true; } } diff --git a/src/Core/LogManager.h b/src/Core/LogManager.h index 8c8d091..0ed21b8 100644 --- a/src/Core/LogManager.h +++ b/src/Core/LogManager.h @@ -35,10 +35,12 @@ namespace Mad { namespace Core { +class Application; class ThreadManager; class LogManager : public Configurable { private: + friend class Application; friend class ThreadManager; typedef LoggerBase::MessageCategory MessageCategory; @@ -76,7 +78,7 @@ class LogManager : public Configurable { }; - static LogManager logManager; + Application *application; boost::shared_ptr consoleLogger; @@ -101,7 +103,8 @@ class LogManager : public Configurable { queueCond.notify_one(); } - LogManager() : consoleLogger(new ConsoleLogger), configured(false), running(false) {} + LogManager(Application *application0); + ~LogManager(); protected: virtual bool handleConfigEntry(const ConfigEntry &entry, bool handled); @@ -134,10 +137,6 @@ class LogManager : public Configurable { remoteLoggers.erase(logger); remoteLoggerLock.unlock(); } - - static LogManager *get() { - return &logManager; - } }; } diff --git a/src/Core/Logger.cpp b/src/Core/Logger.cpp deleted file mode 100644 index e4e0341..0000000 --- a/src/Core/Logger.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Logger.cpp - * - * Copyright (C) 2008 Johannes Thorn - * - * 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 . - */ - -#include "Logger.h" -#include "LogManager.h" - -#include - -namespace Mad { -namespace Core { - -void Logger::logfv(MessageCategory category, MessageLevel level, const char *format, va_list ap) { - int size = 100; - char *buf = (char*)std::malloc(size); - - // If buffer is too small, try again with bigger buffer - while(true) { - va_list ap2; - - va_copy(ap2, ap); - int n = std::vsnprintf(buf, size, format, ap2); - va_end(ap2); - - if(n > -1 && n < size) { - log(category, level, buf); - std::free(buf); - return; - } - - if(n > -1) - size = n+1; - else - size *= 2; - - buf = (char*)std::realloc(buf, size); - } - -} - -void Logger::log(MessageCategory category, MessageLevel level, const std::string &message) { - LogManager::get()->log(category, level, std::time(0), message); -} - -void Logger::logf(MessageCategory category, MessageLevel level, const char *format, ...) { - va_list ap; - va_start(ap, format); - logfv(category, level, format, ap); - va_end(ap); -} - -void Logger::logf(MessageCategory category, const char *format, ...) { - va_list ap; - va_start(ap, format); - logfv(category, DEFAULT, format, ap); - va_end(ap); -} - -void Logger::logf(MessageLevel level, const char *format, ...) { - va_list ap; - va_start(ap, format); - logfv(GENERAL, level, format, ap); - va_end(ap); -} - -void Logger::logf(const char *format, ...) { - va_list ap; - va_start(ap, format); - logfv(GENERAL, DEFAULT, format, ap); - va_end(ap); -} - -} -} diff --git a/src/Core/Logger.h b/src/Core/Logger.h index 3de22d2..6a8e752 100644 --- a/src/Core/Logger.h +++ b/src/Core/Logger.h @@ -22,7 +22,6 @@ #include "LoggerBase.h" -#include #include #include @@ -32,34 +31,11 @@ namespace Core { class LogManager; class Logger : public LoggerBase { - private: + protected: friend class LogManager; - static void logfv(MessageCategory category, MessageLevel level, const char *format, va_list ap); - - protected: virtual void logMessage(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message) = 0; - public: - static void log(MessageCategory category, MessageLevel level, const std::string &message); - - static void log(MessageCategory category, const std::string &message) { - log(category, DEFAULT, message); - } - - static void log(MessageLevel level, const std::string &message) { - log(GENERAL, level, message); - } - - static void log(const std::string &message) { - log(GENERAL, DEFAULT, message); - } - - - static void logf(MessageCategory category, MessageLevel level, const char *format, ...); - static void logf(MessageCategory category, const char *format, ...); - static void logf(MessageLevel level, const char *format, ...); - static void logf(const char *format, ...); }; } diff --git a/src/Core/RemoteLogger.h b/src/Core/RemoteLogger.h index f5c8a6b..ca982a9 100644 --- a/src/Core/RemoteLogger.h +++ b/src/Core/RemoteLogger.h @@ -22,6 +22,7 @@ #include "LoggerBase.h" +#include #include namespace Mad { diff --git a/src/Core/Signals/GenericSignal.h b/src/Core/Signals/GenericSignal.h index 641f575..af5bae9 100644 --- a/src/Core/Signals/GenericSignal.h +++ b/src/Core/Signals/GenericSignal.h @@ -37,6 +37,8 @@ class GenericSignal : protected SignalBase { protected: std::map handlers; + GenericSignal(Application *application) : SignalBase(application) {} + public: Connection connect(const slot_type &slot) { boost::lock_guard lock(mutex); diff --git a/src/Core/Signals/Signal0.h b/src/Core/Signals/Signal0.h index ccfb548..c01a82a 100644 --- a/src/Core/Signals/Signal0.h +++ b/src/Core/Signals/Signal0.h @@ -21,6 +21,7 @@ #define MAD_CORE_SIGNALS_SIGNAL0_H_ #include "GenericSignal.h" +#include "../Application.h" #include "../ThreadManager.h" #include @@ -31,11 +32,13 @@ namespace Signals { class Signal0 : public GenericSignal > { public: + Signal0(Application *application) : GenericSignal >(application) {} + void emit() { boost::lock_guard lock(mutex); for(std::map::iterator handler = handlers.begin(); handler != handlers.end(); ++handler) - ThreadManager::get()->pushWork(handler->second); + getApplication()->getThreadManager()->pushWork(handler->second); } }; diff --git a/src/Core/Signals/Signal1.h b/src/Core/Signals/Signal1.h index e4a946c..0a03c39 100644 --- a/src/Core/Signals/Signal1.h +++ b/src/Core/Signals/Signal1.h @@ -21,6 +21,7 @@ #define MAD_CORE_SIGNALS_SIGNAL1_H_ #include "GenericSignal.h" +#include "../Application.h" #include "../ThreadManager.h" #include @@ -32,11 +33,13 @@ namespace Signals { template class Signal1 : public GenericSignal > { public: + Signal1(Application *application) : GenericSignal >(application) {} + void emit(T1 arg1) { boost::lock_guard lock(this->mutex); for(typename std::map >::slot_type>::iterator handler = this->handlers.begin(); handler != this->handlers.end(); ++handler) - ThreadManager::get()->pushWork(boost::bind(handler->second, arg1)); + this->getApplication()->getThreadManager()->pushWork(boost::bind(handler->second, arg1)); } }; diff --git a/src/Core/Signals/Signal2.h b/src/Core/Signals/Signal2.h index 41045d7..0966258 100644 --- a/src/Core/Signals/Signal2.h +++ b/src/Core/Signals/Signal2.h @@ -21,6 +21,7 @@ #define MAD_CORE_SIGNALS_SIGNAL2_H_ #include "GenericSignal.h" +#include "../Application.h" #include "../ThreadManager.h" #include @@ -32,11 +33,13 @@ namespace Signals { template class Signal2 : public GenericSignal > { public: + Signal2(Application *application) : GenericSignal >(application) {} + void emit(T1 arg1, T2 arg2) { boost::lock_guard lock(this->mutex); for(typename std::map >::slot_type>::iterator handler = this->handlers.begin(); handler != this->handlers.end(); ++handler) - ThreadManager::get()->pushWork(boost::bind(handler->second, arg1, arg2)); + this->getApplication()->getThreadManager()->pushWork(boost::bind(handler->second, arg1, arg2)); } }; diff --git a/src/Core/Signals/SignalBase.h b/src/Core/Signals/SignalBase.h index 1a5d5a3..8ec8483 100644 --- a/src/Core/Signals/SignalBase.h +++ b/src/Core/Signals/SignalBase.h @@ -27,12 +27,17 @@ namespace Mad { namespace Core { + +class Application; + namespace Signals { class SignalBase : private boost::noncopyable { private: friend class Connection; + Application *application; + unsigned long connectionId; protected: @@ -42,8 +47,12 @@ class SignalBase : private boost::noncopyable { return Connection(this, connectionId++); } - SignalBase() : connectionId(0) {} + SignalBase(Application *application0) : application(application0), connectionId(0) {} virtual ~SignalBase() {} + + Application* getApplication() { + return application; + } }; } diff --git a/src/Core/ThreadManager.cpp b/src/Core/ThreadManager.cpp index abc0bc6..c34a734 100644 --- a/src/Core/ThreadManager.cpp +++ b/src/Core/ThreadManager.cpp @@ -19,7 +19,7 @@ #include "ThreadManager.h" -#include "Logger.h" +#include "Application.h" #include "LogManager.h" #include @@ -27,9 +27,6 @@ namespace Mad { namespace Core { -ThreadManager ThreadManager::threadManager; - - void ThreadManager::workerFunc() { while(true) { boost::unique_lock lock(runLock); @@ -61,7 +58,7 @@ void ThreadManager::workerFunc() { void ThreadManager::detach() { if(isThisMainThread()) { - Logger::log(Logger::CRITICAL, "Tried to detach main thread! This is just WRONG!"); + application->log(Logger::CRITICAL, "Tried to detach main thread! This is just WRONG!"); return; } @@ -91,22 +88,20 @@ void ThreadManager::pushWork(const boost::function0 &newWork) { } -void ThreadManager::doInit() { - running = true; - +ThreadManager::ThreadManager(Application *application0) : application(application0), running(true) { threadLock.lock(); - ioWorker.reset(new boost::asio::io_service::work(ioService)); + ioWorker.reset(new boost::asio::io_service::work(application->getIOService())); mainThreadId = boost::this_thread::get_id(); workerThread.reset(new boost::thread(&ThreadManager::workerFunc, this)); - loggerThread.reset(new boost::thread(&LogManager::loggerThread, LogManager::get())); - ioThread.reset(new boost::thread((std::size_t(boost::asio::io_service::*)())&boost::asio::io_service::run, &ioService)); + loggerThread.reset(new boost::thread(&LogManager::loggerThread, application->getLogManager())); + ioThread.reset(new boost::thread((std::size_t(boost::asio::io_service::*)())&boost::asio::io_service::run, &application->getIOService())); threadLock.unlock(); } -void ThreadManager::doDeinit() { +ThreadManager::~ThreadManager() { if(!isThisMainThread()) { // TODO Critical error!!! return; @@ -131,11 +126,11 @@ void ThreadManager::doDeinit() { // IO thread is next ioWorker.reset(); - ioService.stop(); + application->getIOService().stop(); ioThread->join(); // Finally, the logger thread has to die - LogManager::get()->stopLoggerThread(); + application->getLogManager()->stopLoggerThread(); loggerThread->join(); } diff --git a/src/Core/ThreadManager.h b/src/Core/ThreadManager.h index b512ad4..c20cb1f 100644 --- a/src/Core/ThreadManager.h +++ b/src/Core/ThreadManager.h @@ -22,8 +22,6 @@ #include -#include "Initializable.h" - #include #include @@ -36,8 +34,14 @@ namespace Mad { namespace Core { -class ThreadManager : public Initializable { +class Application; + +class ThreadManager : private boost::noncopyable { private: + friend class Application; + + Application *application; + boost::thread::id mainThreadId; boost::shared_ptr workerThread, loggerThread, ioThread; std::map > threads; @@ -53,11 +57,8 @@ class ThreadManager : public Initializable { boost::scoped_ptr ioWorker; - boost::asio::io_service ioService; - - static ThreadManager threadManager; - - ThreadManager() {} + ThreadManager(Application *application0); + ~ThreadManager(); void workerFunc(); void ioFunc(); @@ -80,10 +81,6 @@ class ThreadManager : public Initializable { thread->join(); } - protected: - virtual void doInit(); - virtual void doDeinit(); - public: bool isThisMainThread() { return (mainThreadId == boost::this_thread::get_id()); @@ -94,17 +91,9 @@ class ThreadManager : public Initializable { return (workerThread->get_id() == boost::this_thread::get_id()); } - boost::asio::io_service& getIOService() { - return ioService; - } - void detach(); void pushWork(const boost::function0 &newWork); - - static ThreadManager* get() { - return &threadManager; - } }; } -- cgit v1.2.3