Skip to content

Commit

Permalink
[fiber] Add fiber identifier type
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Apr 20, 2024
1 parent 1b27b3c commit 8a71392
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/modm/processing/fiber/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
9 changes: 8 additions & 1 deletion src/modm/processing/fiber/scheduler.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -37,13 +37,20 @@ 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;

protected:
Task* last{nullptr};
Task* current{nullptr};

uintptr_t
get_id() const
{
return reinterpret_cast<uintptr_t>(current);
}

void
runNext(Task* task)
{
Expand Down
10 changes: 7 additions & 3 deletions src/modm/processing/fiber/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,15 +78,15 @@ 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);
}

/// @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);
Expand All @@ -94,7 +98,7 @@ class Task
start();

/// @returns if the fiber is attached to a scheduler.
bool
[[nodiscard]] bool
isRunning() const
{
return scheduler;
Expand Down

0 comments on commit 8a71392

Please sign in to comment.