diff --git a/src/evloop.cpp b/src/evloop.cpp new file mode 100644 index 0000000..ba8f528 --- /dev/null +++ b/src/evloop.cpp @@ -0,0 +1,78 @@ +#include "evloop.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace duck { +namespace { + auto time_rand = std::bind(std::uniform_int_distribution(2, 8), std::mt19937(std::time(0))); + + class HtmlFetchTimer : public ev::timer { + public: + HtmlFetchTimer (ev::loop_ref& loop, tf::Subflow* subflow, std::string&& url) : + m_subflow(subflow), + m_url(std::move(url)) + { + this->set(loop); + this->set(this); + } + + void on_timer() { + m_subflow->emplace([this]() {std::cout << "Timer elapsed for " << m_url << "!\n";}); + } + + void start (double delay) { + ev::timer::start(delay, 0.0); + } + + private: + tf::Subflow* m_subflow; + 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(m_loop, &subflow, "lalalala"); + m_timer->start(static_cast(time_rand())); + + this->m_loop.run(0); + } + + ev::default_loop m_loop; + tf::Taskflow m_taskflow; + tf::Executor m_executor; + std::unique_ptr m_timer; + }; +} //unnamed namespace + +void test() { + //const auto processor_count = std::thread::hardware_concurrency(); + EvThreadPool worker; //(std::max(2U, processor_count) - 1); + worker.start(); + worker.join(); +} +} //namespace duck diff --git a/src/evloop.hpp b/src/evloop.hpp new file mode 100644 index 0000000..7af6f1c --- /dev/null +++ b/src/evloop.hpp @@ -0,0 +1,5 @@ +#pragma once + +namespace duck { + void test(); +} //namespace duck diff --git a/src/meson.build b/src/meson.build index d0a173b..78f9538 100644 --- a/src/meson.build +++ b/src/meson.build @@ -9,6 +9,10 @@ sqlitecpp_dep = dependency('sqlitecpp', version: '>=3.0.0', fallback: ['SQLiteCpp', 'sqlitecpp_dep'], ) +ev_dep = dependency('libev', version: '>=4.31') +threads_dep = dependency('threads') +taskflow_dep = dependency('Cpp-Taskflow', version: '>=2.4.0', method: 'cmake') + base_url = get_option('base_url').strip() if not base_url.endswith('/') base_url = base_url + '/' @@ -29,8 +33,15 @@ executable(meson.project_name(), 'oro/dateconv.cpp', 'oro/items.cpp', 'oro/shops.cpp', + 'evloop.cpp', project_config_file, install: true, - dependencies: [restc_cpp_dep, sqlitecpp_dep], + dependencies: [ + restc_cpp_dep, + sqlitecpp_dep, + ev_dep, + threads_dep, + taskflow_dep, + ], include_directories: date_incdir, ) diff --git a/subprojects/restc-cpp/meson.build b/subprojects/restc-cpp/meson.build index adde014..8a7d309 100644 --- a/subprojects/restc-cpp/meson.build +++ b/subprojects/restc-cpp/meson.build @@ -1,6 +1,6 @@ project('restc-cpp', 'cpp', version: '0.9.2', - meson_version: '>=0.49.2', + meson_version: '>=0.50.0', default_options: ['cpp_std=gnu++17'], ) @@ -17,6 +17,7 @@ boost_dep = dependency('boost', 'context', 'coroutine', 'chrono', 'log' ] ) +rapidjson_dep = dependency('RapidJSON') if not get_option('embedded_restc_cpp') if get_option('restc_cpp_with_zlib') @@ -52,7 +53,13 @@ restc_cpp_target = library(meson.project_name(), zipreaderimpl, install: true, include_directories: [pvt_incdir, pub_incdir], - dependencies: [zlib_dep, tls_dep, thread_dep, boost_dep], + dependencies: [ + zlib_dep, + tls_dep, + thread_dep, + boost_dep, + rapidjson_dep, + ], cpp_args: compiler_opts, )