Skip to content

Commit 5f8a558

Browse files
committed
[fiber] Add automatic watermarking to scheduling options
1 parent d447b91 commit 5f8a558

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/modm/processing/fiber/module.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,13 @@ Counts are implemented as 16-bits.
279279

280280
## Stack Usage
281281

282-
To measure the stack usage of a fiber, you need to explicitly watermark the
283-
stack *before* running the fiber, then you may query the stack usage inside or
284-
outside the fiber:
282+
To measure the stack usage of a fiber, you need to start the scheduler with the
283+
`AutoWatermark` option, then you may query the stack usage inside or outside
284+
the fiber:
285285

286286
```cpp
287-
// You must watermark the stack *before* running the fiber!
288-
fiber1.stack_watermark();
289-
// now you can run the fibers via the scheduler
290-
modm::fiber::Scheduler::run();
291-
// can be called from inside or outside the fiber, before or after running!
287+
modm::fiber::Scheduler::run(modm::fiber::Scheduler::AutoWatermark);
288+
// can be called from anywhere at any time!
292289
size_t total = fiber.stack_size();
293290
size_t used = fiber.stack_usage();
294291
```

src/modm/processing/fiber/scheduler.hpp.in

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,26 @@ namespace modm::fiber
3636
*/
3737
class Scheduler
3838
{
39+
public:
40+
/// Scheduling options
41+
enum
42+
Options : uint8_t
43+
{
44+
/// On every fiber start, watermark the fiber stack
45+
AutoWatermark = 0b1,
46+
};
47+
48+
protected:
49+
/// @cond
3950
friend class Task;
4051
friend void modm::this_fiber::yield();
4152
friend modm::fiber::id modm::this_fiber::get_id();
4253
Scheduler(const Scheduler&) = delete;
4354
Scheduler& operator=(const Scheduler&) = delete;
4455

45-
protected:
4656
Task* last{nullptr};
4757
Task* current{nullptr};
58+
Options options{0};
4859

4960
uintptr_t inline
5061
get_id() const
@@ -136,6 +147,9 @@ protected:
136147
void inline
137148
add(Task* task)
138149
{
150+
if (options & Options::AutoWatermark) {
151+
task->stack_watermark();
152+
}
139153
task->scheduler = this;
140154
if (last == nullptr)
141155
{
@@ -147,10 +161,17 @@ protected:
147161
}
148162

149163
bool inline
150-
start()
164+
start(Options options)
151165
{
166+
this->options = options;
152167
if (empty()) return false;
153168
current = last->next;
169+
if (options & Options::AutoWatermark) {
170+
do {
171+
current->stack_watermark();
172+
current = current->next;
173+
} while (current != last->next);
174+
}
154175
%% if with_psplim
155176
modm_context_start(&current->ctx);
156177
%% else
@@ -159,6 +180,7 @@ protected:
159180
%% endif
160181
return true;
161182
}
183+
/// @endcond
162184

163185
protected:
164186
/// Returns the currently active scheduler.
@@ -187,9 +209,9 @@ public:
187209

188210
/// Runs the currently active scheduler.
189211
static inline void
190-
run()
212+
run(Options options = Options(0))
191213
{
192-
instance().start();
214+
instance().start(options);
193215
}
194216
};
195217

0 commit comments

Comments
 (0)