From b6441edfacd8cad9414d22a81a4e6552663634dc Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Mon, 2 Mar 2026 20:17:51 -0500 Subject: [PATCH] BLooperList: Use rw_lock. Improves efficiency, as some functions can use a read lock instead of a write lock. Eliminates another statically-created BLocker. --- headers/build/private/app/LooperList.h | 1 - headers/private/app/LooperList.h | 5 ++-- src/build/libbe/app/Messenger.cpp | 1 - src/kits/app/LooperList.cpp | 39 +++++++++----------------- 4 files changed, 16 insertions(+), 30 deletions(-) delete mode 100644 headers/build/private/app/LooperList.h diff --git a/headers/build/private/app/LooperList.h b/headers/build/private/app/LooperList.h deleted file mode 100644 index aa8bd8521c..0000000000 --- a/headers/build/private/app/LooperList.h +++ /dev/null @@ -1 +0,0 @@ -#include <../private/app/LooperList.h> diff --git a/headers/private/app/LooperList.h b/headers/private/app/LooperList.h index 49686280bc..d6ec1ce761 100644 --- a/headers/private/app/LooperList.h +++ b/headers/private/app/LooperList.h @@ -11,9 +11,8 @@ #include -#include #include -#include +#include class BList; @@ -78,7 +77,7 @@ private: void AssertLocked(); private: - BLocker fLock; + rw_lock fLock; std::vector fData; }; diff --git a/src/build/libbe/app/Messenger.cpp b/src/build/libbe/app/Messenger.cpp index 97d45be59d..72ec240f8c 100644 --- a/src/build/libbe/app/Messenger.cpp +++ b/src/build/libbe/app/Messenger.cpp @@ -14,7 +14,6 @@ #include #include #include -#include #include #include #include diff --git a/src/kits/app/LooperList.cpp b/src/kits/app/LooperList.cpp index d468c2b2a4..ef2ecaddee 100644 --- a/src/kits/app/LooperList.cpp +++ b/src/kits/app/LooperList.cpp @@ -12,7 +12,6 @@ #include "LooperList.h" -#include #include #include @@ -27,37 +26,36 @@ BLooperList gLooperList; BLooperList::BLooperList() - : - fLock("BLooperList lock") { + rw_lock_init(&fLock, "BLooperList rwlock"); } bool BLooperList::Lock() { - return fLock.Lock(); + return rw_lock_write_lock(&fLock) == B_OK; } void BLooperList::Unlock() { - fLock.Unlock(); + rw_lock_write_unlock(&fLock); } bool BLooperList::IsLocked() { - return fLock.IsLocked(); + return fLock.holder == find_thread(NULL); } void BLooperList::AddLooper(BLooper* looper) { - BAutolock locker(fLock); + WriteLocker locker(fLock); AssertLocked(); if (!IsLooperValid(looper)) { LooperDataIterator i @@ -76,9 +74,7 @@ BLooperList::AddLooper(BLooper* looper) bool BLooperList::IsLooperValid(const BLooper* looper) { - BAutolock locker(fLock); - AssertLocked(); - + ReadLocker locker(fLock); return find_if(fData.begin(), fData.end(), FindLooperPred(looper)) != fData.end(); } @@ -87,7 +83,7 @@ BLooperList::IsLooperValid(const BLooper* looper) bool BLooperList::RemoveLooper(BLooper* looper) { - BAutolock locker(fLock); + WriteLocker locker(fLock); AssertLocked(); LooperDataIterator i = find_if(fData.begin(), fData.end(), @@ -104,8 +100,7 @@ BLooperList::RemoveLooper(BLooper* looper) void BLooperList::GetLooperList(BList* list) { - BAutolock locker(fLock); - AssertLocked(); + ReadLocker locker(fLock); for (uint32 i = 0; i < fData.size(); ++i) { if (fData[i].looper) @@ -117,8 +112,7 @@ BLooperList::GetLooperList(BList* list) int32 BLooperList::CountLoopers() { - BAutolock locker(fLock); - AssertLocked(); + ReadLocker locker(fLock); return (int32)fData.size(); } @@ -126,8 +120,7 @@ BLooperList::CountLoopers() BLooper* BLooperList::LooperAt(int32 index) { - BAutolock locker(fLock); - AssertLocked(); + ReadLocker locker(fLock); BLooper* looper = NULL; if (index < (int32)fData.size()) @@ -140,8 +133,7 @@ BLooperList::LooperAt(int32 index) BLooper* BLooperList::LooperForThread(thread_id thread) { - BAutolock locker(fLock); - AssertLocked(); + ReadLocker locker(fLock); BLooper* looper = NULL; LooperDataIterator i @@ -156,8 +148,7 @@ BLooperList::LooperForThread(thread_id thread) BLooper* BLooperList::LooperForName(const char* name) { - BAutolock locker(fLock); - AssertLocked(); + ReadLocker locker(fLock); BLooper* looper = NULL; LooperDataIterator i @@ -172,8 +163,7 @@ BLooperList::LooperForName(const char* name) BLooper* BLooperList::LooperForPort(port_id port) { - BAutolock locker(fLock); - AssertLocked(); + ReadLocker locker(fLock); BLooper* looper = NULL; LooperDataIterator i @@ -188,8 +178,7 @@ BLooperList::LooperForPort(port_id port) void BLooperList::InitAfterFork() { - // We need to reinitialize the locker to get a new semaphore - new (&fLock) BLocker("BLooperList lock"); + rw_lock_init(&fLock, "BLooperList lock"); fData.clear(); }