From b347027e136caf7e46f25343dd341510f30170dc Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 20 Aug 2018 11:09:05 +1000 Subject: [PATCH] HAL_Linux: added Semaphore_Recursive --- libraries/AP_HAL_Linux/Semaphores.cpp | 16 ++++++++++++++++ libraries/AP_HAL_Linux/Semaphores.h | 11 +++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/libraries/AP_HAL_Linux/Semaphores.cpp b/libraries/AP_HAL_Linux/Semaphores.cpp index 4fb6796640..0bcf98057c 100644 --- a/libraries/AP_HAL_Linux/Semaphores.cpp +++ b/libraries/AP_HAL_Linux/Semaphores.cpp @@ -6,6 +6,21 @@ extern const AP_HAL::HAL& hal; using namespace Linux; +// construct a semaphore +Semaphore::Semaphore() +{ + pthread_mutex_init(&_lock, nullptr); +} + +// construct a recursive semaphore (allows a thread to take it more than once) +Semaphore_Recursive::Semaphore_Recursive() +{ + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&_lock, &attr); +} + bool Semaphore::give() { return pthread_mutex_unlock(&_lock) == 0; @@ -33,3 +48,4 @@ bool Semaphore::take_nonblocking() { return pthread_mutex_trylock(&_lock) == 0; } + diff --git a/libraries/AP_HAL_Linux/Semaphores.h b/libraries/AP_HAL_Linux/Semaphores.h index decec18e16..b43b547913 100644 --- a/libraries/AP_HAL_Linux/Semaphores.h +++ b/libraries/AP_HAL_Linux/Semaphores.h @@ -10,14 +10,17 @@ namespace Linux { class Semaphore : public AP_HAL::Semaphore { public: - Semaphore() { - pthread_mutex_init(&_lock, nullptr); - } + Semaphore(); bool give(); bool take(uint32_t timeout_ms); bool take_nonblocking(); -private: +protected: pthread_mutex_t _lock; }; +class Semaphore_Recursive : public Semaphore { +public: + Semaphore_Recursive(); +}; + }