diff --git a/Source/Async/AuGroupState.cpp b/Source/Async/AuGroupState.cpp index f65025ac..ad6bfa01 100644 --- a/Source/Async/AuGroupState.cpp +++ b/Source/Async/AuGroupState.cpp @@ -20,12 +20,14 @@ namespace Aurora::Async AuSPtr GroupState::GetThreadByIndex(ThreadId_t uIndex) { - // TODO: deinit mutex - if (AuArraySize(this->wpWorkers) > uIndex) { - if (auto pState = AuTryLockMemoryType(this->wpWorkers[uIndex])) + AU_LOCK_GUARD(this->deinitMutex); + if (AuArraySize(this->wpWorkers) > uIndex) { - return pState; + if (auto pState = AuTryLockMemoryType(this->wpWorkers[uIndex])) + { + return pState; + } } } @@ -41,9 +43,12 @@ namespace Aurora::Async void GroupState::Decommit(ThreadId_t id) { - if (AuArraySize(this->wpWorkers) > id) { - this->wpWorkers[id] = {}; + AU_LOCK_GUARD(this->deinitMutex); + if (AuArraySize(this->wpWorkers) > id) + { + this->wpWorkers[id] = {}; + } } AU_LOCK_GUARD(this->workersMutex); diff --git a/Source/Async/AuGroupState.hpp b/Source/Async/AuGroupState.hpp index a5e4cf5a..f85643d4 100644 --- a/Source/Async/AuGroupState.hpp +++ b/Source/Async/AuGroupState.hpp @@ -25,6 +25,7 @@ namespace Aurora::Async // tracked workers AuThreadPrimitives::Mutex workersMutex; + AuFutexMutex deinitMutex; AuBST> workers; AuWPtr wpWorkers[32]; // linear non-locking lookup table // diff --git a/Source/Async/ThreadPool.cpp b/Source/Async/ThreadPool.cpp index cf38b5e4..4be07c09 100644 --- a/Source/Async/ThreadPool.cpp +++ b/Source/Async/ThreadPool.cpp @@ -176,7 +176,6 @@ namespace Aurora::Async size_t ThreadPool::GetThreadWorkersCount(ThreadGroup_t group) { - AU_LOCK_GUARD(this->pRWReadView); return GetGroup(group)->workers.size(); } @@ -706,8 +705,6 @@ namespace Aurora::Async AuBST> ThreadPool::GetThreads() { - AU_LOCK_GUARD(this->pRWReadView); - AuBST> ret; for (auto pGroup : this->threadGroups_) @@ -719,6 +716,7 @@ namespace Aurora::Async continue; } + AU_LOCK_GUARD(pGroup->workersMutex); AuTryReserve(workers, pGroup->workers.size()); for (const auto &thread : pGroup->workers) @@ -764,11 +762,10 @@ namespace Aurora::Async void ThreadPool::Signal(WorkerId_t workerId) { - AU_LOCK_GUARD(this->pRWReadView); - auto group = GetGroup(workerId.first); if (workerId.second == Async::kThreadIdAny) { + AU_LOCK_GUARD(group->workersMutex); for (auto &jobWorker : group->workers) { jobWorker.second->running->Set(); @@ -782,8 +779,6 @@ namespace Aurora::Async AuSPtr ThreadPool::WorkerToLoopSource(WorkerId_t workerId) { - AU_LOCK_GUARD(this->pRWReadView); - auto a = GetThreadHandle(workerId); if (!a) { @@ -1288,7 +1283,6 @@ namespace Aurora::Async } { - AU_LOCK_GUARD(this->rwlock_->AsWritable()); state->Decommit(id.second); } } @@ -1361,8 +1355,6 @@ namespace Aurora::Async AuList> ThreadPool::GetThreadHandles(WorkerId_t id) { - AU_LOCK_GUARD(this->pRWReadView); - auto group = GetGroup(id.first); if (!group) { @@ -1379,6 +1371,7 @@ namespace Aurora::Async } else { + AU_LOCK_GUARD(group->workersMutex); for (const auto &[key, value] : group->workers) { ret.push_back(value);