Skip to content

Commit 26a69f2

Browse files
Add a test, fix the error (kind of)
1 parent 78ffa82 commit 26a69f2

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

Inc/HALAL/Services/Time/Scheduler.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,16 @@ struct Scheduler {
5050
if(microseconds == 0) [[unlikely]] microseconds = 1;
5151
return register_task(microseconds, func, false);
5252
}
53-
static inline void cancel_timeout(uint8_t id) { unregister_task(id); }
53+
static inline void cancel_timeout(uint8_t id) {
54+
/* NOTE: This does not fix this case:
55+
1. id = set_timeout(x, func)
56+
2. timeout ends, func gets called and removed internally
57+
3. id_2 = set_timeout(y, func_2) // id will be equal to id_2
58+
4. clear_timeout(id) -> will remove the second timeout
59+
*/
60+
if(!tasks_[id].repeating) return;
61+
unregister_task(id);
62+
}
5463

5564
// static void global_timer_callback();
5665

Tests/Time/scheduler_test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,22 @@ TEST_F(SchedulerTests, GlobalTickOverflow) {
9494
// 100 ticks /20 ticks/task = 5 executions.
9595
EXPECT_EQ(count, 5);
9696
}
97+
98+
TEST_F(SchedulerTests, TimeoutClearAddTask) {
99+
uint8_t timeout_id = Scheduler::set_timeout(10, &fake_workload);
100+
Scheduler::start();
101+
102+
constexpr int NUM_TICKS = 100;
103+
for(int i = 0; i < NUM_TICKS; i++) {
104+
TIM2_BASE->CNT++;
105+
Scheduler::update();
106+
}
107+
108+
// timeout is already done here
109+
uint8_t task_id = Scheduler::register_task(20, &fake_workload);
110+
111+
// after timeout, cancel task
112+
Scheduler::cancel_timeout(timeout_id);
113+
114+
EXPECT_EQ(Scheduler::active_task_count_, 1);
115+
}

0 commit comments

Comments
 (0)