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