Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1 | #ifndef AOS_IPC_LIB_LOCKLESS_QUEUE_H_ |
| 2 | #define AOS_IPC_LIB_LOCKLESS_QUEUE_H_ |
| 3 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 4 | #include <sys/signalfd.h> |
| 5 | #include <sys/types.h> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 6 | |
| 7 | #include <csignal> |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 8 | #include <optional> |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 9 | #include <vector> |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 10 | |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 11 | #include "absl/types/span.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 12 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 13 | #include "aos/ipc_lib/aos_sync.h" |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 14 | #include "aos/ipc_lib/data_alignment.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 15 | #include "aos/ipc_lib/index.h" |
| 16 | #include "aos/time/time.h" |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 17 | #include "aos/uuid.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 18 | |
| 19 | namespace aos { |
| 20 | namespace ipc_lib { |
| 21 | |
| 22 | // Structure to hold the state required to wake a watcher. |
| 23 | struct Watcher { |
| 24 | // Mutex that the watcher locks. If the futex is 0 (or FUTEX_OWNER_DIED), |
| 25 | // then this watcher is invalid. The futex variable will then hold the tid of |
| 26 | // the watcher, or FUTEX_OWNER_DIED if the task died. |
| 27 | // |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 28 | // Note: this is only modified with the queue_setup_lock lock held, but may |
| 29 | // always be read. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 30 | // Any state modification should happen before the lock is acquired. |
| 31 | aos_mutex tid; |
| 32 | |
| 33 | // PID of the watcher. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 34 | std::atomic<pid_t> pid; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 35 | |
| 36 | // RT priority of the watcher. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 37 | std::atomic<int> priority; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | // Structure to hold the state required to send messages. |
| 41 | struct Sender { |
| 42 | // Mutex that the sender locks. If the futex is 0 (or FUTEX_OWNER_DIED), then |
| 43 | // this sender is invalid. The futex variable will then hold the tid of the |
| 44 | // sender, or FUTEX_OWNER_DIED if the task died. |
| 45 | // |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 46 | // Note: this is only modified with the queue_setup_lock lock held, but may |
| 47 | // always be read. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 48 | aos_mutex tid; |
| 49 | |
| 50 | // Index of the message we will be filling out. |
| 51 | AtomicIndex scratch_index; |
| 52 | |
| 53 | // Index of the element being swapped with scratch_index, or Invalid if there |
| 54 | // is nothing to do. |
| 55 | AtomicIndex to_replace; |
| 56 | }; |
| 57 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 58 | // Structure to hold the state required to pin messages. |
| 59 | struct Pinner { |
| 60 | // The same as Sender::tid. See there for docs. |
| 61 | aos_mutex tid; |
| 62 | |
| 63 | // Queue index of the message we have pinned, or Invalid if there isn't one. |
| 64 | AtomicQueueIndex pinned; |
| 65 | |
| 66 | // This should always be valid. |
| 67 | // |
| 68 | // Note that this is fully independent from pinned. It's just a place to stash |
| 69 | // a message, to ensure there's always an unpinned one for a writer to grab. |
| 70 | AtomicIndex scratch_index; |
| 71 | }; |
| 72 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 73 | // Structure representing a message. |
| 74 | struct Message { |
| 75 | struct Header { |
| 76 | // Index of this message in the queue. Needs to match the index this |
| 77 | // message is written into the queue at. The data in this message is only |
| 78 | // valid if it matches the index in the queue both before and after all the |
| 79 | // data is read. |
| 80 | // |
| 81 | // Note: a value of 0xffffffff always means that the contents aren't valid. |
| 82 | AtomicQueueIndex queue_index; |
| 83 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 84 | // Timestamp of the message. Needs to be monotonically incrementing in the |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 85 | // queue, which means that time needs to be re-sampled every time a write |
| 86 | // fails. |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 87 | monotonic_clock::time_point monotonic_sent_time; |
| 88 | realtime_clock::time_point realtime_sent_time; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 89 | // Timestamps of the message from the remote node. These are transparently |
| 90 | // passed through. |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 91 | monotonic_clock::time_point monotonic_remote_time; |
| 92 | realtime_clock::time_point realtime_remote_time; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 93 | |
| 94 | // Queue index from the remote node. |
| 95 | uint32_t remote_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 96 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 97 | // Remote boot UUID for this message. |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 98 | UUID source_boot_uuid; |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 99 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 100 | size_t length; |
| 101 | } header; |
| 102 | |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 103 | // Returns the start of the data buffer, given that message_data_size is |
| 104 | // the same one used to allocate this message's memory. |
| 105 | char *data(size_t message_data_size) { |
| 106 | return RoundedData(message_data_size); |
| 107 | } |
| 108 | const char *data(size_t message_data_size) const { |
| 109 | return RoundedData(message_data_size); |
| 110 | } |
| 111 | |
| 112 | // Returns the pre-buffer redzone, given that message_data_size is the same |
| 113 | // one used to allocate this message's memory. |
| 114 | absl::Span<char> PreRedzone(size_t message_data_size) { |
| 115 | char *const end = data(message_data_size); |
| 116 | const auto result = |
| 117 | absl::Span<char>(&data_pointer[0], end - &data_pointer[0]); |
| 118 | DCHECK_LT(result.size(), kChannelDataRedzone + kChannelDataAlignment); |
| 119 | return result; |
| 120 | } |
| 121 | absl::Span<const char> PreRedzone(size_t message_data_size) const { |
| 122 | const char *const end = data(message_data_size); |
| 123 | const auto result = |
| 124 | absl::Span<const char>(&data_pointer[0], end - &data_pointer[0]); |
| 125 | DCHECK_LT(result.size(), kChannelDataRedzone + kChannelDataAlignment); |
| 126 | return result; |
| 127 | } |
| 128 | |
| 129 | // Returns the post-buffer redzone, given that message_data_size is the same |
| 130 | // one used to allocate this message's memory. |
| 131 | absl::Span<char> PostRedzone(size_t message_data_size, size_t message_size) { |
| 132 | DCHECK_LT(message_data_size, message_size); |
| 133 | char *const redzone_end = reinterpret_cast<char *>(this) + message_size; |
| 134 | char *const data_end = data(message_data_size) + message_data_size; |
| 135 | DCHECK_GT(static_cast<void *>(redzone_end), static_cast<void *>(data_end)); |
| 136 | const auto result = absl::Span<char>(data_end, redzone_end - data_end); |
| 137 | DCHECK_LT(result.size(), kChannelDataRedzone + kChannelDataAlignment * 2); |
| 138 | return result; |
| 139 | } |
| 140 | absl::Span<const char> PostRedzone(size_t message_data_size, |
| 141 | size_t message_size) const { |
| 142 | DCHECK_LT(message_data_size, message_size); |
| 143 | const char *const redzone_end = |
| 144 | reinterpret_cast<const char *>(this) + message_size; |
| 145 | const char *const data_end = data(message_data_size) + message_data_size; |
| 146 | DCHECK_GT(static_cast<const void *>(redzone_end), |
| 147 | static_cast<const void *>(data_end)); |
| 148 | const auto result = |
| 149 | absl::Span<const char>(data_end, redzone_end - data_end); |
| 150 | DCHECK_LT(result.size(), kChannelDataRedzone + kChannelDataAlignment * 2); |
| 151 | return result; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | private: |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 155 | // This returns a non-const pointer into a const object. Be very careful |
| 156 | // about const correctness in publicly accessible APIs using it. |
| 157 | char *RoundedData(size_t message_data_size) const { |
| 158 | return RoundChannelData( |
| 159 | const_cast<char *>(&data_pointer[0] + kChannelDataRedzone), |
| 160 | message_data_size); |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | char data_pointer[]; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | struct LocklessQueueConfiguration { |
| 167 | // Size of the watchers list. |
| 168 | size_t num_watchers; |
| 169 | // Size of the sender list. |
| 170 | size_t num_senders; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 171 | // Size of the pinner list. |
| 172 | size_t num_pinners; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 173 | |
| 174 | // Size of the list of pointers into the messages list. |
| 175 | size_t queue_size; |
| 176 | // Size in bytes of the data stored in each Message. |
| 177 | size_t message_data_size; |
| 178 | |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 179 | size_t message_size() const; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 180 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 181 | size_t num_messages() const { return num_senders + num_pinners + queue_size; } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | // Structure to hold the state of the queue. |
| 185 | // |
| 186 | // Reads and writes are lockless and constant time. |
| 187 | // |
| 188 | // Adding a new watcher doesn't need to be constant time for the watcher (this |
| 189 | // is done before the watcher goes RT), but needs to be RT for the sender. |
| 190 | struct LocklessQueueMemory; |
| 191 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 192 | // Returns the size of the LocklessQueueMemory. |
| 193 | size_t LocklessQueueMemorySize(LocklessQueueConfiguration config); |
| 194 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 195 | // Initializes the queue memory. memory must be either a valid pointer to the |
| 196 | // queue datastructure, or must be zero initialized. |
| 197 | LocklessQueueMemory *InitializeLocklessQueueMemory( |
| 198 | LocklessQueueMemory *memory, LocklessQueueConfiguration config); |
| 199 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 200 | const static unsigned int kWakeupSignal = SIGRTMIN + 2; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 201 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 202 | // A convenient wrapper for accessing a lockless queue. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 203 | class LocklessQueue { |
| 204 | public: |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 205 | LocklessQueue(const LocklessQueueMemory *const_memory, |
| 206 | LocklessQueueMemory *memory, LocklessQueueConfiguration config) |
| 207 | : const_memory_(const_memory), memory_(memory), config_(config) {} |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 208 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 209 | void Initialize(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 210 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 211 | LocklessQueueConfiguration config() const { return config_; } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 212 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 213 | const LocklessQueueMemory *const_memory() { return const_memory_; } |
| 214 | LocklessQueueMemory *memory() { return memory_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 215 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 216 | private: |
| 217 | const LocklessQueueMemory *const_memory_; |
| 218 | LocklessQueueMemory *memory_; |
| 219 | LocklessQueueConfiguration config_; |
| 220 | }; |
| 221 | |
| 222 | class LocklessQueueWatcher { |
| 223 | public: |
| 224 | LocklessQueueWatcher(const LocklessQueueWatcher &) = delete; |
| 225 | LocklessQueueWatcher &operator=(const LocklessQueueWatcher &) = delete; |
| 226 | LocklessQueueWatcher(LocklessQueueWatcher &&other) |
| 227 | : memory_(other.memory_), watcher_index_(other.watcher_index_) { |
| 228 | other.watcher_index_ = -1; |
| 229 | } |
| 230 | LocklessQueueWatcher &operator=(LocklessQueueWatcher &&other) { |
| 231 | std::swap(memory_, other.memory_); |
| 232 | std::swap(watcher_index_, other.watcher_index_); |
| 233 | return *this; |
| 234 | } |
| 235 | |
| 236 | ~LocklessQueueWatcher(); |
| 237 | |
| 238 | // Registers this thread to receive the kWakeupSignal signal when |
| 239 | // LocklessQueueWakeUpper::Wakeup is called. Returns nullopt if there was an |
| 240 | // error in registration. |
| 241 | // TODO(austin): Change the API if we find ourselves with more errors. |
| 242 | static std::optional<LocklessQueueWatcher> Make(LocklessQueue queue, |
| 243 | int priority); |
| 244 | |
| 245 | private: |
| 246 | LocklessQueueWatcher(LocklessQueueMemory *memory, int priority); |
| 247 | |
| 248 | LocklessQueueMemory *memory_ = nullptr; |
| 249 | |
| 250 | // Index in the watcher list that our entry is, or -1 if no watcher is |
| 251 | // registered. |
| 252 | int watcher_index_ = -1; |
| 253 | }; |
| 254 | |
| 255 | class LocklessQueueWakeUpper { |
| 256 | public: |
| 257 | LocklessQueueWakeUpper(LocklessQueue queue); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 258 | |
| 259 | // Sends the kWakeupSignal to all threads which have called RegisterWakeup. |
| 260 | // |
| 261 | // priority of 0 means nonrt. nonrt could have issues, so we don't PI boost |
| 262 | // if nonrt. |
| 263 | int Wakeup(int current_priority); |
| 264 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 265 | private: |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 266 | // Memory and datastructure used to sort a list of watchers to wake |
| 267 | // up. This isn't a copy of Watcher since tid is simpler to work with here |
| 268 | // than the futex above. |
| 269 | struct WatcherCopy { |
| 270 | pid_t tid; |
| 271 | pid_t pid; |
| 272 | int priority; |
| 273 | }; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 274 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 275 | const LocklessQueueMemory *const memory_; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 276 | const int pid_; |
| 277 | const uid_t uid_; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 278 | |
| 279 | ::std::vector<WatcherCopy> watcher_copy_; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 280 | }; |
| 281 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 282 | // Sender for blocks of data. The resources associated with a sender are |
| 283 | // scoped to this object's lifetime. |
| 284 | class LocklessQueueSender { |
| 285 | public: |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 286 | // Enum of possible sending errors |
| 287 | // Send returns GOOD if the messages was sent successfully, INVALID_REDZONE if |
| 288 | // one of a message's redzones has invalid data, or MESSAGES_SENT_TOO_FAST if |
| 289 | // more than queue_size messages were going to be sent in a |
| 290 | // channel_storage_duration_. |
| 291 | enum class Result { GOOD, INVALID_REDZONE, MESSAGES_SENT_TOO_FAST }; |
| 292 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 293 | LocklessQueueSender(const LocklessQueueSender &) = delete; |
| 294 | LocklessQueueSender &operator=(const LocklessQueueSender &) = delete; |
| 295 | LocklessQueueSender(LocklessQueueSender &&other) |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 296 | : memory_(other.memory_), |
| 297 | sender_index_(other.sender_index_), |
| 298 | channel_storage_duration_(other.channel_storage_duration_) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 299 | other.memory_ = nullptr; |
| 300 | other.sender_index_ = -1; |
| 301 | } |
| 302 | LocklessQueueSender &operator=(LocklessQueueSender &&other) { |
| 303 | std::swap(memory_, other.memory_); |
| 304 | std::swap(sender_index_, other.sender_index_); |
| 305 | return *this; |
| 306 | } |
| 307 | |
| 308 | ~LocklessQueueSender(); |
| 309 | |
| 310 | // Creates a sender. If we couldn't allocate a sender, returns nullopt. |
| 311 | // TODO(austin): Change the API if we find ourselves with more errors. |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 312 | static std::optional<LocklessQueueSender> Make( |
| 313 | LocklessQueue queue, monotonic_clock::duration channel_storage_duration); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 314 | |
| 315 | // Sends a message without copying the data. |
| 316 | // Copy at most size() bytes of data into the memory pointed to by Data(), |
| 317 | // and then call Send(). |
| 318 | // Note: calls to Data() are expensive enough that you should cache it. |
| 319 | size_t size() const; |
| 320 | void *Data(); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 321 | LocklessQueueSender::Result Send( |
| 322 | size_t length, monotonic_clock::time_point monotonic_remote_time, |
| 323 | realtime_clock::time_point realtime_remote_time, |
| 324 | uint32_t remote_queue_index, const UUID &source_boot_uuid, |
| 325 | monotonic_clock::time_point *monotonic_sent_time = nullptr, |
| 326 | realtime_clock::time_point *realtime_sent_time = nullptr, |
| 327 | uint32_t *queue_index = nullptr); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 328 | |
| 329 | // Sends up to length data. Does not wakeup the target. |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 330 | LocklessQueueSender::Result Send( |
| 331 | const char *data, size_t length, |
| 332 | monotonic_clock::time_point monotonic_remote_time, |
| 333 | realtime_clock::time_point realtime_remote_time, |
| 334 | uint32_t remote_queue_index, const UUID &source_boot_uuid, |
| 335 | monotonic_clock::time_point *monotonic_sent_time = nullptr, |
| 336 | realtime_clock::time_point *realtime_sent_time = nullptr, |
| 337 | uint32_t *queue_index = nullptr); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 338 | |
| 339 | int buffer_index() const; |
| 340 | |
| 341 | private: |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 342 | LocklessQueueSender(LocklessQueueMemory *memory, |
| 343 | monotonic_clock::duration channel_storage_duration); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 344 | |
| 345 | // Pointer to the backing memory. |
| 346 | LocklessQueueMemory *memory_ = nullptr; |
| 347 | |
| 348 | // Index into the sender list. |
| 349 | int sender_index_ = -1; |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 350 | |
| 351 | // Storage duration of the channel used to check if messages were sent too |
| 352 | // fast |
| 353 | const monotonic_clock::duration channel_storage_duration_; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 354 | }; |
| 355 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 356 | std::ostream &operator<<(std::ostream &os, const LocklessQueueSender::Result r); |
| 357 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 358 | // Pinner for blocks of data. The resources associated with a pinner are |
| 359 | // scoped to this object's lifetime. |
| 360 | class LocklessQueuePinner { |
| 361 | public: |
| 362 | LocklessQueuePinner(const LocklessQueuePinner &) = delete; |
| 363 | LocklessQueuePinner &operator=(const LocklessQueuePinner &) = delete; |
| 364 | LocklessQueuePinner(LocklessQueuePinner &&other) |
| 365 | : memory_(other.memory_), |
| 366 | const_memory_(other.const_memory_), |
| 367 | pinner_index_(other.pinner_index_) { |
| 368 | other.pinner_index_ = -1; |
| 369 | } |
| 370 | LocklessQueuePinner &operator=(LocklessQueuePinner &&other) { |
| 371 | std::swap(memory_, other.memory_); |
| 372 | std::swap(const_memory_, other.const_memory_); |
| 373 | std::swap(pinner_index_, other.pinner_index_); |
| 374 | return *this; |
| 375 | } |
| 376 | |
| 377 | ~LocklessQueuePinner(); |
| 378 | |
| 379 | // Creates a pinner. If we couldn't allocate a pinner, returns nullopt. |
| 380 | // TODO(austin): Change the API if we find ourselves with more errors. |
| 381 | static std::optional<LocklessQueuePinner> Make(LocklessQueue queue); |
| 382 | |
| 383 | // Attempts to pin the message at queue_index. |
| 384 | // Un-pins the previous message. |
| 385 | // Returns the buffer index (non-negative) if it succeeds. |
| 386 | // Returns -1 if that message is no longer in the queue. |
| 387 | int PinIndex(uint32_t queue_index); |
| 388 | |
| 389 | // Read at most size() bytes of data into the memory pointed to by Data(). |
| 390 | // Note: calls to Data() are expensive enough that you should cache it. |
| 391 | // Don't call Data() before a successful PinIndex call. |
| 392 | size_t size() const; |
| 393 | const void *Data() const; |
| 394 | |
| 395 | private: |
| 396 | LocklessQueuePinner(LocklessQueueMemory *memory, |
| 397 | const LocklessQueueMemory *const_memory); |
| 398 | |
| 399 | // Pointer to the backing memory. |
| 400 | LocklessQueueMemory *memory_ = nullptr; |
| 401 | const LocklessQueueMemory *const_memory_ = nullptr; |
| 402 | |
| 403 | // Index into the pinner list. |
| 404 | int pinner_index_ = -1; |
| 405 | }; |
| 406 | |
| 407 | class LocklessQueueReader { |
| 408 | public: |
| 409 | enum class Result { TOO_OLD, GOOD, NOTHING_NEW, OVERWROTE }; |
| 410 | |
| 411 | LocklessQueueReader(LocklessQueue queue) : memory_(queue.const_memory()) { |
| 412 | queue.Initialize(); |
| 413 | } |
| 414 | |
| 415 | // If you ask for a queue index 2 past the newest, you will still get |
| 416 | // NOTHING_NEW until that gets overwritten with new data. If you ask for an |
| 417 | // element newer than QueueSize() from the current message, we consider it |
| 418 | // behind by a large amount and return TOO_OLD. If the message is modified |
| 419 | // out from underneath us as we read it, return OVERWROTE. |
| 420 | // |
| 421 | // data may be nullptr to indicate the data should not be copied. |
| 422 | Result Read(uint32_t queue_index, |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 423 | monotonic_clock::time_point *monotonic_sent_time, |
| 424 | realtime_clock::time_point *realtime_sent_time, |
| 425 | monotonic_clock::time_point *monotonic_remote_time, |
| 426 | realtime_clock::time_point *realtime_remote_time, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 427 | uint32_t *remote_queue_index, UUID *source_boot_uuid, |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 428 | size_t *length, char *data) const; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 429 | |
| 430 | // Returns the index to the latest queue message. Returns empty_queue_index() |
| 431 | // if there are no messages in the queue. Do note that this index wraps if |
| 432 | // more than 2^32 messages are sent. |
| 433 | QueueIndex LatestIndex() const; |
| 434 | |
| 435 | private: |
| 436 | const LocklessQueueMemory *const memory_; |
| 437 | }; |
| 438 | |
| 439 | // Returns the number of messages which are logically in the queue at a time. |
| 440 | size_t LocklessQueueSize(const LocklessQueueMemory *memory); |
| 441 | |
| 442 | // Returns the number of bytes queue users are allowed to read/write within each |
| 443 | // message. |
| 444 | size_t LocklessQueueMessageDataSize(const LocklessQueueMemory *memory); |
| 445 | |
| 446 | // TODO(austin): Return the oldest queue index. This lets us catch up nicely |
| 447 | // if we got behind. |
| 448 | // The easiest way to implement this is likely going to be to reserve the |
| 449 | // first modulo of values for the initial time around, and never reuse them. |
| 450 | // That lets us do a simple atomic read of the next index and deduce what has |
| 451 | // happened. It will involve the simplest atomic operations. |
| 452 | |
| 453 | // TODO(austin): Make it so we can find the indices which were sent just |
| 454 | // before and after a time with a binary search. |
| 455 | |
| 456 | // Prints to stdout the data inside the queue for debugging. |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame^] | 457 | void PrintLocklessQueueMemory(const LocklessQueueMemory *memory); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 458 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 459 | } // namespace ipc_lib |
| 460 | } // namespace aos |
| 461 | |
| 462 | #endif // AOS_IPC_LIB_LOCKLESS_QUEUE_H_ |