blob: 11368ae8b0129b0858e4837a884403db45240d72 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#ifndef AOS_EVENTS_EVENT_LOOP_H_
2#define AOS_EVENTS_EVENT_LOOP_H_
3
Brian Silverman6a54ff32020-04-28 16:41:39 -07004#include <sched.h>
5
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include <atomic>
milind1f1dca32021-07-03 13:50:07 -07007#include <ostream>
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include <string>
James Kuszmaul3ae42262019-11-08 12:33:41 -08009#include <string_view>
Alex Perrycb7da4b2019-08-28 19:35:56 -070010
Austin Schuh3054f5f2021-07-21 15:38:01 -070011#include "absl/container/btree_set.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#include "aos/configuration.h"
13#include "aos/configuration_generated.h"
Austin Schuh1af273d2020-03-07 20:11:34 -080014#include "aos/events/channel_preallocated_allocator.h"
Austin Schuh7d87b672019-12-01 20:23:49 -080015#include "aos/events/event_loop_event.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080016#include "aos/events/event_loop_generated.h"
17#include "aos/events/timing_statistics.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070018#include "aos/flatbuffers.h"
Brian Silverman79ec7fc2020-06-08 20:11:22 -050019#include "aos/ftrace.h"
Brian Silvermana1652f32020-01-29 20:41:44 -080020#include "aos/ipc_lib/data_alignment.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070021#include "aos/json_to_flatbuffer.h"
22#include "aos/time/time.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080023#include "aos/util/phased_loop.h"
Austin Schuh4385b142021-03-14 21:31:13 -070024#include "aos/uuid.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070025#include "flatbuffers/flatbuffers.h"
26#include "glog/logging.h"
27
Austin Schuh39788ff2019-12-01 18:22:57 -080028DECLARE_bool(timing_reports);
29DECLARE_int32(timing_report_ms);
30
Alex Perrycb7da4b2019-08-28 19:35:56 -070031namespace aos {
32
Austin Schuh39788ff2019-12-01 18:22:57 -080033class EventLoop;
34class WatcherState;
35
Austin Schuh6231cc32019-12-07 13:06:15 -080036// Struct available on Watchers, Fetchers, Timers, and PhasedLoops with context
37// about the current message.
Alex Perrycb7da4b2019-08-28 19:35:56 -070038struct Context {
Austin Schuhad154822019-12-27 15:45:13 -080039 // Time that the message was sent on this node, or the timer was triggered.
40 monotonic_clock::time_point monotonic_event_time;
41 // Realtime the message was sent on this node. This is set to min_time for
42 // Timers and PhasedLoops.
43 realtime_clock::time_point realtime_event_time;
44
45 // For a single-node configuration, these two are identical to *_event_time.
46 // In a multinode configuration, these are the times that the message was
47 // sent on the original node.
48 monotonic_clock::time_point monotonic_remote_time;
49 realtime_clock::time_point realtime_remote_time;
50
Austin Schuh6231cc32019-12-07 13:06:15 -080051 // The rest are only valid for Watchers and Fetchers.
Brian Silverman4f4e0612020-08-12 19:54:41 -070052
Alex Perrycb7da4b2019-08-28 19:35:56 -070053 // Index in the queue.
54 uint32_t queue_index;
Austin Schuhad154822019-12-27 15:45:13 -080055 // Index into the remote queue. Useful to determine if data was lost. In a
56 // single-node configuration, this will match queue_index.
57 uint32_t remote_queue_index;
58
Alex Perrycb7da4b2019-08-28 19:35:56 -070059 // Size of the data sent.
60 size_t size;
61 // Pointer to the data.
Brian Silvermaneaa41d62020-07-08 19:47:35 -070062 const void *data;
Austin Schuh678078e2020-08-01 14:30:36 -070063
Brian Silverman4f4e0612020-08-12 19:54:41 -070064 // Index of the message buffer. This will be in [0, NumberBuffers) on
65 // read_method=PIN channels, and -1 for other channels.
66 //
67 // This only tells you about the underlying storage for this message, not
68 // anything about its position in the queue. This is only useful for advanced
69 // zero-copy use cases, on read_method=PIN channels.
70 //
71 // This will uniquely identify a message on this channel at a point in time.
72 // For senders, this point in time is while the sender has the message. With
73 // read_method==PIN, this point in time includes while the caller has access
74 // to this context. For other read_methods, this point in time may be before
75 // the caller has access to this context, which makes this pretty useless.
76 int buffer_index;
77
Austin Schuh8902fa52021-03-14 22:39:24 -070078 // UUID of the remote node which sent this message, or this node in the case
79 // of events which are local to this node.
Austin Schuha9012be2021-07-21 15:19:11 -070080 UUID source_boot_uuid = UUID::Zero();
Austin Schuh8902fa52021-03-14 22:39:24 -070081
Austin Schuhca75b6a2020-12-15 21:12:24 -080082 // Efficiently copies the flatbuffer into a FlatbufferVector, allocating
Austin Schuh678078e2020-08-01 14:30:36 -070083 // memory in the process. It is vital that T matches the type of the
84 // underlying flatbuffer.
85 template <typename T>
86 FlatbufferVector<T> CopyFlatBuffer() const {
Brian Silverman354697a2020-09-22 21:06:32 -070087 ResizeableBuffer buffer;
88 buffer.resize(size);
89 memcpy(buffer.data(), data, size);
90 return FlatbufferVector<T>(std::move(buffer));
Austin Schuh678078e2020-08-01 14:30:36 -070091 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070092};
93
94// Raw version of fetcher. Contains a local variable that the fetcher will
95// update. This is used for reflection and as an interface to implement typed
96// fetchers.
97class RawFetcher {
98 public:
Austin Schuh39788ff2019-12-01 18:22:57 -080099 RawFetcher(EventLoop *event_loop, const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700100 RawFetcher(const RawFetcher &) = delete;
101 RawFetcher &operator=(const RawFetcher &) = delete;
Austin Schuh39788ff2019-12-01 18:22:57 -0800102 virtual ~RawFetcher();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700103
Austin Schuh39788ff2019-12-01 18:22:57 -0800104 // Fetches the next message in the queue without blocking. Returns true if
105 // there was a new message and we got it.
106 bool FetchNext();
107
108 // Fetches the latest message without blocking.
109 bool Fetch();
110
111 // Returns the channel this fetcher uses.
112 const Channel *channel() const { return channel_; }
113 // Returns the context for the current message.
114 const Context &context() const { return context_; }
115
116 protected:
117 EventLoop *event_loop() { return event_loop_; }
Austin Schuh3054f5f2021-07-21 15:38:01 -0700118 const EventLoop *event_loop() const { return event_loop_; }
Austin Schuh39788ff2019-12-01 18:22:57 -0800119
Alex Perrycb7da4b2019-08-28 19:35:56 -0700120 Context context_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800121
122 private:
123 friend class EventLoop;
124 // Implementation
125 virtual std::pair<bool, monotonic_clock::time_point> DoFetchNext() = 0;
126 virtual std::pair<bool, monotonic_clock::time_point> DoFetch() = 0;
127
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500128 EventLoop *const event_loop_;
129 const Channel *const channel_;
130 const std::string ftrace_prefix_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800131
132 internal::RawFetcherTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500133 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700134};
135
136// Raw version of sender. Sends a block of data. This is used for reflection
137// and as a building block to implement typed senders.
138class RawSender {
139 public:
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700140 using SharedSpan = std::shared_ptr<const absl::Span<const uint8_t>>;
141
milind1f1dca32021-07-03 13:50:07 -0700142 enum class [[nodiscard]] Error{
143 // Represents success and no error
144 kOk,
145
146 // Error for messages on channels being sent faster than their
147 // frequency and channel storage duration allow
148 kMessagesSentTooFast};
149
Austin Schuh39788ff2019-12-01 18:22:57 -0800150 RawSender(EventLoop *event_loop, const Channel *channel);
151 RawSender(const RawSender &) = delete;
152 RawSender &operator=(const RawSender &) = delete;
153
154 virtual ~RawSender();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700155
Alex Perrycb7da4b2019-08-28 19:35:56 -0700156 virtual void *data() = 0;
157 virtual size_t size() = 0;
milind1f1dca32021-07-03 13:50:07 -0700158
159 // Sends a message without copying it. The users starts by copying up to
160 // size() bytes into the data backed by data(). They then call Send to send.
161 // Returns Error::kOk on a successful send, or
162 // Error::kMessagesSentTooFast if messages were sent too fast. If provided,
163 // monotonic_remote_time, realtime_remote_time, and remote_queue_index are
164 // attached to the message and are available in the context on the read side.
165 // If they are not populated, the read side will get the sent times instead.
166 Error Send(size_t size);
167 Error Send(size_t size, monotonic_clock::time_point monotonic_remote_time,
168 realtime_clock::time_point realtime_remote_time,
169 uint32_t remote_queue_index, const UUID &source_boot_uuid);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700170
171 // Sends a single block of data by copying it.
Austin Schuhad154822019-12-27 15:45:13 -0800172 // The remote arguments have the same meaning as in Send above.
milind1f1dca32021-07-03 13:50:07 -0700173 // Returns Error::kMessagesSentTooFast if messages were sent too fast
174 Error Send(const void *data, size_t size);
175 Error Send(const void *data, size_t size,
176 monotonic_clock::time_point monotonic_remote_time,
177 realtime_clock::time_point realtime_remote_time,
178 uint32_t remote_queue_index, const UUID &source_boot_uuid);
179
180 // CHECKs that no sending Error occurred and logs the channel_ data if
181 // one did
182 void CheckOk(const Error err);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700183
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700184 // Sends a single block of data by refcounting it to avoid copies. The data
185 // must not change after being passed into Send. The remote arguments have the
186 // same meaning as in Send above.
milind1f1dca32021-07-03 13:50:07 -0700187 Error Send(const SharedSpan data);
188 Error Send(const SharedSpan data,
189 monotonic_clock::time_point monotonic_remote_time,
190 realtime_clock::time_point realtime_remote_time,
191 uint32_t remote_queue_index, const UUID &remote_boot_uuid);
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700192
Austin Schuh54cf95f2019-11-29 13:14:18 -0800193 const Channel *channel() const { return channel_; }
194
Austin Schuhad154822019-12-27 15:45:13 -0800195 // Returns the time_points that the last message was sent at.
196 aos::monotonic_clock::time_point monotonic_sent_time() const {
197 return monotonic_sent_time_;
198 }
199 aos::realtime_clock::time_point realtime_sent_time() const {
200 return realtime_sent_time_;
201 }
202 // Returns the queue index that this was sent with.
203 uint32_t sent_queue_index() const { return sent_queue_index_; }
204
Brian Silvermana1652f32020-01-29 20:41:44 -0800205 // Returns the associated flatbuffers-style allocator. This must be
206 // deallocated before the message is sent.
Austin Schuh1af273d2020-03-07 20:11:34 -0800207 ChannelPreallocatedAllocator *fbb_allocator() {
208 fbb_allocator_ = ChannelPreallocatedAllocator(
209 reinterpret_cast<uint8_t *>(data()), size(), channel());
Brian Silvermana1652f32020-01-29 20:41:44 -0800210 return &fbb_allocator_;
211 }
212
Brian Silverman4f4e0612020-08-12 19:54:41 -0700213 // Index of the buffer which is currently exposed by data() and the various
214 // other accessors. This is the message the caller should be filling out.
215 virtual int buffer_index() = 0;
216
Alex Perrycb7da4b2019-08-28 19:35:56 -0700217 protected:
Austin Schuh39788ff2019-12-01 18:22:57 -0800218 EventLoop *event_loop() { return event_loop_; }
Austin Schuh3054f5f2021-07-21 15:38:01 -0700219 const EventLoop *event_loop() const { return event_loop_; }
Austin Schuh54cf95f2019-11-29 13:14:18 -0800220
Austin Schuhb5c6f972021-03-14 21:53:07 -0700221 monotonic_clock::time_point monotonic_sent_time_ = monotonic_clock::min_time;
222 realtime_clock::time_point realtime_sent_time_ = realtime_clock::min_time;
Austin Schuhad154822019-12-27 15:45:13 -0800223 uint32_t sent_queue_index_ = 0xffffffff;
224
Austin Schuh39788ff2019-12-01 18:22:57 -0800225 private:
226 friend class EventLoop;
227
milind1f1dca32021-07-03 13:50:07 -0700228 virtual Error DoSend(const void *data, size_t size,
229 monotonic_clock::time_point monotonic_remote_time,
230 realtime_clock::time_point realtime_remote_time,
231 uint32_t remote_queue_index,
232 const UUID &source_boot_uuid) = 0;
233 virtual Error DoSend(size_t size,
234 monotonic_clock::time_point monotonic_remote_time,
235 realtime_clock::time_point realtime_remote_time,
236 uint32_t remote_queue_index,
237 const UUID &source_boot_uuid) = 0;
238 virtual Error DoSend(const SharedSpan data,
239 monotonic_clock::time_point monotonic_remote_time,
240 realtime_clock::time_point realtime_remote_time,
241 uint32_t remote_queue_index,
242 const UUID &source_boot_uuid);
Austin Schuh39788ff2019-12-01 18:22:57 -0800243
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500244 EventLoop *const event_loop_;
245 const Channel *const channel_;
246 const std::string ftrace_prefix_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700247
Austin Schuh39788ff2019-12-01 18:22:57 -0800248 internal::RawSenderTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500249 Ftrace ftrace_;
Brian Silvermana1652f32020-01-29 20:41:44 -0800250
Austin Schuh1af273d2020-03-07 20:11:34 -0800251 ChannelPreallocatedAllocator fbb_allocator_{nullptr, 0, nullptr};
Austin Schuh39788ff2019-12-01 18:22:57 -0800252};
Alex Perrycb7da4b2019-08-28 19:35:56 -0700253
milind1f1dca32021-07-03 13:50:07 -0700254// Needed for compatibility with glog
255std::ostream &operator<<(std::ostream &os, const RawSender::Error err);
256
Alex Perrycb7da4b2019-08-28 19:35:56 -0700257// Fetches the newest message from a channel.
258// This provides a polling based interface for channels.
259template <typename T>
260class Fetcher {
261 public:
262 Fetcher() {}
263
264 // Fetches the next message. Returns true if it fetched a new message. This
265 // method will only return messages sent after the Fetcher was created.
Brian Silvermana1652f32020-01-29 20:41:44 -0800266 bool FetchNext() {
267 const bool result = fetcher_->FetchNext();
268 if (result) {
269 CheckChannelDataAlignment(fetcher_->context().data,
270 fetcher_->context().size);
271 }
272 return result;
273 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700274
275 // Fetches the most recent message. Returns true if it fetched a new message.
276 // This will return the latest message regardless of if it was sent before or
277 // after the fetcher was created.
Brian Silvermana1652f32020-01-29 20:41:44 -0800278 bool Fetch() {
279 const bool result = fetcher_->Fetch();
280 if (result) {
281 CheckChannelDataAlignment(fetcher_->context().data,
282 fetcher_->context().size);
283 }
284 return result;
285 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700286
287 // Returns a pointer to the contained flatbuffer, or nullptr if there is no
288 // available message.
289 const T *get() const {
Austin Schuh39788ff2019-12-01 18:22:57 -0800290 return fetcher_->context().data != nullptr
291 ? flatbuffers::GetRoot<T>(
292 reinterpret_cast<const char *>(fetcher_->context().data))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700293 : nullptr;
294 }
295
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700296 // Returns the channel this fetcher uses
297 const Channel *channel() const { return fetcher_->channel(); }
298
Alex Perrycb7da4b2019-08-28 19:35:56 -0700299 // Returns the context holding timestamps and other metadata about the
300 // message.
301 const Context &context() const { return fetcher_->context(); }
302
303 const T &operator*() const { return *get(); }
304 const T *operator->() const { return get(); }
305
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700306 // Returns true if this fetcher is valid and connected to a channel.
Milind Upadhyay49174a72021-04-10 16:24:57 -0700307 bool valid() const { return static_cast<bool>(fetcher_); }
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700308
Austin Schuhca75b6a2020-12-15 21:12:24 -0800309 // Copies the current flatbuffer into a FlatbufferVector.
310 FlatbufferVector<T> CopyFlatBuffer() const {
311 return context().template CopyFlatBuffer<T>();
312 }
313
Alex Perrycb7da4b2019-08-28 19:35:56 -0700314 private:
315 friend class EventLoop;
316 Fetcher(::std::unique_ptr<RawFetcher> fetcher)
317 : fetcher_(::std::move(fetcher)) {}
318 ::std::unique_ptr<RawFetcher> fetcher_;
319};
320
321// Sends messages to a channel.
322template <typename T>
323class Sender {
324 public:
325 Sender() {}
326
327 // Represents a single message about to be sent to the queue.
328 // The lifecycle goes:
329 //
330 // Builder builder = sender.MakeBuilder();
331 // T::Builder t_builder = builder.MakeBuilder<T>();
332 // Populate(&t_builder);
333 // builder.Send(t_builder.Finish());
334 class Builder {
335 public:
Austin Schuh1af273d2020-03-07 20:11:34 -0800336 Builder(RawSender *sender, ChannelPreallocatedAllocator *allocator)
Brian Silverman9dd793b2020-01-31 23:52:21 -0800337 : fbb_(allocator->size(), allocator),
338 allocator_(allocator),
339 sender_(sender) {
Brian Silvermana1652f32020-01-29 20:41:44 -0800340 CheckChannelDataAlignment(allocator->data(), allocator->size());
Austin Schuhd7b15da2020-02-17 15:06:11 -0800341 fbb_.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700342 }
Brian Silvermana1652f32020-01-29 20:41:44 -0800343 Builder() {}
344 Builder(const Builder &) = delete;
345 Builder(Builder &&) = default;
346
347 Builder &operator=(const Builder &) = delete;
348 Builder &operator=(Builder &&) = default;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700349
350 flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; }
351
352 template <typename T2>
353 typename T2::Builder MakeBuilder() {
354 return typename T2::Builder(fbb_);
355 }
356
milind1f1dca32021-07-03 13:50:07 -0700357 RawSender::Error Send(flatbuffers::Offset<T> offset) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700358 fbb_.Finish(offset);
milind1f1dca32021-07-03 13:50:07 -0700359 const auto err = sender_->Send(fbb_.GetSize());
Brian Silverman9dd793b2020-01-31 23:52:21 -0800360 // Ensure fbb_ knows it shouldn't access the memory any more.
361 fbb_ = flatbuffers::FlatBufferBuilder();
milind1f1dca32021-07-03 13:50:07 -0700362 return err;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700363 }
364
milind1f1dca32021-07-03 13:50:07 -0700365 // Equivalent to RawSender::CheckOk
366 void CheckOk(const RawSender::Error err) { sender_->CheckOk(err); };
367
Alex Perrycb7da4b2019-08-28 19:35:56 -0700368 // CHECKs that this message was sent.
Brian Silverman9dd793b2020-01-31 23:52:21 -0800369 void CheckSent() {
370 CHECK(!allocator_->is_allocated()) << ": Message was not sent yet";
371 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700372
Brian Silverman341b57e2020-06-23 16:23:18 -0700373 // Detaches a buffer, for later use calling Sender::Send directly.
374 //
375 // Note that the underlying memory remains with the Sender, so creating
376 // another Builder before destroying the FlatbufferDetachedBuffer will fail.
377 FlatbufferDetachedBuffer<T> Detach(flatbuffers::Offset<T> offset) {
378 fbb_.Finish(offset);
379 return fbb_.Release();
380 }
381
Alex Perrycb7da4b2019-08-28 19:35:56 -0700382 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700383 flatbuffers::FlatBufferBuilder fbb_;
Austin Schuh1af273d2020-03-07 20:11:34 -0800384 ChannelPreallocatedAllocator *allocator_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700385 RawSender *sender_;
386 };
387
388 // Constructs an above builder.
Brian Silverman9dd793b2020-01-31 23:52:21 -0800389 //
390 // Only a single one of these may be "alive" for this object at any point in
391 // time. After calling Send on the result, it is no longer "alive". This means
392 // that you must manually reset a variable holding the return value (by
393 // assigning a default-constructed Builder to it) before calling this method
394 // again to overwrite the value in the variable.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700395 Builder MakeBuilder();
396
Austin Schuha28cbc32019-12-27 16:28:04 -0800397 // Sends a prebuilt flatbuffer.
milind1f1dca32021-07-03 13:50:07 -0700398 RawSender::Error Send(const NonSizePrefixedFlatbuffer<T> &flatbuffer);
Austin Schuha28cbc32019-12-27 16:28:04 -0800399
Brian Silverman341b57e2020-06-23 16:23:18 -0700400 // Sends a prebuilt flatbuffer which was detached from a Builder created via
401 // MakeBuilder() on this object.
milind1f1dca32021-07-03 13:50:07 -0700402 RawSender::Error SendDetached(FlatbufferDetachedBuffer<T> detached);
403
404 // Equivalent to RawSender::CheckOk
405 void CheckOk(const RawSender::Error err) { sender_->CheckOk(err); };
Brian Silverman341b57e2020-06-23 16:23:18 -0700406
Austin Schuh39788ff2019-12-01 18:22:57 -0800407 // Returns the name of the underlying queue.
Austin Schuh1e869472019-12-01 13:36:10 -0800408 const Channel *channel() const { return sender_->channel(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700409
Austin Schuha0c41ba2020-09-10 22:59:14 -0700410 operator bool() const { return sender_ ? true : false; }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800411
Austin Schuh7bc59052020-02-16 23:48:33 -0800412 // Returns the time_points that the last message was sent at.
413 aos::monotonic_clock::time_point monotonic_sent_time() const {
414 return sender_->monotonic_sent_time();
415 }
416 aos::realtime_clock::time_point realtime_sent_time() const {
417 return sender_->realtime_sent_time();
418 }
419 // Returns the queue index that this was sent with.
420 uint32_t sent_queue_index() const { return sender_->sent_queue_index(); }
421
Brian Silverman4f4e0612020-08-12 19:54:41 -0700422 // Returns the buffer index which MakeBuilder() will expose access to. This is
423 // the buffer the caller can fill out.
424 int buffer_index() const { return sender_->buffer_index(); }
425
Alex Perrycb7da4b2019-08-28 19:35:56 -0700426 private:
427 friend class EventLoop;
428 Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {}
429 std::unique_ptr<RawSender> sender_;
430};
431
milind1f1dca32021-07-03 13:50:07 -0700432// Class for keeping a count of send failures on a certain channel
433class SendFailureCounter {
434 public:
435 inline void Count(const RawSender::Error err) {
436 failures_ += static_cast<size_t>(err != RawSender::Error::kOk);
437 just_failed_ = (err != RawSender::Error::kOk);
438 }
439
440 inline size_t failures() const { return failures_; }
441 inline bool just_failed() const { return just_failed_; }
442
443 private:
444 size_t failures_ = 0;
445 bool just_failed_ = false;
446};
447
Brian Silverman4f4e0612020-08-12 19:54:41 -0700448// Interface for timers.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700449class TimerHandler {
450 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800451 virtual ~TimerHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700452
453 // Timer should sleep until base, base + offset, base + offset * 2, ...
454 // If repeat_offset isn't set, the timer only expires once.
455 virtual void Setup(monotonic_clock::time_point base,
456 monotonic_clock::duration repeat_offset =
457 ::aos::monotonic_clock::zero()) = 0;
458
459 // Stop future calls to callback().
460 virtual void Disable() = 0;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800461
462 // Sets and gets the name of the timer. Set this if you want a descriptive
463 // name in the timing report.
464 void set_name(std::string_view name) { name_ = std::string(name); }
465 const std::string_view name() const { return name_; }
466
Austin Schuh39788ff2019-12-01 18:22:57 -0800467 protected:
468 TimerHandler(EventLoop *event_loop, std::function<void()> fn);
469
Austin Schuhcde39fd2020-02-22 20:58:24 -0800470 monotonic_clock::time_point Call(
471 std::function<monotonic_clock::time_point()> get_time,
472 monotonic_clock::time_point event_time);
Austin Schuh39788ff2019-12-01 18:22:57 -0800473
Austin Schuh1540c2f2019-11-29 21:59:29 -0800474 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800475 friend class EventLoop;
476
477 EventLoop *event_loop_;
478 // The function to call when Call is called.
479 std::function<void()> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800480 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800481
482 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500483 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700484};
485
486// Interface for phased loops. They are built on timers.
487class PhasedLoopHandler {
488 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800489 virtual ~PhasedLoopHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700490
491 // Sets the interval and offset. Any changes to interval and offset only take
492 // effect when the handler finishes running.
Austin Schuh39788ff2019-12-01 18:22:57 -0800493 void set_interval_and_offset(const monotonic_clock::duration interval,
494 const monotonic_clock::duration offset) {
495 phased_loop_.set_interval_and_offset(interval, offset);
496 }
Austin Schuh1540c2f2019-11-29 21:59:29 -0800497
498 // Sets and gets the name of the timer. Set this if you want a descriptive
499 // name in the timing report.
500 void set_name(std::string_view name) { name_ = std::string(name); }
501 const std::string_view name() const { return name_; }
502
Austin Schuh39788ff2019-12-01 18:22:57 -0800503 protected:
504 void Call(std::function<monotonic_clock::time_point()> get_time,
505 std::function<void(monotonic_clock::time_point)> schedule);
506
507 PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn,
508 const monotonic_clock::duration interval,
509 const monotonic_clock::duration offset);
510
Austin Schuh1540c2f2019-11-29 21:59:29 -0800511 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800512 friend class EventLoop;
513
514 void Reschedule(std::function<void(monotonic_clock::time_point)> schedule,
515 monotonic_clock::time_point monotonic_now) {
516 cycles_elapsed_ += phased_loop_.Iterate(monotonic_now);
517 schedule(phased_loop_.sleep_time());
518 }
519
520 virtual void Schedule(monotonic_clock::time_point sleep_time) = 0;
521
522 EventLoop *event_loop_;
523 std::function<void(int)> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800524 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800525 time::PhasedLoop phased_loop_;
526
527 int cycles_elapsed_ = 0;
528
529 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500530 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700531};
532
Austin Schuh3054f5f2021-07-21 15:38:01 -0700533// Note, it is supported to create only:
534// multiple fetchers, and (one sender or one watcher) per <name, type>
535// tuple.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700536class EventLoop {
537 public:
Austin Schuh3054f5f2021-07-21 15:38:01 -0700538 // Holds configuration by reference for the lifetime of this object. It may
539 // never be mutated externally in any way.
Austin Schuh83c7f702021-01-19 22:36:29 -0800540 EventLoop(const Configuration *configuration);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700541
Austin Schuh39788ff2019-12-01 18:22:57 -0800542 virtual ~EventLoop();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700543
544 // Current time.
Stephan Pleines559fa6c2022-01-06 17:23:51 -0800545 virtual monotonic_clock::time_point monotonic_now() const = 0;
546 virtual realtime_clock::time_point realtime_now() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700547
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700548 template <typename T>
Austin Schuha0654152021-02-21 21:38:24 -0800549 const Channel *GetChannel(const std::string_view channel_name) {
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800550 return configuration::GetChannel(configuration(), channel_name,
Austin Schuh0de30f32020-12-06 12:44:28 -0800551 T::GetFullyQualifiedName(), name(), node(),
Austin Schuha0654152021-02-21 21:38:24 -0800552 true);
553 }
milind1f1dca32021-07-03 13:50:07 -0700554 // Returns true if the channel exists in the configuration.
Austin Schuha0654152021-02-21 21:38:24 -0800555 template <typename T>
556 bool HasChannel(const std::string_view channel_name) {
557 return GetChannel<T>(channel_name) != nullptr;
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700558 }
559
Brian Silverman631b6262021-11-10 12:25:08 -0800560 // Like MakeFetcher, but returns an invalid fetcher if the given channel is
561 // not readable on this node or does not exist.
562 template <typename T>
563 Fetcher<T> TryMakeFetcher(const std::string_view channel_name) {
564 const Channel *const channel = GetChannel<T>(channel_name);
565 if (channel == nullptr) {
566 return Fetcher<T>();
567 }
568
569 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
570 return Fetcher<T>();
571 }
572
573 return Fetcher<T>(MakeRawFetcher(channel));
574 }
575
Alex Perrycb7da4b2019-08-28 19:35:56 -0700576 // Makes a class that will always fetch the most recent value
577 // sent to the provided channel.
578 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800579 Fetcher<T> MakeFetcher(const std::string_view channel_name) {
Brian Silverman631b6262021-11-10 12:25:08 -0800580 CHECK(HasChannel<T>(channel_name))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700581 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
582 << T::GetFullyQualifiedName() << "\" } not found in config.";
583
Brian Silverman631b6262021-11-10 12:25:08 -0800584 Fetcher<T> result = TryMakeFetcher<T>(channel_name);
585 if (!result.valid()) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800586 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
587 << "\", \"type\": \"" << T::GetFullyQualifiedName()
588 << "\" } is not able to be fetched on this node. Check your "
589 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800590 }
591
Brian Silverman631b6262021-11-10 12:25:08 -0800592 return result;
593 }
594
595 // Like MakeSender, but returns an invalid sender if the given channel is
James Kuszmaulb8887592021-12-09 14:54:50 -0800596 // not sendable on this node or does not exist.
Brian Silverman631b6262021-11-10 12:25:08 -0800597 template <typename T>
598 Sender<T> TryMakeSender(const std::string_view channel_name) {
599 const Channel *channel = GetChannel<T>(channel_name);
600 if (channel == nullptr) {
601 return Sender<T>();
602 }
603
604 if (!configuration::ChannelIsSendableOnNode(channel, node())) {
605 return Sender<T>();
606 }
607
608 return Sender<T>(MakeRawSender(channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700609 }
610
611 // Makes class that allows constructing and sending messages to
612 // the provided channel.
613 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800614 Sender<T> MakeSender(const std::string_view channel_name) {
Brian Silverman631b6262021-11-10 12:25:08 -0800615 CHECK(HasChannel<T>(channel_name))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700616 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
Austin Schuh28fedcb2020-02-08 15:59:58 -0800617 << T::GetFullyQualifiedName() << "\" } not found in config for "
Austin Schuh2f8fd752020-09-01 22:38:28 -0700618 << name()
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800619 << (configuration::MultiNode(configuration())
Austin Schuh2f8fd752020-09-01 22:38:28 -0700620 ? absl::StrCat(" on node ", node()->name()->string_view())
621 : ".");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700622
Brian Silverman631b6262021-11-10 12:25:08 -0800623 Sender<T> result = TryMakeSender<T>(channel_name);
624 if (!result) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800625 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
626 << "\", \"type\": \"" << T::GetFullyQualifiedName()
627 << "\" } is not able to be sent on this node. Check your "
628 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800629 }
630
Brian Silverman631b6262021-11-10 12:25:08 -0800631 return result;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700632 }
633
634 // This will watch messages sent to the provided channel.
635 //
Brian Silverman454bc112020-03-05 14:21:25 -0800636 // w must have a non-polymorphic operator() (aka it can only be called with a
637 // single set of arguments; no overloading or templates). It must be callable
638 // with this signature:
639 // void(const MessageType &);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700640 //
Brian Silverman454bc112020-03-05 14:21:25 -0800641 // Lambdas are a common form for w. A std::function will work too.
642 //
643 // Note that bind expressions have polymorphic call operators, so they are not
644 // allowed.
645 //
646 // We template Watch as a whole instead of using std::function<void(const T
647 // &)> to allow deducing MessageType from lambdas and other things which are
648 // implicitly convertible to std::function, but not actually std::function
649 // instantiations. Template deduction guides might allow solving this
650 // differently in newer versions of C++, but those have their own corner
651 // cases.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700652 template <typename Watch>
Brian Silverman454bc112020-03-05 14:21:25 -0800653 void MakeWatcher(const std::string_view channel_name, Watch &&w);
654
655 // Like MakeWatcher, but doesn't have access to the message data. This may be
656 // implemented to use less resources than an equivalent MakeWatcher.
657 //
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800658 // The function will still have access to context(), although that will have
659 // its data field set to nullptr.
Brian Silverman454bc112020-03-05 14:21:25 -0800660 template <typename MessageType>
661 void MakeNoArgWatcher(const std::string_view channel_name,
662 std::function<void()> w);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700663
664 // The passed in function will be called when the event loop starts.
665 // Use this to run code once the thread goes into "real-time-mode",
666 virtual void OnRun(::std::function<void()> on_run) = 0;
667
Austin Schuh217a9782019-12-21 23:02:50 -0800668 // Gets the name of the event loop. This is the application name.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800669 virtual const std::string_view name() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700670
Austin Schuh217a9782019-12-21 23:02:50 -0800671 // Returns the node that this event loop is running on. Returns nullptr if we
672 // are running in single-node mode.
673 virtual const Node *node() const = 0;
674
Alex Perrycb7da4b2019-08-28 19:35:56 -0700675 // Creates a timer that executes callback when the timer expires
676 // Returns a TimerHandle for configuration of the timer
milind-u61227f22021-08-29 15:58:33 -0700677 // TODO(milind): callback should take the number of cycles elapsed as a
678 // parameter.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700679 virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0;
680
681 // Creates a timer that executes callback periodically at the specified
682 // interval and offset. Returns a PhasedLoopHandler for interacting with the
683 // timer.
684 virtual PhasedLoopHandler *AddPhasedLoop(
685 ::std::function<void(int)> callback,
686 const monotonic_clock::duration interval,
687 const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0;
688
Austin Schuh217a9782019-12-21 23:02:50 -0800689 // TODO(austin): OnExit for cleanup.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700690
Austin Schuh3054f5f2021-07-21 15:38:01 -0700691 // May be safely called from any thread.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700692 bool is_running() const { return is_running_.load(); }
693
694 // Sets the scheduler priority to run the event loop at. This may not be
695 // called after we go into "real-time-mode".
696 virtual void SetRuntimeRealtimePriority(int priority) = 0;
Austin Schuh39788ff2019-12-01 18:22:57 -0800697 virtual int priority() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700698
Brian Silverman6a54ff32020-04-28 16:41:39 -0700699 // Sets the scheduler affinity to run the event loop with. This may only be
700 // called before Run().
701 virtual void SetRuntimeAffinity(const cpu_set_t &cpuset) = 0;
702
Austin Schuh217a9782019-12-21 23:02:50 -0800703 // Fetches new messages from the provided channel (path, type).
704 //
705 // Note: this channel must be a member of the exact configuration object this
706 // was built with.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700707 virtual std::unique_ptr<RawFetcher> MakeRawFetcher(
708 const Channel *channel) = 0;
709
Austin Schuh217a9782019-12-21 23:02:50 -0800710 // Watches channel (name, type) for new messages.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700711 virtual void MakeRawWatcher(
712 const Channel *channel,
713 std::function<void(const Context &context, const void *message)>
714 watcher) = 0;
715
Brian Silverman454bc112020-03-05 14:21:25 -0800716 // Watches channel (name, type) for new messages, without needing to extract
717 // the message contents. Default implementation simply re-uses MakeRawWatcher.
718 virtual void MakeRawNoArgWatcher(
719 const Channel *channel,
720 std::function<void(const Context &context)> watcher) {
721 MakeRawWatcher(channel, [watcher](const Context &context, const void *) {
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800722 Context new_context = context;
723 new_context.data = nullptr;
Brian Silverman4f4e0612020-08-12 19:54:41 -0700724 new_context.buffer_index = -1;
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800725 watcher(new_context);
Brian Silverman454bc112020-03-05 14:21:25 -0800726 });
727 }
728
Austin Schuh217a9782019-12-21 23:02:50 -0800729 // Creates a raw sender for the provided channel. This is used for reflection
730 // based sending.
731 // Note: this ignores any node constraints. Ignore at your own peril.
732 virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0;
733
Austin Schuh6231cc32019-12-07 13:06:15 -0800734 // Returns the context for the current callback.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700735 const Context &context() const { return context_; }
736
737 // Returns the configuration that this event loop was built with.
738 const Configuration *configuration() const { return configuration_; }
739
Austin Schuh39788ff2019-12-01 18:22:57 -0800740 // Prevents the event loop from sending a timing report.
Brian Silvermanbf889922021-11-10 12:41:57 -0800741 void SkipTimingReport();
Austin Schuh39788ff2019-12-01 18:22:57 -0800742
Brian Silverman4f4e0612020-08-12 19:54:41 -0700743 // Prevents AOS_LOG being sent to message on /aos.
Tyler Chatow67ddb032020-01-12 14:30:04 -0800744 void SkipAosLog() { skip_logger_ = true; }
745
Brian Silverman4f4e0612020-08-12 19:54:41 -0700746 // Returns the number of buffers for this channel. This corresponds with the
747 // range of Context::buffer_index values for this channel.
748 virtual int NumberBuffers(const Channel *channel) = 0;
749
Austin Schuh20ac95d2020-12-05 17:24:19 -0800750 // Returns the boot UUID.
Austin Schuh83c7f702021-01-19 22:36:29 -0800751 virtual const UUID &boot_uuid() const = 0;
Austin Schuh20ac95d2020-12-05 17:24:19 -0800752
Alex Perrycb7da4b2019-08-28 19:35:56 -0700753 protected:
Austin Schuh217a9782019-12-21 23:02:50 -0800754 // Sets the name of the event loop. This is the application name.
755 virtual void set_name(const std::string_view name) = 0;
756
Alex Perrycb7da4b2019-08-28 19:35:56 -0700757 void set_is_running(bool value) { is_running_.store(value); }
758
Austin Schuh39788ff2019-12-01 18:22:57 -0800759 // Validates that channel exists inside configuration_ and finds its index.
760 int ChannelIndex(const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700761
Brian Silverman5120afb2020-01-31 17:44:35 -0800762 // Returns the state for the watcher on the corresponding channel. This
763 // watcher must exist before calling this.
764 WatcherState *GetWatcherState(const Channel *channel);
765
Brian Silverman6d2b3592020-06-18 14:40:15 -0700766 // Returns a Sender's protected RawSender.
Brian Silverman5120afb2020-01-31 17:44:35 -0800767 template <typename T>
768 static RawSender *GetRawSender(aos::Sender<T> *sender) {
769 return sender->sender_.get();
770 }
771
Brian Silverman6d2b3592020-06-18 14:40:15 -0700772 // Returns a Fetcher's protected RawFetcher.
773 template <typename T>
774 static RawFetcher *GetRawFetcher(aos::Fetcher<T> *fetcher) {
775 return fetcher->fetcher_.get();
776 }
777
Austin Schuh6231cc32019-12-07 13:06:15 -0800778 // Context available for watchers, timers, and phased loops.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700779 Context context_;
780
Austin Schuh39788ff2019-12-01 18:22:57 -0800781 friend class RawSender;
782 friend class TimerHandler;
783 friend class RawFetcher;
784 friend class PhasedLoopHandler;
785 friend class WatcherState;
786
787 // Methods used to implement timing reports.
788 void NewSender(RawSender *sender);
789 void DeleteSender(RawSender *sender);
790 TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer);
791 PhasedLoopHandler *NewPhasedLoop(
792 std::unique_ptr<PhasedLoopHandler> phased_loop);
793 void NewFetcher(RawFetcher *fetcher);
794 void DeleteFetcher(RawFetcher *fetcher);
795 WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher);
796
Brian Silverman0fc69932020-01-24 21:54:02 -0800797 // Tracks that we have a (single) watcher on the given channel.
798 void TakeWatcher(const Channel *channel);
799 // Tracks that we have at least one sender on the given channel.
800 void TakeSender(const Channel *channel);
801
Austin Schuh39788ff2019-12-01 18:22:57 -0800802 std::vector<RawSender *> senders_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800803 std::vector<RawFetcher *> fetchers_;
804
Austin Schuh39788ff2019-12-01 18:22:57 -0800805 std::vector<std::unique_ptr<TimerHandler>> timers_;
806 std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800807 std::vector<std::unique_ptr<WatcherState>> watchers_;
808
Brian Silvermance418d02021-11-03 11:25:52 -0700809 // Does nothing if timing reports are disabled.
Austin Schuh39788ff2019-12-01 18:22:57 -0800810 void SendTimingReport();
Brian Silvermance418d02021-11-03 11:25:52 -0700811
Austin Schuh39788ff2019-12-01 18:22:57 -0800812 void UpdateTimingReport();
813 void MaybeScheduleTimingReports();
814
815 std::unique_ptr<RawSender> timing_report_sender_;
816
Austin Schuh7d87b672019-12-01 20:23:49 -0800817 // Tracks which event sources (timers and watchers) have data, and which
818 // don't. Added events may not change their event_time().
819 // TODO(austin): Test case 1: timer triggers at t1, handler takes until after
820 // t2 to run, t2 should then be picked up without a context switch.
821 void AddEvent(EventLoopEvent *event);
822 void RemoveEvent(EventLoopEvent *event);
823 size_t EventCount() const { return events_.size(); }
824 EventLoopEvent *PopEvent();
825 EventLoopEvent *PeekEvent() { return events_.front(); }
826 void ReserveEvents();
827
828 std::vector<EventLoopEvent *> events_;
Brian Silvermanbd405c02020-06-23 16:25:23 -0700829 size_t event_generation_ = 1;
Austin Schuh7d87b672019-12-01 20:23:49 -0800830
Tyler Chatow67ddb032020-01-12 14:30:04 -0800831 // If true, don't send AOS_LOG to /aos
832 bool skip_logger_ = false;
833
Austin Schuha9012be2021-07-21 15:19:11 -0700834 // Sets context_ for a timed event which is supposed to happen at the provided
835 // time.
836 void SetTimerContext(monotonic_clock::time_point monotonic_event_time);
837
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800838 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800839 virtual pid_t GetTid() = 0;
840
841 FlatbufferDetachedBuffer<timing::Report> timing_report_;
842
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800843 ::std::atomic<bool> is_running_{false};
844
Alex Perrycb7da4b2019-08-28 19:35:56 -0700845 const Configuration *configuration_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800846
847 // If true, don't send out timing reports.
848 bool skip_timing_report_ = false;
Brian Silverman0fc69932020-01-24 21:54:02 -0800849
milind1f1dca32021-07-03 13:50:07 -0700850 SendFailureCounter timing_report_failure_counter_;
851
Brian Silverman0fc69932020-01-24 21:54:02 -0800852 absl::btree_set<const Channel *> taken_watchers_, taken_senders_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700853};
854
855} // namespace aos
856
Austin Schuhb8075812020-10-19 09:36:49 -0700857#include "aos/events/event_loop_tmpl.h" // IWYU pragma: export
Alex Perrycb7da4b2019-08-28 19:35:56 -0700858
859#endif // AOS_EVENTS_EVENT_LOOP_H