Implement async dispatcher #221
parent
c452e74e0c
commit
0be7bd5360
@ -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();
|
||||
}
|
||||
}
|
@ -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
|
Loading…
Reference in new issue