Skip to content

Commit

Permalink
rework packed tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
yosriayed committed Apr 19, 2024
1 parent 46485db commit 8316de8
Show file tree
Hide file tree
Showing 8 changed files with 519 additions and 159 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ endif()
if(BUILD_TESTS)

add_subdirectory(test)

endif()

2 changes: 1 addition & 1 deletion include/plz/circbuff/channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class source
source_notify_function_id register_notify_function(Func&& func)
{
auto id = m_notify_function_id_counter++;
m_notif_funcs.push_back({ id, std::forward<Func>(func) });
m_notif_funcs.emplace_back({ id, std::move(func) });
return id;
}

Expand Down
35 changes: 16 additions & 19 deletions include/plz/expected/packaged_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <type_traits>

#include "plz/help/type_traits.hpp"
#include "plz/packaged_task_base.hpp"

#include "future.hpp"

Expand All @@ -21,7 +20,7 @@ namespace plz::expected
template <typename Function, typename... Args>
requires std::invocable<Function, Args...> &&
specialization_of<std::invoke_result_t<Function, Args...>, std::expected>
class packaged_task : public packaged_task_base
class packaged_task
{
friend class plz::thread_pool;

Expand All @@ -37,23 +36,23 @@ class packaged_task : public packaged_task_base

using future_type = future<expected_value_type, expected_error_type>;

packaged_task(Function&& func, Args&&... t_args)
packaged_task(Function&& func, Args&&... args)
: m_func{ std::forward<Function>(func) },
m_arguments{ std::forward<Args>(t_args)... },
m_arguments{ std::forward<Args>(args)... },
m_promise{ make_promise<expected_value_type, expected_error_type>() }
{
}

packaged_task(const packaged_task&) = delete;
packaged_task& operator=(const packaged_task&) = delete;
packaged_task(packaged_task&& t_other)
packaged_task(packaged_task&& other)
{
std::swap(m_func, t_other.m_func);
std::swap(m_arguments, t_other.m_arguments);
std::swap(m_promise, t_other.m_promise);
std::swap(m_func, other.m_func);
std::swap(m_arguments, other.m_arguments);
std::swap(m_promise, other.m_promise);
}

void run(std::stop_token thread_stop_token) override
void operator()()
{
m_promise.set_result(std::apply(m_func, m_arguments));
}
Expand All @@ -72,7 +71,7 @@ class packaged_task : public packaged_task_base
template <typename Function, typename... Args>
requires std::invocable<Function, Args..., std::stop_token> &&
specialization_of<std::invoke_result_t<Function, Args..., std::stop_token>, std::expected>
class packaged_task_st : public packaged_task_base
class packaged_task_st
{
friend class plz::thread_pool;

Expand All @@ -89,24 +88,23 @@ class packaged_task_st : public packaged_task_base

using future_type = future<expected_value_type, expected_error_type>;

packaged_task_st(Function&& func, Args&&... t_args)
packaged_task_st(Function&& func, Args&&... args)
: m_func{ std::forward<Function>(func) },
m_arguments{ std::forward<Args>(t_args)... },
m_arguments{ std::forward<Args>(args)... },
m_promise{ make_promise<expected_value_type, expected_error_type>() }
{
}

packaged_task_st(const packaged_task_st&) = delete;
packaged_task_st& operator=(const packaged_task_st&) = delete;
packaged_task_st(packaged_task_st&& t_other)
packaged_task_st(packaged_task_st&& other)
{
std::swap(m_func, t_other.m_func);
std::swap(m_arguments, t_other.m_arguments);
std::swap(m_promise, t_other.m_promise);
std::swap(m_stop_token, t_other.m_stop_token);
std::swap(m_func, other.m_func);
std::swap(m_arguments, other.m_arguments);
std::swap(m_promise, other.m_promise);
}

void run(std::stop_token thread_stop_token) override
void operator()(std::stop_token thread_stop_token)
{
m_promise.set_result(std::apply(
m_func, std::tuple_cat(m_arguments, std::make_tuple(thread_stop_token))));
Expand All @@ -121,7 +119,6 @@ class packaged_task_st : public packaged_task_base
function_type m_func;
function_arguments m_arguments;
promise<expected_value_type, expected_error_type> m_promise;
std::stop_token m_stop_token;
};
} // namespace plz::expected

Expand Down
Loading

0 comments on commit 8316de8

Please sign in to comment.