Skip to content

Commit f836b0e

Browse files
is_pending
1 parent 3e95efc commit f836b0e

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

include/olga_scheduler/olga_scheduler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ static inline void olga_cancel(olga_t* const self, olga_event_t* const event)
136136
event->deadline = INT64_MIN;
137137
}
138138

139+
/// True if the event is currently pending in the scheduler.
140+
static inline bool olga_is_pending(const olga_t* const self, const olga_event_t* const event)
141+
{
142+
assert(self != NULL);
143+
assert(event != NULL);
144+
return cavl2_is_inserted(self->events, &event->base);
145+
}
146+
139147
/// Execute pending events strictly in the order of their deadlines until there are no pending events left.
140148
/// Events with the same deadline are executed in the FIFO order.
141149
/// The handler receives a freshly sampled `now` taken immediately before invocation.

tests/test_olga_scheduler_c.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,31 @@ TEST(OlgaSchedulerC, Cancel)
202202
EXPECT_EQ(log.ids, (std::vector<int>{ 2 }));
203203
}
204204

205+
TEST(OlgaSchedulerC, IsPending)
206+
{
207+
TestClock clock{ .now = 0 };
208+
olga_t sched;
209+
olga_init(&sched, &clock, clock_now);
210+
211+
CallbackCtx ctx{ .log = nullptr, .id = 0, .expected_deadline = INT64_MIN, .clock = &clock, .advance_by = 0 };
212+
olga_event_t evt = OLGA_EVENT_INIT;
213+
214+
EXPECT_FALSE(olga_is_pending(&sched, &evt));
215+
216+
olga_defer(&sched, 100, &ctx, record_handler, &evt);
217+
EXPECT_TRUE(olga_is_pending(&sched, &evt));
218+
219+
olga_cancel(&sched, &evt);
220+
EXPECT_FALSE(olga_is_pending(&sched, &evt));
221+
222+
olga_defer(&sched, 100, &ctx, record_handler, &evt);
223+
EXPECT_TRUE(olga_is_pending(&sched, &evt));
224+
225+
clock.now = 100;
226+
(void)olga_spin(&sched);
227+
EXPECT_FALSE(olga_is_pending(&sched, &evt));
228+
}
229+
205230
TEST(OlgaSchedulerC, OverdueSingle)
206231
{
207232
TestClock clock{ .now = 0 };

0 commit comments

Comments
 (0)