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/eventfd.hpp" |
| 23 | #include <sys/eventfd.h> |
| 24 | #include "private/abort.hpp" |
| 25 | #include "private/engine.hpp" |
| 26 | #include "private/system.hpp" |
| 27 | |
| 28 | using namespace shareddatalayer; |
| 29 | |
| 30 | namespace |
| 31 | { |
| 32 | /* |
| 33 | * Simple wrapper for executing the given callback without expecting |
| 34 | * exceptions. If callback throws, we'll crash. |
| 35 | */ |
| 36 | void execute(const EventFD::Callback& callback) noexcept |
| 37 | { |
| 38 | callback(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | EventFD::EventFD(Engine& engine): |
| 43 | EventFD(System::getSystem(), engine) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | EventFD::EventFD(System& system, Engine& engine): |
| 48 | system(system), |
| 49 | fd(system, system.eventfd(0U, EFD_CLOEXEC | EFD_NONBLOCK)) |
| 50 | { |
| 51 | engine.addMonitoredFD(fd, Engine::EVENT_IN, std::bind(&EventFD::handleEvents, this)); |
| 52 | } |
| 53 | |
| 54 | EventFD::~EventFD() |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | void EventFD::post(const Callback& callback) |
| 59 | { |
| 60 | if (!callback) |
| 61 | SHAREDDATALAYER_ABORT("A null callback was provided"); |
| 62 | |
| 63 | atomicPushBack(callback); |
| 64 | static const uint64_t value(1U); |
| 65 | system.write(fd, &value, sizeof(value)); |
| 66 | } |
| 67 | |
| 68 | void EventFD::atomicPushBack(const Callback& callback) |
| 69 | { |
| 70 | std::lock_guard<std::mutex> guard(callbacksMutex); |
| 71 | callbacks.push_back(callback); |
| 72 | } |
| 73 | |
| 74 | EventFD::Callbacks EventFD::atomicPopAll() |
| 75 | { |
| 76 | std::lock_guard<std::mutex> guard(callbacksMutex); |
| 77 | Callbacks extractedCallbacks; |
| 78 | std::swap(callbacks, extractedCallbacks); |
| 79 | return extractedCallbacks; |
| 80 | } |
| 81 | |
| 82 | void EventFD::handleEvents() |
| 83 | { |
| 84 | uint64_t value; |
| 85 | system.read(fd, &value, sizeof(value)); |
| 86 | executeCallbacks(); |
| 87 | } |
| 88 | |
| 89 | void EventFD::executeCallbacks() |
| 90 | { |
| 91 | Callbacks callbacks(atomicPopAll()); |
| 92 | while (!callbacks.empty()) |
| 93 | popAndExecuteFirstCallback(callbacks); |
| 94 | } |
| 95 | |
| 96 | void EventFD::popAndExecuteFirstCallback(Callbacks& callbacks) |
| 97 | { |
| 98 | const auto callback(callbacks.front()); |
| 99 | callbacks.pop_front(); |
| 100 | execute(callback); |
| 101 | } |