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