blob: 3b3c6b6539f28e38f796232507b4631680212720 [file] [log] [blame]
Rolf Badorekef2bf512019-08-20 11:17:15 +03001/*
2 Copyright (c) 2018-2019 Nokia.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
Timo Tietavainena0745d22019-11-28 09:55:22 +020017/*
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
20*/
21
Rolf Badorekef2bf512019-08-20 11:17:15 +030022#include "private/timerfd.hpp"
23#include "private/engine.hpp"
24#include "private/system.hpp"
25
26using namespace shareddatalayer;
27
28TimerFD::TimerFD(Engine& engine):
29 TimerFD(System::getSystem(), engine)
30{
31}
32
33TimerFD::TimerFD(System& system, Engine& engine):
34 system(system),
35 fd(system, system.timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC))
36{
37 engine.addMonitoredFD(fd, Engine::EVENT_IN, std::bind(&TimerFD::handleEvents, this));
38}
39
40TimerFD::~TimerFD()
41{
42}
43
44void TimerFD::arm(Timer& timer, const Timer::Duration& duration, const Timer::Callback& cb)
45{
46 const auto absolute(toAbsolute(duration));
47 const auto i(queue.insert(std::make_pair(absolute, std::make_pair(&timer, cb))));
48 timer.iterator = i;
49 if (isFirst(i))
50 armTimerFD(absolute);
51}
52
53bool TimerFD::isFirst(Queue::iterator it) const
54{
55 return queue.begin() == it;
56}
57
58void TimerFD::disarm(const Timer& timer)
59{
60 const bool wasFirst(isFirst(timer.iterator));
61 queue.erase(timer.iterator);
62
63 if (queue.empty())
64 disarmTimerFD();
65 else if (wasFirst)
66 armTimerFD(nextTrigger());
67}
68
69Timer::Duration TimerFD::toAbsolute(const Timer::Duration& duration)
70{
71 return std::chrono::duration_cast<Timer::Duration>(system.time_since_epoch()) + duration;
72}
73
74void TimerFD::handleEvents()
75{
76 if (timerExpired())
77 handleExpiredTimers();
78}
79
80bool TimerFD::timerExpired() const
81{
82 uint64_t count;
83 return (system.read(fd, &count, sizeof(count)) == sizeof(count)) && (count > 0U);
84}
85
86void TimerFD::handleExpiredTimers()
87{
88 const auto now(system.time_since_epoch());
89 do
90 {
91 popAndExecuteFirstTimer();
92 if (queue.empty())
93 {
94 disarmTimerFD();
95 return;
96 }
97 }
98 while (queue.begin()->first <= now);
99 armTimerFD(nextTrigger());
100}
101
102void TimerFD::popAndExecuteFirstTimer()
103{
104 const auto i(queue.begin());
105 const auto cb(i->second.second);
106 queue.erase(i);
107 cb();
108}
109
110Timer::Duration TimerFD::nextTrigger() const
111{
112 return queue.begin()->first;
113}
114
115void TimerFD::disarmTimerFD()
116{
117 setTimeForTimerFD(0, 0);
118}
119
120void TimerFD::armTimerFD(const Timer::Duration& duration)
121{
122 static const long int NANOSECONDS_IN_ONE_SECOND(1E9);
123 setTimeForTimerFD(std::chrono::duration_cast<std::chrono::seconds>(duration).count(),
124 std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count() % NANOSECONDS_IN_ONE_SECOND);
125}
126
127void TimerFD::setTimeForTimerFD(time_t seconds, long int nanoseconds)
128{
129 const itimerspec ts{ { 0, 0 }, { seconds, nanoseconds } };
130 system.timerfd_settime(fd, TFD_TIMER_ABSTIME, &ts, nullptr);
131}