NRP Core  1.4.1
event_loop_interface.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef EVENT_LOOP_INTERFACE_H
23 #define EVENT_LOOP_INTERFACE_H
24 
25 #include <chrono>
26 #include <thread>
27 #include <future>
28 
29 #include <nlohmann/json.hpp>
30 
35 {
36  public:
37 
38  virtual ~EventLoopInterface() = default;
39 
40  EventLoopInterface() = delete;
41 
45  EventLoopInterface(std::chrono::milliseconds timestep, std::chrono::milliseconds timestepThres);
46 
50  virtual void initialize();
51 
55  void runLoopOnce(const std::chrono::time_point<std::chrono::steady_clock>& startTime);
56 
60  void runLoop(std::chrono::milliseconds timeout);
61 
69  void runLoopAsync(std::chrono::milliseconds timeout = std::chrono::milliseconds(0), bool doInit = false);
70 
76  void stopLoop();
77 
81  void shutdown();
82 
88  bool isRunning();
89 
95  void waitForLoopEnd();
96 
97  protected:
98 
102  virtual void initializeCB() = 0;
103 
107  virtual void runLoopCB() = 0;
108 
112  virtual void shutdownCB() = 0;
113 
115  std::chrono::milliseconds _timestep;
117  std::chrono::milliseconds _timestepThres;
119  std::chrono::milliseconds _currentTime = std::chrono::milliseconds(0);
121  unsigned long _iterations = 0L;
122 
128  bool isRunningNotAsync();
129 
130  private:
131 
133  std::future<void> _runFuture;
135  std::atomic<bool> _doRun;
137  bool _isInitialized = false;
138 };
139 
140 
141 #endif // EVENT_LOOP_INTERFACE_H
EventLoopInterface::~EventLoopInterface
virtual ~EventLoopInterface()=default
EventLoopInterface::_currentTime
std::chrono::milliseconds _currentTime
current time clock
Definition: event_loop_interface.h:119
EventLoopInterface::isRunning
bool isRunning()
Returns true if the event loop is currently running, false otherwise.
Definition: event_loop_interface.cpp:132
EventLoopInterface::isRunningNotAsync
bool isRunningNotAsync()
Internal isRunning function.
Definition: event_loop_interface.cpp:140
EventLoopInterface::waitForLoopEnd
void waitForLoopEnd()
Blocks execution until the loop reaches timeout.
Definition: event_loop_interface.cpp:126
EventLoopInterface
Manages simulation loop. Runs physics and brain interface, and synchronizes them via Transfer Functio...
Definition: event_loop_interface.h:34
EventLoopInterface::runLoopOnce
void runLoopOnce(const std::chrono::time_point< std::chrono::steady_clock > &startTime)
Run a single loop.
Definition: event_loop_interface.cpp:32
EventLoopInterface::runLoopCB
virtual void runLoopCB()=0
Execute actions which must be performed every loop.
EventLoopInterface::_iterations
unsigned long _iterations
stores the number of times the loop has been run
Definition: event_loop_interface.h:121
EventLoopInterface::stopLoop
void stopLoop()
Stop loop.
Definition: event_loop_interface.cpp:118
EventLoopInterface::runLoopAsync
void runLoopAsync(std::chrono::milliseconds timeout=std::chrono::milliseconds(0), bool doInit=false)
Run loop in a thread.
Definition: event_loop_interface.cpp:79
EventLoopInterface::initialize
virtual void initialize()
Initialize loop.
Definition: event_loop_interface.cpp:94
EventLoopInterface::initializeCB
virtual void initializeCB()=0
Initialize loop.
EventLoopInterface::shutdownCB
virtual void shutdownCB()=0
Shutdown loop.
EventLoopInterface::shutdown
void shutdown()
Shutdown loop.
Definition: event_loop_interface.cpp:103
EventLoopInterface::runLoop
void runLoop(std::chrono::milliseconds timeout)
Run loop.
Definition: event_loop_interface.cpp:38
EventLoopInterface::_timestepThres
std::chrono::milliseconds _timestepThres
allowed time deviation in event loop timestep execution before printing a warning message
Definition: event_loop_interface.h:117
EventLoopInterface::EventLoopInterface
EventLoopInterface()=delete
EventLoopInterface::_timestep
std::chrono::milliseconds _timestep
timestep of the event loop
Definition: event_loop_interface.h:115