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 <pulkomandy@pulkomandy.tk>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
This commit is contained in:
Jessica Hamilton 2023-01-07 18:56:03 +13:00
parent 37223744b7
commit 4924129ec2

View File

@ -1,4 +1,5 @@
/* /*
* Copyright 2022, Haiku, Inc. All rights reserved.
* Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de. * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
@ -154,6 +155,8 @@ struct LocalRWLock {
if (writer_count == 0) { if (writer_count == 0) {
reader_count++; reader_count++;
return B_OK; return B_OK;
} else if (writer_count == 1 && owner == find_thread(NULL)) {
return EDEADLK;
} }
return _Wait(false, flags, timeout); return _Wait(false, flags, timeout);
@ -167,6 +170,8 @@ struct LocalRWLock {
writer_count++; writer_count++;
owner = find_thread(NULL); owner = find_thread(NULL);
return B_OK; return B_OK;
} else if (writer_count == 1 && owner == find_thread(NULL)) {
return EDEADLK;
} }
return _Wait(true, flags, timeout); return _Wait(true, flags, timeout);