35 #include <sys/timerfd.h> 46 if((iFd_ = timerfd_create(CLOCK_REALTIME,0)) < 0)
52 thread_ = std::move(std::thread{&Timer::scheduler,
this});
59 std::lock_guard<std::mutex> m(mutex_);
66 itimerspec spec{{0,0},{0,1}};
68 timerfd_settime(iFd_,0,&spec,
nullptr);
77 std::lock_guard<std::mutex> m(mutex_);
81 auto iter = timerIdMap_.find(timerId);
83 if(iter != timerIdMap_.end())
89 if(std::get<0>(timePointMap_.begin()->second) == timerId)
96 timePointMap_.erase({iter->second,timerId});
98 timerIdMap_.erase(iter);
113 void EMANE::Utils::Timer::cancel_i()
116 itimerspec spec{{0,0},{0,0}};
117 timerfd_settime(iFd_,0,&spec,
nullptr);
125 std::unique_lock<std::mutex> lock(mutex_);
130 if(timePointMap_.empty())
135 else if(timePoint < timePointMap_.begin()->first.first)
145 timePointMap_.insert(std::make_pair(std::make_pair(timePoint,timerId_),
146 std::make_tuple(timerId_,
152 timerIdMap_.insert(std::make_pair(timerId_,timePoint));
164 void EMANE::Utils::Timer::schedule_i()
167 if(!timePointMap_.empty())
169 auto & timePoint = timePointMap_.begin()->first.first;
171 auto timeSinceEpoch = timePoint.time_since_epoch();
173 auto sec = std::chrono::duration_cast<
Seconds>(timeSinceEpoch);
178 itimerspec spec{{0,0},{sec.count(),nsec.count()}};
180 timerfd_settime(iFd_,TFD_TIMER_ABSTIME,&spec,
nullptr);
185 void EMANE::Utils::Timer::scheduler()
187 std::vector<TimerInfo> expired;
189 std::uint64_t u64Expired{};
195 for(
const auto & info : expired)
197 const TimerId & timerId{std::get<0>(info)};
198 const TimePoint & expireTime{std::get<1>(info)};
199 const Callback & callback{std::get<3>(info)};
200 const TimePoint & scheduleTime{std::get<4>(info)};
217 if(read(iFd_,&u64Expired,
sizeof(u64Expired)) > 0)
219 std::unique_lock<std::mutex> lock(mutex_);
221 auto now = Clock::now();
232 for(
const auto & entry : timePointMap_)
234 const auto & timePoint = std::get<1>(entry.second);
238 expired.push_back(std::move(entry.second));
248 for(
const auto & info : expired)
255 std::tie(timerId,expireTime,interval,callback,std::ignore) = info;
257 timePointMap_.erase({expireTime,timerId});
259 timerIdMap_.erase(timerId);
261 if(interval != Duration::zero())
263 expireTime += interval;
265 timePointMap_.insert(std::make_pair(std::make_pair(expireTime,timerId),
266 std::make_tuple(timerId,
272 timerIdMap_.insert(std::make_pair(timerId,expireTime));
std::chrono::seconds Seconds
TimerId schedule(Function fn, const TimePoint &absoluteTimePoint)
std::chrono::nanoseconds Nanoseconds
Clock::time_point TimePoint
bool cancel(TimerId timerId)