/*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: GroupState.cpp Date: 2021-11-1 Author: Reece ***/ #include #include "Async.hpp" #include "GroupState.hpp" #include namespace Aurora::Async { bool GroupState::Init() { return true; } AuSPtr GroupState::GetThreadByIndex(ThreadId_t uIndex) { // TODO: deinit mutex if (AuArraySize(this->wpWorkers) > uIndex) { if (auto pState = AuTryLockMemoryType(this->wpWorkers[uIndex])) { return pState; } } AU_LOCK_GUARD(this->workersMutex); auto itr = this->workers.find(uIndex); if (itr == this->workers.end()) { return {}; } 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); for (const auto &[id, pWorker] : this->workers) { if (!pWorker) { continue; } AuAtomicAdd(&pWorker->cvHasWork, 1u); pWorker->SetEvent(); } } void GroupState::AddWorker(ThreadId_t id, AuSPtr pState) { AU_LOCK_GUARD(this->workersMutex); if (AuArraySize(this->wpWorkers) > id) { this->wpWorkers[id] = pState; } SysAssert(AuTryInsert(this->workers, AuMakePair(id, pState))); } }