Skip to content

Commit 5d59130

Browse files
committed
added a task trampoline to delete the task after it is done
1 parent 771f0b0 commit 5d59130

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

klib/rtos/task.hpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ namespace klib::rtos {
2121
// stack for this task
2222
size_t stack[StackSize] = {};
2323

24+
/**
25+
* @brief Helper trampoline function that calls the actual task function
26+
* after the function has returned we delete the task from the scheduler
27+
*
28+
* @tparam Args
29+
* @param func
30+
* @param task
31+
* @param parameters
32+
*/
33+
template <typename... Args>
34+
static void task_trampoline(rtos::detail::base_task* task, void (*func)(Args...), Args... parameters) {
35+
// call the function with the parameter
36+
func(parameters...);
37+
38+
// mark the task for deletion
39+
task->marked_for_deletion = true;
40+
41+
// TODO: yield to the scheduler to delete this task
42+
while (true) {
43+
// wait for the scheduler to delete this task
44+
asm volatile("wfi");
45+
}
46+
}
47+
2448
public:
2549
/**
2650
* @brief Construct a task that runs the given function. When the function
@@ -34,11 +58,17 @@ namespace klib::rtos {
3458
task(void (*func)(Args...), Args&&... parameters) {
3559
static_assert(sizeof...(parameters) <= 4, "A maximum of 4 parameters are supported for task functions");
3660

37-
// type alias for type deduction
38-
using func_t = void (*)(Args...);
61+
// type aliases for clarity
62+
using trampoline_t = void (*)(rtos::detail::base_task*, void (*)(Args...), Args...);
3963

4064
// initialize the stack with default values and update the stack pointer
41-
klib::target::rtos::detail::setup_task_stack(static_cast<func_t>(func), stack, StackSize, std::forward<Args>(parameters)...);
65+
stack_pointer = klib::target::rtos::detail::setup_task_stack(
66+
static_cast<trampoline_t>(task_trampoline<Args...>),
67+
stack, StackSize,
68+
static_cast<rtos::detail::base_task*>(this),
69+
static_cast<void (*)(Args...)>(func),
70+
std::forward<Args>(parameters)...
71+
);
4272
}
4373
};
4474
}

0 commit comments

Comments
 (0)