From 171cfa834d2f7a341c848e781e0bcedf55386398 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Mon, 2 Sep 2024 20:29:42 +0200 Subject: [PATCH] kernel/util: Add a MutexTryLocker AutoLock variant. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This can be used to replace mutex_trylock/mutex_unlock pairs. Once the locker has been created, the success of the locking attempt needs to be checked via locker.IsLocked(). Change-Id: Iba4b4ce21cac5059a3577a84a6eebe28d2cc4058 Reviewed-on: https://review.haiku-os.org/c/haiku/+/8179 Tested-by: Commit checker robot Reviewed-by: Jérôme Duval Reviewed-by: waddlesplash --- headers/private/kernel/util/AutoLock.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/headers/private/kernel/util/AutoLock.h b/headers/private/kernel/util/AutoLock.h index 9a2639e52a..c05b22e3f8 100644 --- a/headers/private/kernel/util/AutoLock.h +++ b/headers/private/kernel/util/AutoLock.h @@ -35,6 +35,22 @@ public: typedef AutoLocker MutexLocker; +class MutexTryLocking { +public: + inline bool Lock(mutex *lockable) + { + return mutex_trylock(lockable) == B_OK; + } + + inline void Unlock(mutex *lockable) + { + mutex_unlock(lockable); + } +}; + +typedef AutoLocker MutexTryLocker; + + class RecursiveLockLocking { public: inline bool Lock(recursive_lock *lockable) @@ -301,6 +317,7 @@ typedef AutoLocker using BPrivate::AutoLocker; using BPrivate::MutexLocker; +using BPrivate::MutexTryLocker; using BPrivate::RecursiveLocker; using BPrivate::ReadLocker; using BPrivate::WriteLocker;