ReactivePlusPlus
ReactiveX implementation for C++20
Loading...
Searching...
No Matches
run_loop.hpp
1// ReactivePlusPlus library
2//
3// Copyright Aleksey Loginov 2023 - present.
4// Distributed under the Boost Software License, Version 1.0.
5// (See accompanying file LICENSE_1_0.txt or copy at
6// https://www.boost.org/LICENSE_1_0.txt)
7//
8// Project home: https://github.com/AlexInLog/ReactivePlusPlus
9//
10
11#pragma once
12
13#include <rpp/schedulers/fwd.hpp>
14
15#include <rpp/disposables/details/base_disposable.hpp>
16#include <rpp/disposables/disposable_wrapper.hpp>
17#include <rpp/schedulers/current_thread.hpp>
18#include <rpp/schedulers/details/queue.hpp>
19#include <rpp/schedulers/details/worker.hpp>
20#include <rpp/utils/functors.hpp>
21
22namespace rpp::schedulers
23{
30 class run_loop final
31 {
32 class worker_strategy;
33
34 class state_t final : public rpp::details::base_disposable
35 {
36 public:
37 ~state_t() noexcept override { dispose(); }
38
39 template<typename... Args>
40 void emplace_and_notify(time_point timepoint, Args&&... args)
41 {
42 if (is_disposed())
43 return;
44
45 {
46 std::lock_guard lock{m_mutex};
47 m_queue.emplace(timepoint, std::forward<Args>(args)...);
48 }
49 m_cv.notify_one();
50 }
51
52 std::shared_ptr<details::schedulable_base> pop(bool wait)
53 {
54 while (!is_disposed())
55 {
56 std::unique_lock lock{m_mutex};
57 m_cv.wait(lock, [&] { return !wait || is_disposed() || !m_queue.is_empty(); });
58
59 if (is_disposed())
60 break;
61
62 const auto now = worker_strategy::now();
63 if (is_any_ready_schedulable_unsafe(now))
64 return m_queue.pop();
65
66 if (!wait)
67 break;
68
69 // Sleep until the current top is due, and wake as soon as the top changes
70 // (an earlier schedulable was queued), so the loop recomputes the wait.
71 // The copy keeps the waited-for top alive, so its address cannot be reused
72 // by a newly queued schedulable while we wait.
73 const auto top = m_queue.top();
74 m_cv.wait_until(lock, top->get_timepoint(), [&]() { return is_disposed() || m_queue.is_empty() || m_queue.top().get() != top.get() || m_queue.top()->is_disposed() || m_queue.top()->get_timepoint() <= worker_strategy::now(); });
75 }
76 return {};
77 }
78
79 bool is_any_ready_schedulable()
80 {
81 std::lock_guard lock{m_mutex};
82 return is_any_ready_schedulable_unsafe();
83 }
84
85 bool is_empty()
86 {
87 std::lock_guard lock{m_mutex};
88 return m_queue.is_empty();
89 }
90
91 private:
92 bool is_any_ready_schedulable_unsafe(time_point now = worker_strategy::now()) const
93 {
94 return !m_queue.is_empty() && (m_queue.top()->is_disposed() || m_queue.top()->get_timepoint() <= now);
95 }
96
97 void base_dispose_impl(interface_disposable::Mode) noexcept override
98 {
99 {
100 std::lock_guard lock{m_mutex};
102 }
103 m_cv.notify_one();
104 }
105
106 private:
107 std::mutex m_mutex{};
109
110 std::condition_variable m_cv{};
111 };
112
113 class worker_strategy
114 {
115 public:
116 worker_strategy(const std::weak_ptr<state_t>& state)
117 : m_state{state}
118 {
119 }
120
121 template<rpp::schedulers::constraint::schedulable_handler Handler, typename... Args, constraint::schedulable_fn<Handler, Args...> Fn>
122 void defer_to(time_point tp, Fn&& fn, Handler&& handler, Args&&... args) const
123 {
124 if (const auto shared = m_state.lock())
125 shared->emplace_and_notify(tp, std::forward<Fn>(fn), std::forward<Handler>(handler), std::forward<Args>(args)...);
126 }
127
128 static rpp::schedulers::time_point now() { return details::now(); }
129
130 private:
131 std::weak_ptr<state_t> m_state;
132 };
133
134 public:
135 bool is_empty() const
136 {
137 return m_state->is_empty();
138 }
139
140 bool is_any_ready_schedulable() const
141 {
142 return m_state->is_any_ready_schedulable();
143 }
144
145 void dispatch_if_ready() const
146 {
147 dispatch_impl(false);
148 }
149
150 void dispatch() const
151 {
152 dispatch_impl(true);
153 }
154
155 rpp::schedulers::worker<worker_strategy> create_worker() const
156 {
158 }
159
160 private:
161 void dispatch_impl(bool wait) const
162 {
163 if (auto top = m_state->pop(wait))
164 {
165 if (top->is_disposed())
166 return;
167
168 if (const auto timepoint = (*top)())
169 m_state->emplace_and_notify(timepoint.value(), std::move(top));
170 }
171 }
172
173 private:
174 std::shared_ptr<state_t> m_state = std::make_shared<state_t>();
175 };
176} // namespace rpp::schedulers
scheduler which schedules execution via queueing tasks, but execution of tasks should be manually dis...
Definition run_loop.hpp:31
Definition worker.hpp:23
virtual bool is_disposed() const noexcept=0
Check if this disposable is just disposed.
void dispose() noexcept
Dispose disposable and free any underlying resources and etc.
Definition interface_disposable.hpp:36