Skip to content

Commit 810b9ec

Browse files
committed
LibSync: Add try_lock and error checking to Mutex
This commit adds a try_lock method and checks errors from all the relevante mutex functions and verifies when they fail.
1 parent 344496f commit 810b9ec

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

Libraries/LibSync/Mutex.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* SPDX-License-Identifier: BSD-2-Clause
77
*/
88

9+
#include <AK/Assertions.h>
910
#include <AK/Concepts.h>
1011
#include <AK/Diagnostics.h>
1112
#include <AK/Error.h>
@@ -67,8 +68,11 @@ MutexBase<RecursivePolicy, InterprocessPolicy>::MutexBase()
6768
pthread_mutex_t* mutex_ptr = new (m_storage) pthread_mutex_t;
6869
pthread_mutexattr_t attr;
6970
pthread_mutexattr_init(&attr);
70-
if constexpr (IsSame<RecursivePolicy, PolicyRecursive>)
71+
if constexpr (IsSame<RecursivePolicy, PolicyRecursive>) {
7172
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
73+
} else {
74+
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
75+
}
7276
if constexpr (IsSame<InterprocessPolicy, PolicyInterprocess>)
7377
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
7478
int result = pthread_mutex_init(mutex_ptr, &attr);

Libraries/LibSync/Mutex.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class SYNC_API MutexBase {
5050
~MutexBase();
5151

5252
bool try_lock();
53+
5354
void lock();
5455
void unlock();
5556

0 commit comments

Comments
 (0)