use new tasks interface
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good
This commit is contained in:
@ -22,7 +22,9 @@
|
||||
#ifndef REPERTORY_INCLUDE_UTILS_TASKS_HPP_
|
||||
#define REPERTORY_INCLUDE_UTILS_TASKS_HPP_
|
||||
|
||||
#include "common.hpp"
|
||||
#include "types/repertory.hpp"
|
||||
#include <memory>
|
||||
|
||||
namespace repertory {
|
||||
class app_config;
|
||||
@ -31,9 +33,43 @@ class tasks final {
|
||||
public:
|
||||
static constexpr const auto default_delay_ms{10U};
|
||||
|
||||
struct task_item final {
|
||||
std::function<void(const stop_type &stop_requested)> action;
|
||||
std::uint16_t delay_ms{default_delay_ms};
|
||||
struct task final {
|
||||
std::function<void(const stop_type &task_stopped)> action;
|
||||
};
|
||||
|
||||
class i_task {
|
||||
INTERFACE_SETUP(i_task);
|
||||
|
||||
public:
|
||||
virtual auto wait() const -> bool = 0;
|
||||
};
|
||||
|
||||
using task_ptr = std::shared_ptr<i_task>;
|
||||
|
||||
private:
|
||||
class task_wait : public i_task {
|
||||
private:
|
||||
bool complete{false};
|
||||
mutable std::mutex mtx;
|
||||
mutable std::condition_variable notify;
|
||||
bool success{false};
|
||||
|
||||
public:
|
||||
void set_result(bool result);
|
||||
|
||||
auto wait() const -> bool override;
|
||||
};
|
||||
|
||||
struct scheduled_task final {
|
||||
task item;
|
||||
|
||||
std::uint16_t delay_ms{
|
||||
default_delay_ms,
|
||||
};
|
||||
|
||||
std::shared_ptr<task_wait> wait{
|
||||
std::make_shared<task_wait>(),
|
||||
};
|
||||
};
|
||||
|
||||
public:
|
||||
@ -61,13 +97,13 @@ private:
|
||||
std::mutex start_stop_mutex_;
|
||||
stop_type stop_requested_{false};
|
||||
std::vector<std::unique_ptr<std::jthread>> task_threads_;
|
||||
std::deque<task_item> tasks_;
|
||||
std::deque<scheduled_task> tasks_;
|
||||
|
||||
private:
|
||||
void task_thread();
|
||||
|
||||
public:
|
||||
void schedule(task_item task);
|
||||
auto schedule(task item) -> task_ptr;
|
||||
|
||||
void start(app_config *config);
|
||||
|
||||
|
Reference in New Issue
Block a user