From 66e6cd60d851964859860231cf3f64196c2a186f Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Tue, 2 Feb 2016 21:08:26 -0200 Subject: [PATCH] AP_HAL_Linux: Thread: allow to override run method --- libraries/AP_HAL_Linux/Thread.cpp | 14 ++++++++++++-- libraries/AP_HAL_Linux/Thread.h | 7 +++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/libraries/AP_HAL_Linux/Thread.cpp b/libraries/AP_HAL_Linux/Thread.cpp index 1de306ecdb..ae16323cf3 100644 --- a/libraries/AP_HAL_Linux/Thread.cpp +++ b/libraries/AP_HAL_Linux/Thread.cpp @@ -33,12 +33,22 @@ namespace Linux { void *Thread::_run_trampoline(void *arg) { Thread *thread = static_cast(arg); - - thread->_task(); + thread->_run(); return nullptr; } +bool Thread::_run() +{ + if (!_task) { + return false; + } + + _task(); + + return true; +} + bool Thread::start(const char *name, int policy, int prio) { if (_started) { diff --git a/libraries/AP_HAL_Linux/Thread.h b/libraries/AP_HAL_Linux/Thread.h index 9c1955047f..3c68a1ec1c 100644 --- a/libraries/AP_HAL_Linux/Thread.h +++ b/libraries/AP_HAL_Linux/Thread.h @@ -43,6 +43,13 @@ public: protected: static void *_run_trampoline(void *arg); + /* + * Run the task assigned in the constructor. May be overriden in case it's + * preferred to use Thread as an interface or when user wants to aggregate + * some initialization or teardown for the thread. + */ + virtual bool _run(); + task_t _task; bool _started; pthread_t _ctx;