diff --git a/src/modm/processing/fiber/functions.hpp b/src/modm/processing/fiber/functions.hpp index f737e76f54..eeea4ddd54 100644 --- a/src/modm/processing/fiber/functions.hpp +++ b/src/modm/processing/fiber/functions.hpp @@ -42,6 +42,13 @@ yield() modm::fiber::Scheduler::instance().yield(); } +/// Returns the id of the current fiber +inline modm::fiber::id +get_id() +{ + return modm::fiber::Scheduler::instance().get_id(); +} + /** * Yields the current fiber until the time interval has elapsed. * This functionality is a convenience wrapper around `modm::Timeout` diff --git a/src/modm/processing/fiber/scheduler.hpp.in b/src/modm/processing/fiber/scheduler.hpp.in index c379020d34..f639b75343 100644 --- a/src/modm/processing/fiber/scheduler.hpp.in +++ b/src/modm/processing/fiber/scheduler.hpp.in @@ -20,7 +20,7 @@ %% endif // forward declaration -namespace modm::this_fiber { void yield(); } +namespace modm::this_fiber { void yield(); modm::fiber::id get_id(); } namespace modm::fiber { @@ -37,6 +37,7 @@ class Scheduler { friend class Task; friend void modm::this_fiber::yield(); + friend modm::fiber::id modm::this_fiber::get_id(); Scheduler(const Scheduler&) = delete; Scheduler& operator=(const Scheduler&) = delete; @@ -44,6 +45,12 @@ protected: Task* last{nullptr}; Task* current{nullptr}; + uintptr_t + get_id() const + { + return reinterpret_cast(current); + } + void runNext(Task* task) { diff --git a/src/modm/processing/fiber/task.hpp b/src/modm/processing/fiber/task.hpp index a0270add92..1b850983d1 100644 --- a/src/modm/processing/fiber/task.hpp +++ b/src/modm/processing/fiber/task.hpp @@ -29,6 +29,10 @@ Start Later, // Manually add the fiber to a scheduler. }; +/// Identifier of a fiber task. +/// @ingroup modm_processing_fiber +using id = uintptr_t; + /** * The fiber task connects the callable fiber object with the fiber context and * scheduler. It constructs the fiber function on the stack if necessary, and @@ -74,7 +78,7 @@ class Task /// @returns the stack usage as measured by a watermark level. /// @see `modm_context_stack_usage()`. - size_t + [[nodiscard]] size_t stack_usage() const { return modm_context_stack_usage(&ctx); @@ -82,7 +86,7 @@ class Task /// @returns if the bottom word on the stack has been overwritten. /// @see `modm_context_stack_overflow()`. - bool + [[nodiscard]] bool stack_overflow() const { return modm_context_stack_overflow(&ctx); @@ -94,7 +98,7 @@ class Task start(); /// @returns if the fiber is attached to a scheduler. - bool + [[nodiscard]] bool isRunning() const { return scheduler;