kernel/util: Add a MutexTryLocker AutoLock variant.

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 <[email protected]>
Reviewed-by: Jérôme Duval <[email protected]>
Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
Michael Lotz
2024-09-05 15:16:11 +00:00
committed by waddlesplash
parent b9addf62e9
commit 171cfa834d
+17
View File
@@ -35,6 +35,22 @@ public:
typedef AutoLocker<mutex, MutexLocking> 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<mutex, MutexTryLocking> MutexTryLocker;
class RecursiveLockLocking {
public:
inline bool Lock(recursive_lock *lockable)
@@ -301,6 +317,7 @@ typedef AutoLocker<seqlock, InterruptsWriteSequentialLocking>
using BPrivate::AutoLocker;
using BPrivate::MutexLocker;
using BPrivate::MutexTryLocker;
using BPrivate::RecursiveLocker;
using BPrivate::ReadLocker;
using BPrivate::WriteLocker;