[*] A patch to fix some of the regressions introduced in the first step in overhauling AuAsync

i beat my wife with cardboard spoons. jolly fun.
This commit is contained in:
Reece Wilson 2023-06-07 20:35:31 +01:00
parent 50f25e147a
commit 9f52ca0f1a
4 changed files with 48 additions and 33 deletions

View File

@ -42,7 +42,7 @@ namespace Aurora::Threading
}
else if constexpr (kBoolArrowOp)
{
if ((*this->pAnnoying_)->TryLock())
if ((*pInterface)->TryLock())
{
this->pAnnoying_ = pInterface;
}

View File

@ -103,7 +103,7 @@ namespace Aurora::Async
}
else if (unlocker.pool.get() == this)
{
return primitive->TryLock();
return primitive->LockMS(timeoutMs);
}
}
@ -457,17 +457,15 @@ namespace Aurora::Async
// Block if no work items are present
if (state->pendingWorkItems.empty())
{
if (this->shuttingdown_)
if (this->shuttingdown_ & 2)
{
//this->EarlyExitTick();
break;
}
state->cvVariable->WaitForSignal();
if (this->shuttingdown_)
if (this->shuttingdown_ & 2)
{
//this->EarlyExitTick();
break;
}
}
@ -653,6 +651,7 @@ namespace Aurora::Async
}
}
// Ehhhh
// We need this fix to a specific V8 deinit lockup
//this->Poll();
@ -679,34 +678,34 @@ namespace Aurora::Async
// then release them from the runners/workers list
// then release all group contexts
AuList<AuThreads::ThreadShared_t> threads;
AuList<AuSPtr<ThreadState>> states;
{
AU_LOCK_GUARD(this->rwlock_->AsWritable());
AU_LOCK_GUARD(this->rwlock_->AsReadable());
for (auto &[groupId, group] : this->threads_)
{
for (auto &[id, worker] : group->workers)
for (auto &[id, pState] : group->workers)
{
auto pState = group->GetThreadByIndex(worker);
if (pState->cvWorkMutex && pState->cvVariable)
// main loop:
if (pState && pState->cvWorkMutex && pState->cvVariable)
{
bool bLocked = pState->cvWorkMutex->TryLock();
worker->shuttingdown = true;
pState->cvVariable->Broadcast();
if (bLocked) pState->cvWorkMutex->Unlock();
states.push_back(pState);
pState->shuttingdown = true;
}
else
{
worker->shuttingdown = true;
pState->shuttingdown = true;
}
// thread object:
if (!group->IsSysThread()) // bug?
{
worker->threadObject->SendExitSignal();
threads.push_back(worker->threadObject);
pState->threadObject->SendExitSignal();
threads.push_back(pState->threadObject);
}
auto &event = worker->running;
// unrefreeze signals:
auto &event = pState->running;
if (event)
{
event->Set();
@ -715,7 +714,16 @@ namespace Aurora::Async
}
}
{
for (const auto &pState : states)
{
AU_LOCK_GUARD(pState->cvWorkMutex);
pState->cvVariable->Broadcast();
pState->eventLs->Set();
}
}
// Final sync to exit
{
for (const auto &id : toBarrier)
{
@ -729,7 +737,8 @@ namespace Aurora::Async
{
handle->rejecting = false;
}
this->Barrier(id, 0, false, true);
handle->isDeadEvent->LockMS(250);
}
}
@ -1310,13 +1319,15 @@ namespace Aurora::Async
#if 0
// TODO... i know what to do
#else
// this will do for now
if (!jobWorker->rejecting &&
!this->shutdown)
{
this->Barrier(AuAsync::GetCurrentWorkerPId(), 0, false , true);
}
//this->Barrier(AuAsync::GetCurrentWorkerPId(), 0, false, false);
//// this will do for now
//if (!jobWorker->rejecting &&
// !this->shutdown)
//{
// this->Barrier(AuAsync::GetCurrentWorkerPId(), 0, false , true);
//}
////this->Barrier(AuAsync::GetCurrentWorkerPId(), 0, false, false);
// UPDATE: 2023
// TODO: regressed - it wont do
#endif
}
@ -1331,21 +1342,23 @@ namespace Aurora::Async
auto id = GetCurrentThread();
auto state = GetGroup(id.first);
AuList<AuSPtr<AuThreads::IThreadFeature>> features;
{
AU_LOCK_GUARD(this->rwlock_->AsWritable());
//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();
CleanUpWorker(id);
// Abort scheduled tasks
TerminateSceduledTasks(this, id);
// Prevent deadlocks
jobWorker->syncSema->Unlock(10000); // prevent ::Barrier dead-locks
jobWorker->syncSema->Unlock(10); // prevent ::Barrier dead-locks
{
AU_LOCK_GUARD(jobWorker->externalFencesLock);
@ -1382,7 +1395,7 @@ namespace Aurora::Async
features.clear();
}
{
AU_LOCK_GUARD(this->rwlock_->AsWritable());
auto itr = state->workers.find(id.second);

View File

@ -137,7 +137,7 @@ namespace Aurora::Async
ThreadDb_t threads_;
AuUInt32 shuttingdown_ {};
bool shutdown {};
AuThreadPrimitives::RWLock rwlock_;
AuThreadPrimitives::RWRenterableLock rwlock_;
AuThreadPrimitives::Event shutdownEvent_;
std::atomic_int tasksRunning_;
bool runnersRunning_ {};

View File

@ -29,7 +29,8 @@ namespace Aurora::Async
struct ThreadState
{
ThreadState() : running(true, false, true),
cvVariable(AuUnsafeRaiiToShared(cvWorkMutex.AsPointer()))
cvVariable(AuUnsafeRaiiToShared(cvWorkMutex.AsPointer())),
isDeadEvent(false, false, true)
{
}
@ -41,6 +42,7 @@ namespace Aurora::Async
AuThreads::ThreadShared_t threadObject;
AuWPtr<GroupState> parent;
AuThreadPrimitives::Semaphore syncSema;
AuThreadPrimitives::Event isDeadEvent;
AuList<AuSPtr<AuThreads::IThreadFeature>> features;
bool rejecting {};
bool exiting {};