From 9f74634dde2625a6d0b8f9530bd4f684daca9f33 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sat, 22 May 2021 11:59:20 +0100 Subject: [PATCH] ScopeTryLocker: allow to force lock Signed-off-by: falkTX --- distrho/extra/Mutex.hpp | 19 +++++++++++-------- distrho/extra/Thread.hpp | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/distrho/extra/Mutex.hpp b/distrho/extra/Mutex.hpp index 2f80515a..90332c30 100644 --- a/distrho/extra/Mutex.hpp +++ b/distrho/extra/Mutex.hpp @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2016 Filipe Coelho + * Copyright (C) 2012-2021 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this @@ -39,7 +39,7 @@ public: /* * Constructor. */ - Mutex(bool inheritPriority = true) noexcept + Mutex(const bool inheritPriority = true) noexcept : fMutex() { pthread_mutexattr_t attr; @@ -61,9 +61,9 @@ public: /* * Lock the mutex. */ - void lock() const noexcept + bool lock() const noexcept { - pthread_mutex_lock(&fMutex); + return (pthread_mutex_lock(&fMutex) == 0); } /* @@ -86,7 +86,6 @@ public: private: mutable pthread_mutex_t fMutex; - DISTRHO_PREVENT_HEAP_ALLOCATION DISTRHO_DECLARE_NON_COPY_CLASS(Mutex) }; @@ -133,12 +132,13 @@ public: /* * Lock the mutex. */ - void lock() const noexcept + bool lock() const noexcept { #ifdef DISTRHO_OS_WINDOWS EnterCriticalSection(&fSection); + return true; #else - pthread_mutex_lock(&fMutex); + return (pthread_mutex_lock(&fMutex) == 0); #endif } @@ -174,7 +174,6 @@ private: mutable pthread_mutex_t fMutex; #endif - DISTRHO_PREVENT_HEAP_ALLOCATION DISTRHO_DECLARE_NON_COPY_CLASS(RecursiveMutex) }; @@ -295,6 +294,10 @@ public: : fMutex(mutex), fLocked(mutex.tryLock()) {} + ScopeTryLocker(const Mutex& mutex, const bool forceLock) noexcept + : fMutex(mutex), + fLocked(forceLock ? mutex.lock() : mutex.tryLock()) {} + ~ScopeTryLocker() noexcept { if (fLocked) diff --git a/distrho/extra/Thread.hpp b/distrho/extra/Thread.hpp index c888cf8b..5a4ecffa 100644 --- a/distrho/extra/Thread.hpp +++ b/distrho/extra/Thread.hpp @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2016 Filipe Coelho + * Copyright (C) 2012-2021 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this