blob: c2498b37e030ad8a4d6867ea871cde325703d6b1 [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
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700142 enum class [[nodiscard]] Error {
milind1f1dca32021-07-03 13:50:07 -0700143 // 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
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700148 kMessagesSentTooFast,
149 // Access to Redzone was attempted in Sender Queue
150 kInvalidRedzone
151 };
milind1f1dca32021-07-03 13:50:07 -0700152
Austin Schuh39788ff2019-12-01 18:22:57 -0800153 RawSender(EventLoop *event_loop, const Channel *channel);
154 RawSender(const RawSender &) = delete;
155 RawSender &operator=(const RawSender &) = delete;
156
157 virtual ~RawSender();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700158
Alex Perrycb7da4b2019-08-28 19:35:56 -0700159 virtual void *data() = 0;
160 virtual size_t size() = 0;
milind1f1dca32021-07-03 13:50:07 -0700161
162 // Sends a message without copying it. The users starts by copying up to
163 // size() bytes into the data backed by data(). They then call Send to send.
164 // Returns Error::kOk on a successful send, or
165 // Error::kMessagesSentTooFast if messages were sent too fast. If provided,
166 // monotonic_remote_time, realtime_remote_time, and remote_queue_index are
167 // attached to the message and are available in the context on the read side.
168 // If they are not populated, the read side will get the sent times instead.
169 Error Send(size_t size);
170 Error Send(size_t size, monotonic_clock::time_point monotonic_remote_time,
171 realtime_clock::time_point realtime_remote_time,
172 uint32_t remote_queue_index, const UUID &source_boot_uuid);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700173
174 // Sends a single block of data by copying it.
Austin Schuhad154822019-12-27 15:45:13 -0800175 // The remote arguments have the same meaning as in Send above.
milind1f1dca32021-07-03 13:50:07 -0700176 // Returns Error::kMessagesSentTooFast if messages were sent too fast
177 Error Send(const void *data, size_t size);
178 Error Send(const void *data, size_t size,
179 monotonic_clock::time_point monotonic_remote_time,
180 realtime_clock::time_point realtime_remote_time,
181 uint32_t remote_queue_index, const UUID &source_boot_uuid);
182
183 // CHECKs that no sending Error occurred and logs the channel_ data if
184 // one did
185 void CheckOk(const Error err);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700186
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700187 // Sends a single block of data by refcounting it to avoid copies. The data
188 // must not change after being passed into Send. The remote arguments have the
189 // same meaning as in Send above.
milind1f1dca32021-07-03 13:50:07 -0700190 Error Send(const SharedSpan data);
191 Error Send(const SharedSpan data,
192 monotonic_clock::time_point monotonic_remote_time,
193 realtime_clock::time_point realtime_remote_time,
194 uint32_t remote_queue_index, const UUID &remote_boot_uuid);
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700195
Austin Schuh54cf95f2019-11-29 13:14:18 -0800196 const Channel *channel() const { return channel_; }
197
Austin Schuhad154822019-12-27 15:45:13 -0800198 // Returns the time_points that the last message was sent at.
199 aos::monotonic_clock::time_point monotonic_sent_time() const {
200 return monotonic_sent_time_;
201 }
202 aos::realtime_clock::time_point realtime_sent_time() const {
203 return realtime_sent_time_;
204 }
205 // Returns the queue index that this was sent with.
206 uint32_t sent_queue_index() const { return sent_queue_index_; }
207
Brian Silvermana1652f32020-01-29 20:41:44 -0800208 // Returns the associated flatbuffers-style allocator. This must be
209 // deallocated before the message is sent.
Austin Schuh1af273d2020-03-07 20:11:34 -0800210 ChannelPreallocatedAllocator *fbb_allocator() {
211 fbb_allocator_ = ChannelPreallocatedAllocator(
212 reinterpret_cast<uint8_t *>(data()), size(), channel());
Brian Silvermana1652f32020-01-29 20:41:44 -0800213 return &fbb_allocator_;
214 }
215
Brian Silverman4f4e0612020-08-12 19:54:41 -0700216 // Index of the buffer which is currently exposed by data() and the various
217 // other accessors. This is the message the caller should be filling out.
218 virtual int buffer_index() = 0;
219
Alex Perrycb7da4b2019-08-28 19:35:56 -0700220 protected:
Austin Schuh39788ff2019-12-01 18:22:57 -0800221 EventLoop *event_loop() { return event_loop_; }
Austin Schuh3054f5f2021-07-21 15:38:01 -0700222 const EventLoop *event_loop() const { return event_loop_; }
Austin Schuh54cf95f2019-11-29 13:14:18 -0800223
Austin Schuhb5c6f972021-03-14 21:53:07 -0700224 monotonic_clock::time_point monotonic_sent_time_ = monotonic_clock::min_time;
225 realtime_clock::time_point realtime_sent_time_ = realtime_clock::min_time;
Austin Schuhad154822019-12-27 15:45:13 -0800226 uint32_t sent_queue_index_ = 0xffffffff;
227
Austin Schuh39788ff2019-12-01 18:22:57 -0800228 private:
229 friend class EventLoop;
230
milind1f1dca32021-07-03 13:50:07 -0700231 virtual Error DoSend(const void *data, size_t size,
232 monotonic_clock::time_point monotonic_remote_time,
233 realtime_clock::time_point realtime_remote_time,
234 uint32_t remote_queue_index,
235 const UUID &source_boot_uuid) = 0;
236 virtual Error DoSend(size_t size,
237 monotonic_clock::time_point monotonic_remote_time,
238 realtime_clock::time_point realtime_remote_time,
239 uint32_t remote_queue_index,
240 const UUID &source_boot_uuid) = 0;
241 virtual Error DoSend(const SharedSpan data,
242 monotonic_clock::time_point monotonic_remote_time,
243 realtime_clock::time_point realtime_remote_time,
244 uint32_t remote_queue_index,
245 const UUID &source_boot_uuid);
Austin Schuh39788ff2019-12-01 18:22:57 -0800246
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500247 EventLoop *const event_loop_;
248 const Channel *const channel_;
249 const std::string ftrace_prefix_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700250
Austin Schuh39788ff2019-12-01 18:22:57 -0800251 internal::RawSenderTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500252 Ftrace ftrace_;
Brian Silvermana1652f32020-01-29 20:41:44 -0800253
Austin Schuh1af273d2020-03-07 20:11:34 -0800254 ChannelPreallocatedAllocator fbb_allocator_{nullptr, 0, nullptr};
Austin Schuh39788ff2019-12-01 18:22:57 -0800255};
Alex Perrycb7da4b2019-08-28 19:35:56 -0700256
milind1f1dca32021-07-03 13:50:07 -0700257// Needed for compatibility with glog
258std::ostream &operator<<(std::ostream &os, const RawSender::Error err);
259
Alex Perrycb7da4b2019-08-28 19:35:56 -0700260// Fetches the newest message from a channel.
261// This provides a polling based interface for channels.
262template <typename T>
263class Fetcher {
264 public:
265 Fetcher() {}
266
267 // Fetches the next message. Returns true if it fetched a new message. This
268 // method will only return messages sent after the Fetcher was created.
Brian Silvermana1652f32020-01-29 20:41:44 -0800269 bool FetchNext() {
270 const bool result = fetcher_->FetchNext();
271 if (result) {
272 CheckChannelDataAlignment(fetcher_->context().data,
273 fetcher_->context().size);
274 }
275 return result;
276 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700277
278 // Fetches the most recent message. Returns true if it fetched a new message.
279 // This will return the latest message regardless of if it was sent before or
280 // after the fetcher was created.
Brian Silvermana1652f32020-01-29 20:41:44 -0800281 bool Fetch() {
282 const bool result = fetcher_->Fetch();
283 if (result) {
284 CheckChannelDataAlignment(fetcher_->context().data,
285 fetcher_->context().size);
286 }
287 return result;
288 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700289
290 // Returns a pointer to the contained flatbuffer, or nullptr if there is no
291 // available message.
292 const T *get() const {
Austin Schuh39788ff2019-12-01 18:22:57 -0800293 return fetcher_->context().data != nullptr
294 ? flatbuffers::GetRoot<T>(
295 reinterpret_cast<const char *>(fetcher_->context().data))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700296 : nullptr;
297 }
298
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700299 // Returns the channel this fetcher uses
300 const Channel *channel() const { return fetcher_->channel(); }
301
Alex Perrycb7da4b2019-08-28 19:35:56 -0700302 // Returns the context holding timestamps and other metadata about the
303 // message.
304 const Context &context() const { return fetcher_->context(); }
305
306 const T &operator*() const { return *get(); }
307 const T *operator->() const { return get(); }
308
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700309 // Returns true if this fetcher is valid and connected to a channel.
Milind Upadhyay49174a72021-04-10 16:24:57 -0700310 bool valid() const { return static_cast<bool>(fetcher_); }
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700311
Austin Schuhca75b6a2020-12-15 21:12:24 -0800312 // Copies the current flatbuffer into a FlatbufferVector.
313 FlatbufferVector<T> CopyFlatBuffer() const {
314 return context().template CopyFlatBuffer<T>();
315 }
316
Alex Perrycb7da4b2019-08-28 19:35:56 -0700317 private:
318 friend class EventLoop;
319 Fetcher(::std::unique_ptr<RawFetcher> fetcher)
320 : fetcher_(::std::move(fetcher)) {}
321 ::std::unique_ptr<RawFetcher> fetcher_;
322};
323
324// Sends messages to a channel.
325template <typename T>
326class Sender {
327 public:
328 Sender() {}
329
330 // Represents a single message about to be sent to the queue.
331 // The lifecycle goes:
332 //
333 // Builder builder = sender.MakeBuilder();
334 // T::Builder t_builder = builder.MakeBuilder<T>();
335 // Populate(&t_builder);
336 // builder.Send(t_builder.Finish());
337 class Builder {
338 public:
Austin Schuh1af273d2020-03-07 20:11:34 -0800339 Builder(RawSender *sender, ChannelPreallocatedAllocator *allocator)
Brian Silverman9dd793b2020-01-31 23:52:21 -0800340 : fbb_(allocator->size(), allocator),
341 allocator_(allocator),
342 sender_(sender) {
Brian Silvermana1652f32020-01-29 20:41:44 -0800343 CheckChannelDataAlignment(allocator->data(), allocator->size());
Austin Schuhd7b15da2020-02-17 15:06:11 -0800344 fbb_.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700345 }
Brian Silvermana1652f32020-01-29 20:41:44 -0800346 Builder() {}
347 Builder(const Builder &) = delete;
348 Builder(Builder &&) = default;
349
350 Builder &operator=(const Builder &) = delete;
351 Builder &operator=(Builder &&) = default;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700352
353 flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; }
354
355 template <typename T2>
356 typename T2::Builder MakeBuilder() {
357 return typename T2::Builder(fbb_);
358 }
359
milind1f1dca32021-07-03 13:50:07 -0700360 RawSender::Error Send(flatbuffers::Offset<T> offset) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700361 fbb_.Finish(offset);
milind1f1dca32021-07-03 13:50:07 -0700362 const auto err = sender_->Send(fbb_.GetSize());
Brian Silverman9dd793b2020-01-31 23:52:21 -0800363 // Ensure fbb_ knows it shouldn't access the memory any more.
364 fbb_ = flatbuffers::FlatBufferBuilder();
milind1f1dca32021-07-03 13:50:07 -0700365 return err;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700366 }
367
milind1f1dca32021-07-03 13:50:07 -0700368 // Equivalent to RawSender::CheckOk
369 void CheckOk(const RawSender::Error err) { sender_->CheckOk(err); };
370
Alex Perrycb7da4b2019-08-28 19:35:56 -0700371 // CHECKs that this message was sent.
Brian Silverman9dd793b2020-01-31 23:52:21 -0800372 void CheckSent() {
373 CHECK(!allocator_->is_allocated()) << ": Message was not sent yet";
374 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700375
Brian Silverman341b57e2020-06-23 16:23:18 -0700376 // Detaches a buffer, for later use calling Sender::Send directly.
377 //
378 // Note that the underlying memory remains with the Sender, so creating
379 // another Builder before destroying the FlatbufferDetachedBuffer will fail.
380 FlatbufferDetachedBuffer<T> Detach(flatbuffers::Offset<T> offset) {
381 fbb_.Finish(offset);
382 return fbb_.Release();
383 }
384
Alex Perrycb7da4b2019-08-28 19:35:56 -0700385 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700386 flatbuffers::FlatBufferBuilder fbb_;
Austin Schuh1af273d2020-03-07 20:11:34 -0800387 ChannelPreallocatedAllocator *allocator_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700388 RawSender *sender_;
389 };
390
391 // Constructs an above builder.
Brian Silverman9dd793b2020-01-31 23:52:21 -0800392 //
393 // Only a single one of these may be "alive" for this object at any point in
394 // time. After calling Send on the result, it is no longer "alive". This means
395 // that you must manually reset a variable holding the return value (by
396 // assigning a default-constructed Builder to it) before calling this method
397 // again to overwrite the value in the variable.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700398 Builder MakeBuilder();
399
Austin Schuha28cbc32019-12-27 16:28:04 -0800400 // Sends a prebuilt flatbuffer.
milind1f1dca32021-07-03 13:50:07 -0700401 RawSender::Error Send(const NonSizePrefixedFlatbuffer<T> &flatbuffer);
Austin Schuha28cbc32019-12-27 16:28:04 -0800402
Brian Silverman341b57e2020-06-23 16:23:18 -0700403 // Sends a prebuilt flatbuffer which was detached from a Builder created via
404 // MakeBuilder() on this object.
milind1f1dca32021-07-03 13:50:07 -0700405 RawSender::Error SendDetached(FlatbufferDetachedBuffer<T> detached);
406
407 // Equivalent to RawSender::CheckOk
408 void CheckOk(const RawSender::Error err) { sender_->CheckOk(err); };
Brian Silverman341b57e2020-06-23 16:23:18 -0700409
Austin Schuh39788ff2019-12-01 18:22:57 -0800410 // Returns the name of the underlying queue.
Austin Schuh1e869472019-12-01 13:36:10 -0800411 const Channel *channel() const { return sender_->channel(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700412
Austin Schuhe33c08d2022-02-03 18:15:21 -0800413 // TODO(austin): Deprecate the operator bool.
Austin Schuha0c41ba2020-09-10 22:59:14 -0700414 operator bool() const { return sender_ ? true : false; }
Austin Schuhe33c08d2022-02-03 18:15:21 -0800415 bool valid() const { return static_cast<bool>(sender_); }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800416
Austin Schuh7bc59052020-02-16 23:48:33 -0800417 // Returns the time_points that the last message was sent at.
418 aos::monotonic_clock::time_point monotonic_sent_time() const {
419 return sender_->monotonic_sent_time();
420 }
421 aos::realtime_clock::time_point realtime_sent_time() const {
422 return sender_->realtime_sent_time();
423 }
424 // Returns the queue index that this was sent with.
425 uint32_t sent_queue_index() const { return sender_->sent_queue_index(); }
426
Brian Silverman4f4e0612020-08-12 19:54:41 -0700427 // Returns the buffer index which MakeBuilder() will expose access to. This is
428 // the buffer the caller can fill out.
429 int buffer_index() const { return sender_->buffer_index(); }
430
Alex Perrycb7da4b2019-08-28 19:35:56 -0700431 private:
432 friend class EventLoop;
433 Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {}
434 std::unique_ptr<RawSender> sender_;
435};
436
milind1f1dca32021-07-03 13:50:07 -0700437// Class for keeping a count of send failures on a certain channel
438class SendFailureCounter {
439 public:
440 inline void Count(const RawSender::Error err) {
441 failures_ += static_cast<size_t>(err != RawSender::Error::kOk);
442 just_failed_ = (err != RawSender::Error::kOk);
443 }
444
445 inline size_t failures() const { return failures_; }
446 inline bool just_failed() const { return just_failed_; }
447
448 private:
449 size_t failures_ = 0;
450 bool just_failed_ = false;
451};
452
Brian Silverman4f4e0612020-08-12 19:54:41 -0700453// Interface for timers.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700454class TimerHandler {
455 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800456 virtual ~TimerHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700457
458 // Timer should sleep until base, base + offset, base + offset * 2, ...
459 // If repeat_offset isn't set, the timer only expires once.
460 virtual void Setup(monotonic_clock::time_point base,
461 monotonic_clock::duration repeat_offset =
462 ::aos::monotonic_clock::zero()) = 0;
463
464 // Stop future calls to callback().
465 virtual void Disable() = 0;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800466
467 // Sets and gets the name of the timer. Set this if you want a descriptive
468 // name in the timing report.
469 void set_name(std::string_view name) { name_ = std::string(name); }
470 const std::string_view name() const { return name_; }
471
Austin Schuh39788ff2019-12-01 18:22:57 -0800472 protected:
473 TimerHandler(EventLoop *event_loop, std::function<void()> fn);
474
Austin Schuhcde39fd2020-02-22 20:58:24 -0800475 monotonic_clock::time_point Call(
476 std::function<monotonic_clock::time_point()> get_time,
477 monotonic_clock::time_point event_time);
Austin Schuh39788ff2019-12-01 18:22:57 -0800478
Austin Schuh1540c2f2019-11-29 21:59:29 -0800479 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800480 friend class EventLoop;
481
482 EventLoop *event_loop_;
483 // The function to call when Call is called.
484 std::function<void()> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800485 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800486
487 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500488 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700489};
490
491// Interface for phased loops. They are built on timers.
492class PhasedLoopHandler {
493 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800494 virtual ~PhasedLoopHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700495
496 // Sets the interval and offset. Any changes to interval and offset only take
497 // effect when the handler finishes running.
Austin Schuh39788ff2019-12-01 18:22:57 -0800498 void set_interval_and_offset(const monotonic_clock::duration interval,
499 const monotonic_clock::duration offset) {
500 phased_loop_.set_interval_and_offset(interval, offset);
501 }
Austin Schuh1540c2f2019-11-29 21:59:29 -0800502
503 // Sets and gets the name of the timer. Set this if you want a descriptive
504 // name in the timing report.
505 void set_name(std::string_view name) { name_ = std::string(name); }
506 const std::string_view name() const { return name_; }
507
Austin Schuh39788ff2019-12-01 18:22:57 -0800508 protected:
509 void Call(std::function<monotonic_clock::time_point()> get_time,
510 std::function<void(monotonic_clock::time_point)> schedule);
511
512 PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn,
513 const monotonic_clock::duration interval,
514 const monotonic_clock::duration offset);
515
Austin Schuh1540c2f2019-11-29 21:59:29 -0800516 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800517 friend class EventLoop;
518
519 void Reschedule(std::function<void(monotonic_clock::time_point)> schedule,
520 monotonic_clock::time_point monotonic_now) {
521 cycles_elapsed_ += phased_loop_.Iterate(monotonic_now);
522 schedule(phased_loop_.sleep_time());
523 }
524
525 virtual void Schedule(monotonic_clock::time_point sleep_time) = 0;
526
527 EventLoop *event_loop_;
528 std::function<void(int)> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800529 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800530 time::PhasedLoop phased_loop_;
531
532 int cycles_elapsed_ = 0;
533
534 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500535 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700536};
537
Austin Schuh3054f5f2021-07-21 15:38:01 -0700538// Note, it is supported to create only:
539// multiple fetchers, and (one sender or one watcher) per <name, type>
540// tuple.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700541class EventLoop {
542 public:
Austin Schuh3054f5f2021-07-21 15:38:01 -0700543 // Holds configuration by reference for the lifetime of this object. It may
544 // never be mutated externally in any way.
Austin Schuh83c7f702021-01-19 22:36:29 -0800545 EventLoop(const Configuration *configuration);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700546
Austin Schuh39788ff2019-12-01 18:22:57 -0800547 virtual ~EventLoop();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700548
549 // Current time.
Stephan Pleines559fa6c2022-01-06 17:23:51 -0800550 virtual monotonic_clock::time_point monotonic_now() const = 0;
551 virtual realtime_clock::time_point realtime_now() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700552
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700553 template <typename T>
Austin Schuha0654152021-02-21 21:38:24 -0800554 const Channel *GetChannel(const std::string_view channel_name) {
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800555 return configuration::GetChannel(configuration(), channel_name,
Austin Schuh0de30f32020-12-06 12:44:28 -0800556 T::GetFullyQualifiedName(), name(), node(),
Austin Schuha0654152021-02-21 21:38:24 -0800557 true);
558 }
milind1f1dca32021-07-03 13:50:07 -0700559 // Returns true if the channel exists in the configuration.
Austin Schuha0654152021-02-21 21:38:24 -0800560 template <typename T>
561 bool HasChannel(const std::string_view channel_name) {
562 return GetChannel<T>(channel_name) != nullptr;
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700563 }
564
Brian Silverman631b6262021-11-10 12:25:08 -0800565 // Like MakeFetcher, but returns an invalid fetcher if the given channel is
566 // not readable on this node or does not exist.
567 template <typename T>
568 Fetcher<T> TryMakeFetcher(const std::string_view channel_name) {
569 const Channel *const channel = GetChannel<T>(channel_name);
570 if (channel == nullptr) {
571 return Fetcher<T>();
572 }
573
574 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
575 return Fetcher<T>();
576 }
577
578 return Fetcher<T>(MakeRawFetcher(channel));
579 }
580
Alex Perrycb7da4b2019-08-28 19:35:56 -0700581 // Makes a class that will always fetch the most recent value
582 // sent to the provided channel.
583 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800584 Fetcher<T> MakeFetcher(const std::string_view channel_name) {
Brian Silverman631b6262021-11-10 12:25:08 -0800585 CHECK(HasChannel<T>(channel_name))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700586 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
587 << T::GetFullyQualifiedName() << "\" } not found in config.";
588
Brian Silverman631b6262021-11-10 12:25:08 -0800589 Fetcher<T> result = TryMakeFetcher<T>(channel_name);
590 if (!result.valid()) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800591 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
592 << "\", \"type\": \"" << T::GetFullyQualifiedName()
593 << "\" } is not able to be fetched on this node. Check your "
594 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800595 }
596
Brian Silverman631b6262021-11-10 12:25:08 -0800597 return result;
598 }
599
600 // Like MakeSender, but returns an invalid sender if the given channel is
James Kuszmaulb8887592021-12-09 14:54:50 -0800601 // not sendable on this node or does not exist.
Brian Silverman631b6262021-11-10 12:25:08 -0800602 template <typename T>
603 Sender<T> TryMakeSender(const std::string_view channel_name) {
604 const Channel *channel = GetChannel<T>(channel_name);
605 if (channel == nullptr) {
606 return Sender<T>();
607 }
608
609 if (!configuration::ChannelIsSendableOnNode(channel, node())) {
610 return Sender<T>();
611 }
612
613 return Sender<T>(MakeRawSender(channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700614 }
615
616 // Makes class that allows constructing and sending messages to
617 // the provided channel.
618 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800619 Sender<T> MakeSender(const std::string_view channel_name) {
Brian Silverman631b6262021-11-10 12:25:08 -0800620 CHECK(HasChannel<T>(channel_name))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700621 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
Austin Schuh28fedcb2020-02-08 15:59:58 -0800622 << T::GetFullyQualifiedName() << "\" } not found in config for "
Austin Schuh2f8fd752020-09-01 22:38:28 -0700623 << name()
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800624 << (configuration::MultiNode(configuration())
Austin Schuh2f8fd752020-09-01 22:38:28 -0700625 ? absl::StrCat(" on node ", node()->name()->string_view())
626 : ".");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700627
Brian Silverman631b6262021-11-10 12:25:08 -0800628 Sender<T> result = TryMakeSender<T>(channel_name);
629 if (!result) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800630 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
631 << "\", \"type\": \"" << T::GetFullyQualifiedName()
632 << "\" } is not able to be sent on this node. Check your "
633 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800634 }
635
Brian Silverman631b6262021-11-10 12:25:08 -0800636 return result;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700637 }
638
639 // This will watch messages sent to the provided channel.
640 //
Brian Silverman454bc112020-03-05 14:21:25 -0800641 // w must have a non-polymorphic operator() (aka it can only be called with a
642 // single set of arguments; no overloading or templates). It must be callable
643 // with this signature:
644 // void(const MessageType &);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700645 //
Brian Silverman454bc112020-03-05 14:21:25 -0800646 // Lambdas are a common form for w. A std::function will work too.
647 //
648 // Note that bind expressions have polymorphic call operators, so they are not
649 // allowed.
650 //
651 // We template Watch as a whole instead of using std::function<void(const T
652 // &)> to allow deducing MessageType from lambdas and other things which are
653 // implicitly convertible to std::function, but not actually std::function
654 // instantiations. Template deduction guides might allow solving this
655 // differently in newer versions of C++, but those have their own corner
656 // cases.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700657 template <typename Watch>
Brian Silverman454bc112020-03-05 14:21:25 -0800658 void MakeWatcher(const std::string_view channel_name, Watch &&w);
659
660 // Like MakeWatcher, but doesn't have access to the message data. This may be
661 // implemented to use less resources than an equivalent MakeWatcher.
662 //
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800663 // The function will still have access to context(), although that will have
664 // its data field set to nullptr.
Brian Silverman454bc112020-03-05 14:21:25 -0800665 template <typename MessageType>
666 void MakeNoArgWatcher(const std::string_view channel_name,
667 std::function<void()> w);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700668
669 // The passed in function will be called when the event loop starts.
670 // Use this to run code once the thread goes into "real-time-mode",
671 virtual void OnRun(::std::function<void()> on_run) = 0;
672
Austin Schuh217a9782019-12-21 23:02:50 -0800673 // Gets the name of the event loop. This is the application name.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800674 virtual const std::string_view name() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700675
Austin Schuh217a9782019-12-21 23:02:50 -0800676 // Returns the node that this event loop is running on. Returns nullptr if we
677 // are running in single-node mode.
678 virtual const Node *node() const = 0;
679
Alex Perrycb7da4b2019-08-28 19:35:56 -0700680 // Creates a timer that executes callback when the timer expires
681 // Returns a TimerHandle for configuration of the timer
milind-u61227f22021-08-29 15:58:33 -0700682 // TODO(milind): callback should take the number of cycles elapsed as a
683 // parameter.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700684 virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0;
685
686 // Creates a timer that executes callback periodically at the specified
687 // interval and offset. Returns a PhasedLoopHandler for interacting with the
688 // timer.
689 virtual PhasedLoopHandler *AddPhasedLoop(
690 ::std::function<void(int)> callback,
691 const monotonic_clock::duration interval,
692 const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0;
693
Austin Schuh217a9782019-12-21 23:02:50 -0800694 // TODO(austin): OnExit for cleanup.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700695
Austin Schuh3054f5f2021-07-21 15:38:01 -0700696 // May be safely called from any thread.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700697 bool is_running() const { return is_running_.load(); }
698
699 // Sets the scheduler priority to run the event loop at. This may not be
700 // called after we go into "real-time-mode".
701 virtual void SetRuntimeRealtimePriority(int priority) = 0;
Austin Schuh39788ff2019-12-01 18:22:57 -0800702 virtual int priority() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700703
Brian Silverman6a54ff32020-04-28 16:41:39 -0700704 // Sets the scheduler affinity to run the event loop with. This may only be
705 // called before Run().
706 virtual void SetRuntimeAffinity(const cpu_set_t &cpuset) = 0;
707
Austin Schuh217a9782019-12-21 23:02:50 -0800708 // Fetches new messages from the provided channel (path, type).
709 //
710 // Note: this channel must be a member of the exact configuration object this
711 // was built with.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700712 virtual std::unique_ptr<RawFetcher> MakeRawFetcher(
713 const Channel *channel) = 0;
714
Austin Schuh217a9782019-12-21 23:02:50 -0800715 // Watches channel (name, type) for new messages.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700716 virtual void MakeRawWatcher(
717 const Channel *channel,
718 std::function<void(const Context &context, const void *message)>
719 watcher) = 0;
720
Brian Silverman454bc112020-03-05 14:21:25 -0800721 // Watches channel (name, type) for new messages, without needing to extract
722 // the message contents. Default implementation simply re-uses MakeRawWatcher.
723 virtual void MakeRawNoArgWatcher(
724 const Channel *channel,
725 std::function<void(const Context &context)> watcher) {
726 MakeRawWatcher(channel, [watcher](const Context &context, const void *) {
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800727 Context new_context = context;
728 new_context.data = nullptr;
Brian Silverman4f4e0612020-08-12 19:54:41 -0700729 new_context.buffer_index = -1;
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800730 watcher(new_context);
Brian Silverman454bc112020-03-05 14:21:25 -0800731 });
732 }
733
Austin Schuh217a9782019-12-21 23:02:50 -0800734 // Creates a raw sender for the provided channel. This is used for reflection
735 // based sending.
736 // Note: this ignores any node constraints. Ignore at your own peril.
737 virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0;
738
Austin Schuh6231cc32019-12-07 13:06:15 -0800739 // Returns the context for the current callback.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700740 const Context &context() const { return context_; }
741
742 // Returns the configuration that this event loop was built with.
743 const Configuration *configuration() const { return configuration_; }
744
Austin Schuh39788ff2019-12-01 18:22:57 -0800745 // Prevents the event loop from sending a timing report.
Brian Silvermanbf889922021-11-10 12:41:57 -0800746 void SkipTimingReport();
Austin Schuh39788ff2019-12-01 18:22:57 -0800747
Brian Silverman4f4e0612020-08-12 19:54:41 -0700748 // Prevents AOS_LOG being sent to message on /aos.
Tyler Chatow67ddb032020-01-12 14:30:04 -0800749 void SkipAosLog() { skip_logger_ = true; }
750
Brian Silverman4f4e0612020-08-12 19:54:41 -0700751 // Returns the number of buffers for this channel. This corresponds with the
752 // range of Context::buffer_index values for this channel.
753 virtual int NumberBuffers(const Channel *channel) = 0;
754
Austin Schuh20ac95d2020-12-05 17:24:19 -0800755 // Returns the boot UUID.
Austin Schuh83c7f702021-01-19 22:36:29 -0800756 virtual const UUID &boot_uuid() const = 0;
Austin Schuh20ac95d2020-12-05 17:24:19 -0800757
Alex Perrycb7da4b2019-08-28 19:35:56 -0700758 protected:
Austin Schuh217a9782019-12-21 23:02:50 -0800759 // Sets the name of the event loop. This is the application name.
760 virtual void set_name(const std::string_view name) = 0;
761
Alex Perrycb7da4b2019-08-28 19:35:56 -0700762 void set_is_running(bool value) { is_running_.store(value); }
763
Austin Schuh39788ff2019-12-01 18:22:57 -0800764 // Validates that channel exists inside configuration_ and finds its index.
765 int ChannelIndex(const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700766
Brian Silverman5120afb2020-01-31 17:44:35 -0800767 // Returns the state for the watcher on the corresponding channel. This
768 // watcher must exist before calling this.
769 WatcherState *GetWatcherState(const Channel *channel);
770
Brian Silverman6d2b3592020-06-18 14:40:15 -0700771 // Returns a Sender's protected RawSender.
Brian Silverman5120afb2020-01-31 17:44:35 -0800772 template <typename T>
773 static RawSender *GetRawSender(aos::Sender<T> *sender) {
774 return sender->sender_.get();
775 }
776
Brian Silverman6d2b3592020-06-18 14:40:15 -0700777 // Returns a Fetcher's protected RawFetcher.
778 template <typename T>
779 static RawFetcher *GetRawFetcher(aos::Fetcher<T> *fetcher) {
780 return fetcher->fetcher_.get();
781 }
782
Austin Schuh6231cc32019-12-07 13:06:15 -0800783 // Context available for watchers, timers, and phased loops.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700784 Context context_;
785
Austin Schuh39788ff2019-12-01 18:22:57 -0800786 friend class RawSender;
787 friend class TimerHandler;
788 friend class RawFetcher;
789 friend class PhasedLoopHandler;
790 friend class WatcherState;
791
792 // Methods used to implement timing reports.
793 void NewSender(RawSender *sender);
794 void DeleteSender(RawSender *sender);
795 TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer);
796 PhasedLoopHandler *NewPhasedLoop(
797 std::unique_ptr<PhasedLoopHandler> phased_loop);
798 void NewFetcher(RawFetcher *fetcher);
799 void DeleteFetcher(RawFetcher *fetcher);
800 WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher);
801
Brian Silverman0fc69932020-01-24 21:54:02 -0800802 // Tracks that we have a (single) watcher on the given channel.
803 void TakeWatcher(const Channel *channel);
804 // Tracks that we have at least one sender on the given channel.
805 void TakeSender(const Channel *channel);
806
Austin Schuh39788ff2019-12-01 18:22:57 -0800807 std::vector<RawSender *> senders_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800808 std::vector<RawFetcher *> fetchers_;
809
Austin Schuh39788ff2019-12-01 18:22:57 -0800810 std::vector<std::unique_ptr<TimerHandler>> timers_;
811 std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800812 std::vector<std::unique_ptr<WatcherState>> watchers_;
813
Brian Silvermance418d02021-11-03 11:25:52 -0700814 // Does nothing if timing reports are disabled.
Austin Schuh39788ff2019-12-01 18:22:57 -0800815 void SendTimingReport();
Brian Silvermance418d02021-11-03 11:25:52 -0700816
Austin Schuh39788ff2019-12-01 18:22:57 -0800817 void UpdateTimingReport();
818 void MaybeScheduleTimingReports();
819
820 std::unique_ptr<RawSender> timing_report_sender_;
821
Austin Schuh7d87b672019-12-01 20:23:49 -0800822 // Tracks which event sources (timers and watchers) have data, and which
823 // don't. Added events may not change their event_time().
824 // TODO(austin): Test case 1: timer triggers at t1, handler takes until after
825 // t2 to run, t2 should then be picked up without a context switch.
826 void AddEvent(EventLoopEvent *event);
827 void RemoveEvent(EventLoopEvent *event);
828 size_t EventCount() const { return events_.size(); }
829 EventLoopEvent *PopEvent();
830 EventLoopEvent *PeekEvent() { return events_.front(); }
831 void ReserveEvents();
832
833 std::vector<EventLoopEvent *> events_;
Brian Silvermanbd405c02020-06-23 16:25:23 -0700834 size_t event_generation_ = 1;
Austin Schuh7d87b672019-12-01 20:23:49 -0800835
Tyler Chatow67ddb032020-01-12 14:30:04 -0800836 // If true, don't send AOS_LOG to /aos
837 bool skip_logger_ = false;
838
Austin Schuha9012be2021-07-21 15:19:11 -0700839 // Sets context_ for a timed event which is supposed to happen at the provided
840 // time.
841 void SetTimerContext(monotonic_clock::time_point monotonic_event_time);
842
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800843 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800844 virtual pid_t GetTid() = 0;
845
846 FlatbufferDetachedBuffer<timing::Report> timing_report_;
847
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800848 ::std::atomic<bool> is_running_{false};
849
Alex Perrycb7da4b2019-08-28 19:35:56 -0700850 const Configuration *configuration_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800851
852 // If true, don't send out timing reports.
853 bool skip_timing_report_ = false;
Brian Silverman0fc69932020-01-24 21:54:02 -0800854
milind1f1dca32021-07-03 13:50:07 -0700855 SendFailureCounter timing_report_failure_counter_;
856
Brian Silverman0fc69932020-01-24 21:54:02 -0800857 absl::btree_set<const Channel *> taken_watchers_, taken_senders_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700858};
859
860} // namespace aos
861
Austin Schuhb8075812020-10-19 09:36:49 -0700862#include "aos/events/event_loop_tmpl.h" // IWYU pragma: export
Alex Perrycb7da4b2019-08-28 19:35:56 -0700863
864#endif // AOS_EVENTS_EVENT_LOOP_H