BLooperList: Use rw_lock.

Improves efficiency, as some functions can use a read lock
instead of a write lock.

Eliminates another statically-created BLocker.
This commit is contained in:
Augustin Cavalier
2026-03-02 20:17:51 -05:00
parent 5eef2f1d00
commit b6441edfac
4 changed files with 16 additions and 30 deletions
-1
View File
@@ -1 +0,0 @@
#include <../private/app/LooperList.h>
+2 -3
View File
@@ -11,9 +11,8 @@
#include <vector> #include <vector>
#include <Locker.h>
#include <OS.h> #include <OS.h>
#include <SupportDefs.h> #include <locks.h>
class BList; class BList;
@@ -78,7 +77,7 @@ private:
void AssertLocked(); void AssertLocked();
private: private:
BLocker fLock; rw_lock fLock;
std::vector<LooperData> fData; std::vector<LooperData> fData;
}; };
-1
View File
@@ -14,7 +14,6 @@
#include <Application.h> #include <Application.h>
#include <Handler.h> #include <Handler.h>
#include <Looper.h> #include <Looper.h>
#include <LooperList.h>
#include <Message.h> #include <Message.h>
#include <MessagePrivate.h> #include <MessagePrivate.h>
#include <Messenger.h> #include <Messenger.h>
+14 -25
View File
@@ -12,7 +12,6 @@
#include "LooperList.h" #include "LooperList.h"
#include <Autolock.h>
#include <Looper.h> #include <Looper.h>
#include <algorithm> #include <algorithm>
@@ -27,37 +26,36 @@ BLooperList gLooperList;
BLooperList::BLooperList() BLooperList::BLooperList()
:
fLock("BLooperList lock")
{ {
rw_lock_init(&fLock, "BLooperList rwlock");
} }
bool bool
BLooperList::Lock() BLooperList::Lock()
{ {
return fLock.Lock(); return rw_lock_write_lock(&fLock) == B_OK;
} }
void void
BLooperList::Unlock() BLooperList::Unlock()
{ {
fLock.Unlock(); rw_lock_write_unlock(&fLock);
} }
bool bool
BLooperList::IsLocked() BLooperList::IsLocked()
{ {
return fLock.IsLocked(); return fLock.holder == find_thread(NULL);
} }
void void
BLooperList::AddLooper(BLooper* looper) BLooperList::AddLooper(BLooper* looper)
{ {
BAutolock locker(fLock); WriteLocker locker(fLock);
AssertLocked(); AssertLocked();
if (!IsLooperValid(looper)) { if (!IsLooperValid(looper)) {
LooperDataIterator i LooperDataIterator i
@@ -76,9 +74,7 @@ BLooperList::AddLooper(BLooper* looper)
bool bool
BLooperList::IsLooperValid(const BLooper* looper) BLooperList::IsLooperValid(const BLooper* looper)
{ {
BAutolock locker(fLock); ReadLocker locker(fLock);
AssertLocked();
return find_if(fData.begin(), fData.end(), return find_if(fData.begin(), fData.end(),
FindLooperPred(looper)) != fData.end(); FindLooperPred(looper)) != fData.end();
} }
@@ -87,7 +83,7 @@ BLooperList::IsLooperValid(const BLooper* looper)
bool bool
BLooperList::RemoveLooper(BLooper* looper) BLooperList::RemoveLooper(BLooper* looper)
{ {
BAutolock locker(fLock); WriteLocker locker(fLock);
AssertLocked(); AssertLocked();
LooperDataIterator i = find_if(fData.begin(), fData.end(), LooperDataIterator i = find_if(fData.begin(), fData.end(),
@@ -104,8 +100,7 @@ BLooperList::RemoveLooper(BLooper* looper)
void void
BLooperList::GetLooperList(BList* list) BLooperList::GetLooperList(BList* list)
{ {
BAutolock locker(fLock); ReadLocker locker(fLock);
AssertLocked();
for (uint32 i = 0; i < fData.size(); ++i) { for (uint32 i = 0; i < fData.size(); ++i) {
if (fData[i].looper) if (fData[i].looper)
@@ -117,8 +112,7 @@ BLooperList::GetLooperList(BList* list)
int32 int32
BLooperList::CountLoopers() BLooperList::CountLoopers()
{ {
BAutolock locker(fLock); ReadLocker locker(fLock);
AssertLocked();
return (int32)fData.size(); return (int32)fData.size();
} }
@@ -126,8 +120,7 @@ BLooperList::CountLoopers()
BLooper* BLooper*
BLooperList::LooperAt(int32 index) BLooperList::LooperAt(int32 index)
{ {
BAutolock locker(fLock); ReadLocker locker(fLock);
AssertLocked();
BLooper* looper = NULL; BLooper* looper = NULL;
if (index < (int32)fData.size()) if (index < (int32)fData.size())
@@ -140,8 +133,7 @@ BLooperList::LooperAt(int32 index)
BLooper* BLooper*
BLooperList::LooperForThread(thread_id thread) BLooperList::LooperForThread(thread_id thread)
{ {
BAutolock locker(fLock); ReadLocker locker(fLock);
AssertLocked();
BLooper* looper = NULL; BLooper* looper = NULL;
LooperDataIterator i LooperDataIterator i
@@ -156,8 +148,7 @@ BLooperList::LooperForThread(thread_id thread)
BLooper* BLooper*
BLooperList::LooperForName(const char* name) BLooperList::LooperForName(const char* name)
{ {
BAutolock locker(fLock); ReadLocker locker(fLock);
AssertLocked();
BLooper* looper = NULL; BLooper* looper = NULL;
LooperDataIterator i LooperDataIterator i
@@ -172,8 +163,7 @@ BLooperList::LooperForName(const char* name)
BLooper* BLooper*
BLooperList::LooperForPort(port_id port) BLooperList::LooperForPort(port_id port)
{ {
BAutolock locker(fLock); ReadLocker locker(fLock);
AssertLocked();
BLooper* looper = NULL; BLooper* looper = NULL;
LooperDataIterator i LooperDataIterator i
@@ -188,8 +178,7 @@ BLooperList::LooperForPort(port_id port)
void void
BLooperList::InitAfterFork() BLooperList::InitAfterFork()
{ {
// We need to reinitialize the locker to get a new semaphore rw_lock_init(&fLock, "BLooperList lock");
new (&fLock) BLocker("BLooperList lock");
fData.clear(); fData.clear();
} }