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 | |
| 4 | #include <signal.h> |
| 5 | #include <sys/signalfd.h> |
| 6 | #include <sys/types.h> |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 7 | #include <optional> |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 8 | #include <vector> |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 9 | |
| 10 | #include "aos/ipc_lib/aos_sync.h" |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 11 | #include "aos/ipc_lib/data_alignment.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 12 | #include "aos/ipc_lib/index.h" |
| 13 | #include "aos/time/time.h" |
| 14 | |
| 15 | namespace aos { |
| 16 | namespace ipc_lib { |
| 17 | |
| 18 | // Structure to hold the state required to wake a watcher. |
| 19 | struct Watcher { |
| 20 | // Mutex that the watcher locks. If the futex is 0 (or FUTEX_OWNER_DIED), |
| 21 | // then this watcher is invalid. The futex variable will then hold the tid of |
| 22 | // the watcher, or FUTEX_OWNER_DIED if the task died. |
| 23 | // |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 24 | // Note: this is only modified with the queue_setup_lock lock held, but may |
| 25 | // always be read. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 26 | // Any state modification should happen before the lock is acquired. |
| 27 | aos_mutex tid; |
| 28 | |
| 29 | // PID of the watcher. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 30 | std::atomic<pid_t> pid; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 31 | |
| 32 | // RT priority of the watcher. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 33 | std::atomic<int> priority; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | // Structure to hold the state required to send messages. |
| 37 | struct Sender { |
| 38 | // Mutex that the sender locks. If the futex is 0 (or FUTEX_OWNER_DIED), then |
| 39 | // this sender is invalid. The futex variable will then hold the tid of the |
| 40 | // sender, or FUTEX_OWNER_DIED if the task died. |
| 41 | // |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 42 | // Note: this is only modified with the queue_setup_lock lock held, but may |
| 43 | // always be read. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 44 | aos_mutex tid; |
| 45 | |
| 46 | // Index of the message we will be filling out. |
| 47 | AtomicIndex scratch_index; |
| 48 | |
| 49 | // Index of the element being swapped with scratch_index, or Invalid if there |
| 50 | // is nothing to do. |
| 51 | AtomicIndex to_replace; |
| 52 | }; |
| 53 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 54 | // Structure to hold the state required to pin messages. |
| 55 | struct Pinner { |
| 56 | // The same as Sender::tid. See there for docs. |
| 57 | aos_mutex tid; |
| 58 | |
| 59 | // Queue index of the message we have pinned, or Invalid if there isn't one. |
| 60 | AtomicQueueIndex pinned; |
| 61 | |
| 62 | // This should always be valid. |
| 63 | // |
| 64 | // Note that this is fully independent from pinned. It's just a place to stash |
| 65 | // a message, to ensure there's always an unpinned one for a writer to grab. |
| 66 | AtomicIndex scratch_index; |
| 67 | }; |
| 68 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 69 | // Structure representing a message. |
| 70 | struct Message { |
| 71 | struct Header { |
| 72 | // Index of this message in the queue. Needs to match the index this |
| 73 | // message is written into the queue at. The data in this message is only |
| 74 | // valid if it matches the index in the queue both before and after all the |
| 75 | // data is read. |
| 76 | // |
| 77 | // Note: a value of 0xffffffff always means that the contents aren't valid. |
| 78 | AtomicQueueIndex queue_index; |
| 79 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 80 | // Timestamp of the message. Needs to be monotonically incrementing in the |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 81 | // queue, which means that time needs to be re-sampled every time a write |
| 82 | // fails. |
| 83 | ::aos::monotonic_clock::time_point monotonic_sent_time; |
| 84 | ::aos::realtime_clock::time_point realtime_sent_time; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 85 | // Timestamps of the message from the remote node. These are transparently |
| 86 | // passed through. |
| 87 | ::aos::monotonic_clock::time_point monotonic_remote_time; |
| 88 | ::aos::realtime_clock::time_point realtime_remote_time; |
| 89 | |
| 90 | // Queue index from the remote node. |
| 91 | uint32_t remote_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 92 | |
| 93 | size_t length; |
| 94 | } header; |
| 95 | |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 96 | char *data(size_t message_size) { return RoundedData(message_size); } |
| 97 | const char *data(size_t message_size) const { |
| 98 | return RoundedData(message_size); |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | // This returns a non-const pointer into a const object. Be very careful about |
| 103 | // const correctness in publicly accessible APIs using it. |
| 104 | char *RoundedData(size_t message_size) const { |
| 105 | return RoundChannelData(const_cast<char *>(&data_pointer[0]), message_size); |
| 106 | } |
| 107 | |
| 108 | char data_pointer[]; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | struct LocklessQueueConfiguration { |
| 112 | // Size of the watchers list. |
| 113 | size_t num_watchers; |
| 114 | // Size of the sender list. |
| 115 | size_t num_senders; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 116 | // Size of the pinner list. |
| 117 | size_t num_pinners; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 118 | |
| 119 | // Size of the list of pointers into the messages list. |
| 120 | size_t queue_size; |
| 121 | // Size in bytes of the data stored in each Message. |
| 122 | size_t message_data_size; |
| 123 | |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 124 | size_t message_size() const; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 125 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 126 | size_t num_messages() const { return num_senders + num_pinners + queue_size; } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | // Structure to hold the state of the queue. |
| 130 | // |
| 131 | // Reads and writes are lockless and constant time. |
| 132 | // |
| 133 | // Adding a new watcher doesn't need to be constant time for the watcher (this |
| 134 | // is done before the watcher goes RT), but needs to be RT for the sender. |
| 135 | struct LocklessQueueMemory; |
| 136 | |
| 137 | // Initializes the queue memory. memory must be either a valid pointer to the |
| 138 | // queue datastructure, or must be zero initialized. |
| 139 | LocklessQueueMemory *InitializeLocklessQueueMemory( |
| 140 | LocklessQueueMemory *memory, LocklessQueueConfiguration config); |
| 141 | |
| 142 | // Returns the size of the LocklessQueueMemory. |
| 143 | size_t LocklessQueueMemorySize(LocklessQueueConfiguration config); |
| 144 | |
| 145 | // Prints to stdout the data inside the queue for debugging. |
| 146 | void PrintLocklessQueueMemory(LocklessQueueMemory *memory); |
| 147 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 148 | const static unsigned int kWakeupSignal = SIGRTMIN + 2; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 149 | |
| 150 | // Class to manage sending and receiving data in the lockless queue. This is |
| 151 | // separate from the actual memory backing the queue so that memory can be |
| 152 | // managed with mmap to share across the process boundary. |
| 153 | class LocklessQueue { |
| 154 | public: |
| 155 | LocklessQueue(LocklessQueueMemory *memory, LocklessQueueConfiguration config); |
| 156 | LocklessQueue(const LocklessQueue &) = delete; |
| 157 | LocklessQueue &operator=(const LocklessQueue &) = delete; |
| 158 | |
| 159 | ~LocklessQueue(); |
| 160 | |
| 161 | // Returns the number of messages in the queue. |
| 162 | size_t QueueSize() const; |
| 163 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 164 | size_t message_data_size() const; |
| 165 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 166 | // Registers this thread to receive the kWakeupSignal signal when Wakeup is |
| 167 | // called. Returns false if there was an error in registration. |
| 168 | bool RegisterWakeup(int priority); |
| 169 | // Unregisters the wakeup. |
| 170 | void UnregisterWakeup(); |
| 171 | |
| 172 | // Sends the kWakeupSignal to all threads which have called RegisterWakeup. |
| 173 | // |
| 174 | // priority of 0 means nonrt. nonrt could have issues, so we don't PI boost |
| 175 | // if nonrt. |
| 176 | int Wakeup(int current_priority); |
| 177 | |
| 178 | // If you ask for a queue index 2 past the newest, you will still get |
| 179 | // NOTHING_NEW until that gets overwritten with new data. If you ask for an |
| 180 | // element newer than QueueSize() from the current message, we consider it |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 181 | // behind by a large amount and return TOO_OLD. If the message is modified |
| 182 | // out from underneath us as we read it, return OVERWROTE. |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 183 | // |
| 184 | // data may be nullptr to indicate the data should not be copied. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 185 | enum class ReadResult { TOO_OLD, GOOD, NOTHING_NEW, OVERWROTE }; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 186 | ReadResult Read(uint32_t queue_index, |
| 187 | ::aos::monotonic_clock::time_point *monotonic_sent_time, |
| 188 | ::aos::realtime_clock::time_point *realtime_sent_time, |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 189 | ::aos::monotonic_clock::time_point *monotonic_remote_time, |
| 190 | ::aos::realtime_clock::time_point *realtime_remote_time, |
| 191 | uint32_t *remote_queue_index, size_t *length, char *data); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 192 | |
| 193 | // Returns the index to the latest queue message. Returns empty_queue_index() |
| 194 | // if there are no messages in the queue. Do note that this index wraps if |
| 195 | // more than 2^32 messages are sent. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 196 | QueueIndex LatestQueueIndex(); |
| 197 | static QueueIndex empty_queue_index() { return QueueIndex::Invalid(); } |
| 198 | |
| 199 | // Returns the size of the queue. This is mostly useful for manipulating |
| 200 | // QueueIndex. |
| 201 | size_t queue_size() const; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 202 | |
| 203 | // TODO(austin): Return the oldest queue index. This lets us catch up nicely |
| 204 | // if we got behind. |
| 205 | // The easiest way to implement this is likely going to be to reserve the |
| 206 | // first modulo of values for the initial time around, and never reuse them. |
| 207 | // That lets us do a simple atomic read of the next index and deduce what has |
| 208 | // happened. It will involve the simplest atomic operations. |
| 209 | |
| 210 | // TODO(austin): Make it so we can find the indices which were sent just |
| 211 | // before and after a time with a binary search. |
| 212 | |
| 213 | // Sender for blocks of data. The resources associated with a sender are |
| 214 | // scoped to this object's lifetime. |
| 215 | class Sender { |
| 216 | public: |
| 217 | Sender(const Sender &) = delete; |
| 218 | Sender &operator=(const Sender &) = delete; |
| 219 | Sender(Sender &&other) |
| 220 | : memory_(other.memory_), sender_index_(other.sender_index_) { |
| 221 | other.memory_ = nullptr; |
| 222 | other.sender_index_ = -1; |
| 223 | } |
| 224 | Sender &operator=(Sender &&other) { |
| 225 | memory_ = other.memory_; |
| 226 | sender_index_ = other.sender_index_; |
| 227 | other.memory_ = nullptr; |
| 228 | other.sender_index_ = -1; |
| 229 | return *this; |
| 230 | } |
| 231 | |
| 232 | ~Sender(); |
| 233 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 234 | // Sends a message without copying the data. |
| 235 | // Copy at most size() bytes of data into the memory pointed to by Data(), |
| 236 | // and then call Send(). |
| 237 | // Note: calls to Data() are expensive enough that you should cache it. |
| 238 | size_t size(); |
| 239 | void *Data(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 240 | void Send(size_t length, |
| 241 | aos::monotonic_clock::time_point monotonic_remote_time = |
| 242 | aos::monotonic_clock::min_time, |
| 243 | aos::realtime_clock::time_point realtime_remote_time = |
| 244 | aos::realtime_clock::min_time, |
| 245 | uint32_t remote_queue_index = 0xffffffff, |
| 246 | aos::monotonic_clock::time_point *monotonic_sent_time = nullptr, |
| 247 | aos::realtime_clock::time_point *realtime_sent_time = nullptr, |
| 248 | uint32_t *queue_index = nullptr); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 249 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 250 | // Sends up to length data. Does not wakeup the target. |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 251 | void Send(const char *data, size_t length, |
| 252 | aos::monotonic_clock::time_point monotonic_remote_time = |
| 253 | aos::monotonic_clock::min_time, |
| 254 | aos::realtime_clock::time_point realtime_remote_time = |
| 255 | aos::realtime_clock::min_time, |
| 256 | uint32_t remote_queue_index = 0xffffffff, |
| 257 | aos::monotonic_clock::time_point *monotonic_sent_time = nullptr, |
| 258 | aos::realtime_clock::time_point *realtime_sent_time = nullptr, |
| 259 | uint32_t *queue_index = nullptr); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 260 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame^] | 261 | int buffer_index() const; |
| 262 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 263 | private: |
| 264 | friend class LocklessQueue; |
| 265 | |
| 266 | Sender(LocklessQueueMemory *memory); |
| 267 | |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 268 | // Returns true if this sender is valid. If it isn't valid, any of the |
| 269 | // other methods won't work. This is here to allow the lockless queue to |
| 270 | // only build a sender if there was one available. |
| 271 | bool valid() const { return sender_index_ != -1 && memory_ != nullptr; } |
| 272 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 273 | // Pointer to the backing memory. |
| 274 | LocklessQueueMemory *memory_ = nullptr; |
| 275 | |
| 276 | // Index into the sender list. |
| 277 | int sender_index_ = -1; |
| 278 | }; |
| 279 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 280 | // Pinner for blocks of data. The resources associated with a pinner are |
| 281 | // scoped to this object's lifetime. |
| 282 | class Pinner { |
| 283 | public: |
| 284 | Pinner(const Pinner &) = delete; |
| 285 | Pinner &operator=(const Pinner &) = delete; |
| 286 | Pinner(Pinner &&other) |
| 287 | : memory_(other.memory_), pinner_index_(other.pinner_index_) { |
| 288 | other.memory_ = nullptr; |
| 289 | other.pinner_index_ = -1; |
| 290 | } |
| 291 | Pinner &operator=(Pinner &&other) { |
| 292 | memory_ = other.memory_; |
| 293 | pinner_index_ = other.pinner_index_; |
| 294 | other.memory_ = nullptr; |
| 295 | other.pinner_index_ = -1; |
| 296 | return *this; |
| 297 | } |
| 298 | |
| 299 | ~Pinner(); |
| 300 | |
| 301 | // Attempts to pin the message at queue_index. |
| 302 | // Un-pins the previous message. |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame^] | 303 | // Returns the buffer index (non-negative) if it succeeds. |
| 304 | // Returns -1 if that message is no longer in the queue. |
| 305 | int PinIndex(uint32_t queue_index); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 306 | |
| 307 | // Read at most size() bytes of data into the memory pointed to by Data(). |
| 308 | // Note: calls to Data() are expensive enough that you should cache it. |
| 309 | // Don't call Data() before a successful PinIndex call. |
| 310 | size_t size() const; |
| 311 | const void *Data() const; |
| 312 | |
| 313 | private: |
| 314 | friend class LocklessQueue; |
| 315 | |
| 316 | Pinner(LocklessQueueMemory *memory); |
| 317 | |
| 318 | // Returns true if this pinner is valid. If it isn't valid, any of the |
| 319 | // other methods won't work. This is here to allow the lockless queue to |
| 320 | // only build a pinner if there was one available. |
| 321 | bool valid() const { return pinner_index_ != -1 && memory_ != nullptr; } |
| 322 | |
| 323 | // Pointer to the backing memory. |
| 324 | LocklessQueueMemory *memory_ = nullptr; |
| 325 | |
| 326 | // Index into the pinner list. |
| 327 | int pinner_index_ = -1; |
| 328 | }; |
| 329 | |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 330 | // Creates a sender. If we couldn't allocate a sender, returns nullopt. |
| 331 | // TODO(austin): Change the API if we find ourselves with more errors. |
| 332 | std::optional<Sender> MakeSender(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 333 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 334 | // Creates a pinner. If we couldn't allocate a pinner, returns nullopt. |
| 335 | // TODO(austin): Change the API if we find ourselves with more errors. |
| 336 | std::optional<Pinner> MakePinner(); |
| 337 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 338 | private: |
| 339 | LocklessQueueMemory *memory_ = nullptr; |
| 340 | |
| 341 | // Memory and datastructure used to sort a list of watchers to wake |
| 342 | // up. This isn't a copy of Watcher since tid is simpler to work with here |
| 343 | // than the futex above. |
| 344 | struct WatcherCopy { |
| 345 | pid_t tid; |
| 346 | pid_t pid; |
| 347 | int priority; |
| 348 | }; |
| 349 | // TODO(austin): Don't allocate this memory if we aren't going to send. |
| 350 | ::std::vector<WatcherCopy> watcher_copy_; |
| 351 | |
| 352 | // Index in the watcher list that our entry is, or -1 if no watcher is |
| 353 | // registered. |
| 354 | int watcher_index_ = -1; |
| 355 | |
| 356 | const int pid_; |
| 357 | const uid_t uid_; |
| 358 | }; |
| 359 | |
| 360 | } // namespace ipc_lib |
| 361 | } // namespace aos |
| 362 | |
| 363 | #endif // AOS_IPC_LIB_LOCKLESS_QUEUE_H_ |