39 if (!m_thread.joinable())
43 std::lock_guard lock{m_state->mutex};
44 m_state->is_stoping =
true;
46 m_state->cv.notify_all();
51 void defer_to(time_point time_point, Fn&& fn, Handler&& handler, Args&&... args)
53 m_state->queue.emplace(time_point, std::forward<Fn>(fn), std::forward<Handler>(handler), std::forward<Args>(args)...);
54 m_state->has_fresh_data.store(
true);
62 std::atomic_bool has_fresh_data{
false};
65 static void data_thread(std::shared_ptr<queue_data> state)
67 current_thread::get_queue() = &state->queue;
71 std::unique_lock lock{state->mutex};
72 if (state->queue.is_empty() && state->is_stoping)
75 state->cv.wait(lock, [&] {
return !state->queue.is_empty() || state->is_stoping; });
77 if (state->queue.is_empty())
80 if (state->queue.top()->is_disposed())
86 if (details::s_last_now_time < state->queue.top()->get_timepoint())
88 if (
const auto now = worker_strategy::now(); now < state->queue.top()->get_timepoint())
94 const auto top = state->queue.top();
95 state->cv.wait_until(lock, top->get_timepoint(), [&] { return state->queue.is_empty() || state->queue.top().get() != top.get() || state->queue.top()->is_disposed() || worker_strategy::now() >= state->queue.top()->get_timepoint(); });
100 auto top = state->queue.pop();
101 state->has_fresh_data.store(!state->queue.is_empty());
106 if (
const auto res = top->make_advanced_call())
108 if (!top->is_disposed())
110 if (res->can_run_immediately() && !state->has_fresh_data.load())
113 const auto tp = top->handle_advanced_call(res.value());
114 state->queue.emplace(tp, std::move(top));
121 current_thread::get_queue() =
nullptr;
125 std::shared_ptr<queue_data> m_state = std::make_shared<queue_data>();
129 std::thread m_thread{&data_thread, m_state};
133 class worker_strategy
136 worker_strategy() =
default;
139 void defer_to(time_point tp, Fn&& fn, Handler&& handler, Args&&... args)
const
141 m_state->defer_to(tp, std::forward<Fn>(fn), std::forward<Handler>(handler), std::forward<Args>(args)...);
144 static rpp::schedulers::time_point now() {
return details::now(); }
147 std::shared_ptr<state_t> m_state = std::make_shared<state_t>();