Add async library & move stuff from util to async

This commit is contained in:
Nikita Lisitsa 2020-11-21 16:27:38 +03:00
parent a6b931e8e2
commit 0d64e86ffe
7 changed files with 26 additions and 16 deletions

View file

@ -21,7 +21,7 @@
#include <psemek/pcg/random/uniform_sphere.hpp> #include <psemek/pcg/random/uniform_sphere.hpp>
#include <psemek/util/range.hpp> #include <psemek/util/range.hpp>
#include <psemek/util/clock.hpp> #include <psemek/util/clock.hpp>
#include <psemek/util/threadpool.hpp> #include <psemek/async/threadpool.hpp>
using namespace psemek; using namespace psemek;
@ -216,7 +216,7 @@ struct cloud_app
light_mesh.setup<geom::point<float, 3>>(); light_mesh.setup<geom::point<float, 3>>();
{ {
util::threadpool bg("bg"); async::threadpool bg("bg");
pcg::generator rng(pcg::random_device{}); pcg::generator rng(pcg::random_device{});
pcg::uniform_sphere_vector_distribution<float, 3> d; pcg::uniform_sphere_vector_distribution<float, 3> d;
@ -251,7 +251,7 @@ struct cloud_app
{ {
for (std::size_t y = 0; y < cloud_data.height(); ++y) for (std::size_t y = 0; y < cloud_data.height(); ++y)
{ {
bg.dispatch([&, z, y]{ bg.post([&, z, y]{
for (std::size_t x = 0; x < cloud_data.width(); ++x) for (std::size_t x = 0; x < cloud_data.width(); ++x)
{ {
geom::vector<float, 3> p; geom::vector<float, 3> p;

View file

@ -0,0 +1,6 @@
file(GLOB_RECURSE PSEMEK_ASYNC_HEADERS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "include/*.hpp")
file(GLOB_RECURSE PSEMEK_ASYNC_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "source/*.cpp")
add_library(psemek-async ${PSEMEK_ASYNC_HEADERS} ${PSEMEK_ASYNC_SOURCES})
target_include_directories(psemek-async PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(psemek-async PUBLIC psemek-log psemek-util)

View file

@ -1,11 +1,11 @@
#pragma once #pragma once
#include <psemek/util/executor.hpp> #include <psemek/async/executor.hpp>
#include <deque> #include <deque>
#include <vector> #include <vector>
namespace psemek::util namespace psemek::async
{ {
struct event_loop struct event_loop

View file

@ -6,7 +6,7 @@
#include <chrono> #include <chrono>
#include <memory> #include <memory>
namespace psemek::util namespace psemek::async
{ {
namespace detail namespace detail
@ -64,12 +64,15 @@ namespace psemek::util
struct future struct future
{ {
future() = default; future() = default;
future(future&&) = default;
future(std::shared_ptr<detail::task_state<T>> state) future(std::shared_ptr<detail::task_state<T>> state)
: state_(std::move(state)) : state_(std::move(state))
, f_(state_->promise.get_future()) , f_(state_->promise.get_future())
{} {}
future & operator = (future&&) = default;
~future() ~future()
{ {
reset(); reset();
@ -132,7 +135,7 @@ namespace psemek::util
struct executor struct executor
{ {
using task = movable_function<void()>; using task = util::movable_function<void()>;
using clock = std::chrono::high_resolution_clock; using clock = std::chrono::high_resolution_clock;
// Post the task for execution. Where and when will // Post the task for execution. Where and when will

View file

@ -1,9 +1,10 @@
#pragma once #pragma once
#include <psemek/async/executor.hpp>
#include <psemek/util/thread.hpp> #include <psemek/util/thread.hpp>
#include <psemek/util/synchronyzed_queue.hpp> #include <psemek/util/synchronyzed_queue.hpp>
#include <psemek/util/movable_function.hpp> #include <psemek/util/movable_function.hpp>
#include <psemek/util/executor.hpp>
#include <future> #include <future>
#include <vector> #include <vector>
@ -11,7 +12,7 @@
#include <mutex> #include <mutex>
#include <condition_variable> #include <condition_variable>
namespace psemek::util namespace psemek::async
{ {
struct threadpool struct threadpool

View file

@ -1,8 +1,8 @@
#include <psemek/util/event_loop.hpp> #include <psemek/async/event_loop.hpp>
#include <algorithm> #include <algorithm>
namespace psemek::util namespace psemek::async
{ {
auto event_loop::heap_compare() auto event_loop::heap_compare()

View file

@ -1,11 +1,11 @@
#include <psemek/util/threadpool.hpp> #include <psemek/async/threadpool.hpp>
#include <psemek/util/unused.hpp> #include <psemek/util/unused.hpp>
#include <psemek/util/to_string.hpp> #include <psemek/util/to_string.hpp>
#include <psemek/util/at_scope_exit.hpp>
#include <psemek/log/log.hpp> #include <psemek/log/log.hpp>
namespace psemek::util namespace psemek::async
{ {
namespace namespace
@ -20,8 +20,8 @@ namespace psemek::util
{ {
for (std::size_t th = 0; th < thread_count; ++th) for (std::size_t th = 0; th < thread_count; ++th)
{ {
std::string tname = thread_count == 1 ? name : to_string(name, '#', th); std::string tname = thread_count == 1 ? name : util::to_string(name, '#', th);
threads_.emplace_back([this, tname = std::move(tname), th, thread_count] threads_.emplace_back([this, tname = std::move(tname)]
{ {
log::register_thread(tname); log::register_thread(tname);
for (bool running = true; running;) for (bool running = true; running;)