Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 1 | /* |
| 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 Tietavainen | a0745d2 | 2019-11-28 09:55:22 +0200 | [diff] [blame] | 17 | /* |
| 18 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | * platform project (RICP). |
| 20 | */ |
| 21 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 22 | #include "private/timerfd.hpp" |
| 23 | #include "private/engine.hpp" |
| 24 | #include "private/system.hpp" |
| 25 | |
| 26 | using namespace shareddatalayer; |
| 27 | |
| 28 | TimerFD::TimerFD(Engine& engine): |
| 29 | TimerFD(System::getSystem(), engine) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | TimerFD::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 | |
| 40 | TimerFD::~TimerFD() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | void 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 | |
| 53 | bool TimerFD::isFirst(Queue::iterator it) const |
| 54 | { |
| 55 | return queue.begin() == it; |
| 56 | } |
| 57 | |
| 58 | void 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 | |
| 69 | Timer::Duration TimerFD::toAbsolute(const Timer::Duration& duration) |
| 70 | { |
| 71 | return std::chrono::duration_cast<Timer::Duration>(system.time_since_epoch()) + duration; |
| 72 | } |
| 73 | |
| 74 | void TimerFD::handleEvents() |
| 75 | { |
| 76 | if (timerExpired()) |
| 77 | handleExpiredTimers(); |
| 78 | } |
| 79 | |
| 80 | bool TimerFD::timerExpired() const |
| 81 | { |
| 82 | uint64_t count; |
| 83 | return (system.read(fd, &count, sizeof(count)) == sizeof(count)) && (count > 0U); |
| 84 | } |
| 85 | |
| 86 | void 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 | |
| 102 | void TimerFD::popAndExecuteFirstTimer() |
| 103 | { |
| 104 | const auto i(queue.begin()); |
| 105 | const auto cb(i->second.second); |
| 106 | queue.erase(i); |
| 107 | cb(); |
| 108 | } |
| 109 | |
| 110 | Timer::Duration TimerFD::nextTrigger() const |
| 111 | { |
| 112 | return queue.begin()->first; |
| 113 | } |
| 114 | |
| 115 | void TimerFD::disarmTimerFD() |
| 116 | { |
| 117 | setTimeForTimerFD(0, 0); |
| 118 | } |
| 119 | |
| 120 | void 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 | |
| 127 | void 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 | } |