Skyward boardcore
Loading...
Searching...
No Matches
SignaledDeadlineTask.h
Go to the documentation of this file.
1/* Copyright (c) 2025 Skyward Experimental Rocketry
2 * Authors: Pietro Bortolus, Niccolò Betto
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <ActiveObject.h>
26
27#include <chrono>
28#include <condition_variable>
29#include <mutex>
30
31namespace Boardcore
32{
59{
60public:
71 using Clock = std::chrono::system_clock;
72 using TimePoint = std::chrono::time_point<Clock>;
73
81 unsigned int stackSize = miosix::STACK_DEFAULT_FOR_PTHREAD,
82 miosix::Priority priority = miosix::MAIN_PRIORITY)
83 : ActiveObject(stackSize, priority)
84 {
85 }
86
91 {
92 std::unique_lock<std::mutex> lock(mutex);
93 signaled = true;
94 condvar.notify_all();
95 }
96
101
105 virtual void task() = 0;
106
107protected:
108 void run() override
109 {
110 while (!shouldStop())
111 {
112 // Get the time when the task should run next
113 auto deadline = nextTaskDeadline();
114
115 // Lock the mutex in the smallest scope possible
116 {
117 std::unique_lock<std::mutex> lock(mutex);
118 // Wait until the next deadline or until signaled
119 condvar.wait_until(lock, deadline, [this] { return signaled; });
120 // Reset the signaled flag
121 signaled = false;
122 }
123
124 // Run the task
125 task();
126 }
127 }
128
129private:
130 bool signaled = false;
131 std::mutex mutex;
132 std::condition_variable condvar;
133};
134} // namespace Boardcore
bool shouldStop()
Tells whether or not the ActiveObject should stop its execution.
A task that executes a user-defined function at specific time points, or when signaled.
std::chrono::system_clock Clock
virtual TimePoint nextTaskDeadline()=0
Calculates the next deadline for the task to run.
virtual void task()=0
The user-defined task to run.
SignaledDeadlineTask(unsigned int stackSize=miosix::STACK_DEFAULT_FOR_PTHREAD, miosix::Priority priority=miosix::MAIN_PRIORITY)
Constructor.
std::chrono::time_point< Clock > TimePoint
void signalTask()
Signals the task to run and go to sleep until the next deadline.
This file includes all the types the logdecoder script will decode.