blob: 0dc23d93bbe8103aac8f8c493718954c3c90d515 [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
Alex Perrycb7da4b2019-08-28 19:35:56 -070041DEFINE_string(shm_base, "/dev/shm/aos",
42 "Directory to place queue backing mmaped files in.");
Brennan Coslett6fd3c002023-07-11 17:41:09 -050043// This value is affected by the umask of the process which is calling it
44// and is set to the user's value by default (check yours running `umask` on
45// the command line).
46// Any file mode requested is transformed using: mode & ~umask and the default
47// umask is 0022 (allow any permissions for the user, dont allow writes for
48// groups or others).
49// See https://man7.org/linux/man-pages/man2/umask.2.html for more details.
50// WITH THE DEFAULT UMASK YOU WONT ACTUALLY GET THESE PERMISSIONS :)
Alex Perrycb7da4b2019-08-28 19:35:56 -070051DEFINE_uint32(permissions, 0770,
Brennan Coslett6fd3c002023-07-11 17:41:09 -050052 "Permissions to make shared memory files and folders, "
Brennan Coslettd5077bc2023-07-13 08:49:35 -050053 "affected by the process's umask. "
Brennan Coslett6fd3c002023-07-11 17:41:09 -050054 "See shm_event_loop.cc for more details.");
Austin Schuhe84c3ed2019-12-14 15:29:48 -080055DEFINE_string(application_name, Filename(program_invocation_name),
56 "The application name");
Alex Perrycb7da4b2019-08-28 19:35:56 -070057
58namespace aos {
59
Brian Silverman148d43d2020-06-07 18:19:22 -050060using namespace shm_event_loop_internal;
61
Austin Schuhcdab6192019-12-29 17:47:46 -080062void SetShmBase(const std::string_view base) {
Austin Schuhef323c02020-09-01 14:55:28 -070063 FLAGS_shm_base = std::string(base) + "/aos";
Austin Schuhcdab6192019-12-29 17:47:46 -080064}
65
Brian Silverman4f4e0612020-08-12 19:54:41 -070066namespace {
67
Austin Schuh217a9782019-12-21 23:02:50 -080068const Node *MaybeMyNode(const Configuration *configuration) {
69 if (!configuration->has_nodes()) {
70 return nullptr;
71 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070072
Austin Schuh217a9782019-12-21 23:02:50 -080073 return configuration::GetMyNode(configuration);
74}
Alex Perrycb7da4b2019-08-28 19:35:56 -070075
Philipp Schradera8734662023-08-06 14:49:39 -070076void IgnoreWakeupSignal() {
77 struct sigaction action;
78 action.sa_handler = SIG_IGN;
79 PCHECK(sigemptyset(&action.sa_mask) == 0);
80 action.sa_flags = 0;
81 PCHECK(sigaction(ipc_lib::kWakeupSignal, &action, nullptr) == 0);
82}
83
Austin Schuh39788ff2019-12-01 18:22:57 -080084} // namespace
85
Austin Schuh217a9782019-12-21 23:02:50 -080086ShmEventLoop::ShmEventLoop(const Configuration *configuration)
Austin Schuh83c7f702021-01-19 22:36:29 -080087 : EventLoop(configuration),
88 boot_uuid_(UUID::BootUUID()),
Austin Schuhef323c02020-09-01 14:55:28 -070089 shm_base_(FLAGS_shm_base),
Austin Schuhe84c3ed2019-12-14 15:29:48 -080090 name_(FLAGS_application_name),
Austin Schuh15649d62019-12-28 16:36:38 -080091 node_(MaybeMyNode(configuration)) {
Philipp Schradera8734662023-08-06 14:49:39 -070092 // Ignore the wakeup signal by default. Otherwise, we have race conditions on
93 // shutdown where a wakeup signal will uncleanly terminate the process.
94 // See LocklessQueueWakeUpper::Wakeup() for some more information.
95 IgnoreWakeupSignal();
96
Austin Schuh094d09b2020-11-20 23:26:52 -080097 CHECK(IsInitialized()) << ": Need to initialize AOS first.";
Austin Schuh0debde12022-08-17 16:25:17 -070098 ClearContext();
Austin Schuh15649d62019-12-28 16:36:38 -080099 if (configuration->has_nodes()) {
100 CHECK(node_ != nullptr) << ": Couldn't find node in config.";
101 }
102}
Austin Schuh217a9782019-12-21 23:02:50 -0800103
Brian Silverman148d43d2020-06-07 18:19:22 -0500104namespace shm_event_loop_internal {
Austin Schuh39788ff2019-12-01 18:22:57 -0800105
106class SimpleShmFetcher {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700107 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700108 explicit SimpleShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop,
109 const Channel *channel)
Austin Schuh432784f2020-06-23 17:27:35 -0700110 : event_loop_(event_loop),
111 channel_(channel),
Austin Schuh4d275fc2022-09-16 15:42:45 -0700112 lockless_queue_memory_(shm_base, FLAGS_permissions,
113 event_loop->configuration(), channel),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700114 reader_(lockless_queue_memory_.queue()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700115 context_.data = nullptr;
116 // Point the queue index at the next index to read starting now. This
117 // makes it such that FetchNext will read the next message sent after
118 // the fetcher is created.
119 PointAtNextQueueIndex();
120 }
121
Austin Schuh39788ff2019-12-01 18:22:57 -0800122 ~SimpleShmFetcher() {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700123
Brian Silverman77162972020-08-12 19:52:40 -0700124 // Sets this object to pin or copy data, as configured in the channel.
125 void RetrieveData() {
126 if (channel_->read_method() == ReadMethod::PIN) {
127 PinDataOnFetch();
128 } else {
129 CopyDataOnFetch();
130 }
131 }
132
Brian Silverman3bca5322020-08-12 19:35:29 -0700133 // Sets this object to copy data out of the shared memory into a private
134 // buffer when fetching.
135 void CopyDataOnFetch() {
Brian Silverman77162972020-08-12 19:52:40 -0700136 CHECK(!pin_data());
Brian Silverman3bca5322020-08-12 19:35:29 -0700137 data_storage_.reset(static_cast<char *>(
138 malloc(channel_->max_size() + kChannelDataAlignment - 1)));
139 }
140
Brian Silverman77162972020-08-12 19:52:40 -0700141 // Sets this object to pin data in shared memory when fetching.
142 void PinDataOnFetch() {
143 CHECK(!copy_data());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700144 auto maybe_pinner =
145 ipc_lib::LocklessQueuePinner::Make(lockless_queue_memory_.queue());
Brian Silverman77162972020-08-12 19:52:40 -0700146 if (!maybe_pinner) {
147 LOG(FATAL) << "Failed to create reader on "
148 << configuration::CleanedChannelToString(channel_)
149 << ", too many readers.";
150 }
151 pinner_ = std::move(maybe_pinner.value());
152 }
153
Alex Perrycb7da4b2019-08-28 19:35:56 -0700154 // Points the next message to fetch at the queue index which will be
155 // populated next.
156 void PointAtNextQueueIndex() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700157 actual_queue_index_ = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700158 if (!actual_queue_index_.valid()) {
159 // Nothing in the queue. The next element will show up at the 0th
160 // index in the queue.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700161 actual_queue_index_ = ipc_lib::QueueIndex::Zero(
162 LocklessQueueSize(lockless_queue_memory_.memory()));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700163 } else {
164 actual_queue_index_ = actual_queue_index_.Increment();
165 }
166 }
167
Austin Schuh98ed26f2023-07-19 14:12:28 -0700168 bool FetchNext() { return FetchNextIf(std::ref(should_fetch_)); }
169
170 bool FetchNextIf(std::function<bool(const Context &)> fn) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700171 const ipc_lib::LocklessQueueReader::Result read_result =
Austin Schuh98ed26f2023-07-19 14:12:28 -0700172 DoFetch(actual_queue_index_, std::move(fn));
Austin Schuh432784f2020-06-23 17:27:35 -0700173
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700174 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700175 }
176
Austin Schuh98ed26f2023-07-19 14:12:28 -0700177 bool FetchIf(std::function<bool(const Context &)> fn) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700178 const ipc_lib::QueueIndex queue_index = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700179 // actual_queue_index_ is only meaningful if it was set by Fetch or
180 // FetchNext. This happens when valid_data_ has been set. So, only
181 // skip checking if valid_data_ is true.
182 //
183 // Also, if the latest queue index is invalid, we are empty. So there
184 // is nothing to fetch.
Austin Schuh39788ff2019-12-01 18:22:57 -0800185 if ((context_.data != nullptr &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700186 queue_index == actual_queue_index_.DecrementBy(1u)) ||
187 !queue_index.valid()) {
188 return false;
189 }
190
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700191 const ipc_lib::LocklessQueueReader::Result read_result =
Austin Schuh98ed26f2023-07-19 14:12:28 -0700192 DoFetch(queue_index, std::move(fn));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700194 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::NOTHING_NEW)
Austin Schuhf5652592019-12-29 16:26:15 -0800195 << ": Queue index went backwards. This should never happen. "
196 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700197
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700198 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700199 }
200
Austin Schuh98ed26f2023-07-19 14:12:28 -0700201 bool Fetch() { return FetchIf(std::ref(should_fetch_)); }
202
Austin Schuh39788ff2019-12-01 18:22:57 -0800203 Context context() const { return context_; }
204
Alex Perrycb7da4b2019-08-28 19:35:56 -0700205 bool RegisterWakeup(int priority) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700206 CHECK(!watcher_);
207 watcher_ = ipc_lib::LocklessQueueWatcher::Make(
208 lockless_queue_memory_.queue(), priority);
209 return static_cast<bool>(watcher_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700210 }
211
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700212 void UnregisterWakeup() {
213 CHECK(watcher_);
214 watcher_ = std::nullopt;
215 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700216
Brian Silvermana5450a92020-08-12 19:59:57 -0700217 absl::Span<char> GetMutableSharedMemory() {
218 return lockless_queue_memory_.GetMutableSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800219 }
220
Brian Silvermana5450a92020-08-12 19:59:57 -0700221 absl::Span<const char> GetConstSharedMemory() const {
222 return lockless_queue_memory_.GetConstSharedMemory();
223 }
224
225 absl::Span<const char> GetPrivateMemory() const {
226 if (pin_data()) {
227 return lockless_queue_memory_.GetConstSharedMemory();
228 }
Brian Silverman6d2b3592020-06-18 14:40:15 -0700229 return absl::Span<char>(
230 const_cast<SimpleShmFetcher *>(this)->data_storage_start(),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700231 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()));
Brian Silverman6d2b3592020-06-18 14:40:15 -0700232 }
233
Alex Perrycb7da4b2019-08-28 19:35:56 -0700234 private:
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700235 ipc_lib::LocklessQueueReader::Result DoFetch(
Austin Schuh98ed26f2023-07-19 14:12:28 -0700236 ipc_lib::QueueIndex queue_index,
237 std::function<bool(const Context &context)> fn) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700238 // TODO(austin): Get behind and make sure it dies.
239 char *copy_buffer = nullptr;
240 if (copy_data()) {
241 copy_buffer = data_storage_start();
242 }
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700243 ipc_lib::LocklessQueueReader::Result read_result = reader_.Read(
Brian Silverman3bca5322020-08-12 19:35:29 -0700244 queue_index.index(), &context_.monotonic_event_time,
245 &context_.realtime_event_time, &context_.monotonic_remote_time,
246 &context_.realtime_remote_time, &context_.remote_queue_index,
Austin Schuh98ed26f2023-07-19 14:12:28 -0700247 &context_.source_boot_uuid, &context_.size, copy_buffer, std::move(fn));
Brian Silverman3bca5322020-08-12 19:35:29 -0700248
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700249 if (read_result == ipc_lib::LocklessQueueReader::Result::GOOD) {
Brian Silverman77162972020-08-12 19:52:40 -0700250 if (pin_data()) {
Brian Silverman4f4e0612020-08-12 19:54:41 -0700251 const int pin_result = pinner_->PinIndex(queue_index.index());
252 CHECK(pin_result >= 0)
Brian Silverman77162972020-08-12 19:52:40 -0700253 << ": Got behind while reading and the last message was modified "
254 "out from under us while we tried to pin it. Don't get so far "
255 "behind on: "
256 << configuration::CleanedChannelToString(channel_);
Brian Silverman4f4e0612020-08-12 19:54:41 -0700257 context_.buffer_index = pin_result;
258 } else {
259 context_.buffer_index = -1;
Brian Silverman77162972020-08-12 19:52:40 -0700260 }
261
Brian Silverman3bca5322020-08-12 19:35:29 -0700262 context_.queue_index = queue_index.index();
263 if (context_.remote_queue_index == 0xffffffffu) {
264 context_.remote_queue_index = context_.queue_index;
265 }
266 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
267 context_.monotonic_remote_time = context_.monotonic_event_time;
268 }
269 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
270 context_.realtime_remote_time = context_.realtime_event_time;
271 }
272 const char *const data = DataBuffer();
273 if (data) {
274 context_.data =
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700275 data +
276 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()) -
277 context_.size;
Brian Silverman3bca5322020-08-12 19:35:29 -0700278 } else {
279 context_.data = nullptr;
280 }
281 actual_queue_index_ = queue_index.Increment();
282 }
283
284 // Make sure the data wasn't modified while we were reading it. This
285 // can only happen if you are reading the last message *while* it is
286 // being written to, which means you are pretty far behind.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700287 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::OVERWROTE)
Brian Silverman3bca5322020-08-12 19:35:29 -0700288 << ": Got behind while reading and the last message was modified "
289 "out from under us while we were reading it. Don't get so far "
290 "behind on: "
291 << configuration::CleanedChannelToString(channel_);
292
293 // We fell behind between when we read the index and read the value.
294 // This isn't worth recovering from since this means we went to sleep
295 // for a long time in the middle of this function.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700296 if (read_result == ipc_lib::LocklessQueueReader::Result::TOO_OLD) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700297 event_loop_->SendTimingReport();
298 LOG(FATAL) << "The next message is no longer available. "
299 << configuration::CleanedChannelToString(channel_);
300 }
301
302 return read_result;
303 }
304
305 char *data_storage_start() const {
306 CHECK(copy_data());
Brian Silvermana1652f32020-01-29 20:41:44 -0800307 return RoundChannelData(data_storage_.get(), channel_->max_size());
308 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700309
310 // Note that for some modes the return value will change as new messages are
311 // read.
312 const char *DataBuffer() const {
313 if (copy_data()) {
314 return data_storage_start();
315 }
Brian Silverman77162972020-08-12 19:52:40 -0700316 if (pin_data()) {
317 return static_cast<const char *>(pinner_->Data());
318 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700319 return nullptr;
320 }
321
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800322 bool copy_data() const { return static_cast<bool>(data_storage_); }
Brian Silverman77162972020-08-12 19:52:40 -0700323 bool pin_data() const { return static_cast<bool>(pinner_); }
Brian Silvermana1652f32020-01-29 20:41:44 -0800324
Austin Schuh432784f2020-06-23 17:27:35 -0700325 aos::ShmEventLoop *event_loop_;
Austin Schuhf5652592019-12-29 16:26:15 -0800326 const Channel *const channel_;
Austin Schuh4d275fc2022-09-16 15:42:45 -0700327 ipc_lib::MemoryMappedQueue lockless_queue_memory_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700328 ipc_lib::LocklessQueueReader reader_;
329 // This being nullopt indicates we're not looking for wakeups right now.
330 std::optional<ipc_lib::LocklessQueueWatcher> watcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700331
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700332 ipc_lib::QueueIndex actual_queue_index_ = ipc_lib::QueueIndex::Invalid();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700333
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800334 // This being empty indicates we're not going to copy data.
335 std::unique_ptr<char, decltype(&free)> data_storage_{nullptr, &free};
Austin Schuh39788ff2019-12-01 18:22:57 -0800336
Brian Silverman77162972020-08-12 19:52:40 -0700337 // This being nullopt indicates we're not going to pin messages.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700338 std::optional<ipc_lib::LocklessQueuePinner> pinner_;
Brian Silverman77162972020-08-12 19:52:40 -0700339
Austin Schuh39788ff2019-12-01 18:22:57 -0800340 Context context_;
Austin Schuh82ea7382023-07-14 15:17:34 -0700341
342 // Pre-allocated should_fetch function so we don't allocate.
Austin Schuh98ed26f2023-07-19 14:12:28 -0700343 const std::function<bool(const Context &)> should_fetch_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800344};
345
346class ShmFetcher : public RawFetcher {
347 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700348 explicit ShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop,
349 const Channel *channel)
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800350 : RawFetcher(event_loop, channel),
Austin Schuhef323c02020-09-01 14:55:28 -0700351 simple_shm_fetcher_(shm_base, event_loop, channel) {
Brian Silverman77162972020-08-12 19:52:40 -0700352 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700353 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800354
Austin Schuh3054f5f2021-07-21 15:38:01 -0700355 ~ShmFetcher() override {
356 shm_event_loop()->CheckCurrentThread();
357 context_.data = nullptr;
358 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800359
360 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700361 shm_event_loop()->CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -0800362 if (simple_shm_fetcher_.FetchNext()) {
363 context_ = simple_shm_fetcher_.context();
364 return std::make_pair(true, monotonic_clock::now());
365 }
366 return std::make_pair(false, monotonic_clock::min_time);
367 }
368
Austin Schuh98ed26f2023-07-19 14:12:28 -0700369 std::pair<bool, monotonic_clock::time_point> DoFetchNextIf(
370 std::function<bool(const Context &context)> fn) override {
371 shm_event_loop()->CheckCurrentThread();
372 if (simple_shm_fetcher_.FetchNextIf(std::move(fn))) {
373 context_ = simple_shm_fetcher_.context();
374 return std::make_pair(true, monotonic_clock::now());
375 }
376 return std::make_pair(false, monotonic_clock::min_time);
377 }
378
Austin Schuh39788ff2019-12-01 18:22:57 -0800379 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700380 shm_event_loop()->CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -0800381 if (simple_shm_fetcher_.Fetch()) {
382 context_ = simple_shm_fetcher_.context();
383 return std::make_pair(true, monotonic_clock::now());
384 }
385 return std::make_pair(false, monotonic_clock::min_time);
386 }
387
Austin Schuh98ed26f2023-07-19 14:12:28 -0700388 std::pair<bool, monotonic_clock::time_point> DoFetchIf(
389 std::function<bool(const Context &context)> fn) override {
390 shm_event_loop()->CheckCurrentThread();
391 if (simple_shm_fetcher_.FetchIf(std::move(fn))) {
392 context_ = simple_shm_fetcher_.context();
393 return std::make_pair(true, monotonic_clock::now());
394 }
395 return std::make_pair(false, monotonic_clock::min_time);
396 }
397
Brian Silvermana5450a92020-08-12 19:59:57 -0700398 absl::Span<const char> GetPrivateMemory() const {
Brian Silverman6d2b3592020-06-18 14:40:15 -0700399 return simple_shm_fetcher_.GetPrivateMemory();
400 }
401
Austin Schuh39788ff2019-12-01 18:22:57 -0800402 private:
Austin Schuh3054f5f2021-07-21 15:38:01 -0700403 const ShmEventLoop *shm_event_loop() const {
404 return static_cast<const ShmEventLoop *>(event_loop());
405 }
406
Austin Schuh39788ff2019-12-01 18:22:57 -0800407 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700408};
409
Brian Silvermane1fe2512022-08-14 23:18:50 -0700410class ShmExitHandle : public ExitHandle {
411 public:
412 ShmExitHandle(ShmEventLoop *event_loop) : event_loop_(event_loop) {
413 ++event_loop_->exit_handle_count_;
414 }
415 ~ShmExitHandle() override {
416 CHECK_GT(event_loop_->exit_handle_count_, 0);
417 --event_loop_->exit_handle_count_;
418 }
419
420 void Exit() override { event_loop_->Exit(); }
421
422 private:
423 ShmEventLoop *const event_loop_;
424};
425
Alex Perrycb7da4b2019-08-28 19:35:56 -0700426class ShmSender : public RawSender {
427 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700428 explicit ShmSender(std::string_view shm_base, EventLoop *event_loop,
429 const Channel *channel)
Austin Schuh39788ff2019-12-01 18:22:57 -0800430 : RawSender(event_loop, channel),
Austin Schuh4d275fc2022-09-16 15:42:45 -0700431 lockless_queue_memory_(shm_base, FLAGS_permissions,
432 event_loop->configuration(), channel),
Austin Schuhfff9c3a2023-06-16 18:48:23 -0700433 lockless_queue_sender_(
434 VerifySender(ipc_lib::LocklessQueueSender::Make(
435 lockless_queue_memory_.queue(),
436 configuration::ChannelStorageDuration(
437 event_loop->configuration(), channel)),
438 channel)),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700439 wake_upper_(lockless_queue_memory_.queue()) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700440
Austin Schuh3054f5f2021-07-21 15:38:01 -0700441 ~ShmSender() override { shm_event_loop()->CheckCurrentThread(); }
Austin Schuh39788ff2019-12-01 18:22:57 -0800442
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700443 static ipc_lib::LocklessQueueSender VerifySender(
444 std::optional<ipc_lib::LocklessQueueSender> sender,
Austin Schuhe516ab02020-05-06 21:37:04 -0700445 const Channel *channel) {
446 if (sender) {
447 return std::move(sender.value());
448 }
449 LOG(FATAL) << "Failed to create sender on "
450 << configuration::CleanedChannelToString(channel)
451 << ", too many senders.";
452 }
453
Austin Schuh3054f5f2021-07-21 15:38:01 -0700454 void *data() override {
455 shm_event_loop()->CheckCurrentThread();
456 return lockless_queue_sender_.Data();
457 }
458 size_t size() override {
459 shm_event_loop()->CheckCurrentThread();
460 return lockless_queue_sender_.size();
461 }
milind1f1dca32021-07-03 13:50:07 -0700462
463 Error DoSend(size_t length,
464 aos::monotonic_clock::time_point monotonic_remote_time,
465 aos::realtime_clock::time_point realtime_remote_time,
466 uint32_t remote_queue_index,
467 const UUID &source_boot_uuid) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700468 shm_event_loop()->CheckCurrentThread();
Austin Schuh0f7ed462020-03-28 20:38:34 -0700469 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
470 << ": Sent too big a message on "
471 << configuration::CleanedChannelToString(channel());
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700472 const auto result = lockless_queue_sender_.Send(
473 length, monotonic_remote_time, realtime_remote_time, remote_queue_index,
474 source_boot_uuid, &monotonic_sent_time_, &realtime_sent_time_,
475 &sent_queue_index_);
476 CHECK_NE(result, ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE)
Austin Schuh91ba6392020-10-03 13:27:47 -0700477 << ": Somebody wrote outside the buffer of their message on channel "
478 << configuration::CleanedChannelToString(channel());
479
Austin Schuh65493d62022-08-17 15:10:37 -0700480 wake_upper_.Wakeup(event_loop()->is_running()
481 ? event_loop()->runtime_realtime_priority()
482 : 0);
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700483 return CheckLocklessQueueResult(result);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700484 }
485
milind1f1dca32021-07-03 13:50:07 -0700486 Error DoSend(const void *msg, size_t length,
487 aos::monotonic_clock::time_point monotonic_remote_time,
488 aos::realtime_clock::time_point realtime_remote_time,
489 uint32_t remote_queue_index,
490 const UUID &source_boot_uuid) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700491 shm_event_loop()->CheckCurrentThread();
Austin Schuh0f7ed462020-03-28 20:38:34 -0700492 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
493 << ": Sent too big a message on "
494 << configuration::CleanedChannelToString(channel());
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700495 const auto result = lockless_queue_sender_.Send(
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700496 reinterpret_cast<const char *>(msg), length, monotonic_remote_time,
Austin Schuha9012be2021-07-21 15:19:11 -0700497 realtime_remote_time, remote_queue_index, source_boot_uuid,
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700498 &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_);
499
500 CHECK_NE(result, ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE)
501 << ": Somebody wrote outside the buffer of their message on "
502 "channel "
Austin Schuh91ba6392020-10-03 13:27:47 -0700503 << configuration::CleanedChannelToString(channel());
Austin Schuh65493d62022-08-17 15:10:37 -0700504 wake_upper_.Wakeup(event_loop()->is_running()
505 ? event_loop()->runtime_realtime_priority()
506 : 0);
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700507
508 return CheckLocklessQueueResult(result);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700509 }
510
Brian Silverman5120afb2020-01-31 17:44:35 -0800511 absl::Span<char> GetSharedMemory() const {
Brian Silvermana5450a92020-08-12 19:59:57 -0700512 return lockless_queue_memory_.GetMutableSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800513 }
514
Austin Schuh3054f5f2021-07-21 15:38:01 -0700515 int buffer_index() override {
516 shm_event_loop()->CheckCurrentThread();
517 return lockless_queue_sender_.buffer_index();
518 }
Brian Silverman4f4e0612020-08-12 19:54:41 -0700519
Alex Perrycb7da4b2019-08-28 19:35:56 -0700520 private:
Austin Schuh3054f5f2021-07-21 15:38:01 -0700521 const ShmEventLoop *shm_event_loop() const {
522 return static_cast<const ShmEventLoop *>(event_loop());
523 }
524
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700525 RawSender::Error CheckLocklessQueueResult(
526 const ipc_lib::LocklessQueueSender::Result &result) {
527 switch (result) {
528 case ipc_lib::LocklessQueueSender::Result::GOOD:
529 return Error::kOk;
530 case ipc_lib::LocklessQueueSender::Result::MESSAGES_SENT_TOO_FAST:
531 return Error::kMessagesSentTooFast;
532 case ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE:
533 return Error::kInvalidRedzone;
534 }
535 LOG(FATAL) << "Unknown lockless queue sender result"
536 << static_cast<int>(result);
537 }
538
Austin Schuh4d275fc2022-09-16 15:42:45 -0700539 ipc_lib::MemoryMappedQueue lockless_queue_memory_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700540 ipc_lib::LocklessQueueSender lockless_queue_sender_;
541 ipc_lib::LocklessQueueWakeUpper wake_upper_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700542};
543
Alex Perrycb7da4b2019-08-28 19:35:56 -0700544// Class to manage the state for a Watcher.
Brian Silverman148d43d2020-06-07 18:19:22 -0500545class ShmWatcherState : public WatcherState {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700546 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500547 ShmWatcherState(
Austin Schuhef323c02020-09-01 14:55:28 -0700548 std::string_view shm_base, ShmEventLoop *event_loop,
549 const Channel *channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800550 std::function<void(const Context &context, const void *message)> fn,
551 bool copy_data)
Brian Silverman148d43d2020-06-07 18:19:22 -0500552 : WatcherState(event_loop, channel, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800553 event_loop_(event_loop),
554 event_(this),
Austin Schuhef323c02020-09-01 14:55:28 -0700555 simple_shm_fetcher_(shm_base, event_loop, channel) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700556 if (copy_data) {
Brian Silverman77162972020-08-12 19:52:40 -0700557 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700558 }
559 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700560
Austin Schuh3054f5f2021-07-21 15:38:01 -0700561 ~ShmWatcherState() override {
562 event_loop_->CheckCurrentThread();
563 event_loop_->RemoveEvent(&event_);
564 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800565
566 void Startup(EventLoop *event_loop) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700567 event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800568 simple_shm_fetcher_.PointAtNextQueueIndex();
Austin Schuh65493d62022-08-17 15:10:37 -0700569 CHECK(RegisterWakeup(event_loop->runtime_realtime_priority()));
Austin Schuh39788ff2019-12-01 18:22:57 -0800570 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700571
Alex Perrycb7da4b2019-08-28 19:35:56 -0700572 // Returns true if there is new data available.
Austin Schuh7d87b672019-12-01 20:23:49 -0800573 bool CheckForNewData() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700574 if (!has_new_data_) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800575 has_new_data_ = simple_shm_fetcher_.FetchNext();
Austin Schuh7d87b672019-12-01 20:23:49 -0800576
577 if (has_new_data_) {
578 event_.set_event_time(
Austin Schuhad154822019-12-27 15:45:13 -0800579 simple_shm_fetcher_.context().monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800580 event_loop_->AddEvent(&event_);
581 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700582 }
583
584 return has_new_data_;
585 }
586
Alex Perrycb7da4b2019-08-28 19:35:56 -0700587 // Consumes the data by calling the callback.
Austin Schuh7d87b672019-12-01 20:23:49 -0800588 void HandleEvent() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700589 CHECK(has_new_data_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800590 DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700591 has_new_data_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800592 CheckForNewData();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700593 }
594
Austin Schuh39788ff2019-12-01 18:22:57 -0800595 // Registers us to receive a signal on event reception.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700596 bool RegisterWakeup(int priority) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800597 return simple_shm_fetcher_.RegisterWakeup(priority);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700598 }
599
Austin Schuh39788ff2019-12-01 18:22:57 -0800600 void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700601
Brian Silvermana5450a92020-08-12 19:59:57 -0700602 absl::Span<const char> GetSharedMemory() const {
603 return simple_shm_fetcher_.GetConstSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800604 }
605
Alex Perrycb7da4b2019-08-28 19:35:56 -0700606 private:
607 bool has_new_data_ = false;
608
Austin Schuh7d87b672019-12-01 20:23:49 -0800609 ShmEventLoop *event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500610 EventHandler<ShmWatcherState> event_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800611 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700612};
613
614// Adapter class to adapt a timerfd to a TimerHandler.
Brian Silverman148d43d2020-06-07 18:19:22 -0500615class ShmTimerHandler final : public TimerHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700616 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500617 ShmTimerHandler(ShmEventLoop *shm_event_loop, ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -0800618 : TimerHandler(shm_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800619 shm_event_loop_(shm_event_loop),
620 event_(this) {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800621 shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() {
Austin Schuh5ca13112021-02-07 22:06:53 -0800622 // The timer may fire spuriously. HandleEvent on the event loop will
Austin Schuhcde39fd2020-02-22 20:58:24 -0800623 // call the callback if it is needed. It may also have called it when
624 // processing some other event, and the kernel decided to deliver this
625 // wakeup anyways.
626 timerfd_.Read();
627 shm_event_loop_->HandleEvent();
628 });
Alex Perrycb7da4b2019-08-28 19:35:56 -0700629 }
630
Brian Silverman148d43d2020-06-07 18:19:22 -0500631 ~ShmTimerHandler() {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700632 shm_event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800633 Disable();
634 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
635 }
636
637 void HandleEvent() {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800638 CHECK(!event_.valid());
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700639 disabled_ = false;
Austin Schuhcde39fd2020-02-22 20:58:24 -0800640 const auto monotonic_now = Call(monotonic_clock::now, base_);
641 if (event_.valid()) {
Philipp Schradera6712522023-07-05 20:25:11 -0700642 // If someone called Schedule inside Call, rescheduling is already taken
643 // care of. Bail.
Austin Schuhcde39fd2020-02-22 20:58:24 -0800644 return;
Austin Schuh7d87b672019-12-01 20:23:49 -0800645 }
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700646 if (disabled_) {
647 // Somebody called Disable inside Call, so we don't want to reschedule.
648 // Bail.
649 return;
650 }
Austin Schuh7d87b672019-12-01 20:23:49 -0800651
Austin Schuh4d275fc2022-09-16 15:42:45 -0700652 if (repeat_offset_ == std::chrono::seconds(0)) {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800653 timerfd_.Disable();
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700654 disabled_ = true;
Austin Schuhcde39fd2020-02-22 20:58:24 -0800655 } else {
656 // Compute how many cycles have elapsed and schedule the next iteration
657 // for the next iteration in the future.
658 const int elapsed_cycles =
659 std::max<int>(0, (monotonic_now - base_ + repeat_offset_ -
660 std::chrono::nanoseconds(1)) /
661 repeat_offset_);
662 base_ += repeat_offset_ * elapsed_cycles;
Austin Schuh7d87b672019-12-01 20:23:49 -0800663
Austin Schuhcde39fd2020-02-22 20:58:24 -0800664 // Update the heap and schedule the timerfd wakeup.
Austin Schuh7d87b672019-12-01 20:23:49 -0800665 event_.set_event_time(base_);
666 shm_event_loop_->AddEvent(&event_);
Austin Schuh4d275fc2022-09-16 15:42:45 -0700667 timerfd_.SetTime(base_, std::chrono::seconds(0));
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700668 disabled_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800669 }
670 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700671
Philipp Schradera6712522023-07-05 20:25:11 -0700672 void Schedule(monotonic_clock::time_point base,
673 monotonic_clock::duration repeat_offset) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700674 shm_event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800675 if (event_.valid()) {
676 shm_event_loop_->RemoveEvent(&event_);
677 }
678
Alex Perrycb7da4b2019-08-28 19:35:56 -0700679 timerfd_.SetTime(base, repeat_offset);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800680 base_ = base;
681 repeat_offset_ = repeat_offset;
Austin Schuh7d87b672019-12-01 20:23:49 -0800682 event_.set_event_time(base_);
683 shm_event_loop_->AddEvent(&event_);
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700684 disabled_ = false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700685 }
686
Austin Schuh7d87b672019-12-01 20:23:49 -0800687 void Disable() override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700688 shm_event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800689 shm_event_loop_->RemoveEvent(&event_);
690 timerfd_.Disable();
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700691 disabled_ = true;
Austin Schuh7d87b672019-12-01 20:23:49 -0800692 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700693
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700694 bool IsDisabled() override { return disabled_; }
695
Alex Perrycb7da4b2019-08-28 19:35:56 -0700696 private:
697 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500698 EventHandler<ShmTimerHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700699
Brian Silverman148d43d2020-06-07 18:19:22 -0500700 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700701
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800702 monotonic_clock::time_point base_;
703 monotonic_clock::duration repeat_offset_;
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700704
705 // Used to track if Disable() was called during the callback, so we know not
706 // to reschedule.
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700707 bool disabled_ = true;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700708};
709
710// Adapter class to the timerfd and PhasedLoop.
Brian Silverman148d43d2020-06-07 18:19:22 -0500711class ShmPhasedLoopHandler final : public PhasedLoopHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700712 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500713 ShmPhasedLoopHandler(ShmEventLoop *shm_event_loop,
714 ::std::function<void(int)> fn,
715 const monotonic_clock::duration interval,
716 const monotonic_clock::duration offset)
717 : PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset),
Austin Schuh7d87b672019-12-01 20:23:49 -0800718 shm_event_loop_(shm_event_loop),
719 event_(this) {
720 shm_event_loop_->epoll_.OnReadable(
721 timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); });
722 }
723
724 void HandleEvent() {
725 // The return value for read is the number of cycles that have elapsed.
726 // Because we check to see when this event *should* have happened, there are
727 // cases where Read() will return 0, when 1 cycle has actually happened.
728 // This occurs when the timer interrupt hasn't triggered yet. Therefore,
729 // ignore it. Call handles rescheduling and calculating elapsed cycles
730 // without any extra help.
731 timerfd_.Read();
732 event_.Invalidate();
733
James Kuszmaul20dcc7c2023-01-20 11:06:31 -0800734 Call(monotonic_clock::now);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700735 }
736
Brian Silverman148d43d2020-06-07 18:19:22 -0500737 ~ShmPhasedLoopHandler() override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700738 shm_event_loop_->CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -0800739 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
Austin Schuh7d87b672019-12-01 20:23:49 -0800740 shm_event_loop_->RemoveEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700741 }
742
743 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800744 // Reschedules the timer.
Austin Schuh39788ff2019-12-01 18:22:57 -0800745 void Schedule(monotonic_clock::time_point sleep_time) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700746 shm_event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800747 if (event_.valid()) {
748 shm_event_loop_->RemoveEvent(&event_);
749 }
750
Austin Schuh39788ff2019-12-01 18:22:57 -0800751 timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero());
Austin Schuh7d87b672019-12-01 20:23:49 -0800752 event_.set_event_time(sleep_time);
753 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700754 }
755
756 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500757 EventHandler<ShmPhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700758
Brian Silverman148d43d2020-06-07 18:19:22 -0500759 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700760};
Brian Silverman148d43d2020-06-07 18:19:22 -0500761
762} // namespace shm_event_loop_internal
Alex Perrycb7da4b2019-08-28 19:35:56 -0700763
764::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher(
765 const Channel *channel) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700766 CheckCurrentThread();
Austin Schuhca4828c2019-12-28 14:21:35 -0800767 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
768 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
769 << "\", \"type\": \"" << channel->type()->string_view()
770 << "\" } is not able to be fetched on this node. Check your "
771 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800772 }
773
Austin Schuhef323c02020-09-01 14:55:28 -0700774 return ::std::unique_ptr<RawFetcher>(
775 new ShmFetcher(shm_base_, this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700776}
777
778::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender(
779 const Channel *channel) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700780 CheckCurrentThread();
Brian Silverman0fc69932020-01-24 21:54:02 -0800781 TakeSender(channel);
Austin Schuh39788ff2019-12-01 18:22:57 -0800782
Austin Schuhef323c02020-09-01 14:55:28 -0700783 return ::std::unique_ptr<RawSender>(new ShmSender(shm_base_, this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700784}
785
786void ShmEventLoop::MakeRawWatcher(
787 const Channel *channel,
788 std::function<void(const Context &context, const void *message)> watcher) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700789 CheckCurrentThread();
Brian Silverman0fc69932020-01-24 21:54:02 -0800790 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800791
Austin Schuh39788ff2019-12-01 18:22:57 -0800792 NewWatcher(::std::unique_ptr<WatcherState>(
Austin Schuhef323c02020-09-01 14:55:28 -0700793 new ShmWatcherState(shm_base_, this, channel, std::move(watcher), true)));
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800794}
795
796void ShmEventLoop::MakeRawNoArgWatcher(
797 const Channel *channel,
798 std::function<void(const Context &context)> watcher) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700799 CheckCurrentThread();
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800800 TakeWatcher(channel);
801
Brian Silverman148d43d2020-06-07 18:19:22 -0500802 NewWatcher(::std::unique_ptr<WatcherState>(new ShmWatcherState(
Austin Schuhef323c02020-09-01 14:55:28 -0700803 shm_base_, this, channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800804 [watcher](const Context &context, const void *) { watcher(context); },
805 false)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700806}
807
808TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700809 CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -0800810 return NewTimer(::std::unique_ptr<TimerHandler>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500811 new ShmTimerHandler(this, ::std::move(callback))));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700812}
813
814PhasedLoopHandler *ShmEventLoop::AddPhasedLoop(
815 ::std::function<void(int)> callback,
816 const monotonic_clock::duration interval,
817 const monotonic_clock::duration offset) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700818 CheckCurrentThread();
Brian Silverman148d43d2020-06-07 18:19:22 -0500819 return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>(
820 new ShmPhasedLoopHandler(this, ::std::move(callback), interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700821}
822
823void ShmEventLoop::OnRun(::std::function<void()> on_run) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700824 CheckCurrentThread();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700825 on_run_.push_back(::std::move(on_run));
826}
827
Austin Schuh3054f5f2021-07-21 15:38:01 -0700828void ShmEventLoop::CheckCurrentThread() const {
829 if (__builtin_expect(check_mutex_ != nullptr, false)) {
830 CHECK(check_mutex_->is_locked())
831 << ": The configured mutex is not locked while calling a "
832 "ShmEventLoop function";
833 }
834 if (__builtin_expect(!!check_tid_, false)) {
835 CHECK_EQ(syscall(SYS_gettid), *check_tid_)
836 << ": Being called from the wrong thread";
837 }
838}
839
Austin Schuh5ca13112021-02-07 22:06:53 -0800840// This is a bit tricky because watchers can generate new events at any time (as
841// long as it's in the past). We want to check the watchers at least once before
842// declaring there are no events to handle, and we want to check them again if
843// event processing takes long enough that we find an event after that point in
844// time to handle.
Austin Schuh7d87b672019-12-01 20:23:49 -0800845void ShmEventLoop::HandleEvent() {
Austin Schuh5ca13112021-02-07 22:06:53 -0800846 // Time through which we've checked for new events in watchers.
847 monotonic_clock::time_point checked_until = monotonic_clock::min_time;
848 if (!signalfd_) {
849 // Nothing to check, so we can bail out immediately once we're out of
850 // events.
851 CHECK(watchers_.empty());
852 checked_until = monotonic_clock::max_time;
Austin Schuh7d87b672019-12-01 20:23:49 -0800853 }
854
Austin Schuh5ca13112021-02-07 22:06:53 -0800855 // Loop until we run out of events to check.
Austin Schuh39788ff2019-12-01 18:22:57 -0800856 while (true) {
Austin Schuh5ca13112021-02-07 22:06:53 -0800857 // Time of the next event we know about. If this is before checked_until, we
858 // know there aren't any new events before the next one that we already know
859 // about, so no need to check the watchers.
860 monotonic_clock::time_point next_time = monotonic_clock::max_time;
861
862 if (EventCount() == 0) {
863 if (checked_until != monotonic_clock::min_time) {
864 // No events, and we've already checked the watchers at least once, so
865 // we're all done.
866 //
867 // There's a small chance that a watcher has gotten another event in
868 // between checked_until and now. If so, then the signalfd will be
869 // triggered now and we'll re-enter HandleEvent immediately. This is
870 // unlikely though, so we don't want to spend time checking all the
871 // watchers unnecessarily.
872 break;
873 }
874 } else {
875 next_time = PeekEvent()->event_time();
876 }
Austin Schuh00cad2e2022-12-02 20:11:04 -0800877 monotonic_clock::time_point now;
878 bool new_data = false;
Austin Schuh5ca13112021-02-07 22:06:53 -0800879
880 if (next_time > checked_until) {
881 // Read all of the signals, because there's no point in waking up again
882 // immediately to handle each one if we've fallen behind.
883 //
884 // This is safe before checking for new data on the watchers. If a signal
885 // is cleared here, the corresponding CheckForNewData() call below will
886 // pick it up.
887 while (true) {
888 const signalfd_siginfo result = signalfd_->Read();
889 if (result.ssi_signo == 0) {
890 break;
891 }
892 CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal);
893 }
Austin Schuh00cad2e2022-12-02 20:11:04 -0800894 // This is the last time we can guarantee that if a message is published
895 // before, we will notice it.
896 now = monotonic_clock::now();
Austin Schuh5ca13112021-02-07 22:06:53 -0800897
898 // Check all the watchers for new events.
899 for (std::unique_ptr<WatcherState> &base_watcher : watchers_) {
900 ShmWatcherState *const watcher =
901 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
902
Austin Schuh00cad2e2022-12-02 20:11:04 -0800903 // Track if we got a message.
904 if (watcher->CheckForNewData()) {
905 new_data = true;
906 }
Austin Schuh5ca13112021-02-07 22:06:53 -0800907 }
908 if (EventCount() == 0) {
909 // Still no events, all done now.
910 break;
911 }
912
913 checked_until = now;
914 // Check for any new events we found.
915 next_time = PeekEvent()->event_time();
Austin Schuh00cad2e2022-12-02 20:11:04 -0800916 } else {
917 now = monotonic_clock::now();
Austin Schuh5ca13112021-02-07 22:06:53 -0800918 }
919
920 if (next_time > now) {
Austin Schuh00cad2e2022-12-02 20:11:04 -0800921 // Ok, we got a message with a timestamp *after* we wrote down time. We
922 // need to process it (otherwise we will go to sleep without processing
923 // it), but we also need to make sure no other messages have come in
924 // before it that we would process out of order. Just go around again to
925 // redo the checks.
926 if (new_data) {
927 continue;
928 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800929 break;
930 }
931
Austin Schuh5ca13112021-02-07 22:06:53 -0800932 EventLoopEvent *const event = PopEvent();
Austin Schuh7d87b672019-12-01 20:23:49 -0800933 event->HandleEvent();
Austin Schuh39788ff2019-12-01 18:22:57 -0800934 }
935}
936
Austin Schuh32fd5a72019-12-01 22:20:26 -0800937// RAII class to mask signals.
938class ScopedSignalMask {
939 public:
940 ScopedSignalMask(std::initializer_list<int> signals) {
941 sigset_t sigset;
942 PCHECK(sigemptyset(&sigset) == 0);
943 for (int signal : signals) {
944 PCHECK(sigaddset(&sigset, signal) == 0);
945 }
946
947 PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0);
948 }
949
950 ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); }
951
952 private:
953 sigset_t old_;
954};
955
956// Class to manage the static state associated with killing multiple event
957// loops.
958class SignalHandler {
959 public:
960 // Gets the singleton.
961 static SignalHandler *global() {
962 static SignalHandler loop;
963 return &loop;
964 }
965
966 // Handles the signal with the singleton.
967 static void HandleSignal(int) { global()->DoHandleSignal(); }
968
969 // Registers an event loop to receive Exit() calls.
970 void Register(ShmEventLoop *event_loop) {
971 // Block signals while we have the mutex so we never race with the signal
972 // handler.
973 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
974 std::unique_lock<stl_mutex> locker(mutex_);
975 if (event_loops_.size() == 0) {
976 // The first caller registers the signal handler.
977 struct sigaction new_action;
978 sigemptyset(&new_action.sa_mask);
979 // This makes it so that 2 control c's to a stuck process will kill it by
980 // restoring the original signal handler.
981 new_action.sa_flags = SA_RESETHAND;
982 new_action.sa_handler = &HandleSignal;
983
984 PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0);
985 PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0);
986 PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0);
987 }
988
989 event_loops_.push_back(event_loop);
990 }
991
992 // Unregisters an event loop to receive Exit() calls.
993 void Unregister(ShmEventLoop *event_loop) {
994 // Block signals while we have the mutex so we never race with the signal
995 // handler.
996 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
997 std::unique_lock<stl_mutex> locker(mutex_);
998
Brian Silverman5120afb2020-01-31 17:44:35 -0800999 event_loops_.erase(
1000 std::find(event_loops_.begin(), event_loops_.end(), event_loop));
Austin Schuh32fd5a72019-12-01 22:20:26 -08001001
1002 if (event_loops_.size() == 0u) {
1003 // The last caller restores the original signal handlers.
1004 PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0);
1005 PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0);
1006 PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0);
1007 }
1008 }
1009
1010 private:
1011 void DoHandleSignal() {
1012 // We block signals while grabbing the lock, so there should never be a
1013 // race. Confirm that this is true using trylock.
1014 CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while "
1015 "modifing the event loop list.";
1016 for (ShmEventLoop *event_loop : event_loops_) {
1017 event_loop->Exit();
1018 }
1019 mutex_.unlock();
1020 }
1021
1022 // Mutex to protect all state.
1023 stl_mutex mutex_;
1024 std::vector<ShmEventLoop *> event_loops_;
1025 struct sigaction old_action_int_;
1026 struct sigaction old_action_hup_;
1027 struct sigaction old_action_term_;
1028};
1029
Alex Perrycb7da4b2019-08-28 19:35:56 -07001030void ShmEventLoop::Run() {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001031 CheckCurrentThread();
Austin Schuh32fd5a72019-12-01 22:20:26 -08001032 SignalHandler::global()->Register(this);
Austin Schuh39788ff2019-12-01 18:22:57 -08001033
Alex Perrycb7da4b2019-08-28 19:35:56 -07001034 if (watchers_.size() > 0) {
Austin Schuh5ca13112021-02-07 22:06:53 -08001035 signalfd_.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal}));
Alex Perrycb7da4b2019-08-28 19:35:56 -07001036
Austin Schuh5ca13112021-02-07 22:06:53 -08001037 epoll_.OnReadable(signalfd_->fd(), [this]() { HandleEvent(); });
Alex Perrycb7da4b2019-08-28 19:35:56 -07001038 }
1039
Austin Schuh39788ff2019-12-01 18:22:57 -08001040 MaybeScheduleTimingReports();
1041
Austin Schuh7d87b672019-12-01 20:23:49 -08001042 ReserveEvents();
1043
Tyler Chatow67ddb032020-01-12 14:30:04 -08001044 {
Austin Schuha0c41ba2020-09-10 22:59:14 -07001045 logging::ScopedLogRestorer prev_logger;
Tyler Chatow67ddb032020-01-12 14:30:04 -08001046 AosLogToFbs aos_logger;
1047 if (!skip_logger_) {
Austin Schuhad9e5eb2021-11-19 20:33:55 -08001048 aos_logger.Initialize(&name_, MakeSender<logging::LogMessageFbs>("/aos"));
Austin Schuha0c41ba2020-09-10 22:59:14 -07001049 prev_logger.Swap(aos_logger.implementation());
Tyler Chatow67ddb032020-01-12 14:30:04 -08001050 }
Alex Perrycb7da4b2019-08-28 19:35:56 -07001051
Tyler Chatow67ddb032020-01-12 14:30:04 -08001052 aos::SetCurrentThreadName(name_.substr(0, 16));
Brian Silverman6a54ff32020-04-28 16:41:39 -07001053 const cpu_set_t default_affinity = DefaultAffinity();
1054 if (!CPU_EQUAL(&affinity_, &default_affinity)) {
1055 ::aos::SetCurrentThreadAffinity(affinity_);
1056 }
Tyler Chatow67ddb032020-01-12 14:30:04 -08001057 // Now, all the callbacks are setup. Lock everything into memory and go RT.
1058 if (priority_ != 0) {
1059 ::aos::InitRT();
1060
1061 LOG(INFO) << "Setting priority to " << priority_;
1062 ::aos::SetCurrentThreadRealtimePriority(priority_);
1063 }
1064
1065 set_is_running(true);
1066
1067 // Now that we are realtime (but before the OnRun handlers run), snap the
1068 // queue index.
1069 for (::std::unique_ptr<WatcherState> &watcher : watchers_) {
1070 watcher->Startup(this);
1071 }
1072
1073 // Now that we are RT, run all the OnRun handlers.
Austin Schuha9012be2021-07-21 15:19:11 -07001074 SetTimerContext(monotonic_clock::now());
Tyler Chatow67ddb032020-01-12 14:30:04 -08001075 for (const auto &run : on_run_) {
1076 run();
1077 }
1078
1079 // And start our main event loop which runs all the timers and handles Quit.
1080 epoll_.Run();
1081
1082 // Once epoll exits, there is no useful nonrt work left to do.
1083 set_is_running(false);
1084
1085 // Nothing time or synchronization critical needs to happen after this
1086 // point. Drop RT priority.
1087 ::aos::UnsetCurrentThreadRealtimePriority();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001088 }
1089
Austin Schuh39788ff2019-12-01 18:22:57 -08001090 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -05001091 ShmWatcherState *watcher =
1092 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -07001093 watcher->UnregisterWakeup();
1094 }
1095
1096 if (watchers_.size() > 0) {
Austin Schuh5ca13112021-02-07 22:06:53 -08001097 epoll_.DeleteFd(signalfd_->fd());
1098 signalfd_.reset();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001099 }
Austin Schuh32fd5a72019-12-01 22:20:26 -08001100
1101 SignalHandler::global()->Unregister(this);
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001102
1103 // Trigger any remaining senders or fetchers to be cleared before destroying
1104 // the event loop so the book keeping matches. Do this in the thread that
1105 // created the timing reporter.
1106 timing_report_sender_.reset();
Austin Schuh0debde12022-08-17 16:25:17 -07001107 ClearContext();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001108}
1109
1110void ShmEventLoop::Exit() { epoll_.Quit(); }
1111
Brian Silvermane1fe2512022-08-14 23:18:50 -07001112std::unique_ptr<ExitHandle> ShmEventLoop::MakeExitHandle() {
1113 return std::make_unique<ShmExitHandle>(this);
1114}
1115
Alex Perrycb7da4b2019-08-28 19:35:56 -07001116ShmEventLoop::~ShmEventLoop() {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001117 CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -08001118 // Force everything with a registered fd with epoll to be destroyed now.
1119 timers_.clear();
1120 phased_loops_.clear();
1121 watchers_.clear();
1122
Alex Perrycb7da4b2019-08-28 19:35:56 -07001123 CHECK(!is_running()) << ": ShmEventLoop destroyed while running";
Brian Silvermane1fe2512022-08-14 23:18:50 -07001124 CHECK_EQ(0, exit_handle_count_)
1125 << ": All ExitHandles must be destroyed before the ShmEventLoop";
Alex Perrycb7da4b2019-08-28 19:35:56 -07001126}
1127
Alex Perrycb7da4b2019-08-28 19:35:56 -07001128void ShmEventLoop::SetRuntimeRealtimePriority(int priority) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001129 CheckCurrentThread();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001130 if (is_running()) {
1131 LOG(FATAL) << "Cannot set realtime priority while running.";
1132 }
1133 priority_ = priority;
1134}
1135
Brian Silverman6a54ff32020-04-28 16:41:39 -07001136void ShmEventLoop::SetRuntimeAffinity(const cpu_set_t &cpuset) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001137 CheckCurrentThread();
Brian Silverman6a54ff32020-04-28 16:41:39 -07001138 if (is_running()) {
1139 LOG(FATAL) << "Cannot set affinity while running.";
1140 }
1141 affinity_ = cpuset;
1142}
1143
James Kuszmaul57c2baa2020-01-19 14:52:52 -08001144void ShmEventLoop::set_name(const std::string_view name) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001145 CheckCurrentThread();
James Kuszmaul57c2baa2020-01-19 14:52:52 -08001146 name_ = std::string(name);
1147 UpdateTimingReport();
1148}
1149
Brian Silvermana5450a92020-08-12 19:59:57 -07001150absl::Span<const char> ShmEventLoop::GetWatcherSharedMemory(
1151 const Channel *channel) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001152 CheckCurrentThread();
Brian Silverman148d43d2020-06-07 18:19:22 -05001153 ShmWatcherState *const watcher_state =
1154 static_cast<ShmWatcherState *>(GetWatcherState(channel));
Brian Silverman5120afb2020-01-31 17:44:35 -08001155 return watcher_state->GetSharedMemory();
1156}
1157
Brian Silverman4f4e0612020-08-12 19:54:41 -07001158int ShmEventLoop::NumberBuffers(const Channel *channel) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001159 CheckCurrentThread();
Austin Schuh4d275fc2022-09-16 15:42:45 -07001160 return ipc_lib::MakeQueueConfiguration(configuration(), channel)
1161 .num_messages();
Brian Silverman4f4e0612020-08-12 19:54:41 -07001162}
1163
Brian Silverman5120afb2020-01-31 17:44:35 -08001164absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory(
1165 const aos::RawSender *sender) const {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001166 CheckCurrentThread();
Brian Silverman148d43d2020-06-07 18:19:22 -05001167 return static_cast<const ShmSender *>(sender)->GetSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -08001168}
1169
Brian Silvermana5450a92020-08-12 19:59:57 -07001170absl::Span<const char> ShmEventLoop::GetShmFetcherPrivateMemory(
Brian Silverman6d2b3592020-06-18 14:40:15 -07001171 const aos::RawFetcher *fetcher) const {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001172 CheckCurrentThread();
Brian Silverman6d2b3592020-06-18 14:40:15 -07001173 return static_cast<const ShmFetcher *>(fetcher)->GetPrivateMemory();
1174}
1175
Austin Schuh3054f5f2021-07-21 15:38:01 -07001176pid_t ShmEventLoop::GetTid() {
1177 CheckCurrentThread();
1178 return syscall(SYS_gettid);
1179}
Austin Schuh39788ff2019-12-01 18:22:57 -08001180
Alex Perrycb7da4b2019-08-28 19:35:56 -07001181} // namespace aos