blob: e810e3fe3ab98620286199a307c9d36452f311c1 [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 Schuhe33c08d2022-02-03 18:15:21 -0800410 // TODO(austin): Deprecate the operator bool.
Austin Schuha0c41ba2020-09-10 22:59:14 -0700411 operator bool() const { return sender_ ? true : false; }
Austin Schuhe33c08d2022-02-03 18:15:21 -0800412 bool valid() const { return static_cast<bool>(sender_); }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800413
Austin Schuh7bc59052020-02-16 23:48:33 -0800414 // Returns the time_points that the last message was sent at.
415 aos::monotonic_clock::time_point monotonic_sent_time() const {
416 return sender_->monotonic_sent_time();
417 }
418 aos::realtime_clock::time_point realtime_sent_time() const {
419 return sender_->realtime_sent_time();
420 }
421 // Returns the queue index that this was sent with.
422 uint32_t sent_queue_index() const { return sender_->sent_queue_index(); }
423
Brian Silverman4f4e0612020-08-12 19:54:41 -0700424 // Returns the buffer index which MakeBuilder() will expose access to. This is
425 // the buffer the caller can fill out.
426 int buffer_index() const { return sender_->buffer_index(); }
427
Alex Perrycb7da4b2019-08-28 19:35:56 -0700428 private:
429 friend class EventLoop;
430 Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {}
431 std::unique_ptr<RawSender> sender_;
432};
433
milind1f1dca32021-07-03 13:50:07 -0700434// Class for keeping a count of send failures on a certain channel
435class SendFailureCounter {
436 public:
437 inline void Count(const RawSender::Error err) {
438 failures_ += static_cast<size_t>(err != RawSender::Error::kOk);
439 just_failed_ = (err != RawSender::Error::kOk);
440 }
441
442 inline size_t failures() const { return failures_; }
443 inline bool just_failed() const { return just_failed_; }
444
445 private:
446 size_t failures_ = 0;
447 bool just_failed_ = false;
448};
449
Brian Silverman4f4e0612020-08-12 19:54:41 -0700450// Interface for timers.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700451class TimerHandler {
452 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800453 virtual ~TimerHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700454
455 // Timer should sleep until base, base + offset, base + offset * 2, ...
456 // If repeat_offset isn't set, the timer only expires once.
457 virtual void Setup(monotonic_clock::time_point base,
458 monotonic_clock::duration repeat_offset =
459 ::aos::monotonic_clock::zero()) = 0;
460
461 // Stop future calls to callback().
462 virtual void Disable() = 0;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800463
464 // Sets and gets the name of the timer. Set this if you want a descriptive
465 // name in the timing report.
466 void set_name(std::string_view name) { name_ = std::string(name); }
467 const std::string_view name() const { return name_; }
468
Austin Schuh39788ff2019-12-01 18:22:57 -0800469 protected:
470 TimerHandler(EventLoop *event_loop, std::function<void()> fn);
471
Austin Schuhcde39fd2020-02-22 20:58:24 -0800472 monotonic_clock::time_point Call(
473 std::function<monotonic_clock::time_point()> get_time,
474 monotonic_clock::time_point event_time);
Austin Schuh39788ff2019-12-01 18:22:57 -0800475
Austin Schuh1540c2f2019-11-29 21:59:29 -0800476 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800477 friend class EventLoop;
478
479 EventLoop *event_loop_;
480 // The function to call when Call is called.
481 std::function<void()> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800482 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800483
484 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500485 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700486};
487
488// Interface for phased loops. They are built on timers.
489class PhasedLoopHandler {
490 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800491 virtual ~PhasedLoopHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700492
493 // Sets the interval and offset. Any changes to interval and offset only take
494 // effect when the handler finishes running.
Austin Schuh39788ff2019-12-01 18:22:57 -0800495 void set_interval_and_offset(const monotonic_clock::duration interval,
496 const monotonic_clock::duration offset) {
497 phased_loop_.set_interval_and_offset(interval, offset);
498 }
Austin Schuh1540c2f2019-11-29 21:59:29 -0800499
500 // Sets and gets the name of the timer. Set this if you want a descriptive
501 // name in the timing report.
502 void set_name(std::string_view name) { name_ = std::string(name); }
503 const std::string_view name() const { return name_; }
504
Austin Schuh39788ff2019-12-01 18:22:57 -0800505 protected:
506 void Call(std::function<monotonic_clock::time_point()> get_time,
507 std::function<void(monotonic_clock::time_point)> schedule);
508
509 PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn,
510 const monotonic_clock::duration interval,
511 const monotonic_clock::duration offset);
512
Austin Schuh1540c2f2019-11-29 21:59:29 -0800513 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800514 friend class EventLoop;
515
516 void Reschedule(std::function<void(monotonic_clock::time_point)> schedule,
517 monotonic_clock::time_point monotonic_now) {
518 cycles_elapsed_ += phased_loop_.Iterate(monotonic_now);
519 schedule(phased_loop_.sleep_time());
520 }
521
522 virtual void Schedule(monotonic_clock::time_point sleep_time) = 0;
523
524 EventLoop *event_loop_;
525 std::function<void(int)> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800526 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800527 time::PhasedLoop phased_loop_;
528
529 int cycles_elapsed_ = 0;
530
531 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500532 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700533};
534
Austin Schuh3054f5f2021-07-21 15:38:01 -0700535// Note, it is supported to create only:
536// multiple fetchers, and (one sender or one watcher) per <name, type>
537// tuple.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700538class EventLoop {
539 public:
Austin Schuh3054f5f2021-07-21 15:38:01 -0700540 // Holds configuration by reference for the lifetime of this object. It may
541 // never be mutated externally in any way.
Austin Schuh83c7f702021-01-19 22:36:29 -0800542 EventLoop(const Configuration *configuration);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700543
Austin Schuh39788ff2019-12-01 18:22:57 -0800544 virtual ~EventLoop();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700545
546 // Current time.
Stephan Pleines559fa6c2022-01-06 17:23:51 -0800547 virtual monotonic_clock::time_point monotonic_now() const = 0;
548 virtual realtime_clock::time_point realtime_now() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700549
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700550 template <typename T>
Austin Schuha0654152021-02-21 21:38:24 -0800551 const Channel *GetChannel(const std::string_view channel_name) {
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800552 return configuration::GetChannel(configuration(), channel_name,
Austin Schuh0de30f32020-12-06 12:44:28 -0800553 T::GetFullyQualifiedName(), name(), node(),
Austin Schuha0654152021-02-21 21:38:24 -0800554 true);
555 }
milind1f1dca32021-07-03 13:50:07 -0700556 // Returns true if the channel exists in the configuration.
Austin Schuha0654152021-02-21 21:38:24 -0800557 template <typename T>
558 bool HasChannel(const std::string_view channel_name) {
559 return GetChannel<T>(channel_name) != nullptr;
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700560 }
561
Brian Silverman631b6262021-11-10 12:25:08 -0800562 // Like MakeFetcher, but returns an invalid fetcher if the given channel is
563 // not readable on this node or does not exist.
564 template <typename T>
565 Fetcher<T> TryMakeFetcher(const std::string_view channel_name) {
566 const Channel *const channel = GetChannel<T>(channel_name);
567 if (channel == nullptr) {
568 return Fetcher<T>();
569 }
570
571 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
572 return Fetcher<T>();
573 }
574
575 return Fetcher<T>(MakeRawFetcher(channel));
576 }
577
Alex Perrycb7da4b2019-08-28 19:35:56 -0700578 // Makes a class that will always fetch the most recent value
579 // sent to the provided channel.
580 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800581 Fetcher<T> MakeFetcher(const std::string_view channel_name) {
Brian Silverman631b6262021-11-10 12:25:08 -0800582 CHECK(HasChannel<T>(channel_name))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700583 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
584 << T::GetFullyQualifiedName() << "\" } not found in config.";
585
Brian Silverman631b6262021-11-10 12:25:08 -0800586 Fetcher<T> result = TryMakeFetcher<T>(channel_name);
587 if (!result.valid()) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800588 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
589 << "\", \"type\": \"" << T::GetFullyQualifiedName()
590 << "\" } is not able to be fetched on this node. Check your "
591 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800592 }
593
Brian Silverman631b6262021-11-10 12:25:08 -0800594 return result;
595 }
596
597 // Like MakeSender, but returns an invalid sender if the given channel is
James Kuszmaulb8887592021-12-09 14:54:50 -0800598 // not sendable on this node or does not exist.
Brian Silverman631b6262021-11-10 12:25:08 -0800599 template <typename T>
600 Sender<T> TryMakeSender(const std::string_view channel_name) {
601 const Channel *channel = GetChannel<T>(channel_name);
602 if (channel == nullptr) {
603 return Sender<T>();
604 }
605
606 if (!configuration::ChannelIsSendableOnNode(channel, node())) {
607 return Sender<T>();
608 }
609
610 return Sender<T>(MakeRawSender(channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700611 }
612
613 // Makes class that allows constructing and sending messages to
614 // the provided channel.
615 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800616 Sender<T> MakeSender(const std::string_view channel_name) {
Brian Silverman631b6262021-11-10 12:25:08 -0800617 CHECK(HasChannel<T>(channel_name))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700618 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
Austin Schuh28fedcb2020-02-08 15:59:58 -0800619 << T::GetFullyQualifiedName() << "\" } not found in config for "
Austin Schuh2f8fd752020-09-01 22:38:28 -0700620 << name()
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800621 << (configuration::MultiNode(configuration())
Austin Schuh2f8fd752020-09-01 22:38:28 -0700622 ? absl::StrCat(" on node ", node()->name()->string_view())
623 : ".");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700624
Brian Silverman631b6262021-11-10 12:25:08 -0800625 Sender<T> result = TryMakeSender<T>(channel_name);
626 if (!result) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800627 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
628 << "\", \"type\": \"" << T::GetFullyQualifiedName()
629 << "\" } is not able to be sent on this node. Check your "
630 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800631 }
632
Brian Silverman631b6262021-11-10 12:25:08 -0800633 return result;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700634 }
635
636 // This will watch messages sent to the provided channel.
637 //
Brian Silverman454bc112020-03-05 14:21:25 -0800638 // w must have a non-polymorphic operator() (aka it can only be called with a
639 // single set of arguments; no overloading or templates). It must be callable
640 // with this signature:
641 // void(const MessageType &);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700642 //
Brian Silverman454bc112020-03-05 14:21:25 -0800643 // Lambdas are a common form for w. A std::function will work too.
644 //
645 // Note that bind expressions have polymorphic call operators, so they are not
646 // allowed.
647 //
648 // We template Watch as a whole instead of using std::function<void(const T
649 // &)> to allow deducing MessageType from lambdas and other things which are
650 // implicitly convertible to std::function, but not actually std::function
651 // instantiations. Template deduction guides might allow solving this
652 // differently in newer versions of C++, but those have their own corner
653 // cases.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700654 template <typename Watch>
Brian Silverman454bc112020-03-05 14:21:25 -0800655 void MakeWatcher(const std::string_view channel_name, Watch &&w);
656
657 // Like MakeWatcher, but doesn't have access to the message data. This may be
658 // implemented to use less resources than an equivalent MakeWatcher.
659 //
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800660 // The function will still have access to context(), although that will have
661 // its data field set to nullptr.
Brian Silverman454bc112020-03-05 14:21:25 -0800662 template <typename MessageType>
663 void MakeNoArgWatcher(const std::string_view channel_name,
664 std::function<void()> w);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700665
666 // The passed in function will be called when the event loop starts.
667 // Use this to run code once the thread goes into "real-time-mode",
668 virtual void OnRun(::std::function<void()> on_run) = 0;
669
Austin Schuh217a9782019-12-21 23:02:50 -0800670 // Gets the name of the event loop. This is the application name.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800671 virtual const std::string_view name() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700672
Austin Schuh217a9782019-12-21 23:02:50 -0800673 // Returns the node that this event loop is running on. Returns nullptr if we
674 // are running in single-node mode.
675 virtual const Node *node() const = 0;
676
Alex Perrycb7da4b2019-08-28 19:35:56 -0700677 // Creates a timer that executes callback when the timer expires
678 // Returns a TimerHandle for configuration of the timer
milind-u61227f22021-08-29 15:58:33 -0700679 // TODO(milind): callback should take the number of cycles elapsed as a
680 // parameter.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700681 virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0;
682
683 // Creates a timer that executes callback periodically at the specified
684 // interval and offset. Returns a PhasedLoopHandler for interacting with the
685 // timer.
686 virtual PhasedLoopHandler *AddPhasedLoop(
687 ::std::function<void(int)> callback,
688 const monotonic_clock::duration interval,
689 const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0;
690
Austin Schuh217a9782019-12-21 23:02:50 -0800691 // TODO(austin): OnExit for cleanup.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700692
Austin Schuh3054f5f2021-07-21 15:38:01 -0700693 // May be safely called from any thread.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700694 bool is_running() const { return is_running_.load(); }
695
696 // Sets the scheduler priority to run the event loop at. This may not be
697 // called after we go into "real-time-mode".
698 virtual void SetRuntimeRealtimePriority(int priority) = 0;
Austin Schuh39788ff2019-12-01 18:22:57 -0800699 virtual int priority() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700700
Brian Silverman6a54ff32020-04-28 16:41:39 -0700701 // Sets the scheduler affinity to run the event loop with. This may only be
702 // called before Run().
703 virtual void SetRuntimeAffinity(const cpu_set_t &cpuset) = 0;
704
Austin Schuh217a9782019-12-21 23:02:50 -0800705 // Fetches new messages from the provided channel (path, type).
706 //
707 // Note: this channel must be a member of the exact configuration object this
708 // was built with.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700709 virtual std::unique_ptr<RawFetcher> MakeRawFetcher(
710 const Channel *channel) = 0;
711
Austin Schuh217a9782019-12-21 23:02:50 -0800712 // Watches channel (name, type) for new messages.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700713 virtual void MakeRawWatcher(
714 const Channel *channel,
715 std::function<void(const Context &context, const void *message)>
716 watcher) = 0;
717
Brian Silverman454bc112020-03-05 14:21:25 -0800718 // Watches channel (name, type) for new messages, without needing to extract
719 // the message contents. Default implementation simply re-uses MakeRawWatcher.
720 virtual void MakeRawNoArgWatcher(
721 const Channel *channel,
722 std::function<void(const Context &context)> watcher) {
723 MakeRawWatcher(channel, [watcher](const Context &context, const void *) {
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800724 Context new_context = context;
725 new_context.data = nullptr;
Brian Silverman4f4e0612020-08-12 19:54:41 -0700726 new_context.buffer_index = -1;
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800727 watcher(new_context);
Brian Silverman454bc112020-03-05 14:21:25 -0800728 });
729 }
730
Austin Schuh217a9782019-12-21 23:02:50 -0800731 // Creates a raw sender for the provided channel. This is used for reflection
732 // based sending.
733 // Note: this ignores any node constraints. Ignore at your own peril.
734 virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0;
735
Austin Schuh6231cc32019-12-07 13:06:15 -0800736 // Returns the context for the current callback.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700737 const Context &context() const { return context_; }
738
739 // Returns the configuration that this event loop was built with.
740 const Configuration *configuration() const { return configuration_; }
741
Austin Schuh39788ff2019-12-01 18:22:57 -0800742 // Prevents the event loop from sending a timing report.
Brian Silvermanbf889922021-11-10 12:41:57 -0800743 void SkipTimingReport();
Austin Schuh39788ff2019-12-01 18:22:57 -0800744
Brian Silverman4f4e0612020-08-12 19:54:41 -0700745 // Prevents AOS_LOG being sent to message on /aos.
Tyler Chatow67ddb032020-01-12 14:30:04 -0800746 void SkipAosLog() { skip_logger_ = true; }
747
Brian Silverman4f4e0612020-08-12 19:54:41 -0700748 // Returns the number of buffers for this channel. This corresponds with the
749 // range of Context::buffer_index values for this channel.
750 virtual int NumberBuffers(const Channel *channel) = 0;
751
Austin Schuh20ac95d2020-12-05 17:24:19 -0800752 // Returns the boot UUID.
Austin Schuh83c7f702021-01-19 22:36:29 -0800753 virtual const UUID &boot_uuid() const = 0;
Austin Schuh20ac95d2020-12-05 17:24:19 -0800754
Alex Perrycb7da4b2019-08-28 19:35:56 -0700755 protected:
Austin Schuh217a9782019-12-21 23:02:50 -0800756 // Sets the name of the event loop. This is the application name.
757 virtual void set_name(const std::string_view name) = 0;
758
Alex Perrycb7da4b2019-08-28 19:35:56 -0700759 void set_is_running(bool value) { is_running_.store(value); }
760
Austin Schuh39788ff2019-12-01 18:22:57 -0800761 // Validates that channel exists inside configuration_ and finds its index.
762 int ChannelIndex(const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700763
Brian Silverman5120afb2020-01-31 17:44:35 -0800764 // Returns the state for the watcher on the corresponding channel. This
765 // watcher must exist before calling this.
766 WatcherState *GetWatcherState(const Channel *channel);
767
Brian Silverman6d2b3592020-06-18 14:40:15 -0700768 // Returns a Sender's protected RawSender.
Brian Silverman5120afb2020-01-31 17:44:35 -0800769 template <typename T>
770 static RawSender *GetRawSender(aos::Sender<T> *sender) {
771 return sender->sender_.get();
772 }
773
Brian Silverman6d2b3592020-06-18 14:40:15 -0700774 // Returns a Fetcher's protected RawFetcher.
775 template <typename T>
776 static RawFetcher *GetRawFetcher(aos::Fetcher<T> *fetcher) {
777 return fetcher->fetcher_.get();
778 }
779
Austin Schuh6231cc32019-12-07 13:06:15 -0800780 // Context available for watchers, timers, and phased loops.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700781 Context context_;
782
Austin Schuh39788ff2019-12-01 18:22:57 -0800783 friend class RawSender;
784 friend class TimerHandler;
785 friend class RawFetcher;
786 friend class PhasedLoopHandler;
787 friend class WatcherState;
788
789 // Methods used to implement timing reports.
790 void NewSender(RawSender *sender);
791 void DeleteSender(RawSender *sender);
792 TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer);
793 PhasedLoopHandler *NewPhasedLoop(
794 std::unique_ptr<PhasedLoopHandler> phased_loop);
795 void NewFetcher(RawFetcher *fetcher);
796 void DeleteFetcher(RawFetcher *fetcher);
797 WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher);
798
Brian Silverman0fc69932020-01-24 21:54:02 -0800799 // Tracks that we have a (single) watcher on the given channel.
800 void TakeWatcher(const Channel *channel);
801 // Tracks that we have at least one sender on the given channel.
802 void TakeSender(const Channel *channel);
803
Austin Schuh39788ff2019-12-01 18:22:57 -0800804 std::vector<RawSender *> senders_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800805 std::vector<RawFetcher *> fetchers_;
806
Austin Schuh39788ff2019-12-01 18:22:57 -0800807 std::vector<std::unique_ptr<TimerHandler>> timers_;
808 std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800809 std::vector<std::unique_ptr<WatcherState>> watchers_;
810
Brian Silvermance418d02021-11-03 11:25:52 -0700811 // Does nothing if timing reports are disabled.
Austin Schuh39788ff2019-12-01 18:22:57 -0800812 void SendTimingReport();
Brian Silvermance418d02021-11-03 11:25:52 -0700813
Austin Schuh39788ff2019-12-01 18:22:57 -0800814 void UpdateTimingReport();
815 void MaybeScheduleTimingReports();
816
817 std::unique_ptr<RawSender> timing_report_sender_;
818
Austin Schuh7d87b672019-12-01 20:23:49 -0800819 // Tracks which event sources (timers and watchers) have data, and which
820 // don't. Added events may not change their event_time().
821 // TODO(austin): Test case 1: timer triggers at t1, handler takes until after
822 // t2 to run, t2 should then be picked up without a context switch.
823 void AddEvent(EventLoopEvent *event);
824 void RemoveEvent(EventLoopEvent *event);
825 size_t EventCount() const { return events_.size(); }
826 EventLoopEvent *PopEvent();
827 EventLoopEvent *PeekEvent() { return events_.front(); }
828 void ReserveEvents();
829
830 std::vector<EventLoopEvent *> events_;
Brian Silvermanbd405c02020-06-23 16:25:23 -0700831 size_t event_generation_ = 1;
Austin Schuh7d87b672019-12-01 20:23:49 -0800832
Tyler Chatow67ddb032020-01-12 14:30:04 -0800833 // If true, don't send AOS_LOG to /aos
834 bool skip_logger_ = false;
835
Austin Schuha9012be2021-07-21 15:19:11 -0700836 // Sets context_ for a timed event which is supposed to happen at the provided
837 // time.
838 void SetTimerContext(monotonic_clock::time_point monotonic_event_time);
839
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800840 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800841 virtual pid_t GetTid() = 0;
842
843 FlatbufferDetachedBuffer<timing::Report> timing_report_;
844
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800845 ::std::atomic<bool> is_running_{false};
846
Alex Perrycb7da4b2019-08-28 19:35:56 -0700847 const Configuration *configuration_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800848
849 // If true, don't send out timing reports.
850 bool skip_timing_report_ = false;
Brian Silverman0fc69932020-01-24 21:54:02 -0800851
milind1f1dca32021-07-03 13:50:07 -0700852 SendFailureCounter timing_report_failure_counter_;
853
Brian Silverman0fc69932020-01-24 21:54:02 -0800854 absl::btree_set<const Channel *> taken_watchers_, taken_senders_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700855};
856
857} // namespace aos
858
Austin Schuhb8075812020-10-19 09:36:49 -0700859#include "aos/events/event_loop_tmpl.h" // IWYU pragma: export
Alex Perrycb7da4b2019-08-28 19:35:56 -0700860
861#endif // AOS_EVENTS_EVENT_LOOP_H