backend: idle fd improvements

This commit is contained in:
Vaxry 2024-07-02 14:32:41 +02:00
parent 04b2db8350
commit 874183997f
3 changed files with 22 additions and 9 deletions

View file

@ -143,6 +143,7 @@ namespace Aquamarine {
} idle;
void dispatchIdle();
void updateIdleTimer();
//
struct {

View file

@ -174,14 +174,17 @@ std::vector<Hyprutils::Memory::CSharedPointer<SPollFD>> Aquamarine::CBackend::ge
for (auto& i : implementations) {
auto pollfds = i->pollFDs();
for (auto& p : pollfds) {
log(AQ_LOG_DEBUG, std::format("backend: poll fd {} for implementation {}", p->fd, backendTypeToName(i->type())));
result.emplace_back(p);
}
}
for (auto& sfd : sessionFDs) {
log(AQ_LOG_DEBUG, std::format("backend: poll fd {} for session", sfd->fd));
result.emplace_back(sfd);
}
log(AQ_LOG_DEBUG, std::format("backend: poll fd {} for idle", idle.fd));
result.emplace_back(makeShared<SPollFD>(idle.fd, [this]() { dispatchIdle(); }));
return result;
@ -224,9 +227,15 @@ const std::vector<SP<IBackendImplementation>>& Aquamarine::CBackend::getImplemen
void Aquamarine::CBackend::addIdleEvent(SP<std::function<void(void)>> fn) {
auto r = idle.pending.emplace_back(fn);
// update timerfd
updateIdleTimer();
}
void Aquamarine::CBackend::updateIdleTimer() {
uint64_t ADD_NS = idle.pending.empty() ? TIMESPEC_NSEC_PER_SEC * 240ULL /* 240s, 4 mins */ : 0;
timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
timespecAddNs(&now, ADD_NS);
itimerspec ts = {.it_value = now};
@ -246,4 +255,6 @@ void Aquamarine::CBackend::dispatchIdle() {
if (i && *i)
(*i)();
}
updateIdleTimer();
}

View file

@ -147,22 +147,23 @@ void Aquamarine::CHeadlessBackend::dispatchTimers() {
}
void Aquamarine::CHeadlessBackend::updateTimerFD() {
long long lowest = 42069133769LL;
long long lowestNs = TIMESPEC_NSEC_PER_SEC * 240 /* 240s, 4 mins */;
const auto clocknow = std::chrono::steady_clock::now();
bool any = false;
for (auto& t : timers.timers) {
auto delta = std::chrono::duration_cast<std::chrono::microseconds>(t.when - clocknow).count();
auto delta = std::chrono::duration_cast<std::chrono::microseconds>(t.when - clocknow).count() * 1000 /* µs -> ns */;
if (delta < lowest)
lowest = delta;
if (delta < lowestNs)
lowestNs = delta;
}
if (lowest < 0)
lowest = 0;
if (lowestNs < 0)
lowestNs = 0;
timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
timespecAddNs(&now, lowest * 1000); // µs -> ns => * 1000
timespecAddNs(&now, lowestNs);
itimerspec ts = {.it_value = now};