blob: 7f06ee0e6b7679c7bbad2b51b79a042c98614ca3 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#include "aos/events/shm_event_loop.h"
2
Alex Perrycb7da4b2019-08-28 19:35:56 -07003#include <sys/stat.h>
Austin Schuh39788ff2019-12-01 18:22:57 -08004#include <sys/syscall.h>
Alex Perrycb7da4b2019-08-28 19:35:56 -07005#include <sys/types.h>
Tyler Chatow67ddb032020-01-12 14:30:04 -08006
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include <algorithm>
8#include <atomic>
9#include <chrono>
Austin Schuh39788ff2019-12-01 18:22:57 -080010#include <iterator>
Alex Perrycb7da4b2019-08-28 19:35:56 -070011#include <stdexcept>
12
Philipp Schrader790cb542023-07-05 21:06:52 -070013#include "glog/logging.h"
14
Tyler Chatow67ddb032020-01-12 14:30:04 -080015#include "aos/events/aos_logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070016#include "aos/events/epoll.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080017#include "aos/events/event_loop_generated.h"
18#include "aos/events/timing_statistics.h"
Austin Schuh094d09b2020-11-20 23:26:52 -080019#include "aos/init.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070020#include "aos/ipc_lib/lockless_queue.h"
Austin Schuh4d275fc2022-09-16 15:42:45 -070021#include "aos/ipc_lib/memory_mapped_queue.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070022#include "aos/realtime.h"
Austin Schuh32fd5a72019-12-01 22:20:26 -080023#include "aos/stl_mutex/stl_mutex.h"
Austin Schuhfccb2d02020-01-26 16:11:19 -080024#include "aos/util/file.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070025#include "aos/util/phased_loop.h"
26
Austin Schuhe84c3ed2019-12-14 15:29:48 -080027namespace {
28
29// Returns the portion of the path after the last /. This very much assumes
30// that the application name is null terminated.
31const char *Filename(const char *path) {
32 const std::string_view path_string_view = path;
33 auto last_slash_pos = path_string_view.find_last_of("/");
34
35 return last_slash_pos == std::string_view::npos ? path
36 : path + last_slash_pos + 1;
37}
38
39} // namespace
40
Brennan Coslett6fd3c002023-07-11 17:41:09 -050041// This value is affected by the umask of the process which is calling it
42// and is set to the user's value by default (check yours running `umask` on
43// the command line).
44// Any file mode requested is transformed using: mode & ~umask and the default
45// umask is 0022 (allow any permissions for the user, dont allow writes for
46// groups or others).
47// See https://man7.org/linux/man-pages/man2/umask.2.html for more details.
48// WITH THE DEFAULT UMASK YOU WONT ACTUALLY GET THESE PERMISSIONS :)
Alex Perrycb7da4b2019-08-28 19:35:56 -070049DEFINE_uint32(permissions, 0770,
Brennan Coslett6fd3c002023-07-11 17:41:09 -050050 "Permissions to make shared memory files and folders, "
Brennan Coslettd5077bc2023-07-13 08:49:35 -050051 "affected by the process's umask. "
Brennan Coslett6fd3c002023-07-11 17:41:09 -050052 "See shm_event_loop.cc for more details.");
Austin Schuhe84c3ed2019-12-14 15:29:48 -080053DEFINE_string(application_name, Filename(program_invocation_name),
54 "The application name");
Alex Perrycb7da4b2019-08-28 19:35:56 -070055
56namespace aos {
57
Brian Silverman148d43d2020-06-07 18:19:22 -050058using namespace shm_event_loop_internal;
59
Brian Silverman4f4e0612020-08-12 19:54:41 -070060namespace {
61
Austin Schuh217a9782019-12-21 23:02:50 -080062const Node *MaybeMyNode(const Configuration *configuration) {
63 if (!configuration->has_nodes()) {
64 return nullptr;
65 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070066
Austin Schuh217a9782019-12-21 23:02:50 -080067 return configuration::GetMyNode(configuration);
68}
Alex Perrycb7da4b2019-08-28 19:35:56 -070069
Philipp Schradera8734662023-08-06 14:49:39 -070070void IgnoreWakeupSignal() {
71 struct sigaction action;
72 action.sa_handler = SIG_IGN;
73 PCHECK(sigemptyset(&action.sa_mask) == 0);
74 action.sa_flags = 0;
75 PCHECK(sigaction(ipc_lib::kWakeupSignal, &action, nullptr) == 0);
76}
77
Austin Schuh39788ff2019-12-01 18:22:57 -080078} // namespace
79
Austin Schuh217a9782019-12-21 23:02:50 -080080ShmEventLoop::ShmEventLoop(const Configuration *configuration)
Austin Schuh83c7f702021-01-19 22:36:29 -080081 : EventLoop(configuration),
82 boot_uuid_(UUID::BootUUID()),
Austin Schuhef323c02020-09-01 14:55:28 -070083 shm_base_(FLAGS_shm_base),
Austin Schuhe84c3ed2019-12-14 15:29:48 -080084 name_(FLAGS_application_name),
Austin Schuh15649d62019-12-28 16:36:38 -080085 node_(MaybeMyNode(configuration)) {
Philipp Schradera8734662023-08-06 14:49:39 -070086 // Ignore the wakeup signal by default. Otherwise, we have race conditions on
87 // shutdown where a wakeup signal will uncleanly terminate the process.
88 // See LocklessQueueWakeUpper::Wakeup() for some more information.
89 IgnoreWakeupSignal();
90
Austin Schuh094d09b2020-11-20 23:26:52 -080091 CHECK(IsInitialized()) << ": Need to initialize AOS first.";
Austin Schuh0debde12022-08-17 16:25:17 -070092 ClearContext();
Austin Schuh15649d62019-12-28 16:36:38 -080093 if (configuration->has_nodes()) {
94 CHECK(node_ != nullptr) << ": Couldn't find node in config.";
95 }
96}
Austin Schuh217a9782019-12-21 23:02:50 -080097
Brian Silverman148d43d2020-06-07 18:19:22 -050098namespace shm_event_loop_internal {
Austin Schuh39788ff2019-12-01 18:22:57 -080099
100class SimpleShmFetcher {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700101 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700102 explicit SimpleShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop,
103 const Channel *channel)
Austin Schuh432784f2020-06-23 17:27:35 -0700104 : event_loop_(event_loop),
105 channel_(channel),
Austin Schuh4d275fc2022-09-16 15:42:45 -0700106 lockless_queue_memory_(shm_base, FLAGS_permissions,
107 event_loop->configuration(), channel),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700108 reader_(lockless_queue_memory_.queue()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700109 context_.data = nullptr;
110 // Point the queue index at the next index to read starting now. This
111 // makes it such that FetchNext will read the next message sent after
112 // the fetcher is created.
113 PointAtNextQueueIndex();
114 }
115
Austin Schuh39788ff2019-12-01 18:22:57 -0800116 ~SimpleShmFetcher() {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700117
Brian Silverman77162972020-08-12 19:52:40 -0700118 // Sets this object to pin or copy data, as configured in the channel.
119 void RetrieveData() {
120 if (channel_->read_method() == ReadMethod::PIN) {
121 PinDataOnFetch();
122 } else {
123 CopyDataOnFetch();
124 }
125 }
126
Brian Silverman3bca5322020-08-12 19:35:29 -0700127 // Sets this object to copy data out of the shared memory into a private
128 // buffer when fetching.
129 void CopyDataOnFetch() {
Brian Silverman77162972020-08-12 19:52:40 -0700130 CHECK(!pin_data());
Brian Silverman3bca5322020-08-12 19:35:29 -0700131 data_storage_.reset(static_cast<char *>(
132 malloc(channel_->max_size() + kChannelDataAlignment - 1)));
133 }
134
Brian Silverman77162972020-08-12 19:52:40 -0700135 // Sets this object to pin data in shared memory when fetching.
136 void PinDataOnFetch() {
137 CHECK(!copy_data());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700138 auto maybe_pinner =
139 ipc_lib::LocklessQueuePinner::Make(lockless_queue_memory_.queue());
Brian Silverman77162972020-08-12 19:52:40 -0700140 if (!maybe_pinner) {
141 LOG(FATAL) << "Failed to create reader on "
142 << configuration::CleanedChannelToString(channel_)
143 << ", too many readers.";
144 }
145 pinner_ = std::move(maybe_pinner.value());
146 }
147
Alex Perrycb7da4b2019-08-28 19:35:56 -0700148 // Points the next message to fetch at the queue index which will be
149 // populated next.
150 void PointAtNextQueueIndex() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700151 actual_queue_index_ = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700152 if (!actual_queue_index_.valid()) {
153 // Nothing in the queue. The next element will show up at the 0th
154 // index in the queue.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700155 actual_queue_index_ = ipc_lib::QueueIndex::Zero(
156 LocklessQueueSize(lockless_queue_memory_.memory()));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700157 } else {
158 actual_queue_index_ = actual_queue_index_.Increment();
159 }
160 }
161
Austin Schuh2b4661a2023-09-20 21:37:33 -0700162 bool FetchNext() { return FetchNextIf(should_fetch_); }
Austin Schuh98ed26f2023-07-19 14:12:28 -0700163
164 bool FetchNextIf(std::function<bool(const Context &)> fn) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700165 const ipc_lib::LocklessQueueReader::Result read_result =
Austin Schuh98ed26f2023-07-19 14:12:28 -0700166 DoFetch(actual_queue_index_, std::move(fn));
Austin Schuh432784f2020-06-23 17:27:35 -0700167
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700168 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700169 }
170
Austin Schuh98ed26f2023-07-19 14:12:28 -0700171 bool FetchIf(std::function<bool(const Context &)> fn) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700172 const ipc_lib::QueueIndex queue_index = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700173 // actual_queue_index_ is only meaningful if it was set by Fetch or
174 // FetchNext. This happens when valid_data_ has been set. So, only
175 // skip checking if valid_data_ is true.
176 //
177 // Also, if the latest queue index is invalid, we are empty. So there
178 // is nothing to fetch.
Austin Schuh39788ff2019-12-01 18:22:57 -0800179 if ((context_.data != nullptr &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700180 queue_index == actual_queue_index_.DecrementBy(1u)) ||
181 !queue_index.valid()) {
182 return false;
183 }
184
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700185 const ipc_lib::LocklessQueueReader::Result read_result =
Austin Schuh98ed26f2023-07-19 14:12:28 -0700186 DoFetch(queue_index, std::move(fn));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700187
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700188 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::NOTHING_NEW)
Austin Schuhf5652592019-12-29 16:26:15 -0800189 << ": Queue index went backwards. This should never happen. "
190 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700191
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700192 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 }
194
Austin Schuh2b4661a2023-09-20 21:37:33 -0700195 bool Fetch() { return FetchIf(should_fetch_); }
Austin Schuh98ed26f2023-07-19 14:12:28 -0700196
Austin Schuh39788ff2019-12-01 18:22:57 -0800197 Context context() const { return context_; }
198
Alex Perrycb7da4b2019-08-28 19:35:56 -0700199 bool RegisterWakeup(int priority) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700200 CHECK(!watcher_);
201 watcher_ = ipc_lib::LocklessQueueWatcher::Make(
202 lockless_queue_memory_.queue(), priority);
203 return static_cast<bool>(watcher_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700204 }
205
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700206 void UnregisterWakeup() {
207 CHECK(watcher_);
208 watcher_ = std::nullopt;
209 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700210
Brian Silvermana5450a92020-08-12 19:59:57 -0700211 absl::Span<char> GetMutableSharedMemory() {
212 return lockless_queue_memory_.GetMutableSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800213 }
214
Brian Silvermana5450a92020-08-12 19:59:57 -0700215 absl::Span<const char> GetConstSharedMemory() const {
216 return lockless_queue_memory_.GetConstSharedMemory();
217 }
218
219 absl::Span<const char> GetPrivateMemory() const {
220 if (pin_data()) {
221 return lockless_queue_memory_.GetConstSharedMemory();
222 }
Brian Silverman6d2b3592020-06-18 14:40:15 -0700223 return absl::Span<char>(
224 const_cast<SimpleShmFetcher *>(this)->data_storage_start(),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700225 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()));
Brian Silverman6d2b3592020-06-18 14:40:15 -0700226 }
227
Alex Perrycb7da4b2019-08-28 19:35:56 -0700228 private:
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700229 ipc_lib::LocklessQueueReader::Result DoFetch(
Austin Schuh98ed26f2023-07-19 14:12:28 -0700230 ipc_lib::QueueIndex queue_index,
231 std::function<bool(const Context &context)> fn) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700232 // TODO(austin): Get behind and make sure it dies.
233 char *copy_buffer = nullptr;
234 if (copy_data()) {
235 copy_buffer = data_storage_start();
236 }
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700237 ipc_lib::LocklessQueueReader::Result read_result = reader_.Read(
Brian Silverman3bca5322020-08-12 19:35:29 -0700238 queue_index.index(), &context_.monotonic_event_time,
239 &context_.realtime_event_time, &context_.monotonic_remote_time,
240 &context_.realtime_remote_time, &context_.remote_queue_index,
Austin Schuh98ed26f2023-07-19 14:12:28 -0700241 &context_.source_boot_uuid, &context_.size, copy_buffer, std::move(fn));
Brian Silverman3bca5322020-08-12 19:35:29 -0700242
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700243 if (read_result == ipc_lib::LocklessQueueReader::Result::GOOD) {
Brian Silverman77162972020-08-12 19:52:40 -0700244 if (pin_data()) {
Brian Silverman4f4e0612020-08-12 19:54:41 -0700245 const int pin_result = pinner_->PinIndex(queue_index.index());
246 CHECK(pin_result >= 0)
Brian Silverman77162972020-08-12 19:52:40 -0700247 << ": Got behind while reading and the last message was modified "
248 "out from under us while we tried to pin it. Don't get so far "
249 "behind on: "
250 << configuration::CleanedChannelToString(channel_);
Brian Silverman4f4e0612020-08-12 19:54:41 -0700251 context_.buffer_index = pin_result;
252 } else {
253 context_.buffer_index = -1;
Brian Silverman77162972020-08-12 19:52:40 -0700254 }
255
Brian Silverman3bca5322020-08-12 19:35:29 -0700256 context_.queue_index = queue_index.index();
257 if (context_.remote_queue_index == 0xffffffffu) {
258 context_.remote_queue_index = context_.queue_index;
259 }
260 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
261 context_.monotonic_remote_time = context_.monotonic_event_time;
262 }
263 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
264 context_.realtime_remote_time = context_.realtime_event_time;
265 }
266 const char *const data = DataBuffer();
267 if (data) {
268 context_.data =
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700269 data +
270 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()) -
271 context_.size;
Brian Silverman3bca5322020-08-12 19:35:29 -0700272 } else {
273 context_.data = nullptr;
274 }
275 actual_queue_index_ = queue_index.Increment();
276 }
277
278 // Make sure the data wasn't modified while we were reading it. This
279 // can only happen if you are reading the last message *while* it is
280 // being written to, which means you are pretty far behind.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700281 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::OVERWROTE)
Brian Silverman3bca5322020-08-12 19:35:29 -0700282 << ": Got behind while reading and the last message was modified "
283 "out from under us while we were reading it. Don't get so far "
284 "behind on: "
285 << configuration::CleanedChannelToString(channel_);
286
287 // We fell behind between when we read the index and read the value.
288 // This isn't worth recovering from since this means we went to sleep
289 // for a long time in the middle of this function.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700290 if (read_result == ipc_lib::LocklessQueueReader::Result::TOO_OLD) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700291 event_loop_->SendTimingReport();
292 LOG(FATAL) << "The next message is no longer available. "
293 << configuration::CleanedChannelToString(channel_);
294 }
295
296 return read_result;
297 }
298
299 char *data_storage_start() const {
300 CHECK(copy_data());
Brian Silvermana1652f32020-01-29 20:41:44 -0800301 return RoundChannelData(data_storage_.get(), channel_->max_size());
302 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700303
304 // Note that for some modes the return value will change as new messages are
305 // read.
306 const char *DataBuffer() const {
307 if (copy_data()) {
308 return data_storage_start();
309 }
Brian Silverman77162972020-08-12 19:52:40 -0700310 if (pin_data()) {
311 return static_cast<const char *>(pinner_->Data());
312 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700313 return nullptr;
314 }
315
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800316 bool copy_data() const { return static_cast<bool>(data_storage_); }
Brian Silverman77162972020-08-12 19:52:40 -0700317 bool pin_data() const { return static_cast<bool>(pinner_); }
Brian Silvermana1652f32020-01-29 20:41:44 -0800318
Austin Schuh432784f2020-06-23 17:27:35 -0700319 aos::ShmEventLoop *event_loop_;
Austin Schuhf5652592019-12-29 16:26:15 -0800320 const Channel *const channel_;
Austin Schuh4d275fc2022-09-16 15:42:45 -0700321 ipc_lib::MemoryMappedQueue lockless_queue_memory_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700322 ipc_lib::LocklessQueueReader reader_;
323 // This being nullopt indicates we're not looking for wakeups right now.
324 std::optional<ipc_lib::LocklessQueueWatcher> watcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700325
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700326 ipc_lib::QueueIndex actual_queue_index_ = ipc_lib::QueueIndex::Invalid();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700327
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800328 // This being empty indicates we're not going to copy data.
329 std::unique_ptr<char, decltype(&free)> data_storage_{nullptr, &free};
Austin Schuh39788ff2019-12-01 18:22:57 -0800330
Brian Silverman77162972020-08-12 19:52:40 -0700331 // This being nullopt indicates we're not going to pin messages.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700332 std::optional<ipc_lib::LocklessQueuePinner> pinner_;
Brian Silverman77162972020-08-12 19:52:40 -0700333
Austin Schuh39788ff2019-12-01 18:22:57 -0800334 Context context_;
Austin Schuh82ea7382023-07-14 15:17:34 -0700335
336 // Pre-allocated should_fetch function so we don't allocate.
Austin Schuh98ed26f2023-07-19 14:12:28 -0700337 const std::function<bool(const Context &)> should_fetch_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800338};
339
340class ShmFetcher : public RawFetcher {
341 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700342 explicit ShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop,
343 const Channel *channel)
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800344 : RawFetcher(event_loop, channel),
Austin Schuhef323c02020-09-01 14:55:28 -0700345 simple_shm_fetcher_(shm_base, event_loop, channel) {
Brian Silverman77162972020-08-12 19:52:40 -0700346 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700347 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800348
Austin Schuh3054f5f2021-07-21 15:38:01 -0700349 ~ShmFetcher() override {
350 shm_event_loop()->CheckCurrentThread();
351 context_.data = nullptr;
352 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800353
354 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700355 shm_event_loop()->CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -0800356 if (simple_shm_fetcher_.FetchNext()) {
357 context_ = simple_shm_fetcher_.context();
358 return std::make_pair(true, monotonic_clock::now());
359 }
360 return std::make_pair(false, monotonic_clock::min_time);
361 }
362
Austin Schuh98ed26f2023-07-19 14:12:28 -0700363 std::pair<bool, monotonic_clock::time_point> DoFetchNextIf(
364 std::function<bool(const Context &context)> fn) override {
365 shm_event_loop()->CheckCurrentThread();
366 if (simple_shm_fetcher_.FetchNextIf(std::move(fn))) {
367 context_ = simple_shm_fetcher_.context();
368 return std::make_pair(true, monotonic_clock::now());
369 }
370 return std::make_pair(false, monotonic_clock::min_time);
371 }
372
Austin Schuh39788ff2019-12-01 18:22:57 -0800373 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700374 shm_event_loop()->CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -0800375 if (simple_shm_fetcher_.Fetch()) {
376 context_ = simple_shm_fetcher_.context();
377 return std::make_pair(true, monotonic_clock::now());
378 }
379 return std::make_pair(false, monotonic_clock::min_time);
380 }
381
Austin Schuh98ed26f2023-07-19 14:12:28 -0700382 std::pair<bool, monotonic_clock::time_point> DoFetchIf(
383 std::function<bool(const Context &context)> fn) override {
384 shm_event_loop()->CheckCurrentThread();
385 if (simple_shm_fetcher_.FetchIf(std::move(fn))) {
386 context_ = simple_shm_fetcher_.context();
387 return std::make_pair(true, monotonic_clock::now());
388 }
389 return std::make_pair(false, monotonic_clock::min_time);
390 }
391
Brian Silvermana5450a92020-08-12 19:59:57 -0700392 absl::Span<const char> GetPrivateMemory() const {
Brian Silverman6d2b3592020-06-18 14:40:15 -0700393 return simple_shm_fetcher_.GetPrivateMemory();
394 }
395
Austin Schuh39788ff2019-12-01 18:22:57 -0800396 private:
Austin Schuh3054f5f2021-07-21 15:38:01 -0700397 const ShmEventLoop *shm_event_loop() const {
398 return static_cast<const ShmEventLoop *>(event_loop());
399 }
400
Austin Schuh39788ff2019-12-01 18:22:57 -0800401 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700402};
403
Brian Silvermane1fe2512022-08-14 23:18:50 -0700404class ShmExitHandle : public ExitHandle {
405 public:
406 ShmExitHandle(ShmEventLoop *event_loop) : event_loop_(event_loop) {
407 ++event_loop_->exit_handle_count_;
408 }
409 ~ShmExitHandle() override {
410 CHECK_GT(event_loop_->exit_handle_count_, 0);
411 --event_loop_->exit_handle_count_;
412 }
413
414 void Exit() override { event_loop_->Exit(); }
415
416 private:
417 ShmEventLoop *const event_loop_;
418};
419
Alex Perrycb7da4b2019-08-28 19:35:56 -0700420class ShmSender : public RawSender {
421 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700422 explicit ShmSender(std::string_view shm_base, EventLoop *event_loop,
423 const Channel *channel)
Austin Schuh39788ff2019-12-01 18:22:57 -0800424 : RawSender(event_loop, channel),
Austin Schuh4d275fc2022-09-16 15:42:45 -0700425 lockless_queue_memory_(shm_base, FLAGS_permissions,
426 event_loop->configuration(), channel),
Austin Schuhfff9c3a2023-06-16 18:48:23 -0700427 lockless_queue_sender_(
428 VerifySender(ipc_lib::LocklessQueueSender::Make(
429 lockless_queue_memory_.queue(),
430 configuration::ChannelStorageDuration(
431 event_loop->configuration(), channel)),
432 channel)),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700433 wake_upper_(lockless_queue_memory_.queue()) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700434
Austin Schuh3054f5f2021-07-21 15:38:01 -0700435 ~ShmSender() override { shm_event_loop()->CheckCurrentThread(); }
Austin Schuh39788ff2019-12-01 18:22:57 -0800436
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700437 static ipc_lib::LocklessQueueSender VerifySender(
438 std::optional<ipc_lib::LocklessQueueSender> sender,
Austin Schuhe516ab02020-05-06 21:37:04 -0700439 const Channel *channel) {
440 if (sender) {
441 return std::move(sender.value());
442 }
443 LOG(FATAL) << "Failed to create sender on "
444 << configuration::CleanedChannelToString(channel)
445 << ", too many senders.";
446 }
447
Austin Schuh3054f5f2021-07-21 15:38:01 -0700448 void *data() override {
449 shm_event_loop()->CheckCurrentThread();
450 return lockless_queue_sender_.Data();
451 }
452 size_t size() override {
453 shm_event_loop()->CheckCurrentThread();
454 return lockless_queue_sender_.size();
455 }
milind1f1dca32021-07-03 13:50:07 -0700456
457 Error DoSend(size_t length,
458 aos::monotonic_clock::time_point monotonic_remote_time,
459 aos::realtime_clock::time_point realtime_remote_time,
460 uint32_t remote_queue_index,
461 const UUID &source_boot_uuid) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700462 shm_event_loop()->CheckCurrentThread();
Austin Schuh0f7ed462020-03-28 20:38:34 -0700463 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
464 << ": Sent too big a message on "
465 << configuration::CleanedChannelToString(channel());
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700466 const auto result = lockless_queue_sender_.Send(
467 length, monotonic_remote_time, realtime_remote_time, remote_queue_index,
468 source_boot_uuid, &monotonic_sent_time_, &realtime_sent_time_,
469 &sent_queue_index_);
470 CHECK_NE(result, ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE)
Austin Schuh91ba6392020-10-03 13:27:47 -0700471 << ": Somebody wrote outside the buffer of their message on channel "
472 << configuration::CleanedChannelToString(channel());
473
Austin Schuh65493d62022-08-17 15:10:37 -0700474 wake_upper_.Wakeup(event_loop()->is_running()
475 ? event_loop()->runtime_realtime_priority()
476 : 0);
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700477 return CheckLocklessQueueResult(result);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700478 }
479
milind1f1dca32021-07-03 13:50:07 -0700480 Error DoSend(const void *msg, size_t length,
481 aos::monotonic_clock::time_point monotonic_remote_time,
482 aos::realtime_clock::time_point realtime_remote_time,
483 uint32_t remote_queue_index,
484 const UUID &source_boot_uuid) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700485 shm_event_loop()->CheckCurrentThread();
Austin Schuh0f7ed462020-03-28 20:38:34 -0700486 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
487 << ": Sent too big a message on "
488 << configuration::CleanedChannelToString(channel());
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700489 const auto result = lockless_queue_sender_.Send(
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700490 reinterpret_cast<const char *>(msg), length, monotonic_remote_time,
Austin Schuha9012be2021-07-21 15:19:11 -0700491 realtime_remote_time, remote_queue_index, source_boot_uuid,
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700492 &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_);
493
494 CHECK_NE(result, ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE)
495 << ": Somebody wrote outside the buffer of their message on "
496 "channel "
Austin Schuh91ba6392020-10-03 13:27:47 -0700497 << configuration::CleanedChannelToString(channel());
Austin Schuh65493d62022-08-17 15:10:37 -0700498 wake_upper_.Wakeup(event_loop()->is_running()
499 ? event_loop()->runtime_realtime_priority()
500 : 0);
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700501
502 return CheckLocklessQueueResult(result);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700503 }
504
Brian Silverman5120afb2020-01-31 17:44:35 -0800505 absl::Span<char> GetSharedMemory() const {
Brian Silvermana5450a92020-08-12 19:59:57 -0700506 return lockless_queue_memory_.GetMutableSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800507 }
508
Austin Schuh3054f5f2021-07-21 15:38:01 -0700509 int buffer_index() override {
510 shm_event_loop()->CheckCurrentThread();
511 return lockless_queue_sender_.buffer_index();
512 }
Brian Silverman4f4e0612020-08-12 19:54:41 -0700513
Alex Perrycb7da4b2019-08-28 19:35:56 -0700514 private:
Austin Schuh3054f5f2021-07-21 15:38:01 -0700515 const ShmEventLoop *shm_event_loop() const {
516 return static_cast<const ShmEventLoop *>(event_loop());
517 }
518
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700519 RawSender::Error CheckLocklessQueueResult(
520 const ipc_lib::LocklessQueueSender::Result &result) {
521 switch (result) {
522 case ipc_lib::LocklessQueueSender::Result::GOOD:
523 return Error::kOk;
524 case ipc_lib::LocklessQueueSender::Result::MESSAGES_SENT_TOO_FAST:
525 return Error::kMessagesSentTooFast;
526 case ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE:
527 return Error::kInvalidRedzone;
528 }
529 LOG(FATAL) << "Unknown lockless queue sender result"
530 << static_cast<int>(result);
531 }
532
Austin Schuh4d275fc2022-09-16 15:42:45 -0700533 ipc_lib::MemoryMappedQueue lockless_queue_memory_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700534 ipc_lib::LocklessQueueSender lockless_queue_sender_;
535 ipc_lib::LocklessQueueWakeUpper wake_upper_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700536};
537
Alex Perrycb7da4b2019-08-28 19:35:56 -0700538// Class to manage the state for a Watcher.
Brian Silverman148d43d2020-06-07 18:19:22 -0500539class ShmWatcherState : public WatcherState {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700540 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500541 ShmWatcherState(
Austin Schuhef323c02020-09-01 14:55:28 -0700542 std::string_view shm_base, ShmEventLoop *event_loop,
543 const Channel *channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800544 std::function<void(const Context &context, const void *message)> fn,
545 bool copy_data)
Brian Silverman148d43d2020-06-07 18:19:22 -0500546 : WatcherState(event_loop, channel, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800547 event_loop_(event_loop),
548 event_(this),
Austin Schuhef323c02020-09-01 14:55:28 -0700549 simple_shm_fetcher_(shm_base, event_loop, channel) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700550 if (copy_data) {
Brian Silverman77162972020-08-12 19:52:40 -0700551 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700552 }
553 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700554
Austin Schuh3054f5f2021-07-21 15:38:01 -0700555 ~ShmWatcherState() override {
556 event_loop_->CheckCurrentThread();
557 event_loop_->RemoveEvent(&event_);
558 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800559
Philipp Schrader81fa3fb2023-09-17 18:58:35 -0700560 void Construct() override {
561 event_loop_->CheckCurrentThread();
562 CHECK(RegisterWakeup(event_loop_->runtime_realtime_priority()));
563 }
564
565 void Startup() override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700566 event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800567 simple_shm_fetcher_.PointAtNextQueueIndex();
Austin Schuh39788ff2019-12-01 18:22:57 -0800568 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700569
Alex Perrycb7da4b2019-08-28 19:35:56 -0700570 // Returns true if there is new data available.
Austin Schuh7d87b672019-12-01 20:23:49 -0800571 bool CheckForNewData() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700572 if (!has_new_data_) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800573 has_new_data_ = simple_shm_fetcher_.FetchNext();
Austin Schuh7d87b672019-12-01 20:23:49 -0800574
575 if (has_new_data_) {
576 event_.set_event_time(
Austin Schuhad154822019-12-27 15:45:13 -0800577 simple_shm_fetcher_.context().monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800578 event_loop_->AddEvent(&event_);
579 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700580 }
581
582 return has_new_data_;
583 }
584
Alex Perrycb7da4b2019-08-28 19:35:56 -0700585 // Consumes the data by calling the callback.
Austin Schuh7d87b672019-12-01 20:23:49 -0800586 void HandleEvent() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700587 CHECK(has_new_data_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800588 DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700589 has_new_data_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800590 CheckForNewData();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700591 }
592
Austin Schuh39788ff2019-12-01 18:22:57 -0800593 // Registers us to receive a signal on event reception.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700594 bool RegisterWakeup(int priority) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800595 return simple_shm_fetcher_.RegisterWakeup(priority);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700596 }
597
Austin Schuh39788ff2019-12-01 18:22:57 -0800598 void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700599
Brian Silvermana5450a92020-08-12 19:59:57 -0700600 absl::Span<const char> GetSharedMemory() const {
601 return simple_shm_fetcher_.GetConstSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800602 }
603
Alex Perrycb7da4b2019-08-28 19:35:56 -0700604 private:
605 bool has_new_data_ = false;
606
Austin Schuh7d87b672019-12-01 20:23:49 -0800607 ShmEventLoop *event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500608 EventHandler<ShmWatcherState> event_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800609 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700610};
611
612// Adapter class to adapt a timerfd to a TimerHandler.
Brian Silverman148d43d2020-06-07 18:19:22 -0500613class ShmTimerHandler final : public TimerHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700614 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500615 ShmTimerHandler(ShmEventLoop *shm_event_loop, ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -0800616 : TimerHandler(shm_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800617 shm_event_loop_(shm_event_loop),
618 event_(this) {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800619 shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() {
Austin Schuh5ca13112021-02-07 22:06:53 -0800620 // The timer may fire spuriously. HandleEvent on the event loop will
Austin Schuhcde39fd2020-02-22 20:58:24 -0800621 // call the callback if it is needed. It may also have called it when
622 // processing some other event, and the kernel decided to deliver this
623 // wakeup anyways.
624 timerfd_.Read();
625 shm_event_loop_->HandleEvent();
626 });
Alex Perrycb7da4b2019-08-28 19:35:56 -0700627 }
628
Brian Silverman148d43d2020-06-07 18:19:22 -0500629 ~ShmTimerHandler() {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700630 shm_event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800631 Disable();
632 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
633 }
634
635 void HandleEvent() {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800636 CHECK(!event_.valid());
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700637 disabled_ = false;
Austin Schuhcde39fd2020-02-22 20:58:24 -0800638 const auto monotonic_now = Call(monotonic_clock::now, base_);
639 if (event_.valid()) {
Philipp Schradera6712522023-07-05 20:25:11 -0700640 // If someone called Schedule inside Call, rescheduling is already taken
641 // care of. Bail.
Austin Schuhcde39fd2020-02-22 20:58:24 -0800642 return;
Austin Schuh7d87b672019-12-01 20:23:49 -0800643 }
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700644 if (disabled_) {
645 // Somebody called Disable inside Call, so we don't want to reschedule.
646 // Bail.
647 return;
648 }
Austin Schuh7d87b672019-12-01 20:23:49 -0800649
Austin Schuh4d275fc2022-09-16 15:42:45 -0700650 if (repeat_offset_ == std::chrono::seconds(0)) {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800651 timerfd_.Disable();
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700652 disabled_ = true;
Austin Schuhcde39fd2020-02-22 20:58:24 -0800653 } else {
654 // Compute how many cycles have elapsed and schedule the next iteration
655 // for the next iteration in the future.
656 const int elapsed_cycles =
657 std::max<int>(0, (monotonic_now - base_ + repeat_offset_ -
658 std::chrono::nanoseconds(1)) /
659 repeat_offset_);
660 base_ += repeat_offset_ * elapsed_cycles;
Austin Schuh7d87b672019-12-01 20:23:49 -0800661
Austin Schuhcde39fd2020-02-22 20:58:24 -0800662 // Update the heap and schedule the timerfd wakeup.
Austin Schuh7d87b672019-12-01 20:23:49 -0800663 event_.set_event_time(base_);
664 shm_event_loop_->AddEvent(&event_);
Austin Schuh4d275fc2022-09-16 15:42:45 -0700665 timerfd_.SetTime(base_, std::chrono::seconds(0));
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700666 disabled_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800667 }
668 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700669
Philipp Schradera6712522023-07-05 20:25:11 -0700670 void Schedule(monotonic_clock::time_point base,
671 monotonic_clock::duration repeat_offset) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700672 shm_event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800673 if (event_.valid()) {
674 shm_event_loop_->RemoveEvent(&event_);
675 }
676
Alex Perrycb7da4b2019-08-28 19:35:56 -0700677 timerfd_.SetTime(base, repeat_offset);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800678 base_ = base;
679 repeat_offset_ = repeat_offset;
Austin Schuh7d87b672019-12-01 20:23:49 -0800680 event_.set_event_time(base_);
681 shm_event_loop_->AddEvent(&event_);
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700682 disabled_ = false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700683 }
684
Austin Schuh7d87b672019-12-01 20:23:49 -0800685 void Disable() override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700686 shm_event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800687 shm_event_loop_->RemoveEvent(&event_);
688 timerfd_.Disable();
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700689 disabled_ = true;
Austin Schuh7d87b672019-12-01 20:23:49 -0800690 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700691
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700692 bool IsDisabled() override { return disabled_; }
693
Alex Perrycb7da4b2019-08-28 19:35:56 -0700694 private:
695 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500696 EventHandler<ShmTimerHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700697
Brian Silverman148d43d2020-06-07 18:19:22 -0500698 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700699
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800700 monotonic_clock::time_point base_;
701 monotonic_clock::duration repeat_offset_;
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700702
703 // Used to track if Disable() was called during the callback, so we know not
704 // to reschedule.
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700705 bool disabled_ = true;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700706};
707
708// Adapter class to the timerfd and PhasedLoop.
Brian Silverman148d43d2020-06-07 18:19:22 -0500709class ShmPhasedLoopHandler final : public PhasedLoopHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700710 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500711 ShmPhasedLoopHandler(ShmEventLoop *shm_event_loop,
712 ::std::function<void(int)> fn,
713 const monotonic_clock::duration interval,
714 const monotonic_clock::duration offset)
715 : PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset),
Austin Schuh7d87b672019-12-01 20:23:49 -0800716 shm_event_loop_(shm_event_loop),
717 event_(this) {
718 shm_event_loop_->epoll_.OnReadable(
719 timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); });
720 }
721
722 void HandleEvent() {
723 // The return value for read is the number of cycles that have elapsed.
724 // Because we check to see when this event *should* have happened, there are
725 // cases where Read() will return 0, when 1 cycle has actually happened.
726 // This occurs when the timer interrupt hasn't triggered yet. Therefore,
727 // ignore it. Call handles rescheduling and calculating elapsed cycles
728 // without any extra help.
729 timerfd_.Read();
730 event_.Invalidate();
731
James Kuszmaul20dcc7c2023-01-20 11:06:31 -0800732 Call(monotonic_clock::now);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700733 }
734
Brian Silverman148d43d2020-06-07 18:19:22 -0500735 ~ShmPhasedLoopHandler() override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700736 shm_event_loop_->CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -0800737 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
Austin Schuh7d87b672019-12-01 20:23:49 -0800738 shm_event_loop_->RemoveEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700739 }
740
741 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800742 // Reschedules the timer.
Austin Schuh39788ff2019-12-01 18:22:57 -0800743 void Schedule(monotonic_clock::time_point sleep_time) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700744 shm_event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800745 if (event_.valid()) {
746 shm_event_loop_->RemoveEvent(&event_);
747 }
748
Austin Schuh39788ff2019-12-01 18:22:57 -0800749 timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero());
Austin Schuh7d87b672019-12-01 20:23:49 -0800750 event_.set_event_time(sleep_time);
751 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700752 }
753
754 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500755 EventHandler<ShmPhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700756
Brian Silverman148d43d2020-06-07 18:19:22 -0500757 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700758};
Brian Silverman148d43d2020-06-07 18:19:22 -0500759
760} // namespace shm_event_loop_internal
Alex Perrycb7da4b2019-08-28 19:35:56 -0700761
762::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher(
763 const Channel *channel) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700764 CheckCurrentThread();
Austin Schuhca4828c2019-12-28 14:21:35 -0800765 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
766 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
767 << "\", \"type\": \"" << channel->type()->string_view()
768 << "\" } is not able to be fetched on this node. Check your "
769 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800770 }
771
Austin Schuhef323c02020-09-01 14:55:28 -0700772 return ::std::unique_ptr<RawFetcher>(
773 new ShmFetcher(shm_base_, this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700774}
775
776::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender(
777 const Channel *channel) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700778 CheckCurrentThread();
Brian Silverman0fc69932020-01-24 21:54:02 -0800779 TakeSender(channel);
Austin Schuh39788ff2019-12-01 18:22:57 -0800780
Austin Schuhef323c02020-09-01 14:55:28 -0700781 return ::std::unique_ptr<RawSender>(new ShmSender(shm_base_, this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700782}
783
784void ShmEventLoop::MakeRawWatcher(
785 const Channel *channel,
786 std::function<void(const Context &context, const void *message)> watcher) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700787 CheckCurrentThread();
Brian Silverman0fc69932020-01-24 21:54:02 -0800788 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800789
Austin Schuh39788ff2019-12-01 18:22:57 -0800790 NewWatcher(::std::unique_ptr<WatcherState>(
Austin Schuhef323c02020-09-01 14:55:28 -0700791 new ShmWatcherState(shm_base_, this, channel, std::move(watcher), true)));
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800792}
793
794void ShmEventLoop::MakeRawNoArgWatcher(
795 const Channel *channel,
796 std::function<void(const Context &context)> watcher) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700797 CheckCurrentThread();
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800798 TakeWatcher(channel);
799
Brian Silverman148d43d2020-06-07 18:19:22 -0500800 NewWatcher(::std::unique_ptr<WatcherState>(new ShmWatcherState(
Austin Schuhef323c02020-09-01 14:55:28 -0700801 shm_base_, this, channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800802 [watcher](const Context &context, const void *) { watcher(context); },
803 false)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700804}
805
806TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700807 CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -0800808 return NewTimer(::std::unique_ptr<TimerHandler>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500809 new ShmTimerHandler(this, ::std::move(callback))));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700810}
811
812PhasedLoopHandler *ShmEventLoop::AddPhasedLoop(
813 ::std::function<void(int)> callback,
814 const monotonic_clock::duration interval,
815 const monotonic_clock::duration offset) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700816 CheckCurrentThread();
Brian Silverman148d43d2020-06-07 18:19:22 -0500817 return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>(
818 new ShmPhasedLoopHandler(this, ::std::move(callback), interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700819}
820
821void ShmEventLoop::OnRun(::std::function<void()> on_run) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700822 CheckCurrentThread();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700823 on_run_.push_back(::std::move(on_run));
824}
825
Austin Schuh3054f5f2021-07-21 15:38:01 -0700826void ShmEventLoop::CheckCurrentThread() const {
827 if (__builtin_expect(check_mutex_ != nullptr, false)) {
828 CHECK(check_mutex_->is_locked())
829 << ": The configured mutex is not locked while calling a "
830 "ShmEventLoop function";
831 }
832 if (__builtin_expect(!!check_tid_, false)) {
833 CHECK_EQ(syscall(SYS_gettid), *check_tid_)
834 << ": Being called from the wrong thread";
835 }
836}
837
Austin Schuh5ca13112021-02-07 22:06:53 -0800838// This is a bit tricky because watchers can generate new events at any time (as
839// long as it's in the past). We want to check the watchers at least once before
840// declaring there are no events to handle, and we want to check them again if
841// event processing takes long enough that we find an event after that point in
842// time to handle.
Austin Schuh7d87b672019-12-01 20:23:49 -0800843void ShmEventLoop::HandleEvent() {
Austin Schuh5ca13112021-02-07 22:06:53 -0800844 // Time through which we've checked for new events in watchers.
845 monotonic_clock::time_point checked_until = monotonic_clock::min_time;
846 if (!signalfd_) {
847 // Nothing to check, so we can bail out immediately once we're out of
848 // events.
849 CHECK(watchers_.empty());
850 checked_until = monotonic_clock::max_time;
Austin Schuh7d87b672019-12-01 20:23:49 -0800851 }
852
Austin Schuh5ca13112021-02-07 22:06:53 -0800853 // Loop until we run out of events to check.
Austin Schuh39788ff2019-12-01 18:22:57 -0800854 while (true) {
Austin Schuh5ca13112021-02-07 22:06:53 -0800855 // Time of the next event we know about. If this is before checked_until, we
856 // know there aren't any new events before the next one that we already know
857 // about, so no need to check the watchers.
858 monotonic_clock::time_point next_time = monotonic_clock::max_time;
859
860 if (EventCount() == 0) {
861 if (checked_until != monotonic_clock::min_time) {
862 // No events, and we've already checked the watchers at least once, so
863 // we're all done.
864 //
865 // There's a small chance that a watcher has gotten another event in
866 // between checked_until and now. If so, then the signalfd will be
867 // triggered now and we'll re-enter HandleEvent immediately. This is
868 // unlikely though, so we don't want to spend time checking all the
869 // watchers unnecessarily.
870 break;
871 }
872 } else {
873 next_time = PeekEvent()->event_time();
874 }
Austin Schuh00cad2e2022-12-02 20:11:04 -0800875 monotonic_clock::time_point now;
876 bool new_data = false;
Austin Schuh5ca13112021-02-07 22:06:53 -0800877
878 if (next_time > checked_until) {
879 // Read all of the signals, because there's no point in waking up again
880 // immediately to handle each one if we've fallen behind.
881 //
882 // This is safe before checking for new data on the watchers. If a signal
883 // is cleared here, the corresponding CheckForNewData() call below will
884 // pick it up.
885 while (true) {
886 const signalfd_siginfo result = signalfd_->Read();
887 if (result.ssi_signo == 0) {
888 break;
889 }
890 CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal);
891 }
Austin Schuh00cad2e2022-12-02 20:11:04 -0800892 // This is the last time we can guarantee that if a message is published
893 // before, we will notice it.
894 now = monotonic_clock::now();
Austin Schuh5ca13112021-02-07 22:06:53 -0800895
896 // Check all the watchers for new events.
897 for (std::unique_ptr<WatcherState> &base_watcher : watchers_) {
898 ShmWatcherState *const watcher =
899 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
900
Austin Schuh00cad2e2022-12-02 20:11:04 -0800901 // Track if we got a message.
902 if (watcher->CheckForNewData()) {
903 new_data = true;
904 }
Austin Schuh5ca13112021-02-07 22:06:53 -0800905 }
906 if (EventCount() == 0) {
907 // Still no events, all done now.
908 break;
909 }
910
911 checked_until = now;
912 // Check for any new events we found.
913 next_time = PeekEvent()->event_time();
Austin Schuh00cad2e2022-12-02 20:11:04 -0800914 } else {
915 now = monotonic_clock::now();
Austin Schuh5ca13112021-02-07 22:06:53 -0800916 }
917
918 if (next_time > now) {
Austin Schuh00cad2e2022-12-02 20:11:04 -0800919 // Ok, we got a message with a timestamp *after* we wrote down time. We
920 // need to process it (otherwise we will go to sleep without processing
921 // it), but we also need to make sure no other messages have come in
922 // before it that we would process out of order. Just go around again to
923 // redo the checks.
924 if (new_data) {
925 continue;
926 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800927 break;
928 }
929
Austin Schuh5ca13112021-02-07 22:06:53 -0800930 EventLoopEvent *const event = PopEvent();
Austin Schuh7d87b672019-12-01 20:23:49 -0800931 event->HandleEvent();
Austin Schuh39788ff2019-12-01 18:22:57 -0800932 }
933}
934
Austin Schuh32fd5a72019-12-01 22:20:26 -0800935// RAII class to mask signals.
936class ScopedSignalMask {
937 public:
938 ScopedSignalMask(std::initializer_list<int> signals) {
939 sigset_t sigset;
940 PCHECK(sigemptyset(&sigset) == 0);
941 for (int signal : signals) {
942 PCHECK(sigaddset(&sigset, signal) == 0);
943 }
944
945 PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0);
946 }
947
948 ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); }
949
950 private:
951 sigset_t old_;
952};
953
954// Class to manage the static state associated with killing multiple event
955// loops.
956class SignalHandler {
957 public:
958 // Gets the singleton.
959 static SignalHandler *global() {
960 static SignalHandler loop;
961 return &loop;
962 }
963
964 // Handles the signal with the singleton.
965 static void HandleSignal(int) { global()->DoHandleSignal(); }
966
967 // Registers an event loop to receive Exit() calls.
968 void Register(ShmEventLoop *event_loop) {
969 // Block signals while we have the mutex so we never race with the signal
970 // handler.
971 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
972 std::unique_lock<stl_mutex> locker(mutex_);
973 if (event_loops_.size() == 0) {
974 // The first caller registers the signal handler.
975 struct sigaction new_action;
976 sigemptyset(&new_action.sa_mask);
977 // This makes it so that 2 control c's to a stuck process will kill it by
978 // restoring the original signal handler.
979 new_action.sa_flags = SA_RESETHAND;
980 new_action.sa_handler = &HandleSignal;
981
982 PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0);
983 PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0);
984 PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0);
985 }
986
987 event_loops_.push_back(event_loop);
988 }
989
990 // Unregisters an event loop to receive Exit() calls.
991 void Unregister(ShmEventLoop *event_loop) {
992 // Block signals while we have the mutex so we never race with the signal
993 // handler.
994 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
995 std::unique_lock<stl_mutex> locker(mutex_);
996
Brian Silverman5120afb2020-01-31 17:44:35 -0800997 event_loops_.erase(
998 std::find(event_loops_.begin(), event_loops_.end(), event_loop));
Austin Schuh32fd5a72019-12-01 22:20:26 -0800999
1000 if (event_loops_.size() == 0u) {
1001 // The last caller restores the original signal handlers.
1002 PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0);
1003 PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0);
1004 PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0);
1005 }
1006 }
1007
1008 private:
1009 void DoHandleSignal() {
1010 // We block signals while grabbing the lock, so there should never be a
1011 // race. Confirm that this is true using trylock.
1012 CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while "
1013 "modifing the event loop list.";
1014 for (ShmEventLoop *event_loop : event_loops_) {
1015 event_loop->Exit();
1016 }
1017 mutex_.unlock();
1018 }
1019
1020 // Mutex to protect all state.
1021 stl_mutex mutex_;
1022 std::vector<ShmEventLoop *> event_loops_;
1023 struct sigaction old_action_int_;
1024 struct sigaction old_action_hup_;
1025 struct sigaction old_action_term_;
1026};
1027
Alex Perrycb7da4b2019-08-28 19:35:56 -07001028void ShmEventLoop::Run() {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001029 CheckCurrentThread();
Austin Schuh32fd5a72019-12-01 22:20:26 -08001030 SignalHandler::global()->Register(this);
Austin Schuh39788ff2019-12-01 18:22:57 -08001031
Alex Perrycb7da4b2019-08-28 19:35:56 -07001032 if (watchers_.size() > 0) {
Austin Schuh5ca13112021-02-07 22:06:53 -08001033 signalfd_.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal}));
Alex Perrycb7da4b2019-08-28 19:35:56 -07001034
Austin Schuh5ca13112021-02-07 22:06:53 -08001035 epoll_.OnReadable(signalfd_->fd(), [this]() { HandleEvent(); });
Alex Perrycb7da4b2019-08-28 19:35:56 -07001036 }
1037
Austin Schuh39788ff2019-12-01 18:22:57 -08001038 MaybeScheduleTimingReports();
1039
Austin Schuh7d87b672019-12-01 20:23:49 -08001040 ReserveEvents();
1041
Tyler Chatow67ddb032020-01-12 14:30:04 -08001042 {
Austin Schuha0c41ba2020-09-10 22:59:14 -07001043 logging::ScopedLogRestorer prev_logger;
Tyler Chatow67ddb032020-01-12 14:30:04 -08001044 AosLogToFbs aos_logger;
1045 if (!skip_logger_) {
Austin Schuhad9e5eb2021-11-19 20:33:55 -08001046 aos_logger.Initialize(&name_, MakeSender<logging::LogMessageFbs>("/aos"));
Austin Schuha0c41ba2020-09-10 22:59:14 -07001047 prev_logger.Swap(aos_logger.implementation());
Tyler Chatow67ddb032020-01-12 14:30:04 -08001048 }
Alex Perrycb7da4b2019-08-28 19:35:56 -07001049
Tyler Chatow67ddb032020-01-12 14:30:04 -08001050 aos::SetCurrentThreadName(name_.substr(0, 16));
Brian Silverman6a54ff32020-04-28 16:41:39 -07001051 const cpu_set_t default_affinity = DefaultAffinity();
1052 if (!CPU_EQUAL(&affinity_, &default_affinity)) {
1053 ::aos::SetCurrentThreadAffinity(affinity_);
1054 }
Philipp Schrader81fa3fb2023-09-17 18:58:35 -07001055
1056 // Construct the watchers, but don't update the next pointer. This also
1057 // cleans up any watchers that previously died, and puts the nonrt work
1058 // before going realtime. After this happens, we will start queueing
1059 // signals (which may be a bit of extra work to process, but won't cause any
1060 // messages to be lost).
1061 for (::std::unique_ptr<WatcherState> &watcher : watchers_) {
1062 watcher->Construct();
1063 }
1064
Tyler Chatow67ddb032020-01-12 14:30:04 -08001065 // Now, all the callbacks are setup. Lock everything into memory and go RT.
1066 if (priority_ != 0) {
1067 ::aos::InitRT();
1068
1069 LOG(INFO) << "Setting priority to " << priority_;
1070 ::aos::SetCurrentThreadRealtimePriority(priority_);
1071 }
1072
1073 set_is_running(true);
1074
1075 // Now that we are realtime (but before the OnRun handlers run), snap the
Philipp Schrader81fa3fb2023-09-17 18:58:35 -07001076 // queue index pointer to the newest message. This happens in RT so that we
1077 // minimize the risk of losing messages.
Tyler Chatow67ddb032020-01-12 14:30:04 -08001078 for (::std::unique_ptr<WatcherState> &watcher : watchers_) {
Philipp Schrader81fa3fb2023-09-17 18:58:35 -07001079 watcher->Startup();
Tyler Chatow67ddb032020-01-12 14:30:04 -08001080 }
1081
1082 // Now that we are RT, run all the OnRun handlers.
Austin Schuha9012be2021-07-21 15:19:11 -07001083 SetTimerContext(monotonic_clock::now());
Tyler Chatow67ddb032020-01-12 14:30:04 -08001084 for (const auto &run : on_run_) {
1085 run();
1086 }
1087
1088 // And start our main event loop which runs all the timers and handles Quit.
1089 epoll_.Run();
1090
1091 // Once epoll exits, there is no useful nonrt work left to do.
1092 set_is_running(false);
1093
1094 // Nothing time or synchronization critical needs to happen after this
1095 // point. Drop RT priority.
1096 ::aos::UnsetCurrentThreadRealtimePriority();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001097 }
1098
Austin Schuh39788ff2019-12-01 18:22:57 -08001099 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -05001100 ShmWatcherState *watcher =
1101 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -07001102 watcher->UnregisterWakeup();
1103 }
1104
1105 if (watchers_.size() > 0) {
Austin Schuh5ca13112021-02-07 22:06:53 -08001106 epoll_.DeleteFd(signalfd_->fd());
1107 signalfd_.reset();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001108 }
Austin Schuh32fd5a72019-12-01 22:20:26 -08001109
1110 SignalHandler::global()->Unregister(this);
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001111
1112 // Trigger any remaining senders or fetchers to be cleared before destroying
1113 // the event loop so the book keeping matches. Do this in the thread that
1114 // created the timing reporter.
1115 timing_report_sender_.reset();
Austin Schuh0debde12022-08-17 16:25:17 -07001116 ClearContext();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001117}
1118
1119void ShmEventLoop::Exit() { epoll_.Quit(); }
1120
Brian Silvermane1fe2512022-08-14 23:18:50 -07001121std::unique_ptr<ExitHandle> ShmEventLoop::MakeExitHandle() {
1122 return std::make_unique<ShmExitHandle>(this);
1123}
1124
Alex Perrycb7da4b2019-08-28 19:35:56 -07001125ShmEventLoop::~ShmEventLoop() {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001126 CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -08001127 // Force everything with a registered fd with epoll to be destroyed now.
1128 timers_.clear();
1129 phased_loops_.clear();
1130 watchers_.clear();
1131
Alex Perrycb7da4b2019-08-28 19:35:56 -07001132 CHECK(!is_running()) << ": ShmEventLoop destroyed while running";
Brian Silvermane1fe2512022-08-14 23:18:50 -07001133 CHECK_EQ(0, exit_handle_count_)
1134 << ": All ExitHandles must be destroyed before the ShmEventLoop";
Alex Perrycb7da4b2019-08-28 19:35:56 -07001135}
1136
Alex Perrycb7da4b2019-08-28 19:35:56 -07001137void ShmEventLoop::SetRuntimeRealtimePriority(int priority) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001138 CheckCurrentThread();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001139 if (is_running()) {
1140 LOG(FATAL) << "Cannot set realtime priority while running.";
1141 }
1142 priority_ = priority;
1143}
1144
Brian Silverman6a54ff32020-04-28 16:41:39 -07001145void ShmEventLoop::SetRuntimeAffinity(const cpu_set_t &cpuset) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001146 CheckCurrentThread();
Brian Silverman6a54ff32020-04-28 16:41:39 -07001147 if (is_running()) {
1148 LOG(FATAL) << "Cannot set affinity while running.";
1149 }
1150 affinity_ = cpuset;
1151}
1152
James Kuszmaul57c2baa2020-01-19 14:52:52 -08001153void ShmEventLoop::set_name(const std::string_view name) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001154 CheckCurrentThread();
James Kuszmaul57c2baa2020-01-19 14:52:52 -08001155 name_ = std::string(name);
1156 UpdateTimingReport();
1157}
1158
Brian Silvermana5450a92020-08-12 19:59:57 -07001159absl::Span<const char> ShmEventLoop::GetWatcherSharedMemory(
1160 const Channel *channel) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001161 CheckCurrentThread();
Brian Silverman148d43d2020-06-07 18:19:22 -05001162 ShmWatcherState *const watcher_state =
1163 static_cast<ShmWatcherState *>(GetWatcherState(channel));
Brian Silverman5120afb2020-01-31 17:44:35 -08001164 return watcher_state->GetSharedMemory();
1165}
1166
Brian Silverman4f4e0612020-08-12 19:54:41 -07001167int ShmEventLoop::NumberBuffers(const Channel *channel) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001168 CheckCurrentThread();
Austin Schuh4d275fc2022-09-16 15:42:45 -07001169 return ipc_lib::MakeQueueConfiguration(configuration(), channel)
1170 .num_messages();
Brian Silverman4f4e0612020-08-12 19:54:41 -07001171}
1172
Brian Silverman5120afb2020-01-31 17:44:35 -08001173absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory(
1174 const aos::RawSender *sender) const {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001175 CheckCurrentThread();
Brian Silverman148d43d2020-06-07 18:19:22 -05001176 return static_cast<const ShmSender *>(sender)->GetSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -08001177}
1178
Brian Silvermana5450a92020-08-12 19:59:57 -07001179absl::Span<const char> ShmEventLoop::GetShmFetcherPrivateMemory(
Brian Silverman6d2b3592020-06-18 14:40:15 -07001180 const aos::RawFetcher *fetcher) const {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001181 CheckCurrentThread();
Brian Silverman6d2b3592020-06-18 14:40:15 -07001182 return static_cast<const ShmFetcher *>(fetcher)->GetPrivateMemory();
1183}
1184
Austin Schuh3054f5f2021-07-21 15:38:01 -07001185pid_t ShmEventLoop::GetTid() {
1186 CheckCurrentThread();
1187 return syscall(SYS_gettid);
1188}
Austin Schuh39788ff2019-12-01 18:22:57 -08001189
Alex Perrycb7da4b2019-08-28 19:35:56 -07001190} // namespace aos