Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 26 | #include "tests/daemon/rib-io-fixture.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/global.hpp" |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 28 | #include "tests/boost-test.hpp" |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 29 | |
| 30 | #include <boost/exception/diagnostic_information.hpp> |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace nfd::tests { |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 33 | |
| 34 | RibIoFixture::RibIoFixture() |
| 35 | { |
| 36 | std::mutex m; |
| 37 | std::condition_variable cv; |
| 38 | |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 39 | g_mainIo = &getGlobalIoService(); |
| 40 | setMainIoService(g_mainIo); |
| 41 | |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 42 | g_ribThread = std::thread([&] { |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 43 | { |
| 44 | std::lock_guard<std::mutex> lock(m); |
| 45 | g_ribIo = &getGlobalIoService(); |
| 46 | setRibIoService(g_ribIo); |
| 47 | BOOST_ASSERT(&g_io != g_ribIo); |
| 48 | BOOST_ASSERT(g_ribIo == &getRibIoService()); |
| 49 | } |
| 50 | cv.notify_all(); |
| 51 | |
| 52 | try { |
| 53 | while (true) { |
| 54 | { |
| 55 | std::unique_lock<std::mutex> lock(m_ribPollMutex); |
| 56 | m_ribPollStartCv.wait(lock, [this] { return m_shouldStopRibIo || m_shouldPollRibIo; }); |
| 57 | if (m_shouldStopRibIo) { |
| 58 | break; |
| 59 | } |
| 60 | BOOST_ASSERT(m_shouldPollRibIo); |
| 61 | } |
| 62 | |
| 63 | if (g_ribIo->stopped()) { |
| 64 | g_ribIo->reset(); |
| 65 | } |
| 66 | while (g_ribIo->poll() > 0) |
| 67 | ; |
| 68 | |
| 69 | { |
| 70 | std::lock_guard<std::mutex> lock(m_ribPollMutex); |
| 71 | m_shouldPollRibIo = false; |
| 72 | } |
| 73 | m_ribPollEndCv.notify_all(); |
| 74 | } |
| 75 | } |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 76 | catch (...) { |
| 77 | BOOST_WARN_MESSAGE(false, boost::current_exception_diagnostic_information()); |
| 78 | NDN_THROW_NESTED(std::runtime_error("Fatal exception in RIB thread")); |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 79 | } |
| 80 | }); |
| 81 | |
| 82 | { |
| 83 | std::unique_lock<std::mutex> lock(m); |
| 84 | cv.wait(lock, [this] { return g_ribIo != nullptr; }); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | RibIoFixture::~RibIoFixture() |
| 89 | { |
| 90 | { |
| 91 | std::lock_guard<std::mutex> lock(m_ribPollMutex); |
| 92 | m_shouldStopRibIo = true; |
| 93 | } |
| 94 | m_ribPollStartCv.notify_all(); |
| 95 | g_ribThread.join(); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | RibIoFixture::poll() |
| 100 | { |
| 101 | BOOST_ASSERT(&getGlobalIoService() == &g_io); |
| 102 | |
| 103 | size_t nHandlersRun = 0; |
| 104 | do { |
| 105 | { |
| 106 | std::lock_guard<std::mutex> lock(m_ribPollMutex); |
| 107 | m_shouldPollRibIo = true; |
| 108 | } |
| 109 | m_ribPollStartCv.notify_all(); |
| 110 | |
| 111 | if (g_io.stopped()) { |
| 112 | g_io.reset(); |
| 113 | } |
| 114 | |
| 115 | nHandlersRun = g_io.poll(); |
| 116 | |
| 117 | { |
| 118 | std::unique_lock<std::mutex> lock(m_ribPollMutex); |
| 119 | m_ribPollEndCv.wait(lock, [this] { return !m_shouldPollRibIo; }); |
| 120 | } |
| 121 | } while (nHandlersRun > 0); |
| 122 | } |
| 123 | |
| 124 | void |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 125 | RibIoTimeFixture::afterTick() |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 126 | { |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 127 | poll(); |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 130 | } // namespace nfd::tests |