Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/events/shm_event_loop.h" |
| 2 | |
| 3 | #include <sys/mman.h> |
| 4 | #include <sys/stat.h> |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 5 | #include <sys/syscall.h> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | #include <sys/types.h> |
| 7 | #include <unistd.h> |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 8 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | #include <atomic> |
| 11 | #include <chrono> |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 12 | #include <iterator> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | #include <stdexcept> |
| 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" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | #include "aos/ipc_lib/lockless_queue.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 20 | #include "aos/ipc_lib/signalfd.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | #include "aos/realtime.h" |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 22 | #include "aos/stl_mutex/stl_mutex.h" |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame] | 23 | #include "aos/util/file.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | #include "aos/util/phased_loop.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 25 | #include "glog/logging.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 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."); |
| 43 | DEFINE_uint32(permissions, 0770, |
| 44 | "Permissions to make shared memory files and folders."); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 45 | DEFINE_string(application_name, Filename(program_invocation_name), |
| 46 | "The application name"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 47 | |
| 48 | namespace aos { |
| 49 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 50 | using namespace shm_event_loop_internal; |
| 51 | |
Austin Schuh | cdab619 | 2019-12-29 17:47:46 -0800 | [diff] [blame] | 52 | void SetShmBase(const std::string_view base) { |
| 53 | FLAGS_shm_base = std::string(base) + "/dev/shm/aos"; |
| 54 | } |
| 55 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 56 | std::string ShmFolder(const Channel *channel) { |
| 57 | CHECK(channel->has_name()); |
| 58 | CHECK_EQ(channel->name()->string_view()[0], '/'); |
| 59 | return FLAGS_shm_base + channel->name()->str() + "/"; |
| 60 | } |
| 61 | std::string ShmPath(const Channel *channel) { |
| 62 | CHECK(channel->has_type()); |
Austin Schuh | 3328d13 | 2020-02-28 13:54:57 -0800 | [diff] [blame] | 63 | return ShmFolder(channel) + channel->type()->str() + ".v2"; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | class MMapedQueue { |
| 67 | public: |
Austin Schuh | aa79e4e | 2019-12-29 20:43:32 -0800 | [diff] [blame] | 68 | MMapedQueue(const Channel *channel, |
| 69 | const std::chrono::seconds channel_storage_duration) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 70 | std::string path = ShmPath(channel); |
| 71 | |
Austin Schuh | 80c7fce | 2019-12-05 20:48:43 -0800 | [diff] [blame] | 72 | config_.num_watchers = channel->num_watchers(); |
| 73 | config_.num_senders = channel->num_senders(); |
Austin Schuh | aa79e4e | 2019-12-29 20:43:32 -0800 | [diff] [blame] | 74 | config_.queue_size = |
| 75 | channel_storage_duration.count() * channel->frequency(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 76 | config_.message_data_size = channel->max_size(); |
| 77 | |
| 78 | size_ = ipc_lib::LocklessQueueMemorySize(config_); |
| 79 | |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame] | 80 | util::MkdirP(path, FLAGS_permissions); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 81 | |
| 82 | // There are 2 cases. Either the file already exists, or it does not |
| 83 | // already exist and we need to create it. Start by trying to create it. If |
| 84 | // that fails, the file has already been created and we can open it |
| 85 | // normally.. Once the file has been created it wil never be deleted. |
Brian Silverman | f9f30ea | 2020-03-04 23:18:54 -0800 | [diff] [blame] | 86 | int fd = open(path.c_str(), O_RDWR | O_CREAT | O_EXCL, |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 87 | O_CLOEXEC | FLAGS_permissions); |
Brian Silverman | f9f30ea | 2020-03-04 23:18:54 -0800 | [diff] [blame] | 88 | if (fd == -1 && errno == EEXIST) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 89 | VLOG(1) << path << " already created."; |
| 90 | // File already exists. |
Brian Silverman | f9f30ea | 2020-03-04 23:18:54 -0800 | [diff] [blame] | 91 | fd = open(path.c_str(), O_RDWR, O_CLOEXEC); |
| 92 | PCHECK(fd != -1) << ": Failed to open " << path; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 93 | while (true) { |
| 94 | struct stat st; |
Brian Silverman | f9f30ea | 2020-03-04 23:18:54 -0800 | [diff] [blame] | 95 | PCHECK(fstat(fd, &st) == 0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 96 | if (st.st_size != 0) { |
| 97 | CHECK_EQ(static_cast<size_t>(st.st_size), size_) |
| 98 | << ": Size of " << path |
| 99 | << " doesn't match expected size of backing queue file. Did the " |
| 100 | "queue definition change?"; |
| 101 | break; |
| 102 | } else { |
| 103 | // The creating process didn't get around to it yet. Give it a bit. |
| 104 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 105 | VLOG(1) << path << " is zero size, waiting"; |
| 106 | } |
| 107 | } |
| 108 | } else { |
| 109 | VLOG(1) << "Created " << path; |
Brian Silverman | f9f30ea | 2020-03-04 23:18:54 -0800 | [diff] [blame] | 110 | PCHECK(fd != -1) << ": Failed to open " << path; |
| 111 | PCHECK(ftruncate(fd, size_) == 0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Brian Silverman | f9f30ea | 2020-03-04 23:18:54 -0800 | [diff] [blame] | 114 | data_ = mmap(NULL, size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 115 | PCHECK(data_ != MAP_FAILED); |
Brian Silverman | f9f30ea | 2020-03-04 23:18:54 -0800 | [diff] [blame] | 116 | PCHECK(close(fd) == 0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 117 | |
| 118 | ipc_lib::InitializeLocklessQueueMemory(memory(), config_); |
| 119 | } |
| 120 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 121 | ~MMapedQueue() { PCHECK(munmap(data_, size_) == 0); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 122 | |
| 123 | ipc_lib::LocklessQueueMemory *memory() const { |
| 124 | return reinterpret_cast<ipc_lib::LocklessQueueMemory *>(data_); |
| 125 | } |
| 126 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 127 | const ipc_lib::LocklessQueueConfiguration &config() const { return config_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 128 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 129 | absl::Span<char> GetSharedMemory() const { |
| 130 | return absl::Span<char>(static_cast<char *>(data_), size_); |
| 131 | } |
| 132 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 133 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 134 | ipc_lib::LocklessQueueConfiguration config_; |
| 135 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 136 | size_t size_; |
| 137 | void *data_; |
| 138 | }; |
| 139 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 140 | namespace { |
| 141 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 142 | const Node *MaybeMyNode(const Configuration *configuration) { |
| 143 | if (!configuration->has_nodes()) { |
| 144 | return nullptr; |
| 145 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 146 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 147 | return configuration::GetMyNode(configuration); |
| 148 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 149 | |
| 150 | namespace chrono = ::std::chrono; |
| 151 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 152 | } // namespace |
| 153 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 154 | ShmEventLoop::ShmEventLoop(const Configuration *configuration) |
| 155 | : EventLoop(configuration), |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 156 | name_(FLAGS_application_name), |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 157 | node_(MaybeMyNode(configuration)) { |
| 158 | if (configuration->has_nodes()) { |
| 159 | CHECK(node_ != nullptr) << ": Couldn't find node in config."; |
| 160 | } |
| 161 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 162 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 163 | namespace shm_event_loop_internal { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 164 | |
| 165 | class SimpleShmFetcher { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 166 | public: |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 167 | explicit SimpleShmFetcher(EventLoop *event_loop, const Channel *channel, |
| 168 | bool copy_data) |
Austin Schuh | f565259 | 2019-12-29 16:26:15 -0800 | [diff] [blame] | 169 | : channel_(channel), |
Austin Schuh | aa79e4e | 2019-12-29 20:43:32 -0800 | [diff] [blame] | 170 | lockless_queue_memory_( |
| 171 | channel, |
Brian Silverman | 587da25 | 2020-01-01 17:00:47 -0800 | [diff] [blame] | 172 | chrono::ceil<chrono::seconds>(chrono::nanoseconds( |
Austin Schuh | aa79e4e | 2019-12-29 20:43:32 -0800 | [diff] [blame] | 173 | event_loop->configuration()->channel_storage_duration()))), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 174 | lockless_queue_(lockless_queue_memory_.memory(), |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 175 | lockless_queue_memory_.config()) { |
| 176 | if (copy_data) { |
| 177 | data_storage_.reset(static_cast<char *>( |
| 178 | malloc(channel->max_size() + kChannelDataAlignment - 1))); |
| 179 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 180 | context_.data = nullptr; |
| 181 | // Point the queue index at the next index to read starting now. This |
| 182 | // makes it such that FetchNext will read the next message sent after |
| 183 | // the fetcher is created. |
| 184 | PointAtNextQueueIndex(); |
| 185 | } |
| 186 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 187 | ~SimpleShmFetcher() {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 188 | |
| 189 | // Points the next message to fetch at the queue index which will be |
| 190 | // populated next. |
| 191 | void PointAtNextQueueIndex() { |
| 192 | actual_queue_index_ = lockless_queue_.LatestQueueIndex(); |
| 193 | if (!actual_queue_index_.valid()) { |
| 194 | // Nothing in the queue. The next element will show up at the 0th |
| 195 | // index in the queue. |
| 196 | actual_queue_index_ = |
| 197 | ipc_lib::QueueIndex::Zero(lockless_queue_.queue_size()); |
| 198 | } else { |
| 199 | actual_queue_index_ = actual_queue_index_.Increment(); |
| 200 | } |
| 201 | } |
| 202 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 203 | bool FetchNext() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 204 | // TODO(austin): Get behind and make sure it dies both here and with |
| 205 | // Fetch. |
| 206 | ipc_lib::LocklessQueue::ReadResult read_result = lockless_queue_.Read( |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 207 | actual_queue_index_.index(), &context_.monotonic_event_time, |
| 208 | &context_.realtime_event_time, &context_.monotonic_remote_time, |
| 209 | &context_.realtime_remote_time, &context_.remote_queue_index, |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 210 | &context_.size, data_storage_start()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 211 | if (read_result == ipc_lib::LocklessQueue::ReadResult::GOOD) { |
| 212 | context_.queue_index = actual_queue_index_.index(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 213 | if (context_.remote_queue_index == 0xffffffffu) { |
| 214 | context_.remote_queue_index = context_.queue_index; |
| 215 | } |
| 216 | if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) { |
| 217 | context_.monotonic_remote_time = context_.monotonic_event_time; |
| 218 | } |
| 219 | if (context_.realtime_remote_time == aos::realtime_clock::min_time) { |
| 220 | context_.realtime_remote_time = context_.realtime_event_time; |
| 221 | } |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 222 | if (copy_data()) { |
| 223 | context_.data = data_storage_start() + |
| 224 | lockless_queue_.message_data_size() - context_.size; |
| 225 | } else { |
| 226 | context_.data = nullptr; |
| 227 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 228 | actual_queue_index_ = actual_queue_index_.Increment(); |
| 229 | } |
| 230 | |
| 231 | // Make sure the data wasn't modified while we were reading it. This |
| 232 | // can only happen if you are reading the last message *while* it is |
| 233 | // being written to, which means you are pretty far behind. |
| 234 | CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::OVERWROTE) |
| 235 | << ": Got behind while reading and the last message was modified " |
Austin Schuh | f565259 | 2019-12-29 16:26:15 -0800 | [diff] [blame] | 236 | "out from under us while we were reading it. Don't get so far " |
| 237 | "behind. " |
| 238 | << configuration::CleanedChannelToString(channel_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 239 | |
| 240 | CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::TOO_OLD) |
Austin Schuh | f565259 | 2019-12-29 16:26:15 -0800 | [diff] [blame] | 241 | << ": The next message is no longer available. " |
| 242 | << configuration::CleanedChannelToString(channel_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 243 | return read_result == ipc_lib::LocklessQueue::ReadResult::GOOD; |
| 244 | } |
| 245 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 246 | bool Fetch() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 247 | const ipc_lib::QueueIndex queue_index = lockless_queue_.LatestQueueIndex(); |
| 248 | // actual_queue_index_ is only meaningful if it was set by Fetch or |
| 249 | // FetchNext. This happens when valid_data_ has been set. So, only |
| 250 | // skip checking if valid_data_ is true. |
| 251 | // |
| 252 | // Also, if the latest queue index is invalid, we are empty. So there |
| 253 | // is nothing to fetch. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 254 | if ((context_.data != nullptr && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 255 | queue_index == actual_queue_index_.DecrementBy(1u)) || |
| 256 | !queue_index.valid()) { |
| 257 | return false; |
| 258 | } |
| 259 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 260 | ipc_lib::LocklessQueue::ReadResult read_result = lockless_queue_.Read( |
| 261 | queue_index.index(), &context_.monotonic_event_time, |
| 262 | &context_.realtime_event_time, &context_.monotonic_remote_time, |
| 263 | &context_.realtime_remote_time, &context_.remote_queue_index, |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 264 | &context_.size, data_storage_start()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 265 | if (read_result == ipc_lib::LocklessQueue::ReadResult::GOOD) { |
| 266 | context_.queue_index = queue_index.index(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 267 | if (context_.remote_queue_index == 0xffffffffu) { |
| 268 | context_.remote_queue_index = context_.queue_index; |
| 269 | } |
| 270 | if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) { |
| 271 | context_.monotonic_remote_time = context_.monotonic_event_time; |
| 272 | } |
| 273 | if (context_.realtime_remote_time == aos::realtime_clock::min_time) { |
| 274 | context_.realtime_remote_time = context_.realtime_event_time; |
| 275 | } |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 276 | if (copy_data()) { |
| 277 | context_.data = data_storage_start() + |
| 278 | lockless_queue_.message_data_size() - context_.size; |
| 279 | } else { |
| 280 | context_.data = nullptr; |
| 281 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 282 | actual_queue_index_ = queue_index.Increment(); |
| 283 | } |
| 284 | |
| 285 | // Make sure the data wasn't modified while we were reading it. This |
| 286 | // can only happen if you are reading the last message *while* it is |
| 287 | // being written to, which means you are pretty far behind. |
| 288 | CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::OVERWROTE) |
| 289 | << ": Got behind while reading and the last message was modified " |
Austin Schuh | f565259 | 2019-12-29 16:26:15 -0800 | [diff] [blame] | 290 | "out from under us while we were reading it. Don't get so far " |
| 291 | "behind." |
| 292 | << configuration::CleanedChannelToString(channel_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 293 | |
| 294 | CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::NOTHING_NEW) |
Austin Schuh | f565259 | 2019-12-29 16:26:15 -0800 | [diff] [blame] | 295 | << ": Queue index went backwards. This should never happen. " |
| 296 | << configuration::CleanedChannelToString(channel_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 297 | |
| 298 | // We fell behind between when we read the index and read the value. |
| 299 | // This isn't worth recovering from since this means we went to sleep |
| 300 | // for a long time in the middle of this function. |
| 301 | CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::TOO_OLD) |
Austin Schuh | f565259 | 2019-12-29 16:26:15 -0800 | [diff] [blame] | 302 | << ": The next message is no longer available. " |
| 303 | << configuration::CleanedChannelToString(channel_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 304 | return read_result == ipc_lib::LocklessQueue::ReadResult::GOOD; |
| 305 | } |
| 306 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 307 | Context context() const { return context_; } |
| 308 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 309 | bool RegisterWakeup(int priority) { |
| 310 | return lockless_queue_.RegisterWakeup(priority); |
| 311 | } |
| 312 | |
| 313 | void UnregisterWakeup() { lockless_queue_.UnregisterWakeup(); } |
| 314 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 315 | absl::Span<char> GetSharedMemory() const { |
| 316 | return lockless_queue_memory_.GetSharedMemory(); |
| 317 | } |
| 318 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 319 | private: |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 320 | char *data_storage_start() { |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 321 | if (!copy_data()) return nullptr; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 322 | return RoundChannelData(data_storage_.get(), channel_->max_size()); |
| 323 | } |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 324 | bool copy_data() const { return static_cast<bool>(data_storage_); } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 325 | |
Austin Schuh | f565259 | 2019-12-29 16:26:15 -0800 | [diff] [blame] | 326 | const Channel *const channel_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 327 | MMapedQueue lockless_queue_memory_; |
| 328 | ipc_lib::LocklessQueue lockless_queue_; |
| 329 | |
| 330 | ipc_lib::QueueIndex actual_queue_index_ = |
| 331 | ipc_lib::LocklessQueue::empty_queue_index(); |
| 332 | |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 333 | // This being empty indicates we're not going to copy data. |
| 334 | std::unique_ptr<char, decltype(&free)> data_storage_{nullptr, &free}; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 335 | |
| 336 | Context context_; |
| 337 | }; |
| 338 | |
| 339 | class ShmFetcher : public RawFetcher { |
| 340 | public: |
| 341 | explicit ShmFetcher(EventLoop *event_loop, const Channel *channel) |
Austin Schuh | aa79e4e | 2019-12-29 20:43:32 -0800 | [diff] [blame] | 342 | : RawFetcher(event_loop, channel), |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 343 | simple_shm_fetcher_(event_loop, channel, true) {} |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 344 | |
| 345 | ~ShmFetcher() { context_.data = nullptr; } |
| 346 | |
| 347 | std::pair<bool, monotonic_clock::time_point> DoFetchNext() override { |
| 348 | if (simple_shm_fetcher_.FetchNext()) { |
| 349 | context_ = simple_shm_fetcher_.context(); |
| 350 | return std::make_pair(true, monotonic_clock::now()); |
| 351 | } |
| 352 | return std::make_pair(false, monotonic_clock::min_time); |
| 353 | } |
| 354 | |
| 355 | std::pair<bool, monotonic_clock::time_point> DoFetch() override { |
| 356 | if (simple_shm_fetcher_.Fetch()) { |
| 357 | context_ = simple_shm_fetcher_.context(); |
| 358 | return std::make_pair(true, monotonic_clock::now()); |
| 359 | } |
| 360 | return std::make_pair(false, monotonic_clock::min_time); |
| 361 | } |
| 362 | |
| 363 | private: |
| 364 | SimpleShmFetcher simple_shm_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 365 | }; |
| 366 | |
| 367 | class ShmSender : public RawSender { |
| 368 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 369 | explicit ShmSender(EventLoop *event_loop, const Channel *channel) |
| 370 | : RawSender(event_loop, channel), |
Austin Schuh | aa79e4e | 2019-12-29 20:43:32 -0800 | [diff] [blame] | 371 | lockless_queue_memory_( |
| 372 | channel, |
Brian Silverman | 587da25 | 2020-01-01 17:00:47 -0800 | [diff] [blame] | 373 | chrono::ceil<chrono::seconds>(chrono::nanoseconds( |
Austin Schuh | aa79e4e | 2019-12-29 20:43:32 -0800 | [diff] [blame] | 374 | event_loop->configuration()->channel_storage_duration()))), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 375 | lockless_queue_(lockless_queue_memory_.memory(), |
| 376 | lockless_queue_memory_.config()), |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 377 | lockless_queue_sender_( |
| 378 | VerifySender(lockless_queue_.MakeSender(), channel)) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 379 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 380 | ~ShmSender() override {} |
| 381 | |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 382 | static ipc_lib::LocklessQueue::Sender VerifySender( |
| 383 | std::optional<ipc_lib::LocklessQueue::Sender> &&sender, |
| 384 | const Channel *channel) { |
| 385 | if (sender) { |
| 386 | return std::move(sender.value()); |
| 387 | } |
| 388 | LOG(FATAL) << "Failed to create sender on " |
| 389 | << configuration::CleanedChannelToString(channel) |
| 390 | << ", too many senders."; |
| 391 | } |
| 392 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 393 | void *data() override { return lockless_queue_sender_.Data(); } |
| 394 | size_t size() override { return lockless_queue_sender_.size(); } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 395 | bool DoSend(size_t length, |
| 396 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 397 | aos::realtime_clock::time_point realtime_remote_time, |
| 398 | uint32_t remote_queue_index) override { |
Austin Schuh | 0f7ed46 | 2020-03-28 20:38:34 -0700 | [diff] [blame] | 399 | CHECK_LE(length, static_cast<size_t>(channel()->max_size())) |
| 400 | << ": Sent too big a message on " |
| 401 | << configuration::CleanedChannelToString(channel()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 402 | lockless_queue_sender_.Send( |
| 403 | length, monotonic_remote_time, realtime_remote_time, remote_queue_index, |
| 404 | &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 405 | lockless_queue_.Wakeup(event_loop()->priority()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 406 | return true; |
| 407 | } |
| 408 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 409 | bool DoSend(const void *msg, size_t length, |
| 410 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 411 | aos::realtime_clock::time_point realtime_remote_time, |
| 412 | uint32_t remote_queue_index) override { |
Austin Schuh | 0f7ed46 | 2020-03-28 20:38:34 -0700 | [diff] [blame] | 413 | CHECK_LE(length, static_cast<size_t>(channel()->max_size())) |
| 414 | << ": Sent too big a message on " |
| 415 | << configuration::CleanedChannelToString(channel()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 416 | lockless_queue_sender_.Send(reinterpret_cast<const char *>(msg), length, |
| 417 | monotonic_remote_time, realtime_remote_time, |
| 418 | remote_queue_index, &monotonic_sent_time_, |
| 419 | &realtime_sent_time_, &sent_queue_index_); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 420 | lockless_queue_.Wakeup(event_loop()->priority()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 421 | // TODO(austin): Return an error if we send too fast. |
| 422 | return true; |
| 423 | } |
| 424 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 425 | absl::Span<char> GetSharedMemory() const { |
| 426 | return lockless_queue_memory_.GetSharedMemory(); |
| 427 | } |
| 428 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 429 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 430 | MMapedQueue lockless_queue_memory_; |
| 431 | ipc_lib::LocklessQueue lockless_queue_; |
| 432 | ipc_lib::LocklessQueue::Sender lockless_queue_sender_; |
| 433 | }; |
| 434 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 435 | // Class to manage the state for a Watcher. |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 436 | class ShmWatcherState : public WatcherState { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 437 | public: |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 438 | ShmWatcherState( |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 439 | ShmEventLoop *event_loop, const Channel *channel, |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 440 | std::function<void(const Context &context, const void *message)> fn, |
| 441 | bool copy_data) |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 442 | : WatcherState(event_loop, channel, std::move(fn)), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 443 | event_loop_(event_loop), |
| 444 | event_(this), |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 445 | simple_shm_fetcher_(event_loop, channel, copy_data) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 446 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 447 | ~ShmWatcherState() override { event_loop_->RemoveEvent(&event_); } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 448 | |
| 449 | void Startup(EventLoop *event_loop) override { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 450 | simple_shm_fetcher_.PointAtNextQueueIndex(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 451 | CHECK(RegisterWakeup(event_loop->priority())); |
| 452 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 453 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 454 | // Returns true if there is new data available. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 455 | bool CheckForNewData() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 456 | if (!has_new_data_) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 457 | has_new_data_ = simple_shm_fetcher_.FetchNext(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 458 | |
| 459 | if (has_new_data_) { |
| 460 | event_.set_event_time( |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 461 | simple_shm_fetcher_.context().monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 462 | event_loop_->AddEvent(&event_); |
| 463 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | return has_new_data_; |
| 467 | } |
| 468 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 469 | // Consumes the data by calling the callback. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 470 | void HandleEvent() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 471 | CHECK(has_new_data_); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 472 | DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 473 | has_new_data_ = false; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 474 | CheckForNewData(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 477 | // Registers us to receive a signal on event reception. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 478 | bool RegisterWakeup(int priority) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 479 | return simple_shm_fetcher_.RegisterWakeup(priority); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 482 | void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 483 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 484 | absl::Span<char> GetSharedMemory() const { |
| 485 | return simple_shm_fetcher_.GetSharedMemory(); |
| 486 | } |
| 487 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 488 | private: |
| 489 | bool has_new_data_ = false; |
| 490 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 491 | ShmEventLoop *event_loop_; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 492 | EventHandler<ShmWatcherState> event_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 493 | SimpleShmFetcher simple_shm_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | // Adapter class to adapt a timerfd to a TimerHandler. |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 497 | class ShmTimerHandler final : public TimerHandler { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 498 | public: |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 499 | ShmTimerHandler(ShmEventLoop *shm_event_loop, ::std::function<void()> fn) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 500 | : TimerHandler(shm_event_loop, std::move(fn)), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 501 | shm_event_loop_(shm_event_loop), |
| 502 | event_(this) { |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 503 | shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() { |
| 504 | // The timer may fire spurriously. HandleEvent on the event loop will |
| 505 | // call the callback if it is needed. It may also have called it when |
| 506 | // processing some other event, and the kernel decided to deliver this |
| 507 | // wakeup anyways. |
| 508 | timerfd_.Read(); |
| 509 | shm_event_loop_->HandleEvent(); |
| 510 | }); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 513 | ~ShmTimerHandler() { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 514 | Disable(); |
| 515 | shm_event_loop_->epoll_.DeleteFd(timerfd_.fd()); |
| 516 | } |
| 517 | |
| 518 | void HandleEvent() { |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 519 | CHECK(!event_.valid()); |
| 520 | const auto monotonic_now = Call(monotonic_clock::now, base_); |
| 521 | if (event_.valid()) { |
| 522 | // If someone called Setup inside Call, rescheduling is already taken care |
| 523 | // of. Bail. |
| 524 | return; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 527 | if (repeat_offset_ == chrono::seconds(0)) { |
| 528 | timerfd_.Disable(); |
| 529 | } else { |
| 530 | // Compute how many cycles have elapsed and schedule the next iteration |
| 531 | // for the next iteration in the future. |
| 532 | const int elapsed_cycles = |
| 533 | std::max<int>(0, (monotonic_now - base_ + repeat_offset_ - |
| 534 | std::chrono::nanoseconds(1)) / |
| 535 | repeat_offset_); |
| 536 | base_ += repeat_offset_ * elapsed_cycles; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 537 | |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 538 | // Update the heap and schedule the timerfd wakeup. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 539 | event_.set_event_time(base_); |
| 540 | shm_event_loop_->AddEvent(&event_); |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 541 | timerfd_.SetTime(base_, chrono::seconds(0)); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 542 | } |
| 543 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 544 | |
| 545 | void Setup(monotonic_clock::time_point base, |
| 546 | monotonic_clock::duration repeat_offset) override { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 547 | if (event_.valid()) { |
| 548 | shm_event_loop_->RemoveEvent(&event_); |
| 549 | } |
| 550 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 551 | timerfd_.SetTime(base, repeat_offset); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 552 | base_ = base; |
| 553 | repeat_offset_ = repeat_offset; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 554 | event_.set_event_time(base_); |
| 555 | shm_event_loop_->AddEvent(&event_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 558 | void Disable() override { |
| 559 | shm_event_loop_->RemoveEvent(&event_); |
| 560 | timerfd_.Disable(); |
| 561 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 562 | |
| 563 | private: |
| 564 | ShmEventLoop *shm_event_loop_; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 565 | EventHandler<ShmTimerHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 566 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 567 | internal::TimerFd timerfd_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 568 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 569 | monotonic_clock::time_point base_; |
| 570 | monotonic_clock::duration repeat_offset_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 571 | }; |
| 572 | |
| 573 | // Adapter class to the timerfd and PhasedLoop. |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 574 | class ShmPhasedLoopHandler final : public PhasedLoopHandler { |
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 | ShmPhasedLoopHandler(ShmEventLoop *shm_event_loop, |
| 577 | ::std::function<void(int)> fn, |
| 578 | const monotonic_clock::duration interval, |
| 579 | const monotonic_clock::duration offset) |
| 580 | : PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 581 | shm_event_loop_(shm_event_loop), |
| 582 | event_(this) { |
| 583 | shm_event_loop_->epoll_.OnReadable( |
| 584 | timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); }); |
| 585 | } |
| 586 | |
| 587 | void HandleEvent() { |
| 588 | // The return value for read is the number of cycles that have elapsed. |
| 589 | // Because we check to see when this event *should* have happened, there are |
| 590 | // cases where Read() will return 0, when 1 cycle has actually happened. |
| 591 | // This occurs when the timer interrupt hasn't triggered yet. Therefore, |
| 592 | // ignore it. Call handles rescheduling and calculating elapsed cycles |
| 593 | // without any extra help. |
| 594 | timerfd_.Read(); |
| 595 | event_.Invalidate(); |
| 596 | |
| 597 | Call(monotonic_clock::now, [this](monotonic_clock::time_point sleep_time) { |
| 598 | Schedule(sleep_time); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 599 | }); |
| 600 | } |
| 601 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 602 | ~ShmPhasedLoopHandler() override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 603 | shm_event_loop_->epoll_.DeleteFd(timerfd_.fd()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 604 | shm_event_loop_->RemoveEvent(&event_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | private: |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 608 | // Reschedules the timer. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 609 | void Schedule(monotonic_clock::time_point sleep_time) override { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 610 | if (event_.valid()) { |
| 611 | shm_event_loop_->RemoveEvent(&event_); |
| 612 | } |
| 613 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 614 | timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 615 | event_.set_event_time(sleep_time); |
| 616 | shm_event_loop_->AddEvent(&event_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | ShmEventLoop *shm_event_loop_; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 620 | EventHandler<ShmPhasedLoopHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 621 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 622 | internal::TimerFd timerfd_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 623 | }; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 624 | |
| 625 | } // namespace shm_event_loop_internal |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 626 | |
| 627 | ::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher( |
| 628 | const Channel *channel) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 629 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 630 | LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view() |
| 631 | << "\", \"type\": \"" << channel->type()->string_view() |
| 632 | << "\" } is not able to be fetched on this node. Check your " |
| 633 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 636 | return ::std::unique_ptr<RawFetcher>(new ShmFetcher(this, channel)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | ::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender( |
| 640 | const Channel *channel) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 641 | TakeSender(channel); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 642 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 643 | return ::std::unique_ptr<RawSender>(new ShmSender(this, channel)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | void ShmEventLoop::MakeRawWatcher( |
| 647 | const Channel *channel, |
| 648 | std::function<void(const Context &context, const void *message)> watcher) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 649 | TakeWatcher(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 650 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 651 | NewWatcher(::std::unique_ptr<WatcherState>( |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 652 | new ShmWatcherState(this, channel, std::move(watcher), true))); |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | void ShmEventLoop::MakeRawNoArgWatcher( |
| 656 | const Channel *channel, |
| 657 | std::function<void(const Context &context)> watcher) { |
| 658 | TakeWatcher(channel); |
| 659 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 660 | NewWatcher(::std::unique_ptr<WatcherState>(new ShmWatcherState( |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 661 | this, channel, |
| 662 | [watcher](const Context &context, const void *) { watcher(context); }, |
| 663 | false))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 667 | return NewTimer(::std::unique_ptr<TimerHandler>( |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 668 | new ShmTimerHandler(this, ::std::move(callback)))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | PhasedLoopHandler *ShmEventLoop::AddPhasedLoop( |
| 672 | ::std::function<void(int)> callback, |
| 673 | const monotonic_clock::duration interval, |
| 674 | const monotonic_clock::duration offset) { |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 675 | return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>( |
| 676 | new ShmPhasedLoopHandler(this, ::std::move(callback), interval, offset))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | void ShmEventLoop::OnRun(::std::function<void()> on_run) { |
| 680 | on_run_.push_back(::std::move(on_run)); |
| 681 | } |
| 682 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 683 | void ShmEventLoop::HandleEvent() { |
| 684 | // Update all the times for handlers. |
| 685 | for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) { |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 686 | ShmWatcherState *watcher = |
| 687 | reinterpret_cast<ShmWatcherState *>(base_watcher.get()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 688 | |
| 689 | watcher->CheckForNewData(); |
| 690 | } |
| 691 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 692 | while (true) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 693 | if (EventCount() == 0 || |
| 694 | PeekEvent()->event_time() > monotonic_clock::now()) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 695 | break; |
| 696 | } |
| 697 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 698 | EventLoopEvent *event = PopEvent(); |
| 699 | event->HandleEvent(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 703 | // RAII class to mask signals. |
| 704 | class ScopedSignalMask { |
| 705 | public: |
| 706 | ScopedSignalMask(std::initializer_list<int> signals) { |
| 707 | sigset_t sigset; |
| 708 | PCHECK(sigemptyset(&sigset) == 0); |
| 709 | for (int signal : signals) { |
| 710 | PCHECK(sigaddset(&sigset, signal) == 0); |
| 711 | } |
| 712 | |
| 713 | PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0); |
| 714 | } |
| 715 | |
| 716 | ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); } |
| 717 | |
| 718 | private: |
| 719 | sigset_t old_; |
| 720 | }; |
| 721 | |
| 722 | // Class to manage the static state associated with killing multiple event |
| 723 | // loops. |
| 724 | class SignalHandler { |
| 725 | public: |
| 726 | // Gets the singleton. |
| 727 | static SignalHandler *global() { |
| 728 | static SignalHandler loop; |
| 729 | return &loop; |
| 730 | } |
| 731 | |
| 732 | // Handles the signal with the singleton. |
| 733 | static void HandleSignal(int) { global()->DoHandleSignal(); } |
| 734 | |
| 735 | // Registers an event loop to receive Exit() calls. |
| 736 | void Register(ShmEventLoop *event_loop) { |
| 737 | // Block signals while we have the mutex so we never race with the signal |
| 738 | // handler. |
| 739 | ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM}); |
| 740 | std::unique_lock<stl_mutex> locker(mutex_); |
| 741 | if (event_loops_.size() == 0) { |
| 742 | // The first caller registers the signal handler. |
| 743 | struct sigaction new_action; |
| 744 | sigemptyset(&new_action.sa_mask); |
| 745 | // This makes it so that 2 control c's to a stuck process will kill it by |
| 746 | // restoring the original signal handler. |
| 747 | new_action.sa_flags = SA_RESETHAND; |
| 748 | new_action.sa_handler = &HandleSignal; |
| 749 | |
| 750 | PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0); |
| 751 | PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0); |
| 752 | PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0); |
| 753 | } |
| 754 | |
| 755 | event_loops_.push_back(event_loop); |
| 756 | } |
| 757 | |
| 758 | // Unregisters an event loop to receive Exit() calls. |
| 759 | void Unregister(ShmEventLoop *event_loop) { |
| 760 | // Block signals while we have the mutex so we never race with the signal |
| 761 | // handler. |
| 762 | ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM}); |
| 763 | std::unique_lock<stl_mutex> locker(mutex_); |
| 764 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 765 | event_loops_.erase( |
| 766 | std::find(event_loops_.begin(), event_loops_.end(), event_loop)); |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 767 | |
| 768 | if (event_loops_.size() == 0u) { |
| 769 | // The last caller restores the original signal handlers. |
| 770 | PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0); |
| 771 | PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0); |
| 772 | PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | private: |
| 777 | void DoHandleSignal() { |
| 778 | // We block signals while grabbing the lock, so there should never be a |
| 779 | // race. Confirm that this is true using trylock. |
| 780 | CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while " |
| 781 | "modifing the event loop list."; |
| 782 | for (ShmEventLoop *event_loop : event_loops_) { |
| 783 | event_loop->Exit(); |
| 784 | } |
| 785 | mutex_.unlock(); |
| 786 | } |
| 787 | |
| 788 | // Mutex to protect all state. |
| 789 | stl_mutex mutex_; |
| 790 | std::vector<ShmEventLoop *> event_loops_; |
| 791 | struct sigaction old_action_int_; |
| 792 | struct sigaction old_action_hup_; |
| 793 | struct sigaction old_action_term_; |
| 794 | }; |
| 795 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 796 | void ShmEventLoop::Run() { |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 797 | SignalHandler::global()->Register(this); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 798 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 799 | std::unique_ptr<ipc_lib::SignalFd> signalfd; |
| 800 | |
| 801 | if (watchers_.size() > 0) { |
| 802 | signalfd.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal})); |
| 803 | |
| 804 | epoll_.OnReadable(signalfd->fd(), [signalfd_ptr = signalfd.get(), this]() { |
| 805 | signalfd_siginfo result = signalfd_ptr->Read(); |
| 806 | CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal); |
| 807 | |
| 808 | // TODO(austin): We should really be checking *everything*, not just |
| 809 | // watchers, and calling the oldest thing first. That will improve |
| 810 | // determinism a lot. |
| 811 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 812 | HandleEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 813 | }); |
| 814 | } |
| 815 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 816 | MaybeScheduleTimingReports(); |
| 817 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 818 | ReserveEvents(); |
| 819 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 820 | { |
| 821 | AosLogToFbs aos_logger; |
| 822 | if (!skip_logger_) { |
| 823 | aos_logger.Initialize(MakeSender<logging::LogMessageFbs>("/aos")); |
| 824 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 825 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 826 | aos::SetCurrentThreadName(name_.substr(0, 16)); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 827 | const cpu_set_t default_affinity = DefaultAffinity(); |
| 828 | if (!CPU_EQUAL(&affinity_, &default_affinity)) { |
| 829 | ::aos::SetCurrentThreadAffinity(affinity_); |
| 830 | } |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 831 | // Now, all the callbacks are setup. Lock everything into memory and go RT. |
| 832 | if (priority_ != 0) { |
| 833 | ::aos::InitRT(); |
| 834 | |
| 835 | LOG(INFO) << "Setting priority to " << priority_; |
| 836 | ::aos::SetCurrentThreadRealtimePriority(priority_); |
| 837 | } |
| 838 | |
| 839 | set_is_running(true); |
| 840 | |
| 841 | // Now that we are realtime (but before the OnRun handlers run), snap the |
| 842 | // queue index. |
| 843 | for (::std::unique_ptr<WatcherState> &watcher : watchers_) { |
| 844 | watcher->Startup(this); |
| 845 | } |
| 846 | |
| 847 | // Now that we are RT, run all the OnRun handlers. |
| 848 | for (const auto &run : on_run_) { |
| 849 | run(); |
| 850 | } |
| 851 | |
| 852 | // And start our main event loop which runs all the timers and handles Quit. |
| 853 | epoll_.Run(); |
| 854 | |
| 855 | // Once epoll exits, there is no useful nonrt work left to do. |
| 856 | set_is_running(false); |
| 857 | |
| 858 | // Nothing time or synchronization critical needs to happen after this |
| 859 | // point. Drop RT priority. |
| 860 | ::aos::UnsetCurrentThreadRealtimePriority(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 863 | for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) { |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 864 | ShmWatcherState *watcher = |
| 865 | reinterpret_cast<ShmWatcherState *>(base_watcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 866 | watcher->UnregisterWakeup(); |
| 867 | } |
| 868 | |
| 869 | if (watchers_.size() > 0) { |
| 870 | epoll_.DeleteFd(signalfd->fd()); |
| 871 | signalfd.reset(); |
| 872 | } |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 873 | |
| 874 | SignalHandler::global()->Unregister(this); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 875 | |
| 876 | // Trigger any remaining senders or fetchers to be cleared before destroying |
| 877 | // the event loop so the book keeping matches. Do this in the thread that |
| 878 | // created the timing reporter. |
| 879 | timing_report_sender_.reset(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | void ShmEventLoop::Exit() { epoll_.Quit(); } |
| 883 | |
| 884 | ShmEventLoop::~ShmEventLoop() { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 885 | // Force everything with a registered fd with epoll to be destroyed now. |
| 886 | timers_.clear(); |
| 887 | phased_loops_.clear(); |
| 888 | watchers_.clear(); |
| 889 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 890 | CHECK(!is_running()) << ": ShmEventLoop destroyed while running"; |
| 891 | } |
| 892 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 893 | void ShmEventLoop::SetRuntimeRealtimePriority(int priority) { |
| 894 | if (is_running()) { |
| 895 | LOG(FATAL) << "Cannot set realtime priority while running."; |
| 896 | } |
| 897 | priority_ = priority; |
| 898 | } |
| 899 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 900 | void ShmEventLoop::SetRuntimeAffinity(const cpu_set_t &cpuset) { |
| 901 | if (is_running()) { |
| 902 | LOG(FATAL) << "Cannot set affinity while running."; |
| 903 | } |
| 904 | affinity_ = cpuset; |
| 905 | } |
| 906 | |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 907 | void ShmEventLoop::set_name(const std::string_view name) { |
| 908 | name_ = std::string(name); |
| 909 | UpdateTimingReport(); |
| 910 | } |
| 911 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 912 | absl::Span<char> ShmEventLoop::GetWatcherSharedMemory(const Channel *channel) { |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 913 | ShmWatcherState *const watcher_state = |
| 914 | static_cast<ShmWatcherState *>(GetWatcherState(channel)); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 915 | return watcher_state->GetSharedMemory(); |
| 916 | } |
| 917 | |
| 918 | absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory( |
| 919 | const aos::RawSender *sender) const { |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 920 | return static_cast<const ShmSender *>(sender)->GetSharedMemory(); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 921 | } |
| 922 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 923 | pid_t ShmEventLoop::GetTid() { return syscall(SYS_gettid); } |
| 924 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 925 | } // namespace aos |