2021-10-02 10:28:49 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: LSTimer.Linux.cpp
|
|
|
|
Date: 2021-10-1
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "LSTimer.hpp"
|
2022-04-19 21:50:34 +00:00
|
|
|
#include "LSFromFdNonblocking.hpp"
|
|
|
|
#include <Source/Time/Time.hpp>
|
|
|
|
#include <sys/timerfd.h>
|
2021-10-02 10:28:49 +00:00
|
|
|
|
|
|
|
namespace Aurora::Loop
|
|
|
|
{
|
2022-05-04 15:34:02 +00:00
|
|
|
LSTimer::LSTimer(AuUInt32 reschedStepMsOrZero, AuUInt32 maxIterationsOrZero, bool bSingleshot, int handle) :
|
2022-04-19 21:50:34 +00:00
|
|
|
LSHandle(handle),
|
2022-05-04 15:34:02 +00:00
|
|
|
bSingleshot(bSingleshot),
|
|
|
|
reschedStepNsOrZero_(AuMSToNS<AuUInt64>(reschedStepMsOrZero)),
|
2022-04-19 21:50:34 +00:00
|
|
|
maxIterationsOrZero_(maxIterationsOrZero)
|
|
|
|
{
|
2022-05-04 15:34:02 +00:00
|
|
|
this->targetTime_ = AuTime::CurrentClockNS();
|
2022-04-19 21:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LSTimer::~LSTimer()
|
|
|
|
{
|
|
|
|
if ((this->handle != 0) &&
|
|
|
|
(this->handle != -1))
|
|
|
|
{
|
|
|
|
::close(AuExchange(this->handle, -1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LSTimer::UpdateTime(AuUInt64 absTimeMs)
|
|
|
|
{
|
2022-05-04 15:34:02 +00:00
|
|
|
this->targetTime_ = AuMSToNS<AuUInt64>(absTimeMs);
|
|
|
|
UpdateTimeInternal(this->targetTime_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LSTimer::UpdateTimeNs(AuUInt64 absTimeNs)
|
|
|
|
{
|
|
|
|
this->targetTime_ = absTimeNs;
|
2022-04-19 21:50:34 +00:00
|
|
|
UpdateTimeInternal(this->targetTime_);
|
|
|
|
}
|
|
|
|
|
2022-05-04 15:34:02 +00:00
|
|
|
void LSTimer::UpdateTimeInternal(AuUInt64 absTimeNs)
|
2022-04-19 21:50:34 +00:00
|
|
|
{
|
2022-05-04 15:34:02 +00:00
|
|
|
this->targetTime_ = absTimeNs;
|
2022-04-19 21:50:34 +00:00
|
|
|
|
|
|
|
itimerspec spec {};
|
2022-05-04 15:34:02 +00:00
|
|
|
AuTime::auabsns2ts(&spec.it_value, this->targetTime_);
|
|
|
|
AuTime::ns2ts(&spec.it_interval, this->reschedStepNsOrZero_);
|
2022-04-19 21:50:34 +00:00
|
|
|
SysAssert(::timerfd_settime(this->handle, TFD_TIMER_ABSTIME, &spec, nullptr) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LSTimer::UpdateTickRateIfAny(AuUInt32 reschedStepMsOrZero, AuUInt32 maxIterationsOrZero)
|
|
|
|
{
|
2022-05-04 15:34:02 +00:00
|
|
|
this->reschedStepNsOrZero_ = AuMSToNS<AuUInt64>(reschedStepMsOrZero);
|
2022-04-19 21:50:34 +00:00
|
|
|
this->maxIterationsOrZero_ = maxIterationsOrZero;
|
|
|
|
this->count_ = 0;
|
2022-05-04 15:34:02 +00:00
|
|
|
this->UpdateTimeInternal(this->targetTime_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LSTimer::UpdateTickRateIfAnyNs(AuUInt64 reschedStepNsOrZero, AuUInt32 maxIterationsOrZero)
|
|
|
|
{
|
|
|
|
this->reschedStepNsOrZero_ = reschedStepNsOrZero;
|
|
|
|
this->maxIterationsOrZero_ = maxIterationsOrZero;
|
|
|
|
this->count_ = 0;
|
|
|
|
this->UpdateTimeInternal(this->targetTime_);
|
2022-04-19 21:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LSTimer::OnTrigger(AuUInt handle)
|
|
|
|
{
|
|
|
|
return IsSignaledNonblocking();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LSTimer::Stop()
|
|
|
|
{
|
|
|
|
itimerspec spec {};
|
|
|
|
SysAssert(::timerfd_settime(this->handle, 0, &spec, nullptr) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LSTimer::IsSignaled()
|
|
|
|
{
|
|
|
|
return IsSignaledFromNonblockingImpl(this, this, &LSTimer::IsSignaledNonblocking);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LSTimer::WaitOn(AuUInt32 timeout)
|
|
|
|
{
|
|
|
|
return LSHandle::WaitOn(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
ELoopSource LSTimer::GetType()
|
|
|
|
{
|
|
|
|
return ELoopSource::eSourceTimer;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LSTimer::IsSignaledNonblocking()
|
|
|
|
{
|
|
|
|
AuUInt64 oldSemaphoreValue {};
|
|
|
|
auto ok = ::read(this->handle, &oldSemaphoreValue, sizeof(oldSemaphoreValue)) == 8;
|
|
|
|
if (ok && this->maxIterationsOrZero_)
|
|
|
|
{
|
|
|
|
ok = AuAtomicAdd<AuUInt32>(&this->count_, 1) <= this->maxIterationsOrZero_;
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 15:34:02 +00:00
|
|
|
if (ok && !this->reschedStepNsOrZero_ && !bSingleshot)
|
2022-04-19 21:50:34 +00:00
|
|
|
{
|
|
|
|
// Ensure non-step timers remain signaled despite the read above us caneling them
|
2022-05-04 15:34:02 +00:00
|
|
|
this->UpdateTimeInternal(this->targetTime_);
|
2022-04-19 21:50:34 +00:00
|
|
|
}
|
2022-05-04 15:34:02 +00:00
|
|
|
|
2022-04-19 21:50:34 +00:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2022-05-04 15:34:02 +00:00
|
|
|
AUKN_SYM AuSPtr<ITimer> NewLSTimer(AuUInt64 absStartTimeMs, AuUInt32 reschedStepMsOrZero, AuUInt32 maxIterationsOrZero, bool bSingleshot)
|
2022-04-19 21:50:34 +00:00
|
|
|
{
|
|
|
|
int fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-05-04 15:34:02 +00:00
|
|
|
auto object = AuMakeShared<LSTimer>(reschedStepMsOrZero, maxIterationsOrZero, bSingleshot, fd);
|
2022-04-19 21:50:34 +00:00
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
::close(fd);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-05-04 15:34:02 +00:00
|
|
|
if (absStartTimeMs)
|
|
|
|
{
|
|
|
|
object->UpdateTime(absStartTimeMs);
|
|
|
|
}
|
2022-04-19 21:50:34 +00:00
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
2021-10-02 10:28:49 +00:00
|
|
|
}
|