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 | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 27 | #include "tests/test-common.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 28 | #include "common/global.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 | |
| 32 | namespace nfd { |
| 33 | namespace tests { |
| 34 | |
| 35 | RibIoFixture::RibIoFixture() |
| 36 | { |
| 37 | std::mutex m; |
| 38 | std::condition_variable cv; |
| 39 | |
Teng Liang | f59e58f | 2018-09-07 16:41:54 -0700 | [diff] [blame] | 40 | g_mainIo = &getGlobalIoService(); |
| 41 | setMainIoService(g_mainIo); |
| 42 | |
Davide Pesavento | 2bdf60c | 2019-02-19 18:23:45 -0500 | [diff] [blame] | 43 | g_ribThread = std::thread([&] { |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 44 | { |
| 45 | std::lock_guard<std::mutex> lock(m); |
| 46 | g_ribIo = &getGlobalIoService(); |
| 47 | setRibIoService(g_ribIo); |
| 48 | BOOST_ASSERT(&g_io != g_ribIo); |
| 49 | BOOST_ASSERT(g_ribIo == &getRibIoService()); |
| 50 | } |
| 51 | cv.notify_all(); |
| 52 | |
| 53 | try { |
| 54 | while (true) { |
| 55 | { |
| 56 | std::unique_lock<std::mutex> lock(m_ribPollMutex); |
| 57 | m_ribPollStartCv.wait(lock, [this] { return m_shouldStopRibIo || m_shouldPollRibIo; }); |
| 58 | if (m_shouldStopRibIo) { |
| 59 | break; |
| 60 | } |
| 61 | BOOST_ASSERT(m_shouldPollRibIo); |
| 62 | } |
| 63 | |
| 64 | if (g_ribIo->stopped()) { |
| 65 | g_ribIo->reset(); |
| 66 | } |
| 67 | while (g_ribIo->poll() > 0) |
| 68 | ; |
| 69 | |
| 70 | { |
| 71 | std::lock_guard<std::mutex> lock(m_ribPollMutex); |
| 72 | m_shouldPollRibIo = false; |
| 73 | } |
| 74 | m_ribPollEndCv.notify_all(); |
| 75 | } |
| 76 | } |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 77 | catch (...) { |
| 78 | BOOST_WARN_MESSAGE(false, boost::current_exception_diagnostic_information()); |
| 79 | NDN_THROW_NESTED(std::runtime_error("Fatal exception in RIB thread")); |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 80 | } |
| 81 | }); |
| 82 | |
| 83 | { |
| 84 | std::unique_lock<std::mutex> lock(m); |
| 85 | cv.wait(lock, [this] { return g_ribIo != nullptr; }); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | RibIoFixture::~RibIoFixture() |
| 90 | { |
| 91 | { |
| 92 | std::lock_guard<std::mutex> lock(m_ribPollMutex); |
| 93 | m_shouldStopRibIo = true; |
| 94 | } |
| 95 | m_ribPollStartCv.notify_all(); |
| 96 | g_ribThread.join(); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | RibIoFixture::poll() |
| 101 | { |
| 102 | BOOST_ASSERT(&getGlobalIoService() == &g_io); |
| 103 | |
| 104 | size_t nHandlersRun = 0; |
| 105 | do { |
| 106 | { |
| 107 | std::lock_guard<std::mutex> lock(m_ribPollMutex); |
| 108 | m_shouldPollRibIo = true; |
| 109 | } |
| 110 | m_ribPollStartCv.notify_all(); |
| 111 | |
| 112 | if (g_io.stopped()) { |
| 113 | g_io.reset(); |
| 114 | } |
| 115 | |
| 116 | nHandlersRun = g_io.poll(); |
| 117 | |
| 118 | { |
| 119 | std::unique_lock<std::mutex> lock(m_ribPollMutex); |
| 120 | m_ribPollEndCv.wait(lock, [this] { return !m_shouldPollRibIo; }); |
| 121 | } |
| 122 | } while (nHandlersRun > 0); |
| 123 | } |
| 124 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 125 | RibIoTimeFixture::RibIoTimeFixture() |
| 126 | : ClockFixture(g_io) |
| 127 | { |
| 128 | } |
| 129 | |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 130 | void |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 131 | RibIoTimeFixture::pollAfterClockTick() |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 132 | { |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 133 | poll(); |
Teng Liang | 952d6fd | 2018-05-29 21:09:52 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | } // namespace tests |
| 137 | } // namespace nfd |