thread_controller: support stopping threads

This should not affect threads that started only once and never
terminated.
This commit is contained in:
Andrey Gusakov 2024-03-11 20:07:02 +03:00 committed by rusefillc
parent 4d713cabcd
commit 5efe1171f4
1 changed files with 23 additions and 1 deletions

View File

@ -19,7 +19,9 @@ class ThreadController : public chibios_rt::BaseStaticThread<TStackSize>
{
private:
const tprio_t m_prio;
/* TODO: use ref instead of m_started */
bool m_started = false;
chibios_rt::ThreadReference ref;
protected:
// Override this function to implement your controller's thread's behavior.
@ -48,7 +50,27 @@ public:
if (!m_started)
{
m_started = true;
chibios_rt::BaseStaticThread<TStackSize>::start(m_prio);
ref = chibios_rt::BaseStaticThread<TStackSize>::start(m_prio);
}
}
/**
* @brief Request thread termination and waits for termination
*
* Thread should periadicaly execute something like:
* if (chThdShouldTerminateX())
* chThdExit((msg_t)0x0);
*/
void stop()
{
if (m_started) {
/* Asking for thread termination.*/
ref.requestTerminate();
/* Waiting for termination, releasing the reference.*/
ref.wait();
m_started = false;
}
}
};