fix timer wait 0 ms
Some checks failed
rpcrypto-build / build (Debug, himix200.toolchain.cmake) (push) Successful in 1m14s
rpcrypto-build / build (Release, hisiv510.toolchain.cmake) (push) Successful in 1m9s
rpcrypto-build / build (Debug, hisiv510.toolchain.cmake) (push) Successful in 1m13s
rpcrypto-build / build (Release, himix200.toolchain.cmake) (push) Successful in 1m38s
linux-hisiv500-gcc / linux-gcc-hisiv500 (push) Successful in 1m39s
linux-mips64-gcc / linux-gcc-mips64el (push) Has been cancelled
linux-x64-gcc / linux-gcc (push) Has been cancelled
Some checks failed
rpcrypto-build / build (Debug, himix200.toolchain.cmake) (push) Successful in 1m14s
rpcrypto-build / build (Release, hisiv510.toolchain.cmake) (push) Successful in 1m9s
rpcrypto-build / build (Debug, hisiv510.toolchain.cmake) (push) Successful in 1m13s
rpcrypto-build / build (Release, himix200.toolchain.cmake) (push) Successful in 1m38s
linux-hisiv500-gcc / linux-gcc-hisiv500 (push) Successful in 1m39s
linux-mips64-gcc / linux-gcc-mips64el (push) Has been cancelled
linux-x64-gcc / linux-gcc (push) Has been cancelled
This commit is contained in:
parent
7a17509718
commit
64780c4bb1
@ -36,7 +36,7 @@ BreakBeforeTernaryOperators: true
|
|||||||
BreakConstructorInitializers: BeforeColon
|
BreakConstructorInitializers: BeforeColon
|
||||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||||
BreakInheritanceList: BeforeColon
|
BreakInheritanceList: BeforeColon
|
||||||
ColumnLimit: 80
|
ColumnLimit: 120
|
||||||
CompactNamespaces: false
|
CompactNamespaces: false
|
||||||
ContinuationIndentWidth: 4
|
ContinuationIndentWidth: 4
|
||||||
EmptyLineBeforeAccessModifier: LogicalBlock
|
EmptyLineBeforeAccessModifier: LogicalBlock
|
||||||
|
@ -49,20 +49,15 @@ TimerManager::TimerManager()
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
TimerId
|
TimerId
|
||||||
TimerManager::AddTimerImpl(const Timer::ExpireCallback &cb,
|
TimerManager::AddTimerImpl(const Timer::ExpireCallback &cb, uint64_t run_after_ms, uint64_t interval)
|
||||||
uint64_t run_after_ms,
|
|
||||||
uint64_t interval)
|
|
||||||
{
|
{
|
||||||
Timer *timer = nullptr;
|
Timer *timer = nullptr;
|
||||||
{
|
{
|
||||||
MutexGuard guard(timer_inserting_set_lock_);
|
MutexGuard guard(timer_inserting_set_lock_);
|
||||||
timer = new Timer(TimeNowInMicroSeconds() + run_after_ms * 1000,
|
timer = new Timer(TimeNowInMicroSeconds() + run_after_ms * 1000, interval * 1000);
|
||||||
interval * 1000);
|
|
||||||
timer->on_expire() = cb;
|
timer->on_expire() = cb;
|
||||||
if (!timer_inserting_set_.insert({timer->when(), timer}).second) {
|
if (!timer_inserting_set_.insert({timer->when(), timer}).second) {
|
||||||
ULOG_ERROR("system.timer_manager",
|
ULOG_ERROR("system.timer_manager", "Add timer failed, already exists timer_id={}", timer->id());
|
||||||
"Add timer failed, already exists timer_id={}",
|
|
||||||
timer->id());
|
|
||||||
delete timer;
|
delete timer;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -108,17 +103,14 @@ TimerManager::Loop()
|
|||||||
MutexGuard insert_guard(timer_inserting_set_lock_);
|
MutexGuard insert_guard(timer_inserting_set_lock_);
|
||||||
timer_inserting_set.swap(timer_inserting_set_);
|
timer_inserting_set.swap(timer_inserting_set_);
|
||||||
}
|
}
|
||||||
timers_.insert(timer_inserting_set.begin(),
|
timers_.insert(timer_inserting_set.begin(), timer_inserting_set.end());
|
||||||
timer_inserting_set.end());
|
|
||||||
}
|
}
|
||||||
// cancel timer
|
// cancel timer
|
||||||
{
|
{
|
||||||
// TODO imporve
|
// TODO imporve
|
||||||
MutexGuard cancel_guard(timer_canceling_set_lock_);
|
MutexGuard cancel_guard(timer_canceling_set_lock_);
|
||||||
for (auto iter = timers_.begin();
|
for (auto iter = timers_.begin(); !timer_canceling_set_.empty() && iter != timers_.end();) {
|
||||||
!timer_canceling_set_.empty() && iter != timers_.end();) {
|
if (timer_canceling_set_.find(iter->second->id()) != timer_canceling_set_.end()) {
|
||||||
if (timer_canceling_set_.find(iter->second->id())
|
|
||||||
!= timer_canceling_set_.end()) {
|
|
||||||
delete iter->second;
|
delete iter->second;
|
||||||
iter = timers_.erase(iter);
|
iter = timers_.erase(iter);
|
||||||
} else {
|
} else {
|
||||||
@ -135,34 +127,35 @@ TimerManager::Loop()
|
|||||||
timers_cond_.WaitForMilliseconds(timers_guard, 1000);
|
timers_cond_.WaitForMilliseconds(timers_guard, 1000);
|
||||||
} else if (timers_.begin()->first > now) {
|
} else if (timers_.begin()->first > now) {
|
||||||
do {
|
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);
|
timers_cond_.WaitForMilliseconds(timers_guard, wait_time_ms);
|
||||||
now = TimeNowInMicroSeconds();
|
now = TimeNowInMicroSeconds();
|
||||||
} while (timers_.begin()->first > now);
|
} while (timers_.begin()->first > now);
|
||||||
} else {
|
} else {
|
||||||
ULOG_TRACE("system.timer_manager", "need trigger set size: {}",
|
ULOG_TRACE("system.timer_manager", "need trigger set size: {}", timers_.size());
|
||||||
timers_.size());
|
|
||||||
std::set<Entry> expired_timers;
|
std::set<Entry> expired_timers;
|
||||||
while (!timers_.empty() && timers_.begin()->first <= now) {
|
while (!timers_.empty() && timers_.begin()->first <= now) {
|
||||||
expired_timers.insert(*timers_.begin());
|
expired_timers.insert(*timers_.begin());
|
||||||
timers_.erase(timers_.begin());
|
timers_.erase(timers_.begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto iter = expired_timers.begin();
|
for (auto iter = expired_timers.begin(); iter != expired_timers.end(); ++iter) {
|
||||||
iter != expired_timers.end(); ++iter) {
|
|
||||||
uint64_t when = iter->first;
|
uint64_t when = iter->first;
|
||||||
Timer *timer = iter->second;
|
Timer *timer = iter->second;
|
||||||
try {
|
try {
|
||||||
timer->on_expire()();
|
timer->on_expire()();
|
||||||
if (timer->auto_reset()) {
|
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 {
|
} else {
|
||||||
delete timer;
|
delete timer;
|
||||||
}
|
}
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
ULOG_ERROR("system.timer",
|
ULOG_ERROR("system.timer", "timer crash: timer_id={}, reason: {}", iter->second->id(), e.what());
|
||||||
"timer crash: timer_id={}, reason: {}",
|
|
||||||
iter->second->id(), e.what());
|
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
@ -195,9 +188,7 @@ TimerManager::Instance()
|
|||||||
}
|
}
|
||||||
|
|
||||||
TimerId
|
TimerId
|
||||||
TimerManager::AddTimer(const Timer::ExpireCallback &cb,
|
TimerManager::AddTimer(const Timer::ExpireCallback &cb, uint64_t run_after_ms, uint64_t interval)
|
||||||
uint64_t run_after_ms,
|
|
||||||
uint64_t interval)
|
|
||||||
{
|
{
|
||||||
return Instance().AddTimerImpl(cb, run_after_ms, interval);
|
return Instance().AddTimerImpl(cb, run_after_ms, interval);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user