Implement async dispatcher #221

This commit is contained in:
Eduardo Bart
2013-03-04 18:56:22 -03:00
parent c452e74e0c
commit 0be7bd5360
13 changed files with 274 additions and 70 deletions

View File

@@ -27,6 +27,7 @@
#include <framework/core/modulemanager.h>
#include <framework/core/eventdispatcher.h>
#include <framework/core/configmanager.h>
#include "asyncdispatcher.h"
#include <framework/luaengine/luainterface.h>
#include <framework/platform/crashhandler.h>
#include <framework/platform/platform.h>
@@ -76,6 +77,8 @@ void Application::init(std::vector<std::string>& args)
// process args encoding
g_platform.processArgs(args);
g_asyncDispatcher.init();
std::string startupOptions;
for(uint i=1;i<args.size();++i) {
const std::string& arg = args[i];
@@ -109,6 +112,8 @@ void Application::deinit()
// poll remaining events
poll();
g_asyncDispatcher.terminate();
// disable dispatcher events
g_dispatcher.shutdown();
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "asyncdispatcher.h"
AsyncDispatcher g_asyncDispatcher;
void AsyncDispatcher::init()
{
spawn_thread();
}
void AsyncDispatcher::terminate()
{
stop();
m_tasks.clear();
}
void AsyncDispatcher::spawn_thread()
{
m_running = true;
m_threads.push_back(std::thread(std::bind(&AsyncDispatcher::exec_loop, this)));
}
void AsyncDispatcher::stop()
{
m_mutex.lock();
m_running = false;
m_condition.notify_all();
m_mutex.unlock();
for(std::thread& thread : m_threads)
thread.join();
m_threads.clear();
};
void AsyncDispatcher::exec_loop() {
std::unique_lock<std::mutex> lock(m_mutex);
while(true) {
while(m_tasks.size() == 0 && m_running)
m_condition.wait(lock);
if(!m_running)
return;
std::function<void()> task = m_tasks.front();
m_tasks.pop_front();
lock.unlock();
task();
lock.lock();
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef ASYNCDISPATCHER_H
#define ASYNCDISPATCHER_H
#include "declarations.h"
#include <framework/stdext/thread.h>
class AsyncDispatcher {
public:
void init();
void terminate();
void spawn_thread();
void stop();
template<class F>
std::future<typename std::result_of<F()>::type> schedule(const F& task) {
std::lock_guard<std::mutex> lock(m_mutex);
auto prom = std::make_shared<std::promise<typename std::result_of<F()>::type>>();
m_tasks.push_back([=]() { prom->set_value(task()); });
m_condition.notify_all();
return prom->get_future();
}
protected:
void exec_loop();
private:
std::list<std::function<void()>> m_tasks;
std::mutex m_mutex;
std::list<std::thread> m_threads;
std::condition_variable m_condition;
stdext::boolean<false> m_running;
};
extern AsyncDispatcher g_asyncDispatcher;
#endif

View File

@@ -35,6 +35,8 @@ Logger g_logger;
void Logger::log(Fw::LogLevel level, const std::string& message)
{
std::lock_guard<std::recursive_mutex> lock(m_mutex);
#ifdef NDEBUG
if(level == Fw::LogDebug)
return;
@@ -95,6 +97,8 @@ void Logger::log(Fw::LogLevel level, const std::string& message)
void Logger::logFunc(Fw::LogLevel level, const std::string& message, std::string prettyFunction)
{
std::lock_guard<std::recursive_mutex> lock(m_mutex);
prettyFunction = prettyFunction.substr(0, prettyFunction.find_first_of('('));
if(prettyFunction.find_last_of(' ') != std::string::npos)
prettyFunction = prettyFunction.substr(prettyFunction.find_last_of(' ') + 1);
@@ -114,6 +118,8 @@ void Logger::logFunc(Fw::LogLevel level, const std::string& message, std::string
void Logger::fireOldMessages()
{
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if(m_onLog) {
auto backup = m_logMessages;
for(const LogMessage& logMessage : backup) {
@@ -124,6 +130,8 @@ void Logger::fireOldMessages()
void Logger::setLogFile(const std::string& file)
{
std::lock_guard<std::recursive_mutex> lock(m_mutex);
m_outFile.open(stdext::utf8_to_latin1(file.c_str()).c_str(), std::ios::out | std::ios::app);
if(!m_outFile.is_open() || !m_outFile.good()) {
g_logger.error(stdext::format("Unable to save log to '%s'", file));

View File

@@ -25,6 +25,7 @@
#include "../global.h"
#include <framework/stdext/thread.h>
#include <fstream>
struct LogMessage {
@@ -61,6 +62,7 @@ private:
std::list<LogMessage> m_logMessages;
OnLogCallback m_onLog;
std::ofstream m_outFile;
std::recursive_mutex m_mutex;
};
extern Logger g_logger;