2021-11-05 17:34:23 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: ThreadPool.hpp
|
|
|
|
Date: 2021-10-30
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#pragma once
|
|
|
|
|
2023-11-11 10:11:09 +00:00
|
|
|
#include "IThreadPoolInternal.hpp"
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
namespace Aurora::Async
|
|
|
|
{
|
|
|
|
struct GroupState;
|
|
|
|
struct ThreadState;
|
2023-08-09 02:21:14 +00:00
|
|
|
struct IAsyncRunnable;
|
2021-11-05 17:34:23 +00:00
|
|
|
//class WorkItem;
|
|
|
|
|
2023-08-09 02:21:14 +00:00
|
|
|
|
2023-11-11 10:11:09 +00:00
|
|
|
struct ThreadPool :
|
|
|
|
IThreadPool,
|
|
|
|
IThreadPoolInternal,
|
|
|
|
AuEnableSharedFromThis<ThreadPool>
|
2021-11-05 17:34:23 +00:00
|
|
|
{
|
|
|
|
ThreadPool();
|
|
|
|
|
|
|
|
// IThreadPoolInternal
|
2023-11-12 00:18:34 +00:00
|
|
|
bool WaitFor(WorkerPId_t unlocker, const AuSPtr<Threading::IWaitable> &primitive, AuUInt32 ms) override;
|
|
|
|
bool WaitFor(WorkerId_t unlocker, const AuSPtr<Threading::IWaitable> &primitive, AuUInt32 ms);
|
2023-01-23 21:18:58 +00:00
|
|
|
void Run(WorkerId_t target, AuSPtr<IAsyncRunnable> runnable) override;
|
2023-08-09 02:21:14 +00:00
|
|
|
void Run(WorkerId_t target, AuSPtr<IAsyncRunnable> runnable, bool bIncrement);
|
2023-01-23 21:18:58 +00:00
|
|
|
IThreadPool *ToThreadPool() override;
|
2021-11-05 17:34:23 +00:00
|
|
|
|
|
|
|
// IThreadPool
|
2023-01-23 21:18:58 +00:00
|
|
|
virtual bool Spawn(WorkerId_t workerId) override;
|
2021-11-05 17:34:23 +00:00
|
|
|
|
2023-01-23 21:18:58 +00:00
|
|
|
virtual void SetRunningMode(bool eventRunning) override;
|
2021-11-05 17:34:23 +00:00
|
|
|
|
2023-01-23 21:18:58 +00:00
|
|
|
virtual bool Create(WorkerId_t workerId) override;
|
2021-11-05 17:34:23 +00:00
|
|
|
|
2023-01-23 21:18:58 +00:00
|
|
|
virtual bool InRunnerMode() override;
|
2021-11-05 17:34:23 +00:00
|
|
|
|
2023-01-23 21:18:58 +00:00
|
|
|
virtual bool Poll() override;
|
|
|
|
virtual bool RunOnce() override;
|
|
|
|
virtual bool Run() override;
|
|
|
|
|
|
|
|
virtual void Shutdown() override;
|
|
|
|
virtual bool Exiting() override;
|
2023-03-05 12:55:07 +00:00
|
|
|
|
|
|
|
virtual AuUInt32 PollAndCount(bool bStrict = true) override;
|
|
|
|
virtual AuUInt32 RunAllPending() override;
|
2021-11-05 17:34:23 +00:00
|
|
|
|
2023-08-10 02:34:44 +00:00
|
|
|
virtual AuSPtr<IWorkItem> NewWorkItem(const WorkerId_t &worker, const AuSPtr<IWorkItemHandler> &task) override;
|
|
|
|
virtual AuSPtr<IWorkItem> NewWorkFunction(const WorkerId_t &worker, AuVoidFunc callback) override;
|
2021-11-05 17:34:23 +00:00
|
|
|
virtual AuSPtr<IWorkItem> NewFence() override;
|
|
|
|
|
2023-01-23 21:18:58 +00:00
|
|
|
virtual Threading::Threads::ThreadShared_t ResolveHandle(WorkerId_t) override;
|
|
|
|
|
|
|
|
virtual AuBST<ThreadGroup_t, AuList<ThreadId_t>> GetThreads() override;
|
|
|
|
|
|
|
|
virtual WorkerId_t GetCurrentThread() override;
|
2021-11-05 17:34:23 +00:00
|
|
|
|
2023-12-01 09:22:51 +00:00
|
|
|
virtual AuSPtr<AuIO::IIOProcessor> GetIOProcessor(WorkerId_t id) override;
|
|
|
|
virtual AuSPtr<AuIO::Net::INetInterface> GetIONetInterface(WorkerId_t pid) override;
|
|
|
|
virtual AuSPtr<AuIO::Net::INetWorker> GetIONetWorker(WorkerId_t id) override;
|
|
|
|
|
2023-12-07 09:20:23 +00:00
|
|
|
AuSPtr<IO::IIOProcessor> GetSelfIOProcessor();
|
|
|
|
AuSPtr<IO::Net::INetInterface> GetSelfIONetInterface();
|
|
|
|
AuSPtr<IO::Net::INetWorker> GetSelfIONetWorker();
|
|
|
|
|
2023-01-23 21:18:58 +00:00
|
|
|
virtual bool Sync(WorkerId_t workerId, AuUInt32 timeoutMs, bool requireSignal) override;
|
|
|
|
virtual void Signal(WorkerId_t workerId) override;
|
2024-05-03 10:32:31 +00:00
|
|
|
virtual void Wakeup(WorkerId_t workerId) override;
|
2023-01-23 21:18:58 +00:00
|
|
|
virtual AuSPtr<AuLoop::ILoopSource> WorkerToLoopSource(WorkerId_t id) override;
|
|
|
|
virtual void SyncAllSafe() override;
|
2021-11-05 17:34:23 +00:00
|
|
|
|
|
|
|
virtual void AddFeature(WorkerId_t id, AuSPtr<Threading::Threads::IThreadFeature> feature, bool async) override;
|
|
|
|
|
2023-03-05 12:55:07 +00:00
|
|
|
virtual void AssertInThreadGroup(ThreadGroup_t group) override;
|
|
|
|
virtual void AssertWorker(WorkerId_t id) override;
|
2021-11-05 17:34:23 +00:00
|
|
|
|
2023-03-05 12:55:07 +00:00
|
|
|
virtual AuSPtr<AuLoop::ILoopQueue> ToKernelWorkQueue() override;
|
|
|
|
virtual AuSPtr<AuLoop::ILoopQueue> ToKernelWorkQueue(WorkerId_t workerId) override;
|
2022-03-10 15:35:01 +00:00
|
|
|
|
2023-05-24 07:19:47 +00:00
|
|
|
virtual AuSPtr<AuThreading::IWaitable> GetShutdownEvent() override;
|
2023-08-13 08:30:17 +00:00
|
|
|
virtual void AddDependency(AuSPtr<IThreadPool> pPool) override;
|
|
|
|
|
2023-10-24 14:33:30 +00:00
|
|
|
virtual void IncrementAbortFenceOnPool() override;
|
|
|
|
virtual void IncrementAbortFenceOnWorker(WorkerId_t workerId) override;
|
|
|
|
|
|
|
|
virtual AuUInt64 QueryAbortFence(AuOptional<WorkerId_t> optWorkerId) override;
|
|
|
|
virtual bool QueryShouldAbort(AuOptional<WorkerId_t> optWorkerId, AuUInt64 uFenceMagic) override;
|
|
|
|
|
2023-08-13 08:30:17 +00:00
|
|
|
bool IsSelfDepleted();
|
|
|
|
bool IsDepleted();
|
2023-05-24 07:19:47 +00:00
|
|
|
|
2022-03-10 15:35:01 +00:00
|
|
|
//virtual bool ScheduleLoopSource(const AuSPtr<Loop::ILoopSource> &loopSource, WorkerId_t workerId, AuUInt32 timeout, const AuConsumer<AuSPtr<Loop::ILoopSource>, bool> &callback) override;
|
2021-11-05 17:34:23 +00:00
|
|
|
|
|
|
|
// Internal API
|
|
|
|
|
|
|
|
bool Spawn(WorkerId_t workerId, bool create);
|
|
|
|
|
2024-05-02 19:06:41 +00:00
|
|
|
bool InternalRunOne(AuSPtr<ThreadState>, bool block, bool bUntilWork, AuUInt32 &uCount);
|
|
|
|
bool PollInternal(AuSPtr<ThreadState>, bool block, bool bUntilWork, AuUInt32 &uCount);
|
2024-05-01 19:09:35 +00:00
|
|
|
#if defined(__AUHAS_COROUTINES_CO_AWAIT) && defined(AU_LANG_CPP_20_)
|
2024-05-02 19:06:41 +00:00
|
|
|
AuVoidTask PollInternal_ForceCoRoutine(AuSPtr<ThreadState>, bool block, bool bUntilWork, AuUInt32 &uCount, bool &bRet);
|
2024-05-01 19:09:35 +00:00
|
|
|
#endif
|
2024-05-02 19:06:41 +00:00
|
|
|
bool PollInternal_Base(AuSPtr<ThreadState>, bool block, bool bUntilWork, AuUInt32 &uCount);
|
2021-11-05 17:34:23 +00:00
|
|
|
|
|
|
|
size_t GetThreadWorkersCount(ThreadGroup_t group);
|
|
|
|
|
|
|
|
virtual void CleanUpWorker(WorkerId_t wid) {};
|
|
|
|
virtual void CleanWorkerPoolReservedZeroFree() {}; // calls shutdown under async apps
|
|
|
|
|
|
|
|
// Secret old fiber api
|
|
|
|
bool CtxYield();
|
|
|
|
int CtxPollPush();
|
|
|
|
void CtxPollReturn(const AuSPtr<ThreadState> &state, int status, bool hitTask);
|
|
|
|
|
|
|
|
// TLS handle
|
|
|
|
struct WorkerWPId_t : WorkerId_t
|
|
|
|
{
|
|
|
|
WorkerWPId_t()
|
|
|
|
{}
|
|
|
|
|
2023-12-07 09:20:23 +00:00
|
|
|
WorkerWPId_t(const WorkerPId_t &ref) :
|
|
|
|
WorkerId_t(ref.first, ref.second),
|
|
|
|
pool(ref.GetPool())
|
2021-11-05 17:34:23 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
AuWPtr<IThreadPool> pool;
|
|
|
|
};
|
|
|
|
|
|
|
|
AuThreads::TLSVariable<WorkerWPId_t> tlsWorkerId;
|
|
|
|
|
2023-12-04 21:11:32 +00:00
|
|
|
AuSPtr<ThreadState> GetThreadHandle(WorkerId_t id) override;
|
2021-11-05 17:34:23 +00:00
|
|
|
private:
|
|
|
|
// TODO: BarrierMultiple
|
|
|
|
bool Barrier(WorkerId_t, AuUInt32 ms, bool requireSignal, bool drop);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void Entrypoint(WorkerId_t id);
|
|
|
|
|
|
|
|
private:
|
2023-02-08 18:23:37 +00:00
|
|
|
void EarlyExitTick();
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
void ThisExiting();
|
|
|
|
|
|
|
|
AuSPtr<GroupState> GetGroup(ThreadGroup_t type);
|
|
|
|
AuSPtr<ThreadState> GetThreadState();
|
2023-02-08 18:23:37 +00:00
|
|
|
AuSPtr<ThreadState> GetThreadStateNoWarn();
|
2022-03-10 15:35:01 +00:00
|
|
|
AuList<AuSPtr<ThreadState>> GetThreadHandles(WorkerId_t id);
|
|
|
|
|
2023-08-10 00:31:10 +00:00
|
|
|
AuSPtr<GroupState> threadGroups_[255];
|
2023-02-08 18:23:37 +00:00
|
|
|
AuUInt32 shuttingdown_ {};
|
|
|
|
bool shutdown {};
|
2023-11-11 10:11:09 +00:00
|
|
|
|
2023-12-26 21:01:29 +00:00
|
|
|
AuRWRenterableLock rwlock_;
|
2023-11-11 10:11:09 +00:00
|
|
|
AuThreading::IWaitable *pRWReadView {};
|
|
|
|
|
2023-12-26 21:01:29 +00:00
|
|
|
AuEvent shutdownEvent_;
|
2022-09-19 05:42:39 +00:00
|
|
|
bool runnersRunning_ {};
|
2023-08-13 08:30:17 +00:00
|
|
|
AuList<AuWPtr<ThreadPool>> listWeakDeps_;
|
2023-08-17 10:46:25 +00:00
|
|
|
AuList<AuWPtr<ThreadPool>> listWeakDepsParents_;
|
2023-08-09 09:25:22 +00:00
|
|
|
|
|
|
|
friend struct KeepGroupAlive;
|
2021-11-05 17:34:23 +00:00
|
|
|
};
|
|
|
|
}
|