Reece
72a74eb7a4
[*] Refactor a 'Object' member field in a ParseObject struct [+] Added an option to set a command dispatcher thread from an AsyncApp [*] Fix various issues with AsyncApp, stablity improvements [+] Added AddDelayTime
83 lines
2.8 KiB
C++
83 lines
2.8 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: AsyncApp.hpp
|
|
Date: 2021-6-26
|
|
Author: Reece
|
|
***/
|
|
#pragma once
|
|
|
|
namespace Aurora::Async
|
|
{
|
|
struct GroupState;
|
|
struct ThreadState;
|
|
//class WorkItem;
|
|
|
|
|
|
void DecRunningTasks();
|
|
void IncRunningTasks();
|
|
|
|
class AsyncApp : public IAsyncApp
|
|
{
|
|
public:
|
|
AsyncApp();
|
|
|
|
// Main thread logic
|
|
void Start() override;
|
|
void Main() override;
|
|
void Shutdown() override;
|
|
bool Exiting() override;
|
|
void SetConsoleCommandDispatcher(WorkerId_t id) override;
|
|
|
|
// Spawning
|
|
bool Spawn(WorkerId_t) override;
|
|
Threading::Threads::ThreadShared_t ResolveHandle(WorkerId_t) override;
|
|
AuBST<ThreadGroup_t, AuList<ThreadId_t>> GetThreads() override;
|
|
WorkerId_t GetCurrentThread() override;
|
|
|
|
// Synchronization
|
|
bool Sync(ThreadGroup_t group, bool requireSignal, AuUInt32 timeout) override;
|
|
void Signal(ThreadGroup_t group) override;
|
|
|
|
bool WaitFor(WorkerId_t unlocker, Threading::IWaitable *primitive, AuUInt32 ms) override; // when unlocker = this, pump event loop
|
|
bool WaitFor(DispatchTarget_t unlocker, Threading::IWaitable *primitive, AuUInt32 ms) override; // when unlocker = this, pump event loop
|
|
|
|
bool SyncTimeout(ThreadGroup_t group, AuUInt32 ms) override;
|
|
|
|
void SyncAllSafe() override;
|
|
|
|
// Features
|
|
void AddFeature(WorkerId_t id, AuSPtr<Threading::Threads::IThreadFeature> feature, bool async = false) override;
|
|
|
|
// Debug
|
|
void AssertInThreadGroup(ThreadGroup_t group) override;
|
|
void AssertWorker(WorkerId_t id) override;
|
|
|
|
void Run(DispatchTarget_t target, AuSPtr<IAsyncRunnable> runnable);
|
|
|
|
bool Poll(bool block) override;
|
|
|
|
size_t GetThreadWorkersCount(ThreadGroup_t group);
|
|
|
|
private:
|
|
void ThisExiting();
|
|
void ShutdownZero();
|
|
|
|
// TODO: BarrierMultiple
|
|
bool Barrier(WorkerId_t, AuUInt32 ms, bool requireSignal, bool drop);
|
|
|
|
Threading::Primitives::RWLockUnique_t rwlock_;
|
|
|
|
AuSPtr<GroupState> GetGroup(ThreadGroup_t type);
|
|
|
|
AuSPtr<ThreadState> GetThreadState();
|
|
|
|
void Entrypoint(WorkerId_t id);
|
|
|
|
using ThreadDb_t = AuBST<ThreadGroup_t, AuSPtr<GroupState>>;
|
|
|
|
ThreadDb_t threads_;
|
|
bool shuttingdown_ {};
|
|
std::optional<WorkerId_t> commandDispatcher_;
|
|
};
|
|
} |