From 3b2de6de46d4fa832aa5c7eb17623665b0049d27 Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Wed, 26 Oct 2016 11:14:28 -0200 Subject: [PATCH] AP_HAL_Linux: allow PeriodicThread to stop This takes the simplest approach of just waiting for the next time we will process events. --- libraries/AP_HAL_Linux/Thread.cpp | 16 +++++++++++++++- libraries/AP_HAL_Linux/Thread.h | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/libraries/AP_HAL_Linux/Thread.cpp b/libraries/AP_HAL_Linux/Thread.cpp index 8127c49e98..d48a85d4ca 100644 --- a/libraries/AP_HAL_Linux/Thread.cpp +++ b/libraries/AP_HAL_Linux/Thread.cpp @@ -235,7 +235,7 @@ bool PeriodicThread::_run() uint64_t next_run_usec = AP_HAL::micros64() + _period_usec; - while (true) { + while (!_should_exit) { uint64_t dt = next_run_usec - AP_HAL::micros64(); if (dt > _period_usec) { // we've lost sync - restart @@ -248,6 +248,20 @@ bool PeriodicThread::_run() _task(); } + _started = false; + _should_exit = false; + + return true; +} + +bool PeriodicThread::stop() +{ + if (!is_started()) { + return false; + } + + _should_exit = true; + return true; } diff --git a/libraries/AP_HAL_Linux/Thread.h b/libraries/AP_HAL_Linux/Thread.h index d13534f86b..d372d01d79 100644 --- a/libraries/AP_HAL_Linux/Thread.h +++ b/libraries/AP_HAL_Linux/Thread.h @@ -80,6 +80,8 @@ public: bool set_rate(uint32_t rate_hz); + bool stop() override; + protected: bool _run() override;