blob: 5676c53a4050ad6206bd543f791ee389a37f3e53 [file] [log] [blame]
Austin Schuh20b2b082019-09-11 20:42:56 -07001#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 Schuhe516ab02020-05-06 21:37:04 -07007#include <optional>
Brian Silverman177567e2020-08-12 19:51:33 -07008#include <vector>
Austin Schuh20b2b082019-09-11 20:42:56 -07009
10#include "aos/ipc_lib/aos_sync.h"
Brian Silvermana1652f32020-01-29 20:41:44 -080011#include "aos/ipc_lib/data_alignment.h"
Austin Schuh20b2b082019-09-11 20:42:56 -070012#include "aos/ipc_lib/index.h"
13#include "aos/time/time.h"
14
15namespace aos {
16namespace ipc_lib {
17
18// Structure to hold the state required to wake a watcher.
19struct 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 Silvermanfafe1fa2019-12-18 21:42:18 -080024 // Note: this is only modified with the queue_setup_lock lock held, but may
25 // always be read.
Austin Schuh20b2b082019-09-11 20:42:56 -070026 // Any state modification should happen before the lock is acquired.
27 aos_mutex tid;
28
29 // PID of the watcher.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080030 std::atomic<pid_t> pid;
Austin Schuh20b2b082019-09-11 20:42:56 -070031
32 // RT priority of the watcher.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080033 std::atomic<int> priority;
Austin Schuh20b2b082019-09-11 20:42:56 -070034};
35
36// Structure to hold the state required to send messages.
37struct 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 Silvermanfafe1fa2019-12-18 21:42:18 -080042 // Note: this is only modified with the queue_setup_lock lock held, but may
43 // always be read.
Austin Schuh20b2b082019-09-11 20:42:56 -070044 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 Silverman177567e2020-08-12 19:51:33 -070054// Structure to hold the state required to pin messages.
55struct 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 Schuh20b2b082019-09-11 20:42:56 -070069// Structure representing a message.
70struct 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 Silvermanfafe1fa2019-12-18 21:42:18 -080080 // Timestamp of the message. Needs to be monotonically incrementing in the
Austin Schuh20b2b082019-09-11 20:42:56 -070081 // 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 Schuhad154822019-12-27 15:45:13 -080085 // 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 Schuh20b2b082019-09-11 20:42:56 -070092
93 size_t length;
94 } header;
95
Brian Silvermana1652f32020-01-29 20:41:44 -080096 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 Schuh20b2b082019-09-11 20:42:56 -0700109};
110
111struct LocklessQueueConfiguration {
112 // Size of the watchers list.
113 size_t num_watchers;
114 // Size of the sender list.
115 size_t num_senders;
Brian Silverman177567e2020-08-12 19:51:33 -0700116 // Size of the pinner list.
117 size_t num_pinners;
Austin Schuh20b2b082019-09-11 20:42:56 -0700118
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 Schuh4bc4f902019-12-23 18:04:51 -0800124 size_t message_size() const;
Austin Schuh20b2b082019-09-11 20:42:56 -0700125
Brian Silverman177567e2020-08-12 19:51:33 -0700126 size_t num_messages() const { return num_senders + num_pinners + queue_size; }
Austin Schuh20b2b082019-09-11 20:42:56 -0700127};
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.
135struct LocklessQueueMemory;
136
137// Initializes the queue memory. memory must be either a valid pointer to the
138// queue datastructure, or must be zero initialized.
139LocklessQueueMemory *InitializeLocklessQueueMemory(
140 LocklessQueueMemory *memory, LocklessQueueConfiguration config);
141
142// Returns the size of the LocklessQueueMemory.
143size_t LocklessQueueMemorySize(LocklessQueueConfiguration config);
144
145// Prints to stdout the data inside the queue for debugging.
146void PrintLocklessQueueMemory(LocklessQueueMemory *memory);
147
Alex Perrycb7da4b2019-08-28 19:35:56 -0700148const static unsigned int kWakeupSignal = SIGRTMIN + 2;
Austin Schuh20b2b082019-09-11 20:42:56 -0700149
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.
153class 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 Perrycb7da4b2019-08-28 19:35:56 -0700164 size_t message_data_size() const;
165
Austin Schuh20b2b082019-09-11 20:42:56 -0700166 // 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 Perrycb7da4b2019-08-28 19:35:56 -0700181 // 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 Silverman6b8a3c32020-03-06 11:26:14 -0800183 //
184 // data may be nullptr to indicate the data should not be copied.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700185 enum class ReadResult { TOO_OLD, GOOD, NOTHING_NEW, OVERWROTE };
Austin Schuh20b2b082019-09-11 20:42:56 -0700186 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 Schuhad154822019-12-27 15:45:13 -0800189 ::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 Schuh20b2b082019-09-11 20:42:56 -0700192
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 Perrycb7da4b2019-08-28 19:35:56 -0700196 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 Schuh20b2b082019-09-11 20:42:56 -0700202
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 Perrycb7da4b2019-08-28 19:35:56 -0700234 // 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 Schuhad154822019-12-27 15:45:13 -0800240 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 Perrycb7da4b2019-08-28 19:35:56 -0700249
Austin Schuh20b2b082019-09-11 20:42:56 -0700250 // Sends up to length data. Does not wakeup the target.
Austin Schuhad154822019-12-27 15:45:13 -0800251 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 Schuh20b2b082019-09-11 20:42:56 -0700260
261 private:
262 friend class LocklessQueue;
263
264 Sender(LocklessQueueMemory *memory);
265
Austin Schuhe516ab02020-05-06 21:37:04 -0700266 // Returns true if this sender is valid. If it isn't valid, any of the
267 // other methods won't work. This is here to allow the lockless queue to
268 // only build a sender if there was one available.
269 bool valid() const { return sender_index_ != -1 && memory_ != nullptr; }
270
Austin Schuh20b2b082019-09-11 20:42:56 -0700271 // Pointer to the backing memory.
272 LocklessQueueMemory *memory_ = nullptr;
273
274 // Index into the sender list.
275 int sender_index_ = -1;
276 };
277
Brian Silverman177567e2020-08-12 19:51:33 -0700278 // Pinner for blocks of data. The resources associated with a pinner are
279 // scoped to this object's lifetime.
280 class Pinner {
281 public:
282 Pinner(const Pinner &) = delete;
283 Pinner &operator=(const Pinner &) = delete;
284 Pinner(Pinner &&other)
285 : memory_(other.memory_), pinner_index_(other.pinner_index_) {
286 other.memory_ = nullptr;
287 other.pinner_index_ = -1;
288 }
289 Pinner &operator=(Pinner &&other) {
290 memory_ = other.memory_;
291 pinner_index_ = other.pinner_index_;
292 other.memory_ = nullptr;
293 other.pinner_index_ = -1;
294 return *this;
295 }
296
297 ~Pinner();
298
299 // Attempts to pin the message at queue_index.
300 // Un-pins the previous message.
301 // Returns true if it succeeds.
302 // Returns false if that message is no longer in the queue.
303 bool PinIndex(uint32_t queue_index);
304
305 // Read at most size() bytes of data into the memory pointed to by Data().
306 // Note: calls to Data() are expensive enough that you should cache it.
307 // Don't call Data() before a successful PinIndex call.
308 size_t size() const;
309 const void *Data() const;
310
311 private:
312 friend class LocklessQueue;
313
314 Pinner(LocklessQueueMemory *memory);
315
316 // Returns true if this pinner is valid. If it isn't valid, any of the
317 // other methods won't work. This is here to allow the lockless queue to
318 // only build a pinner if there was one available.
319 bool valid() const { return pinner_index_ != -1 && memory_ != nullptr; }
320
321 // Pointer to the backing memory.
322 LocklessQueueMemory *memory_ = nullptr;
323
324 // Index into the pinner list.
325 int pinner_index_ = -1;
326 };
327
Austin Schuhe516ab02020-05-06 21:37:04 -0700328 // Creates a sender. If we couldn't allocate a sender, returns nullopt.
329 // TODO(austin): Change the API if we find ourselves with more errors.
330 std::optional<Sender> MakeSender();
Austin Schuh20b2b082019-09-11 20:42:56 -0700331
Brian Silverman177567e2020-08-12 19:51:33 -0700332 // Creates a pinner. If we couldn't allocate a pinner, returns nullopt.
333 // TODO(austin): Change the API if we find ourselves with more errors.
334 std::optional<Pinner> MakePinner();
335
Austin Schuh20b2b082019-09-11 20:42:56 -0700336 private:
337 LocklessQueueMemory *memory_ = nullptr;
338
339 // Memory and datastructure used to sort a list of watchers to wake
340 // up. This isn't a copy of Watcher since tid is simpler to work with here
341 // than the futex above.
342 struct WatcherCopy {
343 pid_t tid;
344 pid_t pid;
345 int priority;
346 };
347 // TODO(austin): Don't allocate this memory if we aren't going to send.
348 ::std::vector<WatcherCopy> watcher_copy_;
349
350 // Index in the watcher list that our entry is, or -1 if no watcher is
351 // registered.
352 int watcher_index_ = -1;
353
354 const int pid_;
355 const uid_t uid_;
356};
357
358} // namespace ipc_lib
359} // namespace aos
360
361#endif // AOS_IPC_LIB_LOCKLESS_QUEUE_H_