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