From 64780c4bb14b099ebbefa03bb8e3496761dacad3 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Tue, 5 Mar 2024 13:06:36 +0800 Subject: [PATCH] fix timer wait 0 ms --- .clang-format | 2 +- src/ulib/system/timer.cpp | 43 ++++++++++++++++----------------------- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/.clang-format b/.clang-format index 83c2ce5..75ade9a 100644 --- a/.clang-format +++ b/.clang-format @@ -36,7 +36,7 @@ BreakBeforeTernaryOperators: true BreakConstructorInitializers: BeforeColon ConstructorInitializerAllOnOneLineOrOnePerLine: true BreakInheritanceList: BeforeColon -ColumnLimit: 80 +ColumnLimit: 120 CompactNamespaces: false ContinuationIndentWidth: 4 EmptyLineBeforeAccessModifier: LogicalBlock diff --git a/src/ulib/system/timer.cpp b/src/ulib/system/timer.cpp index a926537..ae736a1 100644 --- a/src/ulib/system/timer.cpp +++ b/src/ulib/system/timer.cpp @@ -49,20 +49,15 @@ TimerManager::TimerManager() {} TimerId -TimerManager::AddTimerImpl(const Timer::ExpireCallback &cb, - uint64_t run_after_ms, - uint64_t interval) +TimerManager::AddTimerImpl(const Timer::ExpireCallback &cb, uint64_t run_after_ms, uint64_t interval) { Timer *timer = nullptr; { MutexGuard guard(timer_inserting_set_lock_); - timer = new Timer(TimeNowInMicroSeconds() + run_after_ms * 1000, - interval * 1000); + timer = new Timer(TimeNowInMicroSeconds() + run_after_ms * 1000, interval * 1000); timer->on_expire() = cb; if (!timer_inserting_set_.insert({timer->when(), timer}).second) { - ULOG_ERROR("system.timer_manager", - "Add timer failed, already exists timer_id={}", - timer->id()); + ULOG_ERROR("system.timer_manager", "Add timer failed, already exists timer_id={}", timer->id()); delete timer; return 0; } @@ -108,17 +103,14 @@ TimerManager::Loop() MutexGuard insert_guard(timer_inserting_set_lock_); timer_inserting_set.swap(timer_inserting_set_); } - timers_.insert(timer_inserting_set.begin(), - timer_inserting_set.end()); + timers_.insert(timer_inserting_set.begin(), timer_inserting_set.end()); } // cancel timer { // TODO imporve MutexGuard cancel_guard(timer_canceling_set_lock_); - for (auto iter = timers_.begin(); - !timer_canceling_set_.empty() && iter != timers_.end();) { - if (timer_canceling_set_.find(iter->second->id()) - != timer_canceling_set_.end()) { + for (auto iter = timers_.begin(); !timer_canceling_set_.empty() && iter != timers_.end();) { + if (timer_canceling_set_.find(iter->second->id()) != timer_canceling_set_.end()) { delete iter->second; iter = timers_.erase(iter); } else { @@ -135,34 +127,35 @@ TimerManager::Loop() timers_cond_.WaitForMilliseconds(timers_guard, 1000); } else if (timers_.begin()->first > now) { do { - uint64_t wait_time_ms = (timers_.begin()->first - now) / 1000; + uint64_t wait_time_ms = (timers_.begin()->first - now + 1000) / 1000; + + // FIX: + if (wait_time_ms == 0) { wait_time_ms = 1; } + timers_cond_.WaitForMilliseconds(timers_guard, wait_time_ms); now = TimeNowInMicroSeconds(); } while (timers_.begin()->first > now); } else { - ULOG_TRACE("system.timer_manager", "need trigger set size: {}", - timers_.size()); + ULOG_TRACE("system.timer_manager", "need trigger set size: {}", timers_.size()); std::set expired_timers; while (!timers_.empty() && timers_.begin()->first <= now) { expired_timers.insert(*timers_.begin()); timers_.erase(timers_.begin()); } - for (auto iter = expired_timers.begin(); - iter != expired_timers.end(); ++iter) { + for (auto iter = expired_timers.begin(); iter != expired_timers.end(); ++iter) { uint64_t when = iter->first; Timer *timer = iter->second; try { timer->on_expire()(); if (timer->auto_reset()) { - timers_.insert({now + timer->interval(), timer}); + const uint64_t next_trigger_time = (now - when + timer->interval() - 1) * timer->interval(); + timers_.insert({next_trigger_time, timer}); } else { delete timer; } } catch (const std::exception &e) { - ULOG_ERROR("system.timer", - "timer crash: timer_id={}, reason: {}", - iter->second->id(), e.what()); + ULOG_ERROR("system.timer", "timer crash: timer_id={}, reason: {}", iter->second->id(), e.what()); } catch (...) { // do nothing } @@ -195,9 +188,7 @@ TimerManager::Instance() } TimerId -TimerManager::AddTimer(const Timer::ExpireCallback &cb, - uint64_t run_after_ms, - uint64_t interval) +TimerManager::AddTimer(const Timer::ExpireCallback &cb, uint64_t run_after_ms, uint64_t interval) { return Instance().AddTimerImpl(cb, run_after_ms, interval); }