From 4924129ec28f800045fe98d03532a0fa76105269 Mon Sep 17 00:00:00 2001 From: Jessica Hamilton Date: Sat, 7 Jan 2023 18:56:03 +1300 Subject: [PATCH] libroot: pthread_rwlock_[rd|wr]lock detect simple deadlocks. Whilst these are "may fail" in POSIX, they're easy to detect, so fail early instead of deadlocking. Return EDEADLK when trying to obtain a read/write lock if the current thread already has a write lock. Fixes part of #17971. Change-Id: I15a67c8f56f746b988f79443b6966a05122aa6a9 Reviewed-on: https://review.haiku-os.org/c/haiku/+/5985 Reviewed-by: Adrien Destugues Tested-by: Commit checker robot --- src/system/libroot/posix/pthread/pthread_rwlock.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/system/libroot/posix/pthread/pthread_rwlock.cpp b/src/system/libroot/posix/pthread/pthread_rwlock.cpp index 945650275d..92ba18ebbb 100644 --- a/src/system/libroot/posix/pthread/pthread_rwlock.cpp +++ b/src/system/libroot/posix/pthread/pthread_rwlock.cpp @@ -1,4 +1,5 @@ /* + * Copyright 2022, Haiku, Inc. All rights reserved. * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de. * Distributed under the terms of the MIT License. */ @@ -154,6 +155,8 @@ struct LocalRWLock { if (writer_count == 0) { reader_count++; return B_OK; + } else if (writer_count == 1 && owner == find_thread(NULL)) { + return EDEADLK; } return _Wait(false, flags, timeout); @@ -167,6 +170,8 @@ struct LocalRWLock { writer_count++; owner = find_thread(NULL); return B_OK; + } else if (writer_count == 1 && owner == find_thread(NULL)) { + return EDEADLK; } return _Wait(true, flags, timeout);