Line data Source code
1 : /* 2 : * This file is part of PTN Engine 3 : * 4 : * Copyright (c) 2023 Eduardo ValgĂ´de 5 : * 6 : * Licensed under the Apache License, Version 2.0 (the "License"); 7 : * you may not use this file except in compliance with the License. 8 : * You may obtain a copy of the License at 9 : * 10 : * http://www.apache.org/licenses/LICENSE-2.0 11 : * 12 : * Unless required by applicable law or agreed to in writing, software 13 : * distributed under the License is distributed on an "AS IS" BASIS, 14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 : * See the License for the specific language governing permissions and 16 : * limitations under the License. 17 : */ 18 : 19 : #pragma once 20 : 21 : #include <atomic> 22 : #include <barrier> 23 : #include <chrono> 24 : #include <condition_variable> 25 : #include <mutex> 26 : #include <shared_mutex> 27 : #include <thread> 28 : 29 : namespace ptne 30 : { 31 : 32 : class IPTN_EngineEL; 33 : class Transition; 34 : 35 : //! 36 : //! \brief The EventLoop class manages an event loop thread. 37 : //! 38 : class EventLoop 39 : { 40 : public: 41 : using SleepDuration = std::chrono::duration<long, std::ratio<1, 1000>>; 42 : 43 : ~EventLoop(); 44 : 45 : //! 46 : //! \brief EventLoop 47 : //! \param ptnEngineInternal 48 : //! 49 : explicit EventLoop(IPTN_EngineEL &ptnEngineInternal); 50 : 51 : EventLoop(const EventLoop &) = delete; 52 : EventLoop(EventLoop &&) = delete; 53 : EventLoop &operator=(const EventLoop &) = delete; 54 : EventLoop &operator=(EventLoop &&) = delete; 55 : 56 : //! 57 : //! \brief Inform if the event loop is running. 58 : //! \return true if the event loop thread is running. 59 : //! 60 : bool isRunning() const; 61 : 62 : //! 63 : //! \brief Order the event loop to stop. 64 : //! 65 : void stop() noexcept; 66 : 67 : //! 68 : //! \brief Start the event loop thread. 69 : //! \param log Whether to log or not. 70 : //! \param o The output stream to write the log messages to. 71 : //! 72 : void start(const bool log, std::ostream &o); 73 : 74 : //! 75 : //! \brief Notify the event loop thread of a new event. 76 : //! 77 : void notifyNewEvent(); 78 : 79 : //! 80 : //! \brief Set the event loop watchdog timer period. 81 : //! \param sleepTime The event loop watchdog timer period. 82 : //! 83 : void setSleepDuration(const SleepDuration sleepDuration); 84 : 85 : //! 86 : //! \brief Get the event loop watchdog timer period. 87 : //! \return The event loop watchdog timer period. 88 : //! 89 : SleepDuration getSleepDuration() const; 90 : 91 : private: 92 : //! 93 : //! \brief Event loop function. 94 : //! \param log Flag to turn on logging on or off. 95 : //! \param o Log where to write log messages. 96 : //! 97 : void run(std::stop_token stopToken, const bool log, std::ostream &o); 98 : 99 : //! Reference to the petri net engine. 100 : IPTN_EngineEL &m_ptnEngine; 101 : 102 : //! Barrier to synchronize the event loop thread. 103 : std::unique_ptr<std::barrier<>> m_barrier = nullptr; 104 : 105 : //! Event loop thread. 106 : std::jthread m_eventLoopThread; 107 : 108 : //! Flag if the event loop thread is running. 109 : std::atomic<bool> m_eventLoopThreadRunning = false; 110 : 111 : //! Condition variable to wake up the event loop thread when some event happens. 112 : std::condition_variable m_eventNotifier; 113 : 114 : //! Mutex protecting the event notifier condition variable m_eventNotifier. 115 : mutable std::mutex m_eventNotifierMutex; 116 : 117 : //! While idle, watchdog timer period. 118 78 : SleepDuration m_sleepDuration = std::chrono::milliseconds(100); 119 : 120 : //! Mutex protecting m_sleepDuration. 121 : mutable std::shared_mutex m_sleepDurationMutex; 122 : }; 123 : 124 : } // namespace ptne