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