blob: 984a006aaa55a222c38c56d640b0eebda3e61166 [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
James Kuszmaul93abac12022-04-14 15:05:10 -0700247 void RecordSendResult(const Error error, size_t message_size);
248
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500249 EventLoop *const event_loop_;
250 const Channel *const channel_;
251 const std::string ftrace_prefix_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700252
Austin Schuh39788ff2019-12-01 18:22:57 -0800253 internal::RawSenderTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500254 Ftrace ftrace_;
Brian Silvermana1652f32020-01-29 20:41:44 -0800255
Austin Schuh1af273d2020-03-07 20:11:34 -0800256 ChannelPreallocatedAllocator fbb_allocator_{nullptr, 0, nullptr};
Austin Schuh39788ff2019-12-01 18:22:57 -0800257};
Alex Perrycb7da4b2019-08-28 19:35:56 -0700258
milind1f1dca32021-07-03 13:50:07 -0700259// Needed for compatibility with glog
260std::ostream &operator<<(std::ostream &os, const RawSender::Error err);
261
Alex Perrycb7da4b2019-08-28 19:35:56 -0700262// Fetches the newest message from a channel.
263// This provides a polling based interface for channels.
264template <typename T>
265class Fetcher {
266 public:
267 Fetcher() {}
268
269 // Fetches the next message. Returns true if it fetched a new message. This
270 // method will only return messages sent after the Fetcher was created.
Brian Silvermana1652f32020-01-29 20:41:44 -0800271 bool FetchNext() {
272 const bool result = fetcher_->FetchNext();
273 if (result) {
274 CheckChannelDataAlignment(fetcher_->context().data,
275 fetcher_->context().size);
276 }
277 return result;
278 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700279
280 // Fetches the most recent message. Returns true if it fetched a new message.
281 // This will return the latest message regardless of if it was sent before or
282 // after the fetcher was created.
Brian Silvermana1652f32020-01-29 20:41:44 -0800283 bool Fetch() {
284 const bool result = fetcher_->Fetch();
285 if (result) {
286 CheckChannelDataAlignment(fetcher_->context().data,
287 fetcher_->context().size);
288 }
289 return result;
290 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700291
292 // Returns a pointer to the contained flatbuffer, or nullptr if there is no
293 // available message.
294 const T *get() const {
Austin Schuh39788ff2019-12-01 18:22:57 -0800295 return fetcher_->context().data != nullptr
296 ? flatbuffers::GetRoot<T>(
297 reinterpret_cast<const char *>(fetcher_->context().data))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700298 : nullptr;
299 }
300
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700301 // Returns the channel this fetcher uses
302 const Channel *channel() const { return fetcher_->channel(); }
303
Alex Perrycb7da4b2019-08-28 19:35:56 -0700304 // Returns the context holding timestamps and other metadata about the
305 // message.
306 const Context &context() const { return fetcher_->context(); }
307
308 const T &operator*() const { return *get(); }
309 const T *operator->() const { return get(); }
310
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700311 // Returns true if this fetcher is valid and connected to a channel.
Milind Upadhyay49174a72021-04-10 16:24:57 -0700312 bool valid() const { return static_cast<bool>(fetcher_); }
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700313
Austin Schuhca75b6a2020-12-15 21:12:24 -0800314 // Copies the current flatbuffer into a FlatbufferVector.
315 FlatbufferVector<T> CopyFlatBuffer() const {
316 return context().template CopyFlatBuffer<T>();
317 }
318
Alex Perrycb7da4b2019-08-28 19:35:56 -0700319 private:
320 friend class EventLoop;
321 Fetcher(::std::unique_ptr<RawFetcher> fetcher)
322 : fetcher_(::std::move(fetcher)) {}
323 ::std::unique_ptr<RawFetcher> fetcher_;
324};
325
326// Sends messages to a channel.
327template <typename T>
328class Sender {
329 public:
330 Sender() {}
331
332 // Represents a single message about to be sent to the queue.
333 // The lifecycle goes:
334 //
335 // Builder builder = sender.MakeBuilder();
336 // T::Builder t_builder = builder.MakeBuilder<T>();
337 // Populate(&t_builder);
338 // builder.Send(t_builder.Finish());
339 class Builder {
340 public:
Austin Schuh1af273d2020-03-07 20:11:34 -0800341 Builder(RawSender *sender, ChannelPreallocatedAllocator *allocator)
Brian Silverman9dd793b2020-01-31 23:52:21 -0800342 : fbb_(allocator->size(), allocator),
343 allocator_(allocator),
344 sender_(sender) {
Brian Silvermana1652f32020-01-29 20:41:44 -0800345 CheckChannelDataAlignment(allocator->data(), allocator->size());
Austin Schuhd7b15da2020-02-17 15:06:11 -0800346 fbb_.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700347 }
Brian Silvermana1652f32020-01-29 20:41:44 -0800348 Builder() {}
349 Builder(const Builder &) = delete;
350 Builder(Builder &&) = default;
351
352 Builder &operator=(const Builder &) = delete;
353 Builder &operator=(Builder &&) = default;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700354
355 flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; }
356
357 template <typename T2>
358 typename T2::Builder MakeBuilder() {
359 return typename T2::Builder(fbb_);
360 }
361
milind1f1dca32021-07-03 13:50:07 -0700362 RawSender::Error Send(flatbuffers::Offset<T> offset) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700363 fbb_.Finish(offset);
milind1f1dca32021-07-03 13:50:07 -0700364 const auto err = sender_->Send(fbb_.GetSize());
Brian Silverman9dd793b2020-01-31 23:52:21 -0800365 // Ensure fbb_ knows it shouldn't access the memory any more.
366 fbb_ = flatbuffers::FlatBufferBuilder();
milind1f1dca32021-07-03 13:50:07 -0700367 return err;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700368 }
369
milind1f1dca32021-07-03 13:50:07 -0700370 // Equivalent to RawSender::CheckOk
371 void CheckOk(const RawSender::Error err) { sender_->CheckOk(err); };
372
Alex Perrycb7da4b2019-08-28 19:35:56 -0700373 // CHECKs that this message was sent.
Brian Silverman9dd793b2020-01-31 23:52:21 -0800374 void CheckSent() {
375 CHECK(!allocator_->is_allocated()) << ": Message was not sent yet";
376 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700377
Brian Silverman341b57e2020-06-23 16:23:18 -0700378 // Detaches a buffer, for later use calling Sender::Send directly.
379 //
380 // Note that the underlying memory remains with the Sender, so creating
381 // another Builder before destroying the FlatbufferDetachedBuffer will fail.
382 FlatbufferDetachedBuffer<T> Detach(flatbuffers::Offset<T> offset) {
383 fbb_.Finish(offset);
384 return fbb_.Release();
385 }
386
Alex Perrycb7da4b2019-08-28 19:35:56 -0700387 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700388 flatbuffers::FlatBufferBuilder fbb_;
Austin Schuh1af273d2020-03-07 20:11:34 -0800389 ChannelPreallocatedAllocator *allocator_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700390 RawSender *sender_;
391 };
392
393 // Constructs an above builder.
Brian Silverman9dd793b2020-01-31 23:52:21 -0800394 //
395 // Only a single one of these may be "alive" for this object at any point in
396 // time. After calling Send on the result, it is no longer "alive". This means
397 // that you must manually reset a variable holding the return value (by
398 // assigning a default-constructed Builder to it) before calling this method
399 // again to overwrite the value in the variable.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700400 Builder MakeBuilder();
401
Austin Schuha28cbc32019-12-27 16:28:04 -0800402 // Sends a prebuilt flatbuffer.
milind1f1dca32021-07-03 13:50:07 -0700403 RawSender::Error Send(const NonSizePrefixedFlatbuffer<T> &flatbuffer);
Austin Schuha28cbc32019-12-27 16:28:04 -0800404
Brian Silverman341b57e2020-06-23 16:23:18 -0700405 // Sends a prebuilt flatbuffer which was detached from a Builder created via
406 // MakeBuilder() on this object.
milind1f1dca32021-07-03 13:50:07 -0700407 RawSender::Error SendDetached(FlatbufferDetachedBuffer<T> detached);
408
409 // Equivalent to RawSender::CheckOk
410 void CheckOk(const RawSender::Error err) { sender_->CheckOk(err); };
Brian Silverman341b57e2020-06-23 16:23:18 -0700411
Austin Schuh39788ff2019-12-01 18:22:57 -0800412 // Returns the name of the underlying queue.
Austin Schuh1e869472019-12-01 13:36:10 -0800413 const Channel *channel() const { return sender_->channel(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700414
Austin Schuhe33c08d2022-02-03 18:15:21 -0800415 // TODO(austin): Deprecate the operator bool.
Austin Schuha0c41ba2020-09-10 22:59:14 -0700416 operator bool() const { return sender_ ? true : false; }
Austin Schuhe33c08d2022-02-03 18:15:21 -0800417 bool valid() const { return static_cast<bool>(sender_); }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800418
Austin Schuh7bc59052020-02-16 23:48:33 -0800419 // Returns the time_points that the last message was sent at.
420 aos::monotonic_clock::time_point monotonic_sent_time() const {
421 return sender_->monotonic_sent_time();
422 }
423 aos::realtime_clock::time_point realtime_sent_time() const {
424 return sender_->realtime_sent_time();
425 }
426 // Returns the queue index that this was sent with.
427 uint32_t sent_queue_index() const { return sender_->sent_queue_index(); }
428
Brian Silverman4f4e0612020-08-12 19:54:41 -0700429 // Returns the buffer index which MakeBuilder() will expose access to. This is
430 // the buffer the caller can fill out.
431 int buffer_index() const { return sender_->buffer_index(); }
432
Alex Perrycb7da4b2019-08-28 19:35:56 -0700433 private:
434 friend class EventLoop;
435 Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {}
436 std::unique_ptr<RawSender> sender_;
437};
438
milind1f1dca32021-07-03 13:50:07 -0700439// Class for keeping a count of send failures on a certain channel
440class SendFailureCounter {
441 public:
442 inline void Count(const RawSender::Error err) {
443 failures_ += static_cast<size_t>(err != RawSender::Error::kOk);
444 just_failed_ = (err != RawSender::Error::kOk);
445 }
446
447 inline size_t failures() const { return failures_; }
448 inline bool just_failed() const { return just_failed_; }
449
450 private:
451 size_t failures_ = 0;
452 bool just_failed_ = false;
453};
454
Brian Silverman4f4e0612020-08-12 19:54:41 -0700455// Interface for timers.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700456class TimerHandler {
457 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800458 virtual ~TimerHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700459
460 // Timer should sleep until base, base + offset, base + offset * 2, ...
461 // If repeat_offset isn't set, the timer only expires once.
462 virtual void Setup(monotonic_clock::time_point base,
463 monotonic_clock::duration repeat_offset =
464 ::aos::monotonic_clock::zero()) = 0;
465
466 // Stop future calls to callback().
467 virtual void Disable() = 0;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800468
469 // Sets and gets the name of the timer. Set this if you want a descriptive
470 // name in the timing report.
471 void set_name(std::string_view name) { name_ = std::string(name); }
472 const std::string_view name() const { return name_; }
473
Austin Schuh39788ff2019-12-01 18:22:57 -0800474 protected:
475 TimerHandler(EventLoop *event_loop, std::function<void()> fn);
476
Austin Schuhcde39fd2020-02-22 20:58:24 -0800477 monotonic_clock::time_point Call(
478 std::function<monotonic_clock::time_point()> get_time,
479 monotonic_clock::time_point event_time);
Austin Schuh39788ff2019-12-01 18:22:57 -0800480
Austin Schuh1540c2f2019-11-29 21:59:29 -0800481 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800482 friend class EventLoop;
483
484 EventLoop *event_loop_;
485 // The function to call when Call is called.
486 std::function<void()> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800487 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800488
489 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500490 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700491};
492
493// Interface for phased loops. They are built on timers.
494class PhasedLoopHandler {
495 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800496 virtual ~PhasedLoopHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700497
498 // Sets the interval and offset. Any changes to interval and offset only take
499 // effect when the handler finishes running.
Austin Schuh39788ff2019-12-01 18:22:57 -0800500 void set_interval_and_offset(const monotonic_clock::duration interval,
501 const monotonic_clock::duration offset) {
502 phased_loop_.set_interval_and_offset(interval, offset);
503 }
Austin Schuh1540c2f2019-11-29 21:59:29 -0800504
505 // Sets and gets the name of the timer. Set this if you want a descriptive
506 // name in the timing report.
507 void set_name(std::string_view name) { name_ = std::string(name); }
508 const std::string_view name() const { return name_; }
509
Austin Schuh39788ff2019-12-01 18:22:57 -0800510 protected:
511 void Call(std::function<monotonic_clock::time_point()> get_time,
512 std::function<void(monotonic_clock::time_point)> schedule);
513
514 PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn,
515 const monotonic_clock::duration interval,
516 const monotonic_clock::duration offset);
517
Austin Schuh1540c2f2019-11-29 21:59:29 -0800518 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800519 friend class EventLoop;
520
521 void Reschedule(std::function<void(monotonic_clock::time_point)> schedule,
522 monotonic_clock::time_point monotonic_now) {
523 cycles_elapsed_ += phased_loop_.Iterate(monotonic_now);
524 schedule(phased_loop_.sleep_time());
525 }
526
527 virtual void Schedule(monotonic_clock::time_point sleep_time) = 0;
528
529 EventLoop *event_loop_;
530 std::function<void(int)> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800531 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800532 time::PhasedLoop phased_loop_;
533
534 int cycles_elapsed_ = 0;
535
536 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500537 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700538};
539
Austin Schuh3054f5f2021-07-21 15:38:01 -0700540// Note, it is supported to create only:
541// multiple fetchers, and (one sender or one watcher) per <name, type>
542// tuple.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700543class EventLoop {
544 public:
Austin Schuh3054f5f2021-07-21 15:38:01 -0700545 // Holds configuration by reference for the lifetime of this object. It may
546 // never be mutated externally in any way.
Austin Schuh83c7f702021-01-19 22:36:29 -0800547 EventLoop(const Configuration *configuration);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700548
Austin Schuh39788ff2019-12-01 18:22:57 -0800549 virtual ~EventLoop();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700550
551 // Current time.
Stephan Pleines559fa6c2022-01-06 17:23:51 -0800552 virtual monotonic_clock::time_point monotonic_now() const = 0;
553 virtual realtime_clock::time_point realtime_now() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700554
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700555 template <typename T>
Austin Schuha0654152021-02-21 21:38:24 -0800556 const Channel *GetChannel(const std::string_view channel_name) {
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800557 return configuration::GetChannel(configuration(), channel_name,
Austin Schuh0de30f32020-12-06 12:44:28 -0800558 T::GetFullyQualifiedName(), name(), node(),
Austin Schuha0654152021-02-21 21:38:24 -0800559 true);
560 }
milind1f1dca32021-07-03 13:50:07 -0700561 // Returns true if the channel exists in the configuration.
Austin Schuha0654152021-02-21 21:38:24 -0800562 template <typename T>
563 bool HasChannel(const std::string_view channel_name) {
564 return GetChannel<T>(channel_name) != nullptr;
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700565 }
566
Brian Silverman631b6262021-11-10 12:25:08 -0800567 // Like MakeFetcher, but returns an invalid fetcher if the given channel is
568 // not readable on this node or does not exist.
569 template <typename T>
570 Fetcher<T> TryMakeFetcher(const std::string_view channel_name) {
571 const Channel *const channel = GetChannel<T>(channel_name);
572 if (channel == nullptr) {
573 return Fetcher<T>();
574 }
575
576 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
577 return Fetcher<T>();
578 }
579
580 return Fetcher<T>(MakeRawFetcher(channel));
581 }
582
Alex Perrycb7da4b2019-08-28 19:35:56 -0700583 // Makes a class that will always fetch the most recent value
584 // sent to the provided channel.
585 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800586 Fetcher<T> MakeFetcher(const std::string_view channel_name) {
Brian Silverman631b6262021-11-10 12:25:08 -0800587 CHECK(HasChannel<T>(channel_name))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700588 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
589 << T::GetFullyQualifiedName() << "\" } not found in config.";
590
Brian Silverman631b6262021-11-10 12:25:08 -0800591 Fetcher<T> result = TryMakeFetcher<T>(channel_name);
592 if (!result.valid()) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800593 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
594 << "\", \"type\": \"" << T::GetFullyQualifiedName()
595 << "\" } is not able to be fetched on this node. Check your "
596 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800597 }
598
Brian Silverman631b6262021-11-10 12:25:08 -0800599 return result;
600 }
601
602 // Like MakeSender, but returns an invalid sender if the given channel is
James Kuszmaulb8887592021-12-09 14:54:50 -0800603 // not sendable on this node or does not exist.
Brian Silverman631b6262021-11-10 12:25:08 -0800604 template <typename T>
605 Sender<T> TryMakeSender(const std::string_view channel_name) {
606 const Channel *channel = GetChannel<T>(channel_name);
607 if (channel == nullptr) {
608 return Sender<T>();
609 }
610
611 if (!configuration::ChannelIsSendableOnNode(channel, node())) {
612 return Sender<T>();
613 }
614
615 return Sender<T>(MakeRawSender(channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700616 }
617
618 // Makes class that allows constructing and sending messages to
619 // the provided channel.
620 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800621 Sender<T> MakeSender(const std::string_view channel_name) {
Brian Silverman631b6262021-11-10 12:25:08 -0800622 CHECK(HasChannel<T>(channel_name))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700623 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
Austin Schuh28fedcb2020-02-08 15:59:58 -0800624 << T::GetFullyQualifiedName() << "\" } not found in config for "
Austin Schuh2f8fd752020-09-01 22:38:28 -0700625 << name()
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800626 << (configuration::MultiNode(configuration())
Austin Schuh2f8fd752020-09-01 22:38:28 -0700627 ? absl::StrCat(" on node ", node()->name()->string_view())
628 : ".");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700629
Brian Silverman631b6262021-11-10 12:25:08 -0800630 Sender<T> result = TryMakeSender<T>(channel_name);
631 if (!result) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800632 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
633 << "\", \"type\": \"" << T::GetFullyQualifiedName()
634 << "\" } is not able to be sent on this node. Check your "
635 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800636 }
637
Brian Silverman631b6262021-11-10 12:25:08 -0800638 return result;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700639 }
640
641 // This will watch messages sent to the provided channel.
642 //
Brian Silverman454bc112020-03-05 14:21:25 -0800643 // w must have a non-polymorphic operator() (aka it can only be called with a
644 // single set of arguments; no overloading or templates). It must be callable
645 // with this signature:
646 // void(const MessageType &);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700647 //
Brian Silverman454bc112020-03-05 14:21:25 -0800648 // Lambdas are a common form for w. A std::function will work too.
649 //
650 // Note that bind expressions have polymorphic call operators, so they are not
651 // allowed.
652 //
653 // We template Watch as a whole instead of using std::function<void(const T
654 // &)> to allow deducing MessageType from lambdas and other things which are
655 // implicitly convertible to std::function, but not actually std::function
656 // instantiations. Template deduction guides might allow solving this
657 // differently in newer versions of C++, but those have their own corner
658 // cases.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700659 template <typename Watch>
Brian Silverman454bc112020-03-05 14:21:25 -0800660 void MakeWatcher(const std::string_view channel_name, Watch &&w);
661
662 // Like MakeWatcher, but doesn't have access to the message data. This may be
663 // implemented to use less resources than an equivalent MakeWatcher.
664 //
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800665 // The function will still have access to context(), although that will have
666 // its data field set to nullptr.
Brian Silverman454bc112020-03-05 14:21:25 -0800667 template <typename MessageType>
668 void MakeNoArgWatcher(const std::string_view channel_name,
669 std::function<void()> w);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700670
671 // The passed in function will be called when the event loop starts.
672 // Use this to run code once the thread goes into "real-time-mode",
673 virtual void OnRun(::std::function<void()> on_run) = 0;
674
Austin Schuh217a9782019-12-21 23:02:50 -0800675 // Gets the name of the event loop. This is the application name.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800676 virtual const std::string_view name() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700677
Austin Schuh217a9782019-12-21 23:02:50 -0800678 // Returns the node that this event loop is running on. Returns nullptr if we
679 // are running in single-node mode.
680 virtual const Node *node() const = 0;
681
Alex Perrycb7da4b2019-08-28 19:35:56 -0700682 // Creates a timer that executes callback when the timer expires
683 // Returns a TimerHandle for configuration of the timer
milind-u61227f22021-08-29 15:58:33 -0700684 // TODO(milind): callback should take the number of cycles elapsed as a
685 // parameter.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700686 virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0;
687
688 // Creates a timer that executes callback periodically at the specified
689 // interval and offset. Returns a PhasedLoopHandler for interacting with the
690 // timer.
691 virtual PhasedLoopHandler *AddPhasedLoop(
692 ::std::function<void(int)> callback,
693 const monotonic_clock::duration interval,
694 const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0;
695
Austin Schuh217a9782019-12-21 23:02:50 -0800696 // TODO(austin): OnExit for cleanup.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700697
Austin Schuh3054f5f2021-07-21 15:38:01 -0700698 // May be safely called from any thread.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700699 bool is_running() const { return is_running_.load(); }
700
701 // Sets the scheduler priority to run the event loop at. This may not be
702 // called after we go into "real-time-mode".
703 virtual void SetRuntimeRealtimePriority(int priority) = 0;
Austin Schuh39788ff2019-12-01 18:22:57 -0800704 virtual int priority() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700705
Brian Silverman6a54ff32020-04-28 16:41:39 -0700706 // Sets the scheduler affinity to run the event loop with. This may only be
707 // called before Run().
708 virtual void SetRuntimeAffinity(const cpu_set_t &cpuset) = 0;
709
Austin Schuh217a9782019-12-21 23:02:50 -0800710 // Fetches new messages from the provided channel (path, type).
711 //
712 // Note: this channel must be a member of the exact configuration object this
713 // was built with.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700714 virtual std::unique_ptr<RawFetcher> MakeRawFetcher(
715 const Channel *channel) = 0;
716
Austin Schuh217a9782019-12-21 23:02:50 -0800717 // Watches channel (name, type) for new messages.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700718 virtual void MakeRawWatcher(
719 const Channel *channel,
720 std::function<void(const Context &context, const void *message)>
721 watcher) = 0;
722
Brian Silverman454bc112020-03-05 14:21:25 -0800723 // Watches channel (name, type) for new messages, without needing to extract
724 // the message contents. Default implementation simply re-uses MakeRawWatcher.
725 virtual void MakeRawNoArgWatcher(
726 const Channel *channel,
727 std::function<void(const Context &context)> watcher) {
728 MakeRawWatcher(channel, [watcher](const Context &context, const void *) {
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800729 Context new_context = context;
730 new_context.data = nullptr;
Brian Silverman4f4e0612020-08-12 19:54:41 -0700731 new_context.buffer_index = -1;
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800732 watcher(new_context);
Brian Silverman454bc112020-03-05 14:21:25 -0800733 });
734 }
735
Austin Schuh217a9782019-12-21 23:02:50 -0800736 // Creates a raw sender for the provided channel. This is used for reflection
737 // based sending.
738 // Note: this ignores any node constraints. Ignore at your own peril.
739 virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0;
740
Austin Schuh6231cc32019-12-07 13:06:15 -0800741 // Returns the context for the current callback.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700742 const Context &context() const { return context_; }
743
744 // Returns the configuration that this event loop was built with.
745 const Configuration *configuration() const { return configuration_; }
746
Austin Schuh39788ff2019-12-01 18:22:57 -0800747 // Prevents the event loop from sending a timing report.
Brian Silvermanbf889922021-11-10 12:41:57 -0800748 void SkipTimingReport();
Austin Schuh39788ff2019-12-01 18:22:57 -0800749
Brian Silverman4f4e0612020-08-12 19:54:41 -0700750 // Prevents AOS_LOG being sent to message on /aos.
Tyler Chatow67ddb032020-01-12 14:30:04 -0800751 void SkipAosLog() { skip_logger_ = true; }
752
Brian Silverman4f4e0612020-08-12 19:54:41 -0700753 // Returns the number of buffers for this channel. This corresponds with the
754 // range of Context::buffer_index values for this channel.
755 virtual int NumberBuffers(const Channel *channel) = 0;
756
Austin Schuh20ac95d2020-12-05 17:24:19 -0800757 // Returns the boot UUID.
Austin Schuh83c7f702021-01-19 22:36:29 -0800758 virtual const UUID &boot_uuid() const = 0;
Austin Schuh20ac95d2020-12-05 17:24:19 -0800759
Alex Perrycb7da4b2019-08-28 19:35:56 -0700760 protected:
Austin Schuh217a9782019-12-21 23:02:50 -0800761 // Sets the name of the event loop. This is the application name.
762 virtual void set_name(const std::string_view name) = 0;
763
Alex Perrycb7da4b2019-08-28 19:35:56 -0700764 void set_is_running(bool value) { is_running_.store(value); }
765
Austin Schuh39788ff2019-12-01 18:22:57 -0800766 // Validates that channel exists inside configuration_ and finds its index.
767 int ChannelIndex(const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700768
Brian Silverman5120afb2020-01-31 17:44:35 -0800769 // Returns the state for the watcher on the corresponding channel. This
770 // watcher must exist before calling this.
771 WatcherState *GetWatcherState(const Channel *channel);
772
Brian Silverman6d2b3592020-06-18 14:40:15 -0700773 // Returns a Sender's protected RawSender.
Brian Silverman5120afb2020-01-31 17:44:35 -0800774 template <typename T>
775 static RawSender *GetRawSender(aos::Sender<T> *sender) {
776 return sender->sender_.get();
777 }
778
Brian Silverman6d2b3592020-06-18 14:40:15 -0700779 // Returns a Fetcher's protected RawFetcher.
780 template <typename T>
781 static RawFetcher *GetRawFetcher(aos::Fetcher<T> *fetcher) {
782 return fetcher->fetcher_.get();
783 }
784
Austin Schuh6231cc32019-12-07 13:06:15 -0800785 // Context available for watchers, timers, and phased loops.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700786 Context context_;
787
Austin Schuh39788ff2019-12-01 18:22:57 -0800788 friend class RawSender;
789 friend class TimerHandler;
790 friend class RawFetcher;
791 friend class PhasedLoopHandler;
792 friend class WatcherState;
793
794 // Methods used to implement timing reports.
795 void NewSender(RawSender *sender);
796 void DeleteSender(RawSender *sender);
797 TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer);
798 PhasedLoopHandler *NewPhasedLoop(
799 std::unique_ptr<PhasedLoopHandler> phased_loop);
800 void NewFetcher(RawFetcher *fetcher);
801 void DeleteFetcher(RawFetcher *fetcher);
802 WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher);
803
Brian Silverman0fc69932020-01-24 21:54:02 -0800804 // Tracks that we have a (single) watcher on the given channel.
805 void TakeWatcher(const Channel *channel);
806 // Tracks that we have at least one sender on the given channel.
807 void TakeSender(const Channel *channel);
808
Austin Schuh39788ff2019-12-01 18:22:57 -0800809 std::vector<RawSender *> senders_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800810 std::vector<RawFetcher *> fetchers_;
811
Austin Schuh39788ff2019-12-01 18:22:57 -0800812 std::vector<std::unique_ptr<TimerHandler>> timers_;
813 std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800814 std::vector<std::unique_ptr<WatcherState>> watchers_;
815
Brian Silvermance418d02021-11-03 11:25:52 -0700816 // Does nothing if timing reports are disabled.
Austin Schuh39788ff2019-12-01 18:22:57 -0800817 void SendTimingReport();
Brian Silvermance418d02021-11-03 11:25:52 -0700818
Austin Schuh39788ff2019-12-01 18:22:57 -0800819 void UpdateTimingReport();
820 void MaybeScheduleTimingReports();
821
822 std::unique_ptr<RawSender> timing_report_sender_;
823
Austin Schuh7d87b672019-12-01 20:23:49 -0800824 // Tracks which event sources (timers and watchers) have data, and which
825 // don't. Added events may not change their event_time().
826 // TODO(austin): Test case 1: timer triggers at t1, handler takes until after
827 // t2 to run, t2 should then be picked up without a context switch.
828 void AddEvent(EventLoopEvent *event);
829 void RemoveEvent(EventLoopEvent *event);
830 size_t EventCount() const { return events_.size(); }
831 EventLoopEvent *PopEvent();
832 EventLoopEvent *PeekEvent() { return events_.front(); }
833 void ReserveEvents();
834
835 std::vector<EventLoopEvent *> events_;
Brian Silvermanbd405c02020-06-23 16:25:23 -0700836 size_t event_generation_ = 1;
Austin Schuh7d87b672019-12-01 20:23:49 -0800837
Tyler Chatow67ddb032020-01-12 14:30:04 -0800838 // If true, don't send AOS_LOG to /aos
839 bool skip_logger_ = false;
840
Austin Schuha9012be2021-07-21 15:19:11 -0700841 // Sets context_ for a timed event which is supposed to happen at the provided
842 // time.
843 void SetTimerContext(monotonic_clock::time_point monotonic_event_time);
844
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800845 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800846 virtual pid_t GetTid() = 0;
847
848 FlatbufferDetachedBuffer<timing::Report> timing_report_;
849
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800850 ::std::atomic<bool> is_running_{false};
851
Alex Perrycb7da4b2019-08-28 19:35:56 -0700852 const Configuration *configuration_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800853
854 // If true, don't send out timing reports.
855 bool skip_timing_report_ = false;
Brian Silverman0fc69932020-01-24 21:54:02 -0800856
milind1f1dca32021-07-03 13:50:07 -0700857 SendFailureCounter timing_report_failure_counter_;
858
Brian Silverman0fc69932020-01-24 21:54:02 -0800859 absl::btree_set<const Channel *> taken_watchers_, taken_senders_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700860};
861
862} // namespace aos
863
Austin Schuhb8075812020-10-19 09:36:49 -0700864#include "aos/events/event_loop_tmpl.h" // IWYU pragma: export
Alex Perrycb7da4b2019-08-28 19:35:56 -0700865
866#endif // AOS_EVENTS_EVENT_LOOP_H