Skyward boardcore
Loading...
Searching...
No Matches
ActiveObject.h
Go to the documentation of this file.
1/* Copyright (c) 2015-2016 Skyward Experimental Rocketry
2 * Authors: Federico Terraneo, Matteo Piazzolla
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
26#include <miosix.h>
27
28#include <atomic>
29
30namespace Boardcore
31{
32
39{
40public:
47 ActiveObject(unsigned int stacksize = miosix::STACK_DEFAULT_FOR_PTHREAD,
48 miosix::Priority priority = miosix::MAIN_PRIORITY);
49
50 virtual ~ActiveObject() {};
51
60 virtual bool start();
61
69 virtual void stop();
70
71 bool isRunning();
72
73protected:
80 virtual void run() = 0;
81
90 bool shouldStop();
91
92 miosix::Thread* thread = nullptr;
93
94 std::atomic<bool> stopFlag{false};
95 std::atomic<bool> running{false};
96
97private:
103 static void threadLauncher(void* arg);
104
105 unsigned int stackSize;
106 miosix::Priority priority;
107};
108
109inline ActiveObject::ActiveObject(unsigned int stacksize,
110 miosix::Priority priority)
111 : stackSize(skywardStack(stacksize)), priority(priority)
112{
113}
114
116{
117 if (!running)
118 {
119 stopFlag = false;
120 running = true;
121
122 thread = miosix::Thread::create(threadLauncher, stackSize, priority,
123 reinterpret_cast<void*>(this),
124 miosix::Thread::JOINABLE);
125
126 if (thread == nullptr)
127 running = false;
128
129 return running;
130 }
131
132 return false;
133}
134
136{
137 if (isRunning())
138 {
139 stopFlag = true;
140 thread->join();
141 }
142}
143
144inline bool ActiveObject::isRunning() { return running; }
145
146inline bool ActiveObject::shouldStop() { return stopFlag; }
147
148inline void ActiveObject::threadLauncher(void* arg)
149{
150 reinterpret_cast<ActiveObject*>(arg)->run();
151
152 // When the run function ends, update the status
153 reinterpret_cast<ActiveObject*>(arg)->running = false;
154 reinterpret_cast<ActiveObject*>(arg)->stopFlag = false;
155}
156
157} // namespace Boardcore
virtual void run()=0
std::atomic< bool > stopFlag
ActiveObject(unsigned int stacksize=miosix::STACK_DEFAULT_FOR_PTHREAD, miosix::Priority priority=miosix::MAIN_PRIORITY)
std::atomic< bool > running
bool shouldStop()
Tells whether or not the ActiveObject should stop its execution.
virtual void stop()
Signals the runner thread to terminate and joins the thread.
virtual bool start()
Start the thread associated with this active object.
miosix::Thread * thread
Gives access to the thread object.
This file includes all the types the logdecoder script will decode.
unsigned int skywardStack(unsigned int stack)