Get the concept working
A bit hackish because see ThreadPool.hpp. Also code is dirty and it can't be stopped once running. Building up from here.
This commit is contained in:
parent
399f60190f
commit
9f0fed98e2
8 changed files with 282 additions and 78 deletions
182
src/evloop.cpp
182
src/evloop.cpp
|
@ -1,4 +1,5 @@
|
|||
#include "evloop.hpp"
|
||||
#include "html_fetch_task.hpp"
|
||||
#include <ev++.h>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
@ -7,79 +8,152 @@
|
|||
#include <random>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <future>
|
||||
#include <cassert>
|
||||
|
||||
namespace duck {
|
||||
namespace {
|
||||
auto time_rand = std::bind(std::uniform_int_distribution<int>(2, 8), std::mt19937(std::time(0)));
|
||||
|
||||
class HtmlFetchTimer : public EvTimerTask {
|
||||
class KeepaliveTimer : public ev::timer {
|
||||
public:
|
||||
HtmlFetchTimer (ev::loop_ref& loop, tf::Subflow* subflow, std::string&& url) :
|
||||
EvTimerTask(static_cast<double>(time_rand()), loop, subflow),
|
||||
m_url(std::move(url))
|
||||
{
|
||||
KeepaliveTimer (ev::loop_ref& loop, RunningPool::subpool_type*) {
|
||||
this->set(loop);
|
||||
this->set<KeepaliveTimer, &KeepaliveTimer::on_timer_ev>(this);
|
||||
ev::timer::start(5.0, 5.0);
|
||||
}
|
||||
|
||||
virtual void on_timer() override {
|
||||
subflow().emplace([this]() {std::cout << "Timer elapsed for " << m_url << "!\n";});
|
||||
void on_timer_ev() {
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_url;
|
||||
};
|
||||
|
||||
class EvThreadPool {
|
||||
public:
|
||||
typedef std::thread thread_t;
|
||||
EvThreadPool() :
|
||||
m_loop(ev::AUTO)
|
||||
{
|
||||
}
|
||||
|
||||
void start() {
|
||||
m_taskflow.emplace([this](tf::Subflow& subflow){main_loop(subflow);});
|
||||
m_executor.run(m_taskflow);
|
||||
}
|
||||
|
||||
void join() {
|
||||
m_executor.wait_for_all();
|
||||
std::cout << "all tasks completed\n";
|
||||
}
|
||||
|
||||
private:
|
||||
void main_loop (tf::Subflow& subflow) {
|
||||
m_timer = std::make_unique<HtmlFetchTimer>(m_loop, &subflow, "lalalala");
|
||||
|
||||
this->m_loop.run(0);
|
||||
}
|
||||
|
||||
ev::default_loop m_loop;
|
||||
tf::Taskflow m_taskflow;
|
||||
tf::Executor m_executor;
|
||||
std::unique_ptr<HtmlFetchTimer> m_timer;
|
||||
};
|
||||
} //unnamed namespace
|
||||
|
||||
EvTimerTask::EvTimerTask (double delay, ev::loop_ref& loop, tf::Subflow* subflow) :
|
||||
m_subflow(subflow)
|
||||
{
|
||||
this->set(loop);
|
||||
this->set<EvTimerTask, &EvTimerTask::on_timer_ev>(this);
|
||||
ev::timer::start(delay, 0.0);
|
||||
void lock_mutex_libev (struct ev_loop* ev) noexcept {
|
||||
EvThreadPool* const obj = static_cast<EvThreadPool*>(ev_userdata(ev));
|
||||
obj->lock_mutex_libev();
|
||||
}
|
||||
|
||||
tf::Subflow& EvTimerTask::subflow() {
|
||||
return *m_subflow;
|
||||
void unlock_mutex_libev (struct ev_loop* ev) noexcept {
|
||||
EvThreadPool* const obj = static_cast<EvThreadPool*>(ev_userdata(ev));
|
||||
obj->unlock_mutex_libev();
|
||||
}
|
||||
|
||||
RunningPool::RunningPool (ev::default_loop* loop, RunningPool::subpool_type* sub, std::mutex* mtx, ev::async* async) :
|
||||
m_loop(loop),
|
||||
m_subflow(sub),
|
||||
m_ev_mutex(mtx),
|
||||
m_async(async)
|
||||
{
|
||||
assert(m_ev_mutex);
|
||||
std::cout << "Created RunningPool(" << m_loop << ", " << m_subflow << ")\n";
|
||||
}
|
||||
|
||||
EvThreadPool::EvThreadPool() :
|
||||
m_loop(ev::AUTO)
|
||||
#if THREADPOOL == THREADPOOL_ROAR11
|
||||
, m_pool(std::max(2U, std::thread::hardware_concurrency()) - 1)
|
||||
#endif
|
||||
{
|
||||
assert(nullptr == ev_userdata(m_loop));
|
||||
m_async.set(m_loop);
|
||||
m_async.set<EvThreadPool, &EvThreadPool::do_nothing>(this);
|
||||
m_async.start();
|
||||
ev_set_userdata(m_loop, this);
|
||||
ev_set_loop_release_cb(m_loop, &duck::unlock_mutex_libev, &duck::lock_mutex_libev);
|
||||
}
|
||||
|
||||
RunningPool EvThreadPool::start() {
|
||||
#if THREADPOOL == THREADPOOL_TASKFLOW
|
||||
std::promise<RunningPool::subpool_type*> subflow_promise;
|
||||
m_taskflow.emplace([this,&subflow_promise](RunningPool::subpool_type& sub){
|
||||
subflow_promise.set_value(&sub);
|
||||
//KeepaliveTimer keepalive{m_loop, &sub};
|
||||
m_ev_mutex.lock();
|
||||
m_loop.run(0);
|
||||
m_ev_mutex.unlock();
|
||||
});
|
||||
m_executor.run(m_taskflow);
|
||||
|
||||
std::future<RunningPool::subpool_type*> future_subflow = subflow_promise.get_future();
|
||||
return RunningPool{&m_loop, future_subflow.get(), &m_ev_mutex, &m_async};
|
||||
#else
|
||||
std::cout << "Submitting run job to thread pool\n";
|
||||
m_pool.submit([this]() {
|
||||
//KeepaliveTimer keepalive{m_loop, &m_pool};
|
||||
m_ev_mutex.lock();
|
||||
m_loop.run(0);
|
||||
m_ev_mutex.unlock();
|
||||
});
|
||||
std::cout << "Work submitted, returing RunningPool object\n";
|
||||
return RunningPool{&m_loop, &m_pool, &m_ev_mutex, &m_async};
|
||||
#endif
|
||||
}
|
||||
|
||||
void EvThreadPool::join() {
|
||||
#if THREADPOOL == THREADPOOL_TASKFLOW
|
||||
m_executor.wait_for_all();
|
||||
#else
|
||||
m_pool.join();
|
||||
#endif
|
||||
std::cout << "all tasks completed\n";
|
||||
}
|
||||
|
||||
ev::loop_ref& EvThreadPool::loop() {
|
||||
return m_loop;
|
||||
}
|
||||
|
||||
void EvThreadPool::do_nothing() {
|
||||
}
|
||||
|
||||
void EvThreadPool::lock_mutex_libev() noexcept {
|
||||
try {
|
||||
m_ev_mutex.lock();
|
||||
}
|
||||
catch (const std::system_error&) {
|
||||
assert(false);
|
||||
std::cerr << "Locking mutex failed, this will probably result in bad program behaviour\n";
|
||||
}
|
||||
}
|
||||
|
||||
void EvThreadPool::unlock_mutex_libev() noexcept {
|
||||
m_ev_mutex.unlock();
|
||||
}
|
||||
|
||||
EvTimerTask::EvTimerTask (double delay, ev::loop_ref& loop, RunningPool::subpool_type* subflow, std::mutex* ev_mtx, ev::async* async) :
|
||||
m_subpool(subflow),
|
||||
m_ev_mutex(ev_mtx),
|
||||
m_loop(&loop),
|
||||
m_async(async)
|
||||
{
|
||||
assert(ev_mtx);
|
||||
|
||||
this->set(loop);
|
||||
this->set<EvTimerTask, &EvTimerTask::on_timer_ev>(this);
|
||||
set_timer(delay);
|
||||
}
|
||||
|
||||
void EvTimerTask::set_timer (double delay) {
|
||||
std::unique_lock<std::mutex> lock(*m_ev_mutex);
|
||||
ev_now_update(*m_loop);
|
||||
ev::timer::start(delay, 0.0);
|
||||
m_async->send();
|
||||
}
|
||||
|
||||
RunningPool::subpool_type& EvTimerTask::subflow() {
|
||||
return *m_subpool;
|
||||
}
|
||||
|
||||
void EvTimerTask::on_timer_ev() {
|
||||
std::cout << "EvTimerTask::on_timer_ev()\n";
|
||||
ev::timer::stop();
|
||||
this->on_timer();
|
||||
}
|
||||
|
||||
void test() {
|
||||
//const auto processor_count = std::thread::hardware_concurrency();
|
||||
EvThreadPool worker; //(std::max(2U, processor_count) - 1);
|
||||
worker.start();
|
||||
EvThreadPool worker;
|
||||
auto running_pool = worker.start();
|
||||
|
||||
std::cout << "Instantiating html timer\n";
|
||||
auto fetcher = running_pool.make_timer<HtmlFetchTimer>("test_url_lol");
|
||||
|
||||
worker.join();
|
||||
}
|
||||
} //namespace duck
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue