1010#include < functional>
1111#include < memory>
1212
13- #include < libp2p/outcome/outcome .hpp>
13+ #include < libp2p/basic/cancel .hpp>
1414
1515namespace libp2p ::basic {
16-
17- constexpr std::chrono::milliseconds kZeroTime =
18- std::chrono::milliseconds::zero ();
19-
20- /* *
21- * Feedback from scheduler backend to Scheduler implementation.
22- */
23- class SchedulerBackendFeedback {
24- public:
25- virtual ~SchedulerBackendFeedback () = default ;
26-
27- /* *
28- * Called from backend to fire callbacks
29- * @param current_clock
30- * For timers: non-zero current async ticks in milliseconds since async's
31- * epoch;
32- * For callbacks deferred to next IO loop cycle: zero
33- */
34- virtual void pulse (std::chrono::milliseconds current_clock) noexcept = 0;
35- };
36-
37- /* *
38- * Scheduling engine that depends on implementation:
39- * 1) AsioSchedulerBackend (for asio-based asynchrony, it uses
40- * std::chrono::steady_clock)
41- * 2) ManualSchedulerBackend (for testing purposes,
42- * uses manual time shifts as a async)
43- */
44- class SchedulerBackend {
45- public:
46- virtual ~SchedulerBackend () = default ;
47-
48- /* *
49- * Current async
50- * @return Milliseconds elapsed from async's epoch
51- */
52- virtual std::chrono::milliseconds now () const noexcept = 0;
53-
54- /* *
55- * Implementation-defined defer or delay function.
56- * @param abs_time Time since async's epoch.
57- * If abs_time == 0 then SchedulerBackendFeedback::pulse()
58- * will be called on the next IO loop cycle with zero argument
59- * @param scheduler Weak reference to scheduler (as it may expire)
60- */
61- virtual void setTimer (
62- std::chrono::milliseconds abs_time,
63- std::weak_ptr<SchedulerBackendFeedback> scheduler) = 0;
64- };
65-
6616 /* *
6717 * Scheduler API. Provides callback deferring facilities and low-res timer
6818 */
69- class Scheduler : public SchedulerBackendFeedback ,
70- public std::enable_shared_from_this<Scheduler> {
19+ class Scheduler {
7120 public:
7221 struct Config {
7322 static constexpr std::chrono::milliseconds kMaxTimerThreshold =
@@ -80,79 +29,19 @@ namespace libp2p::basic {
8029 std::chrono::milliseconds max_timer_threshold = kMaxTimerThreshold ;
8130 };
8231
83- enum class Error {
84- // Invalid argument passed
85- kInvalidArgument = 1 ,
86-
87- // Scheduler handle detached, cannot reschedule
88- kHandleDetached ,
89-
90- // Scheduler item not found, cannot reschedule
91- kItemNotFound ,
92- };
93-
94- /* *
95- * Handle provides scoped callbacks lifetime, allows for canceling and
96- * rescheduling
97- */
98- class Handle {
99- public:
100- /* *
101- * { async-time, seq-number } structure for proper ordering and uniqueness
102- * within Scheduler
103- */
104- using Ticket = std::pair<std::chrono::milliseconds, uint64_t >;
105-
106- Handle (const Handle &) = delete ;
107- Handle &operator =(const Handle &) = delete ;
108-
109- Handle () = default ;
110- Handle (Handle &&) = default ;
111-
112- /* *
113- * Ctor. Called from SchedulerImpl
114- * @param ticket unique ticket, used for cancelling and rescheduling
115- * @param scheduler handle's owner object
116- */
117- Handle (Ticket ticket, std::weak_ptr<Scheduler> scheduler);
118-
119- /* *
120- * Move assignment cancels existing ticket, if any
121- */
122- Handle &operator =(Handle &&r) noexcept ;
123-
124- /* *
125- * Non-trivial dtor cancels existing ticket, if any
126- */
127- ~Handle ();
128-
129- /* *
130- * Cancels existing ticket, if any
131- */
132- void cancel () noexcept ;
133-
134- /* *
135- * Reschedules existing ticket, if it is still active.
136- * Allows reentrancy (can be called from inside the callback)
137- * @param delay_from_now Relative time
138- * @return success or error value from Scheduler::Error
139- */
140- outcome::result<void > reschedule (
141- std::chrono::milliseconds delay_from_now) noexcept ;
142-
143- private:
144- Ticket ticket_;
145- std::weak_ptr<Scheduler> scheduler_;
146- };
32+ using Handle = Cancel;
14733
14834 using Callback = std::function<void ()>;
35+ using Time = std::chrono::milliseconds;
36+
37+ virtual ~Scheduler () = default ;
14938
15039 /* *
15140 * Defers callback to be executed during the next IO loop cycle
15241 * @param cb callback
15342 */
15443 void schedule (Callback &&cb) noexcept {
155- std::ignore = scheduleImpl (std::move (cb), kZeroTime , false );
44+ std::ignore = scheduleImpl (std::move (cb), Time::zero () , false );
15645 }
15746
15847 /* *
@@ -172,7 +61,7 @@ namespace libp2p::basic {
17261 * lifetime
17362 */
17463 [[nodiscard]] Handle scheduleWithHandle (Callback &&cb) noexcept {
175- return scheduleImpl (std::move (cb), kZeroTime , true );
64+ return scheduleImpl (std::move (cb), Time::zero () , true );
17665 }
17766
17867 /* *
@@ -201,9 +90,6 @@ namespace libp2p::basic {
20190 bool ) = delete;
20291
20392 protected:
204- // / Handle calls cancel() and reschedule()
205- friend class Handle ;
206-
20793 /* *
20894 * Called from schedule() and scheduleWithHandle() functions
20995 * @param cb callback
@@ -214,24 +100,5 @@ namespace libp2p::basic {
214100 virtual Handle scheduleImpl (Callback &&cb,
215101 std::chrono::milliseconds delay_from_now,
216102 bool make_handle) noexcept = 0;
217-
218- /* *
219- * Called from Handle (from move assignment, destructor, or manually)
220- * @param ticket handle's existing ticket
221- */
222- virtual void cancel (Handle::Ticket ticket) noexcept = 0;
223-
224- /* *
225- * Called from Handle.
226- * Reschedules existing ticket.
227- * Allows reentrancy (can be called from inside the callback)
228- * @param delay_from_now Relative time
229- * @return success or error value from Scheduler::Error
230- */
231- virtual outcome::result<Handle::Ticket> reschedule (
232- Handle::Ticket ticket,
233- std::chrono::milliseconds delay_from_now) noexcept = 0;
234103 };
235104} // namespace libp2p::basic
236-
237- OUTCOME_HPP_DECLARE_ERROR (libp2p::basic, Scheduler::Error)
0 commit comments