Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/events/shm_event_loop.h" |
| 2 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 3 | #include <sys/stat.h> |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 4 | #include <sys/syscall.h> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 5 | #include <sys/types.h> |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <atomic> |
| 9 | #include <chrono> |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 10 | #include <iterator> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | #include <stdexcept> |
| 12 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 13 | #include "absl/flags/flag.h" |
| 14 | #include "absl/log/check.h" |
| 15 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 16 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 17 | #include "aos/events/aos_logging.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 18 | #include "aos/events/epoll.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 19 | #include "aos/events/event_loop_generated.h" |
| 20 | #include "aos/events/timing_statistics.h" |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 21 | #include "aos/init.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 22 | #include "aos/ipc_lib/lockless_queue.h" |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 23 | #include "aos/ipc_lib/memory_mapped_queue.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | #include "aos/realtime.h" |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 25 | #include "aos/stl_mutex/stl_mutex.h" |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame] | 26 | #include "aos/util/file.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 27 | #include "aos/util/phased_loop.h" |
| 28 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 29 | namespace { |
| 30 | |
| 31 | // Returns the portion of the path after the last /. This very much assumes |
| 32 | // that the application name is null terminated. |
| 33 | const char *Filename(const char *path) { |
| 34 | const std::string_view path_string_view = path; |
| 35 | auto last_slash_pos = path_string_view.find_last_of("/"); |
| 36 | |
| 37 | return last_slash_pos == std::string_view::npos ? path |
| 38 | : path + last_slash_pos + 1; |
| 39 | } |
| 40 | |
| 41 | } // namespace |
| 42 | |
Brennan Coslett | 6fd3c00 | 2023-07-11 17:41:09 -0500 | [diff] [blame] | 43 | // 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 :) |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 51 | ABSL_FLAG(uint32_t, permissions, 0770, |
| 52 | "Permissions to make shared memory files and folders, " |
| 53 | "affected by the process's umask. " |
| 54 | "See shm_event_loop.cc for more details."); |
| 55 | ABSL_FLAG(std::string, application_name, Filename(program_invocation_name), |
| 56 | "The application name"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 57 | |
| 58 | namespace aos { |
| 59 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 60 | using namespace shm_event_loop_internal; |
| 61 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 62 | namespace { |
| 63 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 64 | const Node *MaybeMyNode(const Configuration *configuration) { |
| 65 | if (!configuration->has_nodes()) { |
| 66 | return nullptr; |
| 67 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 68 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 69 | return configuration::GetMyNode(configuration); |
| 70 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 71 | |
Philipp Schrader | a873466 | 2023-08-06 14:49:39 -0700 | [diff] [blame] | 72 | void IgnoreWakeupSignal() { |
| 73 | struct sigaction action; |
| 74 | action.sa_handler = SIG_IGN; |
| 75 | PCHECK(sigemptyset(&action.sa_mask) == 0); |
| 76 | action.sa_flags = 0; |
| 77 | PCHECK(sigaction(ipc_lib::kWakeupSignal, &action, nullptr) == 0); |
| 78 | } |
| 79 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 80 | } // namespace |
| 81 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 82 | ShmEventLoop::ShmEventLoop(const Configuration *configuration) |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 83 | : EventLoop(configuration), |
| 84 | boot_uuid_(UUID::BootUUID()), |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 85 | shm_base_(absl::GetFlag(FLAGS_shm_base)), |
| 86 | name_(absl::GetFlag(FLAGS_application_name)), |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 87 | node_(MaybeMyNode(configuration)) { |
Philipp Schrader | a873466 | 2023-08-06 14:49:39 -0700 | [diff] [blame] | 88 | // Ignore the wakeup signal by default. Otherwise, we have race conditions on |
| 89 | // shutdown where a wakeup signal will uncleanly terminate the process. |
| 90 | // See LocklessQueueWakeUpper::Wakeup() for some more information. |
| 91 | IgnoreWakeupSignal(); |
| 92 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 93 | CHECK(IsInitialized()) << ": Need to initialize AOS first."; |
Austin Schuh | 0debde1 | 2022-08-17 16:25:17 -0700 | [diff] [blame] | 94 | ClearContext(); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 95 | if (configuration->has_nodes()) { |
| 96 | CHECK(node_ != nullptr) << ": Couldn't find node in config."; |
| 97 | } |
| 98 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 99 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 100 | namespace shm_event_loop_internal { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 101 | |
| 102 | class SimpleShmFetcher { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 103 | public: |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 104 | explicit SimpleShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop, |
| 105 | const Channel *channel) |
Austin Schuh | 432784f | 2020-06-23 17:27:35 -0700 | [diff] [blame] | 106 | : event_loop_(event_loop), |
| 107 | channel_(channel), |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 108 | lockless_queue_memory_(shm_base, absl::GetFlag(FLAGS_permissions), |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 109 | event_loop->configuration(), channel), |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 110 | reader_(lockless_queue_memory_.queue()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 111 | context_.data = nullptr; |
| 112 | // Point the queue index at the next index to read starting now. This |
| 113 | // makes it such that FetchNext will read the next message sent after |
| 114 | // the fetcher is created. |
| 115 | PointAtNextQueueIndex(); |
| 116 | } |
| 117 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 118 | ~SimpleShmFetcher() {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 119 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 120 | // Sets this object to pin or copy data, as configured in the channel. |
| 121 | void RetrieveData() { |
| 122 | if (channel_->read_method() == ReadMethod::PIN) { |
| 123 | PinDataOnFetch(); |
| 124 | } else { |
| 125 | CopyDataOnFetch(); |
| 126 | } |
| 127 | } |
| 128 | |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 129 | // Sets this object to copy data out of the shared memory into a private |
| 130 | // buffer when fetching. |
| 131 | void CopyDataOnFetch() { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 132 | CHECK(!pin_data()); |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 133 | data_storage_.reset(static_cast<char *>( |
| 134 | malloc(channel_->max_size() + kChannelDataAlignment - 1))); |
| 135 | } |
| 136 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 137 | // Sets this object to pin data in shared memory when fetching. |
| 138 | void PinDataOnFetch() { |
| 139 | CHECK(!copy_data()); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 140 | auto maybe_pinner = |
| 141 | ipc_lib::LocklessQueuePinner::Make(lockless_queue_memory_.queue()); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 142 | if (!maybe_pinner) { |
| 143 | LOG(FATAL) << "Failed to create reader on " |
| 144 | << configuration::CleanedChannelToString(channel_) |
| 145 | << ", too many readers."; |
| 146 | } |
| 147 | pinner_ = std::move(maybe_pinner.value()); |
| 148 | } |
| 149 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 150 | // Points the next message to fetch at the queue index which will be |
| 151 | // populated next. |
| 152 | void PointAtNextQueueIndex() { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 153 | actual_queue_index_ = reader_.LatestIndex(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 154 | if (!actual_queue_index_.valid()) { |
| 155 | // Nothing in the queue. The next element will show up at the 0th |
| 156 | // index in the queue. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 157 | actual_queue_index_ = ipc_lib::QueueIndex::Zero( |
| 158 | LocklessQueueSize(lockless_queue_memory_.memory())); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 159 | } else { |
| 160 | actual_queue_index_ = actual_queue_index_.Increment(); |
| 161 | } |
| 162 | } |
| 163 | |
Austin Schuh | 2b4661a | 2023-09-20 21:37:33 -0700 | [diff] [blame] | 164 | bool FetchNext() { return FetchNextIf(should_fetch_); } |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 165 | |
| 166 | bool FetchNextIf(std::function<bool(const Context &)> fn) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 167 | const ipc_lib::LocklessQueueReader::Result read_result = |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 168 | DoFetch(actual_queue_index_, std::move(fn)); |
Austin Schuh | 432784f | 2020-06-23 17:27:35 -0700 | [diff] [blame] | 169 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 170 | return read_result == ipc_lib::LocklessQueueReader::Result::GOOD; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 173 | bool FetchIf(std::function<bool(const Context &)> fn) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 174 | const ipc_lib::QueueIndex queue_index = reader_.LatestIndex(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 175 | // actual_queue_index_ is only meaningful if it was set by Fetch or |
| 176 | // FetchNext. This happens when valid_data_ has been set. So, only |
| 177 | // skip checking if valid_data_ is true. |
| 178 | // |
| 179 | // Also, if the latest queue index is invalid, we are empty. So there |
| 180 | // is nothing to fetch. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 181 | if ((context_.data != nullptr && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 182 | queue_index == actual_queue_index_.DecrementBy(1u)) || |
| 183 | !queue_index.valid()) { |
| 184 | return false; |
| 185 | } |
| 186 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 187 | const ipc_lib::LocklessQueueReader::Result read_result = |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 188 | DoFetch(queue_index, std::move(fn)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 189 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 190 | CHECK(read_result != ipc_lib::LocklessQueueReader::Result::NOTHING_NEW) |
Austin Schuh | f565259 | 2019-12-29 16:26:15 -0800 | [diff] [blame] | 191 | << ": Queue index went backwards. This should never happen. " |
| 192 | << configuration::CleanedChannelToString(channel_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 193 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 194 | return read_result == ipc_lib::LocklessQueueReader::Result::GOOD; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Austin Schuh | 2b4661a | 2023-09-20 21:37:33 -0700 | [diff] [blame] | 197 | bool Fetch() { return FetchIf(should_fetch_); } |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 198 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 199 | Context context() const { return context_; } |
| 200 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 201 | bool RegisterWakeup(int priority) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 202 | CHECK(!watcher_); |
| 203 | watcher_ = ipc_lib::LocklessQueueWatcher::Make( |
| 204 | lockless_queue_memory_.queue(), priority); |
| 205 | return static_cast<bool>(watcher_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 208 | void UnregisterWakeup() { |
| 209 | CHECK(watcher_); |
| 210 | watcher_ = std::nullopt; |
| 211 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 212 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 213 | absl::Span<char> GetMutableSharedMemory() { |
| 214 | return lockless_queue_memory_.GetMutableSharedMemory(); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 217 | absl::Span<const char> GetConstSharedMemory() const { |
| 218 | return lockless_queue_memory_.GetConstSharedMemory(); |
| 219 | } |
| 220 | |
| 221 | absl::Span<const char> GetPrivateMemory() const { |
| 222 | if (pin_data()) { |
| 223 | return lockless_queue_memory_.GetConstSharedMemory(); |
| 224 | } |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 225 | return absl::Span<char>( |
| 226 | const_cast<SimpleShmFetcher *>(this)->data_storage_start(), |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 227 | LocklessQueueMessageDataSize(lockless_queue_memory_.memory())); |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 230 | private: |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 231 | ipc_lib::LocklessQueueReader::Result DoFetch( |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 232 | ipc_lib::QueueIndex queue_index, |
| 233 | std::function<bool(const Context &context)> fn) { |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 234 | // TODO(austin): Get behind and make sure it dies. |
| 235 | char *copy_buffer = nullptr; |
| 236 | if (copy_data()) { |
| 237 | copy_buffer = data_storage_start(); |
| 238 | } |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 239 | ipc_lib::LocklessQueueReader::Result read_result = reader_.Read( |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 240 | queue_index.index(), &context_.monotonic_event_time, |
| 241 | &context_.realtime_event_time, &context_.monotonic_remote_time, |
Austin Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 242 | &context_.monotonic_remote_transmit_time, |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 243 | &context_.realtime_remote_time, &context_.remote_queue_index, |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 244 | &context_.source_boot_uuid, &context_.size, copy_buffer, std::move(fn)); |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 245 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 246 | if (read_result == ipc_lib::LocklessQueueReader::Result::GOOD) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 247 | if (pin_data()) { |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 248 | const int pin_result = pinner_->PinIndex(queue_index.index()); |
| 249 | CHECK(pin_result >= 0) |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 250 | << ": Got behind while reading and the last message was modified " |
| 251 | "out from under us while we tried to pin it. Don't get so far " |
| 252 | "behind on: " |
| 253 | << configuration::CleanedChannelToString(channel_); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 254 | context_.buffer_index = pin_result; |
| 255 | } else { |
| 256 | context_.buffer_index = -1; |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 259 | context_.queue_index = queue_index.index(); |
| 260 | if (context_.remote_queue_index == 0xffffffffu) { |
| 261 | context_.remote_queue_index = context_.queue_index; |
| 262 | } |
| 263 | if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) { |
| 264 | context_.monotonic_remote_time = context_.monotonic_event_time; |
| 265 | } |
| 266 | if (context_.realtime_remote_time == aos::realtime_clock::min_time) { |
| 267 | context_.realtime_remote_time = context_.realtime_event_time; |
| 268 | } |
| 269 | const char *const data = DataBuffer(); |
| 270 | if (data) { |
| 271 | context_.data = |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 272 | data + |
| 273 | LocklessQueueMessageDataSize(lockless_queue_memory_.memory()) - |
| 274 | context_.size; |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 275 | } else { |
| 276 | context_.data = nullptr; |
| 277 | } |
| 278 | actual_queue_index_ = queue_index.Increment(); |
| 279 | } |
| 280 | |
| 281 | // Make sure the data wasn't modified while we were reading it. This |
| 282 | // can only happen if you are reading the last message *while* it is |
| 283 | // being written to, which means you are pretty far behind. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 284 | CHECK(read_result != ipc_lib::LocklessQueueReader::Result::OVERWROTE) |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 285 | << ": Got behind while reading and the last message was modified " |
| 286 | "out from under us while we were reading it. Don't get so far " |
| 287 | "behind on: " |
| 288 | << configuration::CleanedChannelToString(channel_); |
| 289 | |
| 290 | // We fell behind between when we read the index and read the value. |
| 291 | // This isn't worth recovering from since this means we went to sleep |
| 292 | // for a long time in the middle of this function. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 293 | if (read_result == ipc_lib::LocklessQueueReader::Result::TOO_OLD) { |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 294 | event_loop_->SendTimingReport(); |
| 295 | LOG(FATAL) << "The next message is no longer available. " |
| 296 | << configuration::CleanedChannelToString(channel_); |
| 297 | } |
| 298 | |
| 299 | return read_result; |
| 300 | } |
| 301 | |
| 302 | char *data_storage_start() const { |
| 303 | CHECK(copy_data()); |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 304 | return RoundChannelData(data_storage_.get(), channel_->max_size()); |
| 305 | } |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 306 | |
| 307 | // Note that for some modes the return value will change as new messages are |
| 308 | // read. |
| 309 | const char *DataBuffer() const { |
| 310 | if (copy_data()) { |
| 311 | return data_storage_start(); |
| 312 | } |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 313 | if (pin_data()) { |
| 314 | return static_cast<const char *>(pinner_->Data()); |
| 315 | } |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 316 | return nullptr; |
| 317 | } |
| 318 | |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 319 | bool copy_data() const { return static_cast<bool>(data_storage_); } |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 320 | bool pin_data() const { return static_cast<bool>(pinner_); } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 321 | |
Austin Schuh | 432784f | 2020-06-23 17:27:35 -0700 | [diff] [blame] | 322 | aos::ShmEventLoop *event_loop_; |
Austin Schuh | f565259 | 2019-12-29 16:26:15 -0800 | [diff] [blame] | 323 | const Channel *const channel_; |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 324 | ipc_lib::MemoryMappedQueue lockless_queue_memory_; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 325 | ipc_lib::LocklessQueueReader reader_; |
| 326 | // This being nullopt indicates we're not looking for wakeups right now. |
| 327 | std::optional<ipc_lib::LocklessQueueWatcher> watcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 328 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 329 | ipc_lib::QueueIndex actual_queue_index_ = ipc_lib::QueueIndex::Invalid(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 330 | |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 331 | // This being empty indicates we're not going to copy data. |
| 332 | std::unique_ptr<char, decltype(&free)> data_storage_{nullptr, &free}; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 333 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 334 | // This being nullopt indicates we're not going to pin messages. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 335 | std::optional<ipc_lib::LocklessQueuePinner> pinner_; |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 336 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 337 | Context context_; |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 338 | |
| 339 | // Pre-allocated should_fetch function so we don't allocate. |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 340 | const std::function<bool(const Context &)> should_fetch_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 341 | }; |
| 342 | |
| 343 | class ShmFetcher : public RawFetcher { |
| 344 | public: |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 345 | explicit ShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop, |
| 346 | const Channel *channel) |
Austin Schuh | aa79e4e | 2019-12-29 20:43:32 -0800 | [diff] [blame] | 347 | : RawFetcher(event_loop, channel), |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 348 | simple_shm_fetcher_(shm_base, event_loop, channel) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 349 | simple_shm_fetcher_.RetrieveData(); |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 350 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 351 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 352 | ~ShmFetcher() override { |
| 353 | shm_event_loop()->CheckCurrentThread(); |
| 354 | context_.data = nullptr; |
| 355 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 356 | |
| 357 | std::pair<bool, monotonic_clock::time_point> DoFetchNext() override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 358 | shm_event_loop()->CheckCurrentThread(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 359 | if (simple_shm_fetcher_.FetchNext()) { |
| 360 | context_ = simple_shm_fetcher_.context(); |
| 361 | return std::make_pair(true, monotonic_clock::now()); |
| 362 | } |
| 363 | return std::make_pair(false, monotonic_clock::min_time); |
| 364 | } |
| 365 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 366 | std::pair<bool, monotonic_clock::time_point> DoFetchNextIf( |
| 367 | std::function<bool(const Context &context)> fn) override { |
| 368 | shm_event_loop()->CheckCurrentThread(); |
| 369 | if (simple_shm_fetcher_.FetchNextIf(std::move(fn))) { |
| 370 | context_ = simple_shm_fetcher_.context(); |
| 371 | return std::make_pair(true, monotonic_clock::now()); |
| 372 | } |
| 373 | return std::make_pair(false, monotonic_clock::min_time); |
| 374 | } |
| 375 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 376 | std::pair<bool, monotonic_clock::time_point> DoFetch() override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 377 | shm_event_loop()->CheckCurrentThread(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 378 | if (simple_shm_fetcher_.Fetch()) { |
| 379 | context_ = simple_shm_fetcher_.context(); |
| 380 | return std::make_pair(true, monotonic_clock::now()); |
| 381 | } |
| 382 | return std::make_pair(false, monotonic_clock::min_time); |
| 383 | } |
| 384 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 385 | std::pair<bool, monotonic_clock::time_point> DoFetchIf( |
| 386 | std::function<bool(const Context &context)> fn) override { |
| 387 | shm_event_loop()->CheckCurrentThread(); |
| 388 | if (simple_shm_fetcher_.FetchIf(std::move(fn))) { |
| 389 | context_ = simple_shm_fetcher_.context(); |
| 390 | return std::make_pair(true, monotonic_clock::now()); |
| 391 | } |
| 392 | return std::make_pair(false, monotonic_clock::min_time); |
| 393 | } |
| 394 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 395 | absl::Span<const char> GetPrivateMemory() const { |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 396 | return simple_shm_fetcher_.GetPrivateMemory(); |
| 397 | } |
| 398 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 399 | private: |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 400 | const ShmEventLoop *shm_event_loop() const { |
| 401 | return static_cast<const ShmEventLoop *>(event_loop()); |
| 402 | } |
| 403 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 404 | SimpleShmFetcher simple_shm_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 405 | }; |
| 406 | |
Brian Silverman | e1fe251 | 2022-08-14 23:18:50 -0700 | [diff] [blame] | 407 | class ShmExitHandle : public ExitHandle { |
| 408 | public: |
| 409 | ShmExitHandle(ShmEventLoop *event_loop) : event_loop_(event_loop) { |
| 410 | ++event_loop_->exit_handle_count_; |
| 411 | } |
| 412 | ~ShmExitHandle() override { |
| 413 | CHECK_GT(event_loop_->exit_handle_count_, 0); |
| 414 | --event_loop_->exit_handle_count_; |
| 415 | } |
James Kuszmaul | 9f99808 | 2024-05-23 15:37:35 -0700 | [diff] [blame] | 416 | // Because of how we handle reference counting, we either need to implement |
| 417 | // reference counting in the copy/move constructors or just not support them. |
| 418 | // If we ever develop a need for this object to be movable/copyable, |
| 419 | // supporting it should be straightforwards. |
| 420 | DISALLOW_COPY_AND_ASSIGN(ShmExitHandle); |
Brian Silverman | e1fe251 | 2022-08-14 23:18:50 -0700 | [diff] [blame] | 421 | |
James Kuszmaul | 9f99808 | 2024-05-23 15:37:35 -0700 | [diff] [blame] | 422 | void Exit(Result<void> status) override { |
| 423 | event_loop_->ExitWithStatus(status); |
| 424 | } |
Brian Silverman | e1fe251 | 2022-08-14 23:18:50 -0700 | [diff] [blame] | 425 | |
| 426 | private: |
| 427 | ShmEventLoop *const event_loop_; |
| 428 | }; |
| 429 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 430 | class ShmSender : public RawSender { |
| 431 | public: |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 432 | explicit ShmSender(std::string_view shm_base, EventLoop *event_loop, |
| 433 | const Channel *channel) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 434 | : RawSender(event_loop, channel), |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 435 | lockless_queue_memory_(shm_base, absl::GetFlag(FLAGS_permissions), |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 436 | event_loop->configuration(), channel), |
Austin Schuh | fff9c3a | 2023-06-16 18:48:23 -0700 | [diff] [blame] | 437 | lockless_queue_sender_( |
| 438 | VerifySender(ipc_lib::LocklessQueueSender::Make( |
| 439 | lockless_queue_memory_.queue(), |
| 440 | configuration::ChannelStorageDuration( |
| 441 | event_loop->configuration(), channel)), |
| 442 | channel)), |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 443 | wake_upper_(lockless_queue_memory_.queue()) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 444 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 445 | ~ShmSender() override { shm_event_loop()->CheckCurrentThread(); } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 446 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 447 | static ipc_lib::LocklessQueueSender VerifySender( |
| 448 | std::optional<ipc_lib::LocklessQueueSender> sender, |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 449 | const Channel *channel) { |
| 450 | if (sender) { |
| 451 | return std::move(sender.value()); |
| 452 | } |
| 453 | LOG(FATAL) << "Failed to create sender on " |
| 454 | << configuration::CleanedChannelToString(channel) |
| 455 | << ", too many senders."; |
| 456 | } |
| 457 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 458 | void *data() override { |
| 459 | shm_event_loop()->CheckCurrentThread(); |
| 460 | return lockless_queue_sender_.Data(); |
| 461 | } |
| 462 | size_t size() override { |
| 463 | shm_event_loop()->CheckCurrentThread(); |
| 464 | return lockless_queue_sender_.size(); |
| 465 | } |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 466 | |
| 467 | Error DoSend(size_t length, |
| 468 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 469 | aos::realtime_clock::time_point realtime_remote_time, |
Austin Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 470 | aos::monotonic_clock::time_point monotonic_remote_transmit_time, |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 471 | uint32_t remote_queue_index, |
| 472 | const UUID &source_boot_uuid) override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 473 | shm_event_loop()->CheckCurrentThread(); |
Austin Schuh | 0f7ed46 | 2020-03-28 20:38:34 -0700 | [diff] [blame] | 474 | CHECK_LE(length, static_cast<size_t>(channel()->max_size())) |
| 475 | << ": Sent too big a message on " |
| 476 | << configuration::CleanedChannelToString(channel()); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 477 | const auto result = lockless_queue_sender_.Send( |
Austin Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 478 | length, monotonic_remote_time, realtime_remote_time, |
| 479 | monotonic_remote_transmit_time, remote_queue_index, source_boot_uuid, |
| 480 | &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 481 | CHECK_NE(result, ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE) |
Austin Schuh | 91ba639 | 2020-10-03 13:27:47 -0700 | [diff] [blame] | 482 | << ": Somebody wrote outside the buffer of their message on channel " |
| 483 | << configuration::CleanedChannelToString(channel()); |
| 484 | |
Austin Schuh | 65493d6 | 2022-08-17 15:10:37 -0700 | [diff] [blame] | 485 | wake_upper_.Wakeup(event_loop()->is_running() |
| 486 | ? event_loop()->runtime_realtime_priority() |
| 487 | : 0); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 488 | return CheckLocklessQueueResult(result); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 489 | } |
| 490 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 491 | Error DoSend(const void *msg, size_t length, |
| 492 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 493 | aos::realtime_clock::time_point realtime_remote_time, |
Austin Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 494 | aos::monotonic_clock::time_point monotonic_remote_transmit_time, |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 495 | uint32_t remote_queue_index, |
| 496 | const UUID &source_boot_uuid) override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 497 | shm_event_loop()->CheckCurrentThread(); |
Austin Schuh | 0f7ed46 | 2020-03-28 20:38:34 -0700 | [diff] [blame] | 498 | CHECK_LE(length, static_cast<size_t>(channel()->max_size())) |
| 499 | << ": Sent too big a message on " |
| 500 | << configuration::CleanedChannelToString(channel()); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 501 | const auto result = lockless_queue_sender_.Send( |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 502 | reinterpret_cast<const char *>(msg), length, monotonic_remote_time, |
Austin Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 503 | realtime_remote_time, monotonic_remote_transmit_time, |
| 504 | remote_queue_index, source_boot_uuid, &monotonic_sent_time_, |
| 505 | &realtime_sent_time_, &sent_queue_index_); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 506 | |
| 507 | CHECK_NE(result, ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE) |
| 508 | << ": Somebody wrote outside the buffer of their message on " |
| 509 | "channel " |
Austin Schuh | 91ba639 | 2020-10-03 13:27:47 -0700 | [diff] [blame] | 510 | << configuration::CleanedChannelToString(channel()); |
Austin Schuh | 65493d6 | 2022-08-17 15:10:37 -0700 | [diff] [blame] | 511 | wake_upper_.Wakeup(event_loop()->is_running() |
| 512 | ? event_loop()->runtime_realtime_priority() |
| 513 | : 0); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 514 | |
| 515 | return CheckLocklessQueueResult(result); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 518 | absl::Span<char> GetSharedMemory() const { |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 519 | return lockless_queue_memory_.GetMutableSharedMemory(); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 522 | int buffer_index() override { |
| 523 | shm_event_loop()->CheckCurrentThread(); |
| 524 | return lockless_queue_sender_.buffer_index(); |
| 525 | } |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 526 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 527 | private: |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 528 | const ShmEventLoop *shm_event_loop() const { |
| 529 | return static_cast<const ShmEventLoop *>(event_loop()); |
| 530 | } |
| 531 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 532 | RawSender::Error CheckLocklessQueueResult( |
| 533 | const ipc_lib::LocklessQueueSender::Result &result) { |
| 534 | switch (result) { |
| 535 | case ipc_lib::LocklessQueueSender::Result::GOOD: |
| 536 | return Error::kOk; |
| 537 | case ipc_lib::LocklessQueueSender::Result::MESSAGES_SENT_TOO_FAST: |
| 538 | return Error::kMessagesSentTooFast; |
| 539 | case ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE: |
| 540 | return Error::kInvalidRedzone; |
| 541 | } |
| 542 | LOG(FATAL) << "Unknown lockless queue sender result" |
| 543 | << static_cast<int>(result); |
| 544 | } |
| 545 | |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 546 | ipc_lib::MemoryMappedQueue lockless_queue_memory_; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 547 | ipc_lib::LocklessQueueSender lockless_queue_sender_; |
| 548 | ipc_lib::LocklessQueueWakeUpper wake_upper_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 549 | }; |
| 550 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 551 | // Class to manage the state for a Watcher. |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 552 | class ShmWatcherState : public WatcherState { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 553 | public: |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 554 | ShmWatcherState( |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 555 | std::string_view shm_base, ShmEventLoop *event_loop, |
| 556 | const Channel *channel, |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 557 | std::function<void(const Context &context, const void *message)> fn, |
| 558 | bool copy_data) |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 559 | : WatcherState(event_loop, channel, std::move(fn)), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 560 | event_loop_(event_loop), |
| 561 | event_(this), |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 562 | simple_shm_fetcher_(shm_base, event_loop, channel) { |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 563 | if (copy_data) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 564 | simple_shm_fetcher_.RetrieveData(); |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 567 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 568 | ~ShmWatcherState() override { |
| 569 | event_loop_->CheckCurrentThread(); |
| 570 | event_loop_->RemoveEvent(&event_); |
| 571 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 572 | |
Philipp Schrader | 81fa3fb | 2023-09-17 18:58:35 -0700 | [diff] [blame] | 573 | void Construct() override { |
| 574 | event_loop_->CheckCurrentThread(); |
| 575 | CHECK(RegisterWakeup(event_loop_->runtime_realtime_priority())); |
| 576 | } |
| 577 | |
| 578 | void Startup() override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 579 | event_loop_->CheckCurrentThread(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 580 | simple_shm_fetcher_.PointAtNextQueueIndex(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 581 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 582 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 583 | // Returns true if there is new data available. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 584 | bool CheckForNewData() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 585 | if (!has_new_data_) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 586 | has_new_data_ = simple_shm_fetcher_.FetchNext(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 587 | |
| 588 | if (has_new_data_) { |
| 589 | event_.set_event_time( |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 590 | simple_shm_fetcher_.context().monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 591 | event_loop_->AddEvent(&event_); |
| 592 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | return has_new_data_; |
| 596 | } |
| 597 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 598 | // Consumes the data by calling the callback. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 599 | void HandleEvent() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 600 | CHECK(has_new_data_); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 601 | DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 602 | has_new_data_ = false; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 603 | CheckForNewData(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 606 | // Registers us to receive a signal on event reception. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 607 | bool RegisterWakeup(int priority) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 608 | return simple_shm_fetcher_.RegisterWakeup(priority); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 611 | void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 612 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 613 | absl::Span<const char> GetSharedMemory() const { |
| 614 | return simple_shm_fetcher_.GetConstSharedMemory(); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 617 | private: |
| 618 | bool has_new_data_ = false; |
| 619 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 620 | ShmEventLoop *event_loop_; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 621 | EventHandler<ShmWatcherState> event_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 622 | SimpleShmFetcher simple_shm_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 623 | }; |
| 624 | |
| 625 | // Adapter class to adapt a timerfd to a TimerHandler. |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 626 | class ShmTimerHandler final : public TimerHandler { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 627 | public: |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 628 | ShmTimerHandler(ShmEventLoop *shm_event_loop, ::std::function<void()> fn) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 629 | : TimerHandler(shm_event_loop, std::move(fn)), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 630 | shm_event_loop_(shm_event_loop), |
| 631 | event_(this) { |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 632 | shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() { |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 633 | // The timer may fire spuriously. HandleEvent on the event loop will |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 634 | // call the callback if it is needed. It may also have called it when |
| 635 | // processing some other event, and the kernel decided to deliver this |
| 636 | // wakeup anyways. |
| 637 | timerfd_.Read(); |
| 638 | shm_event_loop_->HandleEvent(); |
| 639 | }); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 642 | ~ShmTimerHandler() { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 643 | shm_event_loop_->CheckCurrentThread(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 644 | Disable(); |
| 645 | shm_event_loop_->epoll_.DeleteFd(timerfd_.fd()); |
| 646 | } |
| 647 | |
| 648 | void HandleEvent() { |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 649 | CHECK(!event_.valid()); |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 650 | disabled_ = false; |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 651 | const auto monotonic_now = Call(monotonic_clock::now, base_); |
| 652 | if (event_.valid()) { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 653 | // If someone called Schedule inside Call, rescheduling is already taken |
| 654 | // care of. Bail. |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 655 | return; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 656 | } |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 657 | if (disabled_) { |
| 658 | // Somebody called Disable inside Call, so we don't want to reschedule. |
| 659 | // Bail. |
| 660 | return; |
| 661 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 662 | |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 663 | if (repeat_offset_ == std::chrono::seconds(0)) { |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 664 | timerfd_.Disable(); |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 665 | disabled_ = true; |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 666 | } else { |
| 667 | // Compute how many cycles have elapsed and schedule the next iteration |
| 668 | // for the next iteration in the future. |
| 669 | const int elapsed_cycles = |
| 670 | std::max<int>(0, (monotonic_now - base_ + repeat_offset_ - |
| 671 | std::chrono::nanoseconds(1)) / |
| 672 | repeat_offset_); |
| 673 | base_ += repeat_offset_ * elapsed_cycles; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 674 | |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 675 | // Update the heap and schedule the timerfd wakeup. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 676 | event_.set_event_time(base_); |
| 677 | shm_event_loop_->AddEvent(&event_); |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 678 | timerfd_.SetTime(base_, std::chrono::seconds(0)); |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 679 | disabled_ = false; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 680 | } |
| 681 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 682 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 683 | void Schedule(monotonic_clock::time_point base, |
| 684 | monotonic_clock::duration repeat_offset) override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 685 | shm_event_loop_->CheckCurrentThread(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 686 | if (event_.valid()) { |
| 687 | shm_event_loop_->RemoveEvent(&event_); |
| 688 | } |
| 689 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 690 | timerfd_.SetTime(base, repeat_offset); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 691 | base_ = base; |
| 692 | repeat_offset_ = repeat_offset; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 693 | event_.set_event_time(base_); |
| 694 | shm_event_loop_->AddEvent(&event_); |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 695 | disabled_ = false; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 698 | void Disable() override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 699 | shm_event_loop_->CheckCurrentThread(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 700 | shm_event_loop_->RemoveEvent(&event_); |
| 701 | timerfd_.Disable(); |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 702 | disabled_ = true; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 703 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 704 | |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 705 | bool IsDisabled() override { return disabled_; } |
| 706 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 707 | private: |
| 708 | ShmEventLoop *shm_event_loop_; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 709 | EventHandler<ShmTimerHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 710 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 711 | internal::TimerFd timerfd_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 712 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 713 | monotonic_clock::time_point base_; |
| 714 | monotonic_clock::duration repeat_offset_; |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 715 | |
| 716 | // Used to track if Disable() was called during the callback, so we know not |
| 717 | // to reschedule. |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 718 | bool disabled_ = true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 719 | }; |
| 720 | |
| 721 | // Adapter class to the timerfd and PhasedLoop. |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 722 | class ShmPhasedLoopHandler final : public PhasedLoopHandler { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 723 | public: |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 724 | ShmPhasedLoopHandler(ShmEventLoop *shm_event_loop, |
| 725 | ::std::function<void(int)> fn, |
| 726 | const monotonic_clock::duration interval, |
| 727 | const monotonic_clock::duration offset) |
| 728 | : PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 729 | shm_event_loop_(shm_event_loop), |
| 730 | event_(this) { |
| 731 | shm_event_loop_->epoll_.OnReadable( |
| 732 | timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); }); |
| 733 | } |
| 734 | |
| 735 | void HandleEvent() { |
| 736 | // The return value for read is the number of cycles that have elapsed. |
| 737 | // Because we check to see when this event *should* have happened, there are |
| 738 | // cases where Read() will return 0, when 1 cycle has actually happened. |
| 739 | // This occurs when the timer interrupt hasn't triggered yet. Therefore, |
| 740 | // ignore it. Call handles rescheduling and calculating elapsed cycles |
| 741 | // without any extra help. |
| 742 | timerfd_.Read(); |
| 743 | event_.Invalidate(); |
| 744 | |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 745 | Call(monotonic_clock::now); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 748 | ~ShmPhasedLoopHandler() override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 749 | shm_event_loop_->CheckCurrentThread(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 750 | shm_event_loop_->epoll_.DeleteFd(timerfd_.fd()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 751 | shm_event_loop_->RemoveEvent(&event_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | private: |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 755 | // Reschedules the timer. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 756 | void Schedule(monotonic_clock::time_point sleep_time) override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 757 | shm_event_loop_->CheckCurrentThread(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 758 | if (event_.valid()) { |
| 759 | shm_event_loop_->RemoveEvent(&event_); |
| 760 | } |
| 761 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 762 | timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 763 | event_.set_event_time(sleep_time); |
| 764 | shm_event_loop_->AddEvent(&event_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | ShmEventLoop *shm_event_loop_; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 768 | EventHandler<ShmPhasedLoopHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 769 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 770 | internal::TimerFd timerfd_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 771 | }; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 772 | |
| 773 | } // namespace shm_event_loop_internal |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 774 | |
| 775 | ::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher( |
| 776 | const Channel *channel) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 777 | CheckCurrentThread(); |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 778 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 779 | LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view() |
| 780 | << "\", \"type\": \"" << channel->type()->string_view() |
| 781 | << "\" } is not able to be fetched on this node. Check your " |
| 782 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 785 | return ::std::unique_ptr<RawFetcher>( |
| 786 | new ShmFetcher(shm_base_, this, channel)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | ::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender( |
| 790 | const Channel *channel) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 791 | CheckCurrentThread(); |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 792 | TakeSender(channel); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 793 | |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 794 | return ::std::unique_ptr<RawSender>(new ShmSender(shm_base_, this, channel)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | void ShmEventLoop::MakeRawWatcher( |
| 798 | const Channel *channel, |
| 799 | std::function<void(const Context &context, const void *message)> watcher) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 800 | CheckCurrentThread(); |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 801 | TakeWatcher(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 802 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 803 | NewWatcher(::std::unique_ptr<WatcherState>( |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 804 | new ShmWatcherState(shm_base_, this, channel, std::move(watcher), true))); |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | void ShmEventLoop::MakeRawNoArgWatcher( |
| 808 | const Channel *channel, |
| 809 | std::function<void(const Context &context)> watcher) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 810 | CheckCurrentThread(); |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 811 | TakeWatcher(channel); |
| 812 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 813 | NewWatcher(::std::unique_ptr<WatcherState>(new ShmWatcherState( |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 814 | shm_base_, this, channel, |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 815 | [watcher](const Context &context, const void *) { watcher(context); }, |
| 816 | false))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 820 | CheckCurrentThread(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 821 | return NewTimer(::std::unique_ptr<TimerHandler>( |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 822 | new ShmTimerHandler(this, ::std::move(callback)))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | PhasedLoopHandler *ShmEventLoop::AddPhasedLoop( |
| 826 | ::std::function<void(int)> callback, |
| 827 | const monotonic_clock::duration interval, |
| 828 | const monotonic_clock::duration offset) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 829 | CheckCurrentThread(); |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 830 | return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>( |
| 831 | new ShmPhasedLoopHandler(this, ::std::move(callback), interval, offset))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | void ShmEventLoop::OnRun(::std::function<void()> on_run) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 835 | CheckCurrentThread(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 836 | on_run_.push_back(::std::move(on_run)); |
| 837 | } |
| 838 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 839 | void ShmEventLoop::CheckCurrentThread() const { |
| 840 | if (__builtin_expect(check_mutex_ != nullptr, false)) { |
| 841 | CHECK(check_mutex_->is_locked()) |
| 842 | << ": The configured mutex is not locked while calling a " |
| 843 | "ShmEventLoop function"; |
| 844 | } |
| 845 | if (__builtin_expect(!!check_tid_, false)) { |
| 846 | CHECK_EQ(syscall(SYS_gettid), *check_tid_) |
| 847 | << ": Being called from the wrong thread"; |
| 848 | } |
| 849 | } |
| 850 | |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 851 | // This is a bit tricky because watchers can generate new events at any time (as |
| 852 | // long as it's in the past). We want to check the watchers at least once before |
| 853 | // declaring there are no events to handle, and we want to check them again if |
| 854 | // event processing takes long enough that we find an event after that point in |
| 855 | // time to handle. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 856 | void ShmEventLoop::HandleEvent() { |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 857 | // Time through which we've checked for new events in watchers. |
| 858 | monotonic_clock::time_point checked_until = monotonic_clock::min_time; |
| 859 | if (!signalfd_) { |
| 860 | // Nothing to check, so we can bail out immediately once we're out of |
| 861 | // events. |
| 862 | CHECK(watchers_.empty()); |
| 863 | checked_until = monotonic_clock::max_time; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 864 | } |
| 865 | |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 866 | // Loop until we run out of events to check. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 867 | while (true) { |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 868 | // Time of the next event we know about. If this is before checked_until, we |
| 869 | // know there aren't any new events before the next one that we already know |
| 870 | // about, so no need to check the watchers. |
| 871 | monotonic_clock::time_point next_time = monotonic_clock::max_time; |
| 872 | |
| 873 | if (EventCount() == 0) { |
| 874 | if (checked_until != monotonic_clock::min_time) { |
| 875 | // No events, and we've already checked the watchers at least once, so |
| 876 | // we're all done. |
| 877 | // |
| 878 | // There's a small chance that a watcher has gotten another event in |
| 879 | // between checked_until and now. If so, then the signalfd will be |
| 880 | // triggered now and we'll re-enter HandleEvent immediately. This is |
| 881 | // unlikely though, so we don't want to spend time checking all the |
| 882 | // watchers unnecessarily. |
| 883 | break; |
| 884 | } |
| 885 | } else { |
| 886 | next_time = PeekEvent()->event_time(); |
| 887 | } |
Austin Schuh | 00cad2e | 2022-12-02 20:11:04 -0800 | [diff] [blame] | 888 | monotonic_clock::time_point now; |
| 889 | bool new_data = false; |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 890 | |
| 891 | if (next_time > checked_until) { |
| 892 | // Read all of the signals, because there's no point in waking up again |
| 893 | // immediately to handle each one if we've fallen behind. |
| 894 | // |
| 895 | // This is safe before checking for new data on the watchers. If a signal |
| 896 | // is cleared here, the corresponding CheckForNewData() call below will |
| 897 | // pick it up. |
| 898 | while (true) { |
| 899 | const signalfd_siginfo result = signalfd_->Read(); |
| 900 | if (result.ssi_signo == 0) { |
| 901 | break; |
| 902 | } |
| 903 | CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal); |
| 904 | } |
Austin Schuh | 00cad2e | 2022-12-02 20:11:04 -0800 | [diff] [blame] | 905 | // This is the last time we can guarantee that if a message is published |
| 906 | // before, we will notice it. |
| 907 | now = monotonic_clock::now(); |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 908 | |
| 909 | // Check all the watchers for new events. |
| 910 | for (std::unique_ptr<WatcherState> &base_watcher : watchers_) { |
| 911 | ShmWatcherState *const watcher = |
| 912 | reinterpret_cast<ShmWatcherState *>(base_watcher.get()); |
| 913 | |
Austin Schuh | 00cad2e | 2022-12-02 20:11:04 -0800 | [diff] [blame] | 914 | // Track if we got a message. |
| 915 | if (watcher->CheckForNewData()) { |
| 916 | new_data = true; |
| 917 | } |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 918 | } |
| 919 | if (EventCount() == 0) { |
| 920 | // Still no events, all done now. |
| 921 | break; |
| 922 | } |
| 923 | |
| 924 | checked_until = now; |
| 925 | // Check for any new events we found. |
| 926 | next_time = PeekEvent()->event_time(); |
Austin Schuh | 00cad2e | 2022-12-02 20:11:04 -0800 | [diff] [blame] | 927 | } else { |
| 928 | now = monotonic_clock::now(); |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | if (next_time > now) { |
Austin Schuh | 00cad2e | 2022-12-02 20:11:04 -0800 | [diff] [blame] | 932 | // Ok, we got a message with a timestamp *after* we wrote down time. We |
| 933 | // need to process it (otherwise we will go to sleep without processing |
| 934 | // it), but we also need to make sure no other messages have come in |
| 935 | // before it that we would process out of order. Just go around again to |
| 936 | // redo the checks. |
| 937 | if (new_data) { |
| 938 | continue; |
| 939 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 940 | break; |
| 941 | } |
| 942 | |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 943 | EventLoopEvent *const event = PopEvent(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 944 | event->HandleEvent(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 948 | // RAII class to mask signals. |
| 949 | class ScopedSignalMask { |
| 950 | public: |
| 951 | ScopedSignalMask(std::initializer_list<int> signals) { |
| 952 | sigset_t sigset; |
| 953 | PCHECK(sigemptyset(&sigset) == 0); |
| 954 | for (int signal : signals) { |
| 955 | PCHECK(sigaddset(&sigset, signal) == 0); |
| 956 | } |
| 957 | |
| 958 | PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0); |
| 959 | } |
| 960 | |
| 961 | ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); } |
| 962 | |
| 963 | private: |
| 964 | sigset_t old_; |
| 965 | }; |
| 966 | |
| 967 | // Class to manage the static state associated with killing multiple event |
| 968 | // loops. |
| 969 | class SignalHandler { |
| 970 | public: |
| 971 | // Gets the singleton. |
| 972 | static SignalHandler *global() { |
| 973 | static SignalHandler loop; |
| 974 | return &loop; |
| 975 | } |
| 976 | |
| 977 | // Handles the signal with the singleton. |
| 978 | static void HandleSignal(int) { global()->DoHandleSignal(); } |
| 979 | |
| 980 | // Registers an event loop to receive Exit() calls. |
| 981 | void Register(ShmEventLoop *event_loop) { |
| 982 | // Block signals while we have the mutex so we never race with the signal |
| 983 | // handler. |
| 984 | ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM}); |
| 985 | std::unique_lock<stl_mutex> locker(mutex_); |
| 986 | if (event_loops_.size() == 0) { |
| 987 | // The first caller registers the signal handler. |
| 988 | struct sigaction new_action; |
| 989 | sigemptyset(&new_action.sa_mask); |
| 990 | // This makes it so that 2 control c's to a stuck process will kill it by |
| 991 | // restoring the original signal handler. |
| 992 | new_action.sa_flags = SA_RESETHAND; |
| 993 | new_action.sa_handler = &HandleSignal; |
| 994 | |
| 995 | PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0); |
| 996 | PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0); |
| 997 | PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0); |
| 998 | } |
| 999 | |
| 1000 | event_loops_.push_back(event_loop); |
| 1001 | } |
| 1002 | |
| 1003 | // Unregisters an event loop to receive Exit() calls. |
| 1004 | void Unregister(ShmEventLoop *event_loop) { |
| 1005 | // Block signals while we have the mutex so we never race with the signal |
| 1006 | // handler. |
| 1007 | ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM}); |
| 1008 | std::unique_lock<stl_mutex> locker(mutex_); |
| 1009 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 1010 | event_loops_.erase( |
| 1011 | std::find(event_loops_.begin(), event_loops_.end(), event_loop)); |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 1012 | |
| 1013 | if (event_loops_.size() == 0u) { |
| 1014 | // The last caller restores the original signal handlers. |
| 1015 | PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0); |
| 1016 | PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0); |
| 1017 | PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0); |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | private: |
| 1022 | void DoHandleSignal() { |
| 1023 | // We block signals while grabbing the lock, so there should never be a |
| 1024 | // race. Confirm that this is true using trylock. |
| 1025 | CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while " |
| 1026 | "modifing the event loop list."; |
| 1027 | for (ShmEventLoop *event_loop : event_loops_) { |
| 1028 | event_loop->Exit(); |
| 1029 | } |
| 1030 | mutex_.unlock(); |
| 1031 | } |
| 1032 | |
| 1033 | // Mutex to protect all state. |
| 1034 | stl_mutex mutex_; |
| 1035 | std::vector<ShmEventLoop *> event_loops_; |
| 1036 | struct sigaction old_action_int_; |
| 1037 | struct sigaction old_action_hup_; |
| 1038 | struct sigaction old_action_term_; |
| 1039 | }; |
| 1040 | |
James Kuszmaul | 9f99808 | 2024-05-23 15:37:35 -0700 | [diff] [blame] | 1041 | Result<void> ShmEventLoop::Run() { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1042 | CheckCurrentThread(); |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 1043 | SignalHandler::global()->Register(this); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1044 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1045 | if (watchers_.size() > 0) { |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 1046 | signalfd_.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal})); |
Brian Silverman | 3697528 | 2021-07-29 12:06:55 -0700 | [diff] [blame] | 1047 | signalfd_->LeaveSignalBlocked(ipc_lib::kWakeupSignal); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1048 | |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 1049 | epoll_.OnReadable(signalfd_->fd(), [this]() { HandleEvent(); }); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1052 | MaybeScheduleTimingReports(); |
| 1053 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1054 | ReserveEvents(); |
| 1055 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1056 | { |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 1057 | logging::ScopedLogRestorer prev_logger; |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1058 | AosLogToFbs aos_logger; |
| 1059 | if (!skip_logger_) { |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 1060 | aos_logger.Initialize(&name_, MakeSender<logging::LogMessageFbs>("/aos")); |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 1061 | prev_logger.Swap(aos_logger.implementation()); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1062 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1063 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1064 | aos::SetCurrentThreadName(name_.substr(0, 16)); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 1065 | const cpu_set_t default_affinity = DefaultAffinity(); |
| 1066 | if (!CPU_EQUAL(&affinity_, &default_affinity)) { |
| 1067 | ::aos::SetCurrentThreadAffinity(affinity_); |
| 1068 | } |
Philipp Schrader | 81fa3fb | 2023-09-17 18:58:35 -0700 | [diff] [blame] | 1069 | |
| 1070 | // Construct the watchers, but don't update the next pointer. This also |
| 1071 | // cleans up any watchers that previously died, and puts the nonrt work |
| 1072 | // before going realtime. After this happens, we will start queueing |
| 1073 | // signals (which may be a bit of extra work to process, but won't cause any |
| 1074 | // messages to be lost). |
| 1075 | for (::std::unique_ptr<WatcherState> &watcher : watchers_) { |
| 1076 | watcher->Construct(); |
| 1077 | } |
| 1078 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1079 | // Now, all the callbacks are setup. Lock everything into memory and go RT. |
| 1080 | if (priority_ != 0) { |
| 1081 | ::aos::InitRT(); |
| 1082 | |
| 1083 | LOG(INFO) << "Setting priority to " << priority_; |
| 1084 | ::aos::SetCurrentThreadRealtimePriority(priority_); |
| 1085 | } |
| 1086 | |
| 1087 | set_is_running(true); |
| 1088 | |
| 1089 | // Now that we are realtime (but before the OnRun handlers run), snap the |
Philipp Schrader | 81fa3fb | 2023-09-17 18:58:35 -0700 | [diff] [blame] | 1090 | // queue index pointer to the newest message. This happens in RT so that we |
| 1091 | // minimize the risk of losing messages. |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1092 | for (::std::unique_ptr<WatcherState> &watcher : watchers_) { |
Philipp Schrader | 81fa3fb | 2023-09-17 18:58:35 -0700 | [diff] [blame] | 1093 | watcher->Startup(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | // Now that we are RT, run all the OnRun handlers. |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1097 | SetTimerContext(monotonic_clock::now()); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1098 | for (const auto &run : on_run_) { |
| 1099 | run(); |
| 1100 | } |
| 1101 | |
| 1102 | // And start our main event loop which runs all the timers and handles Quit. |
| 1103 | epoll_.Run(); |
| 1104 | |
| 1105 | // Once epoll exits, there is no useful nonrt work left to do. |
| 1106 | set_is_running(false); |
| 1107 | |
| 1108 | // Nothing time or synchronization critical needs to happen after this |
| 1109 | // point. Drop RT priority. |
| 1110 | ::aos::UnsetCurrentThreadRealtimePriority(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1113 | for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) { |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 1114 | ShmWatcherState *watcher = |
| 1115 | reinterpret_cast<ShmWatcherState *>(base_watcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1116 | watcher->UnregisterWakeup(); |
| 1117 | } |
| 1118 | |
| 1119 | if (watchers_.size() > 0) { |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 1120 | epoll_.DeleteFd(signalfd_->fd()); |
| 1121 | signalfd_.reset(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1122 | } |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 1123 | |
| 1124 | SignalHandler::global()->Unregister(this); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1125 | |
| 1126 | // Trigger any remaining senders or fetchers to be cleared before destroying |
| 1127 | // the event loop so the book keeping matches. Do this in the thread that |
| 1128 | // created the timing reporter. |
| 1129 | timing_report_sender_.reset(); |
Austin Schuh | 0debde1 | 2022-08-17 16:25:17 -0700 | [diff] [blame] | 1130 | ClearContext(); |
James Kuszmaul | 9f99808 | 2024-05-23 15:37:35 -0700 | [diff] [blame] | 1131 | std::unique_lock<aos::stl_mutex> locker(exit_status_mutex_); |
| 1132 | std::optional<Result<void>> exit_status; |
| 1133 | // Clear the stored exit_status_ and extract it to be returned. |
| 1134 | exit_status_.swap(exit_status); |
| 1135 | return exit_status.value_or(Result<void>{}); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
James Kuszmaul | 9f99808 | 2024-05-23 15:37:35 -0700 | [diff] [blame] | 1138 | void ShmEventLoop::Exit() { |
| 1139 | observed_exit_.test_and_set(); |
| 1140 | // Implicitly defaults exit_status_ to success by not setting it. |
| 1141 | |
| 1142 | epoll_.Quit(); |
| 1143 | } |
| 1144 | |
| 1145 | void ShmEventLoop::ExitWithStatus(Result<void> status) { |
| 1146 | // Only set the exit status if no other Exit*() call got here first. |
| 1147 | if (!observed_exit_.test_and_set()) { |
| 1148 | std::unique_lock<aos::stl_mutex> locker(exit_status_mutex_); |
| 1149 | exit_status_ = std::move(status); |
| 1150 | } else { |
| 1151 | VLOG(1) << "Exit status is already set; not setting it again."; |
| 1152 | } |
| 1153 | Exit(); |
| 1154 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1155 | |
Brian Silverman | e1fe251 | 2022-08-14 23:18:50 -0700 | [diff] [blame] | 1156 | std::unique_ptr<ExitHandle> ShmEventLoop::MakeExitHandle() { |
| 1157 | return std::make_unique<ShmExitHandle>(this); |
| 1158 | } |
| 1159 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1160 | ShmEventLoop::~ShmEventLoop() { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1161 | CheckCurrentThread(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1162 | // Force everything with a registered fd with epoll to be destroyed now. |
| 1163 | timers_.clear(); |
| 1164 | phased_loops_.clear(); |
| 1165 | watchers_.clear(); |
| 1166 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1167 | CHECK(!is_running()) << ": ShmEventLoop destroyed while running"; |
Brian Silverman | e1fe251 | 2022-08-14 23:18:50 -0700 | [diff] [blame] | 1168 | CHECK_EQ(0, exit_handle_count_) |
| 1169 | << ": All ExitHandles must be destroyed before the ShmEventLoop"; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1172 | void ShmEventLoop::SetRuntimeRealtimePriority(int priority) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1173 | CheckCurrentThread(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1174 | if (is_running()) { |
| 1175 | LOG(FATAL) << "Cannot set realtime priority while running."; |
| 1176 | } |
| 1177 | priority_ = priority; |
| 1178 | } |
| 1179 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 1180 | void ShmEventLoop::SetRuntimeAffinity(const cpu_set_t &cpuset) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1181 | CheckCurrentThread(); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 1182 | if (is_running()) { |
| 1183 | LOG(FATAL) << "Cannot set affinity while running."; |
| 1184 | } |
| 1185 | affinity_ = cpuset; |
| 1186 | } |
| 1187 | |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 1188 | void ShmEventLoop::set_name(const std::string_view name) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1189 | CheckCurrentThread(); |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 1190 | name_ = std::string(name); |
| 1191 | UpdateTimingReport(); |
| 1192 | } |
| 1193 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 1194 | absl::Span<const char> ShmEventLoop::GetWatcherSharedMemory( |
| 1195 | const Channel *channel) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1196 | CheckCurrentThread(); |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 1197 | ShmWatcherState *const watcher_state = |
| 1198 | static_cast<ShmWatcherState *>(GetWatcherState(channel)); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 1199 | return watcher_state->GetSharedMemory(); |
| 1200 | } |
| 1201 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1202 | int ShmEventLoop::NumberBuffers(const Channel *channel) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1203 | CheckCurrentThread(); |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 1204 | return ipc_lib::MakeQueueConfiguration(configuration(), channel) |
| 1205 | .num_messages(); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 1208 | absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory( |
| 1209 | const aos::RawSender *sender) const { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1210 | CheckCurrentThread(); |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 1211 | return static_cast<const ShmSender *>(sender)->GetSharedMemory(); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 1214 | absl::Span<const char> ShmEventLoop::GetShmFetcherPrivateMemory( |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 1215 | const aos::RawFetcher *fetcher) const { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1216 | CheckCurrentThread(); |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 1217 | return static_cast<const ShmFetcher *>(fetcher)->GetPrivateMemory(); |
| 1218 | } |
| 1219 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1220 | pid_t ShmEventLoop::GetTid() { |
| 1221 | CheckCurrentThread(); |
| 1222 | return syscall(SYS_gettid); |
| 1223 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1224 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1225 | } // namespace aos |