2023-12-07 09:20:23 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021-2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AuAsyncTimer.cpp
|
|
|
|
Date: 2023-12-06
|
|
|
|
Date: 2021-11-2
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "IAsyncRunnable.hpp"
|
|
|
|
#include "AuAsyncTimer.hpp"
|
|
|
|
#include "ThreadPool.hpp"
|
|
|
|
|
|
|
|
namespace Aurora::Async
|
|
|
|
{
|
|
|
|
AsyncFuncTimer::AsyncFuncTimer(IThreadPoolInternal *owner,
|
|
|
|
const AuSPtr<IAsyncTimerCallback> &pCallback,
|
|
|
|
const WorkerPId_t &worker,
|
|
|
|
AuUInt64 uNextTickTime,
|
|
|
|
AuUInt64 uInterval) :
|
|
|
|
WorkItem(owner, worker, {}),
|
|
|
|
uNextTickTime(uNextTickTime),
|
2024-05-27 19:56:47 +00:00
|
|
|
uInterval(uInterval),
|
|
|
|
pCallback(pCallback)
|
2023-12-07 09:20:23 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncFuncTimer::CancelTimer()
|
|
|
|
{
|
2024-05-28 16:28:14 +00:00
|
|
|
AU_LOCK_GUARD(this->mutex);
|
2023-12-07 09:20:23 +00:00
|
|
|
this->Cancel();
|
2024-05-28 16:28:14 +00:00
|
|
|
AuResetMember(this->pCallback);
|
2023-12-07 09:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt64 AsyncFuncTimer::GetLastTime()
|
|
|
|
{
|
|
|
|
return this->uLastTickTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt64 AsyncFuncTimer::GetTicks()
|
|
|
|
{
|
|
|
|
return this->uTickCount;
|
|
|
|
}
|
|
|
|
|
2024-05-19 16:01:51 +00:00
|
|
|
bool AsyncFuncTimer::IsCatchUp()
|
|
|
|
{
|
|
|
|
return this->bCatchUp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncFuncTimer::SetCatchUp(bool bCatchUp)
|
|
|
|
{
|
|
|
|
this->bCatchUp = bCatchUp;
|
|
|
|
}
|
|
|
|
|
2023-12-07 09:20:23 +00:00
|
|
|
void AsyncFuncTimer::DispatchTask(IWorkItemHandler::ProcessInfo &info)
|
|
|
|
{
|
2024-05-28 16:28:14 +00:00
|
|
|
AU_LOCK_GUARD(this->mutex);
|
2023-12-07 09:20:23 +00:00
|
|
|
info.type = ETickType::eRerun;
|
|
|
|
|
|
|
|
auto uTickCount = ++this->uTickCount;
|
2024-05-19 16:01:51 +00:00
|
|
|
|
2023-12-07 09:20:23 +00:00
|
|
|
this->uLastTickTime = AuTime::SteadyClockNS();
|
|
|
|
auto uDelta = this->uLastTickTime - this->uNextTickTime;
|
2024-05-19 16:01:51 +00:00
|
|
|
|
2023-12-07 09:20:23 +00:00
|
|
|
this->uNextTickTime += this->uInterval;
|
2024-05-19 16:01:51 +00:00
|
|
|
if (!this->bCatchUp)
|
|
|
|
{
|
|
|
|
this->uNextTickTime = AuMax(this->uNextTickTime, this->uLastTickTime);
|
|
|
|
}
|
|
|
|
|
2023-12-07 09:20:23 +00:00
|
|
|
info.reschedSteadyClockAbsNs = this->uNextTickTime;
|
|
|
|
|
2024-05-28 16:28:14 +00:00
|
|
|
if (!this->pCallback)
|
2023-12-07 09:20:23 +00:00
|
|
|
{
|
2024-05-28 16:28:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (this->pCallback->OnTick(uTickCount, uDelta, this->uLastTickTime))
|
|
|
|
{
|
|
|
|
info.type = ETickType::eSchedule;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info.type = ETickType::eFinished;
|
|
|
|
}
|
2023-12-07 09:20:23 +00:00
|
|
|
}
|
2024-05-28 16:28:14 +00:00
|
|
|
catch (...)
|
2023-12-07 09:20:23 +00:00
|
|
|
{
|
2024-05-28 16:28:14 +00:00
|
|
|
info.type = ETickType::eSchedule;
|
|
|
|
SysPushErrorCatch();
|
2023-12-07 09:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsyncFuncTimer::Cleanup()
|
|
|
|
{
|
|
|
|
AuResetMember(this->pCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IAsyncTimer> NewTimer(AuUInt64 uPeriodNS,
|
|
|
|
const AuSPtr<IAsyncTimerCallback> &pCallback,
|
|
|
|
AuOptional<WorkerPId_t> workerPid,
|
|
|
|
AuOptional<AuUInt64> uStartTime)
|
|
|
|
{
|
|
|
|
if (!pCallback)
|
|
|
|
{
|
|
|
|
SysPushErrorArg();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!workerPid)
|
|
|
|
{
|
|
|
|
workerPid = AuAsync::GetCurrentWorkerPId();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uPeriodNS < 100)
|
|
|
|
{
|
|
|
|
SysPushErrorArg();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!uStartTime)
|
|
|
|
{
|
|
|
|
uStartTime = AuTime::SteadyClockNS() + uPeriodNS;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pThreadPool = AuStaticPointerCast<ThreadPool>(workerPid.value().GetPool()).get();
|
|
|
|
auto pRet = AuMakeShared<AsyncFuncTimer>(pThreadPool,
|
|
|
|
pCallback,
|
|
|
|
workerPid.value(),
|
|
|
|
uStartTime.value(),
|
|
|
|
uPeriodNS);
|
|
|
|
if (!pRet)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
pRet->Dispatch();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IAsyncTimer> NewTimer(AuUInt64 uPeriodNS,
|
|
|
|
const AuSupplierConsumer<bool, AuUInt64, AuUInt64, AuUInt64> &callback,
|
|
|
|
AuOptional<WorkerPId_t> workerPid,
|
|
|
|
AuOptional<AuUInt64> uStartTime)
|
|
|
|
{
|
|
|
|
return NewTimer(uPeriodNS,
|
|
|
|
AuMakeSharedThrow<IAsyncTimerCallbackFunctional>(callback),
|
|
|
|
workerPid,
|
|
|
|
uStartTime);
|
|
|
|
}
|
|
|
|
}
|