From deaa18ab0750d602a0099dd6d67170402cd2e196 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Wed, 25 Oct 2023 20:08:42 +0100 Subject: [PATCH] [*] Bug fixes --- Source/Async/GroupState.cpp | 25 +++++++++++++++++++++++ Source/Async/GroupState.hpp | 1 + Source/Async/ThreadPool.cpp | 40 +++++++++++++------------------------ Source/Logging/AuLogger.cpp | 4 ++-- 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/Source/Async/GroupState.cpp b/Source/Async/GroupState.cpp index af951efa..0fb0a47c 100644 --- a/Source/Async/GroupState.cpp +++ b/Source/Async/GroupState.cpp @@ -39,6 +39,31 @@ namespace Aurora::Async return itr->second; } + void GroupState::Decommit(ThreadId_t id) + { + if (AuArraySize(this->wpWorkers) > id) + { + this->wpWorkers[id] = {}; + } + + AU_LOCK_GUARD(this->workersMutex); + + for (auto itr = this->workers.begin(); + itr != this->workers.end(); + ) + { + auto &[id2, pWorker] = *itr; + if (id == id2) + { + itr = this->workers.erase(itr); + } + else + { + itr++; + } + } + } + void GroupState::BroadCast() { AU_LOCK_GUARD(this->workersMutex); diff --git a/Source/Async/GroupState.hpp b/Source/Async/GroupState.hpp index c19e0196..88c8682b 100644 --- a/Source/Async/GroupState.hpp +++ b/Source/Async/GroupState.hpp @@ -38,6 +38,7 @@ namespace Aurora::Async bool Init(); void BroadCast(); + void Decommit(ThreadId_t id); void AddWorker(ThreadId_t id, AuSPtr pState); diff --git a/Source/Async/ThreadPool.cpp b/Source/Async/ThreadPool.cpp index e89e2ace..0f810691 100644 --- a/Source/Async/ThreadPool.cpp +++ b/Source/Async/ThreadPool.cpp @@ -790,9 +790,8 @@ namespace Aurora::Async if (handle) { handle->rejecting = false; + handle->isDeadEvent->LockMS(250); } - - handle->isDeadEvent->LockMS(250); } } @@ -1510,47 +1509,39 @@ namespace Aurora::Async auto id = GetCurrentThread(); auto state = GetGroup(id.first); + auto pLocalState = state->GetThreadByIndex(id.second); + AuList> features; { - //AU_LOCK_GUARD(this->rwlock_->AsWritable()); AU_LOCK_GUARD(this->rwlock_->AsReadable()); - auto itr = state->workers.find(id.second); - auto &jobWorker = itr->second; - - jobWorker->isDeadEvent->Set(); + pLocalState->isDeadEvent->Set(); CleanUpWorker(id); - // Abort scheduled tasks TerminateSceduledTasks(this, id); - // Prevent deadlocks - jobWorker->syncSema->Unlock(10); // prevent ::Barrier dead-locks + pLocalState->syncSema->Unlock(10); // prevent ::Barrier dead-locks { - AU_LOCK_GUARD(jobWorker->externalFencesLock); - jobWorker->exitingflag2 = true; + AU_LOCK_GUARD(pLocalState->externalFencesLock); + pLocalState->exitingflag2 = true; - for (const auto &pIWaitable : jobWorker->externalFences) + for (const auto &pIWaitable : pLocalState->externalFences) { pIWaitable->Unlock(); } - jobWorker->externalFences.clear(); + pLocalState->externalFences.clear(); } { - AU_LOCK_GUARD(jobWorker->featuresMutex); - features = AuExchange(jobWorker->features, {}); + AU_LOCK_GUARD(pLocalState->featuresMutex); + features = AuExchange(pLocalState->features, {}); } } { - // Clean up thread features - // -> transferable TLS handles - // -> thread specific vms - // -> anything your brain wishes to imagination for (const auto &thread : features) { try @@ -1559,19 +1550,16 @@ namespace Aurora::Async } catch (...) { - AuLogWarn("Couldn't clean up thread feature!"); - Debug::PrintError(); + SysPushErrorConcurrentRejected("Couldn't clean up thread feature!"); } } features.clear(); } - + { AU_LOCK_GUARD(this->rwlock_->AsWritable()); - auto itr = state->workers.find(id.second); - auto &jobWorker = itr->second; - state->workers.erase(itr); + state->Decommit(id.second); } } diff --git a/Source/Logging/AuLogger.cpp b/Source/Logging/AuLogger.cpp index c8b8bfaa..080cda01 100644 --- a/Source/Logging/AuLogger.cpp +++ b/Source/Logging/AuLogger.cpp @@ -106,9 +106,9 @@ namespace Aurora::Logging while (!AuTryInsert(gLogTasks, AuMakeTuple(this, uLevel, AuConstReference(msg)))) { SysPushErrorMem("Push failed - trying again"); - spin.Unlock(); + gTaskSpin->Unlock(); AuThreading::ContextYield(); - spin.Lock(); + gTaskSpin->Lock(); } }