Skip to content

Commit d447b91

Browse files
committed
[fiber] Add stack size query to context and task
1 parent d195fab commit d447b91

File tree

7 files changed

+40
-1
lines changed

7 files changed

+40
-1
lines changed

src/modm/processing/fiber/context.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ modm_context_stack_watermark(modm_context_t *ctx);
9797
*/
9898
size_t
9999
modm_context_stack_usage(const modm_context_t *ctx);
100+
101+
/**
102+
* Returns the configured stack size.
103+
*/
104+
size_t
105+
modm_context_stack_size(const modm_context_t *ctx);
100106
/// @}
101107

102108
#ifdef __cplusplus

src/modm/processing/fiber/context_arm64.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ modm_context_stack_usage(const modm_context_t *ctx)
113113
return 0;
114114
}
115115

116+
size_t
117+
modm_context_stack_size(const modm_context_t *ctx)
118+
{
119+
return (ctx->top - ctx->bottom) * StackSizeWord;
120+
}
121+
116122
static modm_context_t main_context;
117123

118124
uintptr_t

src/modm/processing/fiber/context_arm_m.cpp.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ modm_context_stack_usage(const modm_context_t *ctx)
139139
return 0;
140140
}
141141

142+
size_t
143+
modm_context_stack_size(const modm_context_t *ctx)
144+
{
145+
return (ctx->top - ctx->bottom) * StackSizeWord;
146+
}
147+
142148
#define MODM_PUSH_CONTEXT() \
143149
%% if is_cm0
144150
"push {r4-r7, lr} \n\t" \

src/modm/processing/fiber/context_avr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ modm_context_stack_usage(const modm_context_t *ctx)
146146
return 0;
147147
}
148148

149+
size_t
150+
modm_context_stack_size(const modm_context_t *ctx)
151+
{
152+
return (ctx->top - ctx->bottom) * StackSizeWord;
153+
}
154+
149155
extern "C" uintptr_t modm_context_jump_entry(modm_context_t*, modm_context_t*);
150156
extern "C" void modm_context_jump_return(uintptr_t, modm_context_t*);
151157
static uintptr_t main_context_sp;

src/modm/processing/fiber/context_x86_64.cpp.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ modm_context_stack_usage(const modm_context_t *ctx)
120120
return 0;
121121
}
122122

123+
size_t
124+
modm_context_stack_size(const modm_context_t *ctx)
125+
{
126+
return (ctx->top - ctx->bottom) * StackSizeWord;
127+
}
128+
123129
static modm_context_t main_context;
124130

125131
uintptr_t

src/modm/processing/fiber/module.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ fiber1.stack_watermark();
289289
// now you can run the fibers via the scheduler
290290
modm::fiber::Scheduler::run();
291291
// can be called from inside or outside the fiber, before or after running!
292-
size_t bytes = fiber.stack_usage();
292+
size_t total = fiber.stack_size();
293+
size_t used = fiber.stack_usage();
293294
```
294295

295296
Note that stack usage measurement through watermarking can be inaccurate if the

src/modm/processing/fiber/task.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ class Task
142142
return modm_context_stack_usage(&ctx);
143143
}
144144

145+
/// @returns the available stack size excluding the storage of the callable.
146+
/// @see `modm_context_stack_size()`.
147+
[[nodiscard]] size_t inline
148+
stack_size() const
149+
{
150+
return modm_context_stack_size(&ctx);
151+
}
152+
145153
/// Adds the task to the currently active scheduler, if not already running.
146154
/// @returns if the fiber has been scheduled.
147155
bool

0 commit comments

Comments
 (0)