blob: dad44bbfdf104de2aa67de4f03fb36b6635f8986 [file] [log] [blame]
Austin Schuh20b2b082019-09-11 20:42:56 -07001#include "aos/ipc_lib/lockless_queue.h"
2
3#include <linux/futex.h>
4#include <sys/types.h>
5#include <syscall.h>
6#include <unistd.h>
7#include <algorithm>
8#include <iomanip>
9#include <iostream>
10#include <sstream>
11
Austin Schuh20b2b082019-09-11 20:42:56 -070012#include "aos/ipc_lib/lockless_queue_memory.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070013#include "aos/realtime.h"
Austin Schuh20b2b082019-09-11 20:42:56 -070014#include "aos/util/compiler_memory_barrier.h"
Brian Silverman001f24d2020-08-12 19:33:20 -070015#include "gflags/gflags.h"
Austin Schuhf257f3c2019-10-27 21:00:43 -070016#include "glog/logging.h"
Austin Schuh20b2b082019-09-11 20:42:56 -070017
Brian Silverman001f24d2020-08-12 19:33:20 -070018DEFINE_bool(dump_lockless_queue_data, false,
19 "If true, print the data out when dumping the queue.");
20
Austin Schuh20b2b082019-09-11 20:42:56 -070021namespace aos {
22namespace ipc_lib {
Austin Schuh20b2b082019-09-11 20:42:56 -070023namespace {
24
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080025class GrabQueueSetupLockOrDie {
26 public:
27 GrabQueueSetupLockOrDie(LocklessQueueMemory *memory) : memory_(memory) {
28 const int result = mutex_grab(&(memory->queue_setup_lock));
29 CHECK(result == 0 || result == 1) << ": " << result;
30 }
Austin Schuh20b2b082019-09-11 20:42:56 -070031
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080032 ~GrabQueueSetupLockOrDie() { mutex_unlock(&(memory_->queue_setup_lock)); }
33
34 GrabQueueSetupLockOrDie(const GrabQueueSetupLockOrDie &) = delete;
35 GrabQueueSetupLockOrDie &operator=(const GrabQueueSetupLockOrDie &) = delete;
36
37 private:
38 LocklessQueueMemory *const memory_;
39};
40
Brian Silverman177567e2020-08-12 19:51:33 -070041bool IsPinned(LocklessQueueMemory *memory, Index index) {
42 DCHECK(index.valid());
43 const size_t queue_size = memory->queue_size();
44 const QueueIndex message_index =
45 memory->GetMessage(index)->header.queue_index.Load(queue_size);
46 if (!message_index.valid()) {
47 return false;
48 }
49 DCHECK(memory->GetQueue(message_index.Wrapped())->Load() != index)
50 << ": Message is in the queue";
51 for (int pinner_index = 0;
52 pinner_index < static_cast<int>(memory->config.num_pinners);
53 ++pinner_index) {
54 ipc_lib::Pinner *const pinner = memory->GetPinner(pinner_index);
55
56 if (pinner->pinned.RelaxedLoad(queue_size) == message_index) {
57 return true;
58 }
59 }
60 return false;
61}
62
63// Ensures sender->scratch_index (which must contain to_replace) is not pinned.
64//
65// Returns the new scratch_index value.
66Index SwapPinnedSenderScratch(LocklessQueueMemory *const memory,
67 ipc_lib::Sender *const sender,
68 const Index to_replace) {
69 // If anybody's trying to pin this message, then grab a message from a pinner
70 // to write into instead, and leave the message we pulled out of the queue
71 // (currently in our scratch_index) with a pinner.
72 //
73 // This loop will terminate in at most one iteration through the pinners in
74 // any steady-state configuration of the memory. There are only as many
75 // Pinner::pinned values to worry about as there are Pinner::scratch_index
76 // values to check against, plus to_replace, which means there will always be
77 // a free one. We might have to make multiple passes if things are being
78 // changed concurrently though, but nobody dying can make this loop fail to
79 // terminate (because the number of processes that can die is bounded, because
80 // no new ones can start while we've got the lock).
81 for (int pinner_index = 0; true;
82 pinner_index = (pinner_index + 1) % memory->config.num_pinners) {
83 if (!IsPinned(memory, to_replace)) {
84 // No pinners on our current scratch_index, so we're fine now.
85 VLOG(3) << "No pinners: " << to_replace.DebugString();
86 return to_replace;
87 }
88
89 ipc_lib::Pinner *const pinner = memory->GetPinner(pinner_index);
90
91 const Index pinner_scratch = pinner->scratch_index.RelaxedLoad();
92 CHECK(pinner_scratch.valid())
93 << ": Pinner scratch_index should always be valid";
94 if (IsPinned(memory, pinner_scratch)) {
95 // Wouldn't do us any good to swap with this one, so don't bother, and
96 // move onto the next one.
97 VLOG(3) << "Also pinned: " << pinner_scratch.DebugString();
98 continue;
99 }
100
101 sender->to_replace.RelaxedStore(pinner_scratch);
102 aos_compiler_memory_barrier();
103 // Give the pinner the message (which is currently in
104 // sender->scratch_index).
105 if (!pinner->scratch_index.CompareAndExchangeStrong(pinner_scratch,
106 to_replace)) {
107 // Somebody swapped into this pinner before us. The new value is probably
108 // pinned, so we don't want to look at it again immediately.
109 VLOG(3) << "Pinner " << pinner_index
110 << " scratch_index changed: " << pinner_scratch.DebugString()
111 << ", " << to_replace.DebugString();
112 sender->to_replace.RelaxedInvalidate();
113 continue;
114 }
115 aos_compiler_memory_barrier();
116 // Now update the sender's scratch space and record that we succeeded.
117 sender->scratch_index.Store(pinner_scratch);
118 aos_compiler_memory_barrier();
119 // And then record that we succeeded, but definitely after the above
120 // store.
121 sender->to_replace.RelaxedInvalidate();
122 VLOG(3) << "Got new scratch message: " << pinner_scratch.DebugString();
123
124 // If it's in a pinner's scratch_index, it should not be in the queue, which
125 // means nobody new can pin it for real. However, they can still attempt to
126 // pin it, which means we can't verify !IsPinned down here.
127
128 return pinner_scratch;
129 }
130}
131
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700132// Returns true if it succeeded. Returns false if another sender died in the
133// middle.
134bool DoCleanup(LocklessQueueMemory *memory, const GrabQueueSetupLockOrDie &) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800135 // Make sure we start looking at shared memory fresh right now. We'll handle
136 // people dying partway through by either cleaning up after them or not, but
137 // we want to ensure we clean up after anybody who has already died when we
138 // start.
139 aos_compiler_memory_barrier();
140
Austin Schuh20b2b082019-09-11 20:42:56 -0700141 const size_t num_senders = memory->num_senders();
Brian Silverman177567e2020-08-12 19:51:33 -0700142 const size_t num_pinners = memory->num_pinners();
Austin Schuh20b2b082019-09-11 20:42:56 -0700143 const size_t queue_size = memory->queue_size();
144 const size_t num_messages = memory->num_messages();
145
146 // There are a large number of crazy cases here for how things can go wrong
147 // and how we have to recover. They either require us to keep extra track of
148 // what is going on, slowing down the send path, or require a large number of
149 // cases.
150 //
151 // The solution here is to not over-think it. This is running while not real
152 // time during construction. It is allowed to be slow. It will also very
153 // rarely trigger. There is a small uS window where process death is
154 // ambiguous.
155 //
156 // So, build up a list N long, where N is the number of messages. Search
157 // through the entire queue and the sender list (ignoring any dead senders),
158 // and mark down which ones we have seen. Once we have seen all the messages
159 // except the N dead senders, we know which messages are dead. Because the
160 // queue is active while we do this, it may take a couple of go arounds to see
161 // everything.
162
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700163 ::std::vector<bool> need_recovery(num_senders, false);
164
Austin Schuh20b2b082019-09-11 20:42:56 -0700165 // Do the easy case. Find all senders who have died. See if they are either
166 // consistent already, or if they have copied over to_replace to the scratch
167 // index, but haven't cleared to_replace. Count them.
168 size_t valid_senders = 0;
169 for (size_t i = 0; i < num_senders; ++i) {
170 Sender *sender = memory->GetSender(i);
171 const uint32_t tid =
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800172 __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE);
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700173 if (!(tid & FUTEX_OWNER_DIED)) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700174 // Not dead.
175 ++valid_senders;
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700176 continue;
Austin Schuh20b2b082019-09-11 20:42:56 -0700177 }
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700178 VLOG(3) << "Found an easy death for sender " << i;
179 // We can do a relaxed load here because we're the only person touching
180 // this sender at this point.
181 const Index to_replace = sender->to_replace.RelaxedLoad();
182 const Index scratch_index = sender->scratch_index.Load();
183
184 // I find it easiest to think about this in terms of the set of observable
185 // states. The main code progresses through the following states:
186
187 // 1) scratch_index = xxx
188 // to_replace = invalid
189 // This is unambiguous. Already good.
190
191 // 2) scratch_index = xxx
192 // to_replace = yyy
193 // Very ambiguous. Is xxx or yyy the correct one? Need to either roll
194 // this forwards or backwards.
195
196 // 3) scratch_index = yyy
197 // to_replace = yyy
198 // We are in the act of moving to_replace to scratch_index, but didn't
199 // finish. Easy.
Brian Silverman177567e2020-08-12 19:51:33 -0700200 //
201 // If doing a pinner swap, we've definitely done it.
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700202
203 // 4) scratch_index = yyy
204 // to_replace = invalid
205 // Finished, but died. Looks like 1)
206
Brian Silverman177567e2020-08-12 19:51:33 -0700207 // Swapping with a pinner's scratch_index passes through the same states.
208 // We just need to ensure the message that ends up in the senders's
209 // scratch_index isn't pinned, using the same code as sending does.
210
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700211 // Any cleanup code needs to follow the same set of states to be robust to
212 // death, so death can be restarted.
213
214 if (!to_replace.valid()) {
215 // 1) or 4). Make sure we aren't corrupted and declare victory.
216 CHECK(scratch_index.valid());
217
Brian Silverman177567e2020-08-12 19:51:33 -0700218 // If it's in 1) with a pinner, the sender might have a pinned message,
219 // so fix that.
220 SwapPinnedSenderScratch(memory, sender, scratch_index);
221
222 // If it's in 4), it may not have completed this step yet. This will
223 // always be a NOP if it's in 1), verified by a DCHECK.
224 memory->GetMessage(scratch_index)->header.queue_index.RelaxedInvalidate();
225
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700226 __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE);
227 ++valid_senders;
228 continue;
229 }
230
231 // Could be 2) or 3) at this point.
232
233 if (to_replace == scratch_index) {
234 // 3) for sure.
235 // Just need to invalidate to_replace to finish.
236 sender->to_replace.Invalidate();
237
Brian Silverman177567e2020-08-12 19:51:33 -0700238 // Make sure to indicate it's an unused message before a sender gets its
239 // hands on it.
240 memory->GetMessage(scratch_index)->header.queue_index.RelaxedInvalidate();
241 aos_compiler_memory_barrier();
242
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700243 // And mark that we succeeded.
244 __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE);
245 ++valid_senders;
246 continue;
247 }
248
249 // Must be 2). Mark it for later.
250 need_recovery[i] = true;
Austin Schuh20b2b082019-09-11 20:42:56 -0700251 }
252
Brian Silverman177567e2020-08-12 19:51:33 -0700253 // Cleaning up pinners is easy. We don't actually have to do anything, but
254 // invalidating its pinned field might help catch bugs elsewhere trying to
255 // read it before it's set.
256 for (size_t i = 0; i < num_pinners; ++i) {
257 Pinner *const pinner = memory->GetPinner(i);
258 const uint32_t tid =
259 __atomic_load_n(&(pinner->tid.futex), __ATOMIC_ACQUIRE);
260 if (!(tid & FUTEX_OWNER_DIED)) {
261 continue;
262 }
263 pinner->pinned.Invalidate();
264 __atomic_store_n(&(pinner->tid.futex), 0, __ATOMIC_RELEASE);
265 }
266
Austin Schuh20b2b082019-09-11 20:42:56 -0700267 // If all the senders are (or were made) good, there is no need to do the hard
268 // case.
269 if (valid_senders == num_senders) {
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700270 return true;
Austin Schuh20b2b082019-09-11 20:42:56 -0700271 }
272
Alex Perrycb7da4b2019-08-28 19:35:56 -0700273 VLOG(3) << "Starting hard cleanup";
Austin Schuh20b2b082019-09-11 20:42:56 -0700274
275 size_t num_accounted_for = 0;
276 size_t num_missing = 0;
277 ::std::vector<bool> accounted_for(num_messages, false);
278
279 while ((num_accounted_for + num_missing) != num_messages) {
280 num_missing = 0;
281 for (size_t i = 0; i < num_senders; ++i) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800282 Sender *const sender = memory->GetSender(i);
Austin Schuh20b2b082019-09-11 20:42:56 -0700283 const uint32_t tid =
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800284 __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE);
Austin Schuh20b2b082019-09-11 20:42:56 -0700285 if (tid & FUTEX_OWNER_DIED) {
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700286 if (!need_recovery[i]) {
287 return false;
288 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700289 ++num_missing;
Brian Silverman177567e2020-08-12 19:51:33 -0700290 continue;
Austin Schuh20b2b082019-09-11 20:42:56 -0700291 }
Brian Silverman177567e2020-08-12 19:51:33 -0700292 CHECK(!need_recovery[i]) << ": Somebody else recovered a sender: " << i;
293 // We can do a relaxed load here because we're the only person touching
294 // this sender at this point, if it matters. If it's not a dead sender,
295 // then any message it ever has will eventually be accounted for if we
296 // make enough tries through the outer loop.
297 const Index scratch_index = sender->scratch_index.RelaxedLoad();
298 if (!accounted_for[scratch_index.message_index()]) {
299 ++num_accounted_for;
300 }
301 accounted_for[scratch_index.message_index()] = true;
Austin Schuh20b2b082019-09-11 20:42:56 -0700302 }
303
304 for (size_t i = 0; i < queue_size; ++i) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800305 // Same logic as above for scratch_index applies here too.
Austin Schuh20b2b082019-09-11 20:42:56 -0700306 const Index index = memory->GetQueue(i)->RelaxedLoad();
307 if (!accounted_for[index.message_index()]) {
308 ++num_accounted_for;
309 }
310 accounted_for[index.message_index()] = true;
311 }
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700312
Brian Silverman177567e2020-08-12 19:51:33 -0700313 for (size_t pinner_index = 0; pinner_index < num_pinners; ++pinner_index) {
314 // Same logic as above for scratch_index applies here too.
315 const Index index =
316 memory->GetPinner(pinner_index)->scratch_index.RelaxedLoad();
317 if (!accounted_for[index.message_index()]) {
318 ++num_accounted_for;
319 }
320 accounted_for[index.message_index()] = true;
321 }
322
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700323 CHECK_LE(num_accounted_for + num_missing, num_messages);
Austin Schuh20b2b082019-09-11 20:42:56 -0700324 }
325
326 while (num_missing != 0) {
327 const size_t starting_num_missing = num_missing;
328 for (size_t i = 0; i < num_senders; ++i) {
329 Sender *sender = memory->GetSender(i);
330 const uint32_t tid =
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800331 __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE);
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700332 if (!(tid & FUTEX_OWNER_DIED)) {
333 CHECK(!need_recovery[i]) << ": Somebody else recovered a sender: " << i;
334 continue;
335 }
336 if (!need_recovery[i]) {
337 return false;
338 }
339 // We can do relaxed loads here because we're the only person touching
340 // this sender at this point.
341 const Index scratch_index = sender->scratch_index.RelaxedLoad();
342 const Index to_replace = sender->to_replace.RelaxedLoad();
Austin Schuh20b2b082019-09-11 20:42:56 -0700343
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700344 // Candidate.
345 if (to_replace.valid()) {
346 CHECK_LE(to_replace.message_index(), accounted_for.size());
347 }
348 if (scratch_index.valid()) {
349 CHECK_LE(scratch_index.message_index(), accounted_for.size());
350 }
351 if (!to_replace.valid() || accounted_for[to_replace.message_index()]) {
352 CHECK(scratch_index.valid());
353 VLOG(3) << "Sender " << i
354 << " died, to_replace is already accounted for";
355 // If both are accounted for, we are corrupt...
356 CHECK(!accounted_for[scratch_index.message_index()]);
Austin Schuh20b2b082019-09-11 20:42:56 -0700357
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700358 // to_replace is already accounted for. This means that we didn't
359 // atomically insert scratch_index into the queue yet. So
360 // invalidate to_replace.
361 sender->to_replace.Invalidate();
Brian Silverman177567e2020-08-12 19:51:33 -0700362 // Sender definitely will not have gotten here, so finish for it.
363 memory->GetMessage(scratch_index)
364 ->header.queue_index.RelaxedInvalidate();
Austin Schuh20b2b082019-09-11 20:42:56 -0700365
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700366 // And then mark this sender clean.
367 __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE);
368 need_recovery[i] = false;
Austin Schuh20b2b082019-09-11 20:42:56 -0700369
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700370 // And account for scratch_index.
371 accounted_for[scratch_index.message_index()] = true;
372 --num_missing;
373 ++num_accounted_for;
374 } else if (!scratch_index.valid() ||
375 accounted_for[scratch_index.message_index()]) {
376 VLOG(3) << "Sender " << i
377 << " died, scratch_index is already accounted for";
378 // scratch_index is accounted for. That means we did the insert,
379 // but didn't record it.
380 CHECK(to_replace.valid());
Brian Silverman177567e2020-08-12 19:51:33 -0700381
382 // Make sure to indicate it's an unused message before a sender gets its
383 // hands on it.
384 memory->GetMessage(to_replace)->header.queue_index.RelaxedInvalidate();
385 aos_compiler_memory_barrier();
386
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700387 // Finish the transaction. Copy to_replace, then clear it.
Austin Schuh20b2b082019-09-11 20:42:56 -0700388
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700389 sender->scratch_index.Store(to_replace);
390 sender->to_replace.Invalidate();
Austin Schuh20b2b082019-09-11 20:42:56 -0700391
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700392 // And then mark this sender clean.
393 __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE);
394 need_recovery[i] = false;
Austin Schuh20b2b082019-09-11 20:42:56 -0700395
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700396 // And account for to_replace.
397 accounted_for[to_replace.message_index()] = true;
398 --num_missing;
399 ++num_accounted_for;
400 } else {
401 VLOG(3) << "Sender " << i << " died, neither is accounted for";
402 // Ambiguous. There will be an unambiguous one somewhere that we
403 // can do first.
Austin Schuh20b2b082019-09-11 20:42:56 -0700404 }
405 }
406 // CHECK that we are making progress.
407 CHECK_NE(num_missing, starting_num_missing);
408 }
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700409 return true;
410}
411
412void Cleanup(LocklessQueueMemory *memory, const GrabQueueSetupLockOrDie &lock) {
413 // The number of iterations is bounded here because there are only a finite
414 // number of senders in existence which could die, and no new ones can be
415 // created while we're in here holding the lock.
416 while (!DoCleanup(memory, lock)) {
417 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700418}
419
420// Exposes rt_tgsigqueueinfo so we can send the signal *just* to the target
421// thread.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800422// TODO(Brian): Do directly in assembly for armhf at least for efficiency.
Austin Schuh20b2b082019-09-11 20:42:56 -0700423int rt_tgsigqueueinfo(pid_t tgid, pid_t tid, int sig, siginfo_t *si) {
424 return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, si);
425}
426
427} // namespace
428
Austin Schuh4bc4f902019-12-23 18:04:51 -0800429size_t LocklessQueueConfiguration::message_size() const {
430 // Round up the message size so following data is aligned appropriately.
Brian Silvermana1652f32020-01-29 20:41:44 -0800431 return LocklessQueueMemory::AlignmentRoundUp(message_data_size +
432 (kChannelDataAlignment - 1)) +
Austin Schuh4bc4f902019-12-23 18:04:51 -0800433 sizeof(Message);
434}
435
Austin Schuh20b2b082019-09-11 20:42:56 -0700436size_t LocklessQueueMemorySize(LocklessQueueConfiguration config) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800437 // Round up the message size so following data is aligned appropriately.
438 config.message_data_size =
439 LocklessQueueMemory::AlignmentRoundUp(config.message_data_size);
Austin Schuh20b2b082019-09-11 20:42:56 -0700440
441 // As we build up the size, confirm that everything is aligned to the
442 // alignment requirements of the type.
443 size_t size = sizeof(LocklessQueueMemory);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800444 CHECK_EQ(size % alignof(LocklessQueueMemory), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700445
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800446 CHECK_EQ(size % alignof(AtomicIndex), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700447 size += LocklessQueueMemory::SizeOfQueue(config);
448
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800449 CHECK_EQ(size % alignof(Message), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700450 size += LocklessQueueMemory::SizeOfMessages(config);
451
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800452 CHECK_EQ(size % alignof(Watcher), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700453 size += LocklessQueueMemory::SizeOfWatchers(config);
454
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800455 CHECK_EQ(size % alignof(Sender), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700456 size += LocklessQueueMemory::SizeOfSenders(config);
457
Brian Silverman177567e2020-08-12 19:51:33 -0700458 CHECK_EQ(size % alignof(Pinner), 0u);
459 size += LocklessQueueMemory::SizeOfPinners(config);
460
Austin Schuh20b2b082019-09-11 20:42:56 -0700461 return size;
462}
463
464LocklessQueueMemory *InitializeLocklessQueueMemory(
465 LocklessQueueMemory *memory, LocklessQueueConfiguration config) {
466 // Everything should be zero initialized already. So we just need to fill
467 // everything out properly.
468
Brian Silvermanc57ff0a2020-04-28 16:45:13 -0700469 // This is the UID we will use for checking signal-sending permission
470 // compatibility.
471 //
472 // The manpage says:
473 // For a process to have permission to send a signal, it must either be
474 // privileged [...], or the real or effective user ID of the sending process
475 // must equal the real or saved set-user-ID of the target process.
476 //
477 // Processes typically initialize a queue in random order as they start up.
478 // This means we need an algorithm for verifying all processes have
479 // permissions to send each other signals which gives the same answer no
480 // matter what order they attach in. We would also like to avoid maintaining a
481 // shared list of the UIDs of all processes.
482 //
483 // To do this while still giving sufficient flexibility for all current use
484 // cases, we track a single UID for the queue. All processes with a matching
485 // euid+suid must have this UID. Any processes with distinct euid/suid must
486 // instead have a matching ruid. This guarantees signals can be sent between
487 // all processes attached to the queue.
488 //
489 // In particular, this allows a process to change only its euid (to interact
490 // with a queue) while still maintaining privileges via its ruid. However, it
491 // can only use privileges in ways that do not require changing the euid back,
492 // because while the euid is different it will not be able to receive signals.
493 // We can't actually verify that, but we can sanity check that things are
494 // valid when the queue is initialized.
495
496 uid_t uid;
497 {
498 uid_t ruid, euid, suid;
499 PCHECK(getresuid(&ruid, &euid, &suid) == 0);
500 // If these are equal, then use them, even if that's different from the real
501 // UID. This allows processes to keep a real UID of 0 (to have permissions
502 // to perform system-level changes) while still being able to communicate
503 // with processes running unprivileged as a distinct user.
504 if (euid == suid) {
505 uid = euid;
506 VLOG(1) << "Using euid==suid " << uid;
507 } else {
508 uid = ruid;
509 VLOG(1) << "Using ruid " << ruid;
510 }
511 }
512
Austin Schuh20b2b082019-09-11 20:42:56 -0700513 // Grab the mutex. We don't care if the previous reader died. We are going
514 // to check everything anyways.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800515 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory);
Austin Schuh20b2b082019-09-11 20:42:56 -0700516
517 if (!memory->initialized) {
518 // TODO(austin): Check these for out of bounds.
519 memory->config.num_watchers = config.num_watchers;
520 memory->config.num_senders = config.num_senders;
Brian Silverman177567e2020-08-12 19:51:33 -0700521 memory->config.num_pinners = config.num_pinners;
Austin Schuh20b2b082019-09-11 20:42:56 -0700522 memory->config.queue_size = config.queue_size;
Austin Schuh4bc4f902019-12-23 18:04:51 -0800523 memory->config.message_data_size = config.message_data_size;
Austin Schuh20b2b082019-09-11 20:42:56 -0700524
525 const size_t num_messages = memory->num_messages();
526 // There need to be at most MaxMessages() messages allocated.
527 CHECK_LE(num_messages, Index::MaxMessages());
528
529 for (size_t i = 0; i < num_messages; ++i) {
530 memory->GetMessage(Index(QueueIndex::Zero(memory->queue_size()), i))
531 ->header.queue_index.Invalidate();
532 }
533
534 for (size_t i = 0; i < memory->queue_size(); ++i) {
535 // Make the initial counter be the furthest away number. That means that
536 // index 0 should be 0xffff, 1 should be 0, etc.
537 memory->GetQueue(i)->Store(Index(QueueIndex::Zero(memory->queue_size())
538 .IncrementBy(i)
539 .DecrementBy(memory->queue_size()),
540 i));
541 }
542
543 memory->next_queue_index.Invalidate();
Brian Silvermanc57ff0a2020-04-28 16:45:13 -0700544 memory->uid = uid;
Austin Schuh20b2b082019-09-11 20:42:56 -0700545
546 for (size_t i = 0; i < memory->num_senders(); ++i) {
547 ::aos::ipc_lib::Sender *s = memory->GetSender(i);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800548 // Nobody else can possibly be touching these because we haven't set
549 // initialized to true yet.
550 s->scratch_index.RelaxedStore(Index(0xffff, i + memory->queue_size()));
Austin Schuh20b2b082019-09-11 20:42:56 -0700551 s->to_replace.RelaxedInvalidate();
552 }
553
Brian Silverman177567e2020-08-12 19:51:33 -0700554 for (size_t i = 0; i < memory->num_pinners(); ++i) {
555 ::aos::ipc_lib::Pinner *pinner = memory->GetPinner(i);
556 // Nobody else can possibly be touching these because we haven't set
557 // initialized to true yet.
558 pinner->scratch_index.RelaxedStore(
559 Index(0xffff, i + memory->num_senders() + memory->queue_size()));
560 pinner->pinned.Invalidate();
561 }
562
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800563 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -0700564 // Signal everything is done. This needs to be done last, so if we die, we
565 // redo initialization.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800566 memory->initialized = true;
Austin Schuh3328d132020-02-28 13:54:57 -0800567 } else {
Brian Silvermanc57ff0a2020-04-28 16:45:13 -0700568 CHECK_EQ(uid, memory->uid) << ": UIDs must match for all processes";
Austin Schuh20b2b082019-09-11 20:42:56 -0700569 }
570
Austin Schuh20b2b082019-09-11 20:42:56 -0700571 return memory;
572}
573
574LocklessQueue::LocklessQueue(LocklessQueueMemory *memory,
575 LocklessQueueConfiguration config)
576 : memory_(InitializeLocklessQueueMemory(memory, config)),
577 watcher_copy_(memory_->num_watchers()),
578 pid_(getpid()),
579 uid_(getuid()) {}
580
581LocklessQueue::~LocklessQueue() {
582 CHECK_EQ(watcher_index_, -1);
583
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800584 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_);
Austin Schuh20b2b082019-09-11 20:42:56 -0700585 const int num_watchers = memory_->num_watchers();
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800586 // Cleanup is cheap. The next user will do it anyways, so no need for us to do
587 // anything right now.
Austin Schuh20b2b082019-09-11 20:42:56 -0700588
589 // And confirm that nothing is owned by us.
590 for (int i = 0; i < num_watchers; ++i) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800591 CHECK(!death_notification_is_held(&(memory_->GetWatcher(i)->tid)));
Austin Schuh20b2b082019-09-11 20:42:56 -0700592 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700593}
594
595size_t LocklessQueue::QueueSize() const { return memory_->queue_size(); }
596
597bool LocklessQueue::RegisterWakeup(int priority) {
598 // TODO(austin): Make sure signal coalescing is turned on. We don't need
599 // duplicates. That will improve performance under high load.
600
601 // Since everything is self consistent, all we need to do is make sure nobody
602 // else is running. Someone dying will get caught in the generic consistency
603 // check.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800604 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_);
Austin Schuh20b2b082019-09-11 20:42:56 -0700605 const int num_watchers = memory_->num_watchers();
606
607 // Now, find the first empty watcher and grab it.
608 CHECK_EQ(watcher_index_, -1);
609 for (int i = 0; i < num_watchers; ++i) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800610 // If we see a slot the kernel has marked as dead, everything we do reusing
611 // it needs to happen-after whatever that process did before dying.
Brian Silverman2484eea2019-12-21 16:48:46 -0800612 auto *const futex = &(memory_->GetWatcher(i)->tid.futex);
613 const uint32_t tid = __atomic_load_n(futex, __ATOMIC_ACQUIRE);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800614 if (tid == 0 || (tid & FUTEX_OWNER_DIED)) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700615 watcher_index_ = i;
Brian Silverman2484eea2019-12-21 16:48:46 -0800616 // Relaxed is OK here because we're the only task going to touch it
617 // between here and the write in death_notification_init below (other
618 // recovery is blocked by us holding the setup lock).
619 __atomic_store_n(futex, 0, __ATOMIC_RELAXED);
Austin Schuh20b2b082019-09-11 20:42:56 -0700620 break;
621 }
622 }
623
624 // Bail if we failed to find an open slot.
625 if (watcher_index_ == -1) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700626 return false;
627 }
628
629 Watcher *w = memory_->GetWatcher(watcher_index_);
630
631 w->pid = getpid();
632 w->priority = priority;
633
634 // Grabbing a mutex is a compiler and memory barrier, so nothing before will
635 // get rearranged afterwords.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800636 death_notification_init(&(w->tid));
637 return true;
Austin Schuh20b2b082019-09-11 20:42:56 -0700638}
639
640void LocklessQueue::UnregisterWakeup() {
641 // Since everything is self consistent, all we need to do is make sure nobody
642 // else is running. Someone dying will get caught in the generic consistency
643 // check.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800644 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_);
Austin Schuh20b2b082019-09-11 20:42:56 -0700645
646 // Make sure we are registered.
647 CHECK_NE(watcher_index_, -1);
648
649 // Make sure we still own the slot we are supposed to.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800650 CHECK(
651 death_notification_is_held(&(memory_->GetWatcher(watcher_index_)->tid)));
Austin Schuh20b2b082019-09-11 20:42:56 -0700652
653 // The act of unlocking invalidates the entry. Invalidate it.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800654 death_notification_release(&(memory_->GetWatcher(watcher_index_)->tid));
Austin Schuh20b2b082019-09-11 20:42:56 -0700655 // And internally forget the slot.
656 watcher_index_ = -1;
Austin Schuh20b2b082019-09-11 20:42:56 -0700657}
658
659int LocklessQueue::Wakeup(const int current_priority) {
660 const size_t num_watchers = memory_->num_watchers();
661
662 CHECK_EQ(watcher_copy_.size(), num_watchers);
663
664 // Grab a copy so it won't change out from underneath us, and we can sort it
665 // nicely in C++.
666 // Do note that there is still a window where the process can die *after* we
667 // read everything. We will still PI boost and send a signal to the thread in
668 // question. There is no way without pidfd's to close this window, and
669 // creating a pidfd is likely not RT.
670 for (size_t i = 0; i < num_watchers; ++i) {
671 Watcher *w = memory_->GetWatcher(i);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800672 watcher_copy_[i].tid = __atomic_load_n(&(w->tid.futex), __ATOMIC_RELAXED);
673 // Force the load of the TID to come first.
674 aos_compiler_memory_barrier();
675 watcher_copy_[i].pid = w->pid.load(std::memory_order_relaxed);
676 watcher_copy_[i].priority = w->priority.load(std::memory_order_relaxed);
Austin Schuh20b2b082019-09-11 20:42:56 -0700677
678 // Use a priority of -1 to mean an invalid entry to make sorting easier.
679 if (watcher_copy_[i].tid & FUTEX_OWNER_DIED || watcher_copy_[i].tid == 0) {
680 watcher_copy_[i].priority = -1;
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800681 } else {
682 // Ensure all of this happens after we're done looking at the pid+priority
683 // in shared memory.
684 aos_compiler_memory_barrier();
685 if (watcher_copy_[i].tid != static_cast<pid_t>(__atomic_load_n(
686 &(w->tid.futex), __ATOMIC_RELAXED))) {
687 // Confirm that the watcher hasn't been re-used and modified while we
688 // read it. If it has, mark it invalid again.
689 watcher_copy_[i].priority = -1;
690 watcher_copy_[i].tid = 0;
691 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700692 }
693 }
694
695 // Now sort.
696 ::std::sort(watcher_copy_.begin(), watcher_copy_.end(),
697 [](const WatcherCopy &a, const WatcherCopy &b) {
698 return a.priority > b.priority;
699 });
700
701 int count = 0;
702 if (watcher_copy_[0].priority != -1) {
703 const int max_priority =
704 ::std::max(current_priority, watcher_copy_[0].priority);
705 // Boost if we are RT and there is a higher priority sender out there.
706 // Otherwise we might run into priority inversions.
707 if (max_priority > current_priority && current_priority > 0) {
708 SetCurrentThreadRealtimePriority(max_priority);
709 }
710
711 // Build up the siginfo to send.
712 siginfo_t uinfo;
713 memset(&uinfo, 0, sizeof(uinfo));
714
715 uinfo.si_code = SI_QUEUE;
716 uinfo.si_pid = pid_;
717 uinfo.si_uid = uid_;
718 uinfo.si_value.sival_int = 0;
719
720 for (const WatcherCopy &watcher_copy : watcher_copy_) {
721 // The first -1 priority means we are at the end of the valid list.
722 if (watcher_copy.priority == -1) {
723 break;
724 }
725
726 // Send the signal. Target just the thread that sent it so that we can
727 // support multiple watchers in a process (when someone creates multiple
728 // event loops in different threads).
729 rt_tgsigqueueinfo(watcher_copy.pid, watcher_copy.tid, kWakeupSignal,
730 &uinfo);
731
732 ++count;
733 }
734
735 // Drop back down if we were boosted.
736 if (max_priority > current_priority && current_priority > 0) {
737 SetCurrentThreadRealtimePriority(current_priority);
738 }
739 }
740
741 return count;
742}
743
744LocklessQueue::Sender::Sender(LocklessQueueMemory *memory) : memory_(memory) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800745 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_);
Austin Schuh20b2b082019-09-11 20:42:56 -0700746
747 // Since we already have the lock, go ahead and try cleaning up.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800748 Cleanup(memory_, grab_queue_setup_lock);
Austin Schuh20b2b082019-09-11 20:42:56 -0700749
750 const int num_senders = memory_->num_senders();
751
752 for (int i = 0; i < num_senders; ++i) {
753 ::aos::ipc_lib::Sender *s = memory->GetSender(i);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800754 // This doesn't need synchronization because we're the only process doing
755 // initialization right now, and nobody else will be touching senders which
756 // we're interested in.
Austin Schuh20b2b082019-09-11 20:42:56 -0700757 const uint32_t tid = __atomic_load_n(&(s->tid.futex), __ATOMIC_RELAXED);
758 if (tid == 0) {
759 sender_index_ = i;
760 break;
761 }
762 }
763
764 if (sender_index_ == -1) {
Austin Schuhe516ab02020-05-06 21:37:04 -0700765 VLOG(1) << "Too many senders, starting to bail.";
766 return;
Austin Schuh20b2b082019-09-11 20:42:56 -0700767 }
768
Brian Silverman177567e2020-08-12 19:51:33 -0700769 ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_);
Austin Schuh20b2b082019-09-11 20:42:56 -0700770
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800771 // Indicate that we are now alive by taking over the slot. If the previous
772 // owner died, we still want to do this.
Brian Silverman177567e2020-08-12 19:51:33 -0700773 death_notification_init(&(sender->tid));
774
775 const Index scratch_index = sender->scratch_index.RelaxedLoad();
776 Message *const message = memory_->GetMessage(scratch_index);
777 CHECK(!message->header.queue_index.RelaxedLoad(memory_->queue_size()).valid())
778 << ": " << std::hex << scratch_index.get();
Austin Schuh20b2b082019-09-11 20:42:56 -0700779}
780
781LocklessQueue::Sender::~Sender() {
Austin Schuhe516ab02020-05-06 21:37:04 -0700782 if (valid()) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800783 death_notification_release(&(memory_->GetSender(sender_index_)->tid));
Austin Schuh20b2b082019-09-11 20:42:56 -0700784 }
785}
786
Alex Perrycb7da4b2019-08-28 19:35:56 -0700787size_t LocklessQueue::Sender::size() { return memory_->message_data_size(); }
788
789void *LocklessQueue::Sender::Data() {
790 ::aos::ipc_lib::Sender *sender = memory_->GetSender(sender_index_);
Brian Silverman177567e2020-08-12 19:51:33 -0700791 const Index scratch_index = sender->scratch_index.RelaxedLoad();
792 Message *const message = memory_->GetMessage(scratch_index);
793 // We should have invalidated this when we first got the buffer. Verify that
794 // in debug mode.
795 DCHECK(
796 !message->header.queue_index.RelaxedLoad(memory_->queue_size()).valid())
797 << ": " << std::hex << scratch_index.get();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700798
Brian Silvermana1652f32020-01-29 20:41:44 -0800799 return message->data(memory_->message_data_size());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700800}
801
Austin Schuhad154822019-12-27 15:45:13 -0800802void LocklessQueue::Sender::Send(
803 const char *data, size_t length,
804 aos::monotonic_clock::time_point monotonic_remote_time,
805 aos::realtime_clock::time_point realtime_remote_time,
806 uint32_t remote_queue_index,
807 aos::monotonic_clock::time_point *monotonic_sent_time,
808 aos::realtime_clock::time_point *realtime_sent_time,
809 uint32_t *queue_index) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700810 CHECK_LE(length, size());
Austin Schuh67420a42019-12-21 21:55:04 -0800811 // Flatbuffers write from the back of the buffer to the front. If we are
812 // going to write an explicit chunk of memory into the buffer, we need to
813 // adhere to this convention and place it at the end.
814 memcpy((reinterpret_cast<char *>(Data()) + size() - length), data, length);
Austin Schuhad154822019-12-27 15:45:13 -0800815 Send(length, monotonic_remote_time, realtime_remote_time, remote_queue_index,
816 monotonic_sent_time, realtime_sent_time, queue_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700817}
818
Brian Silverman177567e2020-08-12 19:51:33 -0700819LocklessQueue::Pinner::Pinner(LocklessQueueMemory *memory) : memory_(memory) {
820 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_);
821
822 // Since we already have the lock, go ahead and try cleaning up.
823 Cleanup(memory_, grab_queue_setup_lock);
824
825 const int num_pinners = memory_->num_pinners();
826
827 for (int i = 0; i < num_pinners; ++i) {
828 ::aos::ipc_lib::Pinner *p = memory->GetPinner(i);
829 // This doesn't need synchronization because we're the only process doing
830 // initialization right now, and nobody else will be touching pinners which
831 // we're interested in.
832 const uint32_t tid = __atomic_load_n(&(p->tid.futex), __ATOMIC_RELAXED);
833 if (tid == 0) {
834 pinner_index_ = i;
835 break;
836 }
837 }
838
839 if (pinner_index_ == -1) {
840 VLOG(1) << "Too many pinners, starting to bail.";
841 return;
842 }
843
844 ::aos::ipc_lib::Pinner *p = memory_->GetPinner(pinner_index_);
845 p->pinned.Invalidate();
846
847 // Indicate that we are now alive by taking over the slot. If the previous
848 // owner died, we still want to do this.
849 death_notification_init(&(p->tid));
850}
851
852LocklessQueue::Pinner::~Pinner() {
853 if (valid()) {
854 memory_->GetPinner(pinner_index_)->pinned.Invalidate();
855 aos_compiler_memory_barrier();
856 death_notification_release(&(memory_->GetPinner(pinner_index_)->tid));
857 }
858}
859
860// This method doesn't mess with any scratch_index, so it doesn't have to worry
861// about message ownership.
Brian Silverman4f4e0612020-08-12 19:54:41 -0700862int LocklessQueue::Pinner::PinIndex(uint32_t uint32_queue_index) {
Brian Silverman177567e2020-08-12 19:51:33 -0700863 const size_t queue_size = memory_->queue_size();
864 const QueueIndex queue_index =
865 QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index);
866 ipc_lib::Pinner *const pinner = memory_->GetPinner(pinner_index_);
867
868 AtomicIndex *const queue_slot = memory_->GetQueue(queue_index.Wrapped());
869
870 // Indicate that we want to pin this message.
871 pinner->pinned.Store(queue_index);
872 aos_compiler_memory_barrier();
873
874 {
875 const Index message_index = queue_slot->Load();
876 Message *const message = memory_->GetMessage(message_index);
877
878 const QueueIndex message_queue_index =
879 message->header.queue_index.Load(queue_size);
880 if (message_queue_index == queue_index) {
881 VLOG(3) << "Eq: " << std::hex << message_queue_index.index();
882 aos_compiler_memory_barrier();
Brian Silverman4f4e0612020-08-12 19:54:41 -0700883 return message_index.message_index();
Brian Silverman177567e2020-08-12 19:51:33 -0700884 }
885 VLOG(3) << "Message reused: " << std::hex << message_queue_index.index()
886 << ", " << queue_index.index();
887 }
888
889 // Being down here means we asked to pin a message before realizing it's no
890 // longer in the queue, so back that out now.
891 pinner->pinned.Invalidate();
892 VLOG(3) << "Unpinned: " << std::hex << queue_index.index();
Brian Silverman4f4e0612020-08-12 19:54:41 -0700893 return -1;
Brian Silverman177567e2020-08-12 19:51:33 -0700894}
895
896size_t LocklessQueue::Pinner::size() const {
897 return memory_->message_data_size();
898}
899
900const void *LocklessQueue::Pinner::Data() const {
901 const size_t queue_size = memory_->queue_size();
902 ::aos::ipc_lib::Pinner *pinner = memory_->GetPinner(pinner_index_);
903 QueueIndex pinned = pinner->pinned.RelaxedLoad(queue_size);
904 CHECK(pinned.valid());
905 const Message *message = memory_->GetMessage(pinned);
906
907 return message->data(memory_->message_data_size());
908}
909
910std::optional<LocklessQueue::Sender> LocklessQueue::MakeSender() {
911 LocklessQueue::Sender result = LocklessQueue::Sender(memory_);
912 if (result.valid()) {
913 return std::move(result);
914 } else {
915 return std::nullopt;
916 }
917}
918
919std::optional<LocklessQueue::Pinner> LocklessQueue::MakePinner() {
920 LocklessQueue::Pinner result = LocklessQueue::Pinner(memory_);
921 if (result.valid()) {
922 return std::move(result);
923 } else {
924 return std::nullopt;
925 }
926}
927
928namespace {
929
930QueueIndex ZeroOrValid(QueueIndex index) {
931 if (!index.valid()) {
932 return index.Clear();
933 }
934 return index;
935}
936
937} // namespace
938
Austin Schuhad154822019-12-27 15:45:13 -0800939void LocklessQueue::Sender::Send(
940 size_t length, aos::monotonic_clock::time_point monotonic_remote_time,
941 aos::realtime_clock::time_point realtime_remote_time,
942 uint32_t remote_queue_index,
943 aos::monotonic_clock::time_point *monotonic_sent_time,
944 aos::realtime_clock::time_point *realtime_sent_time,
945 uint32_t *queue_index) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700946 const size_t queue_size = memory_->queue_size();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700947 CHECK_LE(length, size());
Austin Schuh20b2b082019-09-11 20:42:56 -0700948
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800949 ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_);
950 // We can do a relaxed load on our sender because we're the only person
951 // modifying it right now.
952 const Index scratch_index = sender->scratch_index.RelaxedLoad();
953 Message *const message = memory_->GetMessage(scratch_index);
Austin Schuh20b2b082019-09-11 20:42:56 -0700954
Brian Silverman177567e2020-08-12 19:51:33 -0700955 // We should have invalidated this when we first got the buffer. Verify that
956 // in debug mode.
957 DCHECK(
958 !message->header.queue_index.RelaxedLoad(memory_->queue_size()).valid())
959 << ": " << std::hex << scratch_index.get();
960
Austin Schuh20b2b082019-09-11 20:42:56 -0700961 message->header.length = length;
Austin Schuhad154822019-12-27 15:45:13 -0800962 // Pass these through. Any alternative behavior can be implemented out a
963 // layer.
964 message->header.remote_queue_index = remote_queue_index;
965 message->header.monotonic_remote_time = monotonic_remote_time;
966 message->header.realtime_remote_time = realtime_remote_time;
Austin Schuh20b2b082019-09-11 20:42:56 -0700967
Brian Silverman177567e2020-08-12 19:51:33 -0700968 Index to_replace = Index::Invalid();
Austin Schuh20b2b082019-09-11 20:42:56 -0700969 while (true) {
970 const QueueIndex actual_next_queue_index =
971 memory_->next_queue_index.Load(queue_size);
972 const QueueIndex next_queue_index = ZeroOrValid(actual_next_queue_index);
973
974 const QueueIndex incremented_queue_index = next_queue_index.Increment();
975
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800976 // This needs to synchronize with whoever the previous writer at this
977 // location was.
Brian Silverman177567e2020-08-12 19:51:33 -0700978 to_replace = memory_->LoadIndex(next_queue_index);
Austin Schuh20b2b082019-09-11 20:42:56 -0700979
980 const QueueIndex decremented_queue_index =
981 next_queue_index.DecrementBy(queue_size);
982
983 // See if we got beat. If we did, try to atomically update
984 // next_queue_index in case the previous writer failed and retry.
985 if (!to_replace.IsPlausible(decremented_queue_index)) {
986 // We don't care about the result. It will either succeed, or we got
987 // beat in fixing it and just need to give up and try again. If we got
988 // beat multiple times, the only way progress can be made is if the queue
989 // is updated as well. This means that if we retry reading
990 // next_queue_index, we will be at most off by one and can retry.
991 //
992 // Both require no further action from us.
993 //
994 // TODO(austin): If we are having fairness issues under contention, we
995 // could have a mode bit in next_queue_index, and could use a lock or some
996 // other form of PI boosting to let the higher priority task win.
997 memory_->next_queue_index.CompareAndExchangeStrong(
998 actual_next_queue_index, incremented_queue_index);
999
Alex Perrycb7da4b2019-08-28 19:35:56 -07001000 VLOG(3) << "We were beat. Try again. Was " << std::hex
1001 << to_replace.get() << ", is " << decremented_queue_index.index();
Austin Schuh20b2b082019-09-11 20:42:56 -07001002 continue;
1003 }
1004
1005 // Confirm that the message is what it should be.
Brian Silverman177567e2020-08-12 19:51:33 -07001006 //
1007 // This is just a best-effort check to skip reading the clocks if possible.
1008 // If this fails, then the compare-exchange below definitely would, so we
1009 // can bail out now.
Austin Schuh20b2b082019-09-11 20:42:56 -07001010 {
Austin Schuh20b2b082019-09-11 20:42:56 -07001011 const QueueIndex previous_index =
Brian Silverman177567e2020-08-12 19:51:33 -07001012 memory_->GetMessage(to_replace)
1013 ->header.queue_index.RelaxedLoad(queue_size);
Austin Schuh20b2b082019-09-11 20:42:56 -07001014 if (previous_index != decremented_queue_index && previous_index.valid()) {
1015 // Retry.
Alex Perrycb7da4b2019-08-28 19:35:56 -07001016 VLOG(3) << "Something fishy happened, queue index doesn't match. "
1017 "Retrying. Previous index was "
1018 << std::hex << previous_index.index() << ", should be "
1019 << decremented_queue_index.index();
Austin Schuh20b2b082019-09-11 20:42:56 -07001020 continue;
1021 }
1022 }
1023
1024 message->header.monotonic_sent_time = ::aos::monotonic_clock::now();
1025 message->header.realtime_sent_time = ::aos::realtime_clock::now();
Austin Schuhad154822019-12-27 15:45:13 -08001026 if (monotonic_sent_time != nullptr) {
1027 *monotonic_sent_time = message->header.monotonic_sent_time;
1028 }
1029 if (realtime_sent_time != nullptr) {
1030 *realtime_sent_time = message->header.realtime_sent_time;
1031 }
1032 if (queue_index != nullptr) {
1033 *queue_index = next_queue_index.index();
1034 }
Austin Schuh20b2b082019-09-11 20:42:56 -07001035
1036 // Before we are fully done filling out the message, update the Sender state
1037 // with the new index to write. This re-uses the barrier for the
1038 // queue_index store.
Alex Perrycb7da4b2019-08-28 19:35:56 -07001039 const Index index_to_write(next_queue_index, scratch_index.message_index());
Austin Schuh20b2b082019-09-11 20:42:56 -07001040
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001041 aos_compiler_memory_barrier();
1042 // We're the only person who cares about our scratch index, besides somebody
1043 // cleaning up after us.
Austin Schuh20b2b082019-09-11 20:42:56 -07001044 sender->scratch_index.RelaxedStore(index_to_write);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001045 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -07001046
1047 message->header.queue_index.Store(next_queue_index);
1048
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001049 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -07001050 // The message is now filled out, and we have a confirmed slot to store
1051 // into.
1052 //
1053 // Start by writing down what we are going to pull out of the queue. This
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001054 // was Invalid before now. Only person who will read this is whoever cleans
1055 // up after us, so no synchronization necessary.
Austin Schuh20b2b082019-09-11 20:42:56 -07001056 sender->to_replace.RelaxedStore(to_replace);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001057 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -07001058
1059 // Then exchange the next index into the queue.
1060 if (!memory_->GetQueue(next_queue_index.Wrapped())
1061 ->CompareAndExchangeStrong(to_replace, index_to_write)) {
1062 // Aw, didn't succeed. Retry.
1063 sender->to_replace.RelaxedInvalidate();
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001064 aos_compiler_memory_barrier();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001065 VLOG(3) << "Failed to wrap into queue";
Austin Schuh20b2b082019-09-11 20:42:56 -07001066 continue;
1067 }
1068
1069 // Then update next_queue_index to save the next user some computation time.
1070 memory_->next_queue_index.CompareAndExchangeStrong(actual_next_queue_index,
1071 incremented_queue_index);
1072
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001073 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -07001074 // Now update the scratch space and record that we succeeded.
1075 sender->scratch_index.Store(to_replace);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001076 aos_compiler_memory_barrier();
1077 // And then record that we succeeded, but definitely after the above store.
Austin Schuh20b2b082019-09-11 20:42:56 -07001078 sender->to_replace.RelaxedInvalidate();
Brian Silverman177567e2020-08-12 19:51:33 -07001079
Austin Schuh20b2b082019-09-11 20:42:56 -07001080 break;
1081 }
Brian Silverman177567e2020-08-12 19:51:33 -07001082
1083 // to_replace is our current scratch_index. It isn't in the queue, which means
1084 // nobody new can pin it. They can set their `pinned` to it, but they will
1085 // back it out, so they don't count. This means that we just need to find a
1086 // message for which no pinner had it in `pinned`, and then we know this
1087 // message will never be pinned. We'll start with to_replace, and if that is
1088 // pinned then we'll look for a new one to use instead.
1089 const Index new_scratch =
1090 SwapPinnedSenderScratch(memory_, sender, to_replace);
1091
1092 // If anybody is looking at this message (they shouldn't be), then try telling
1093 // them about it (best-effort).
1094 memory_->GetMessage(new_scratch)->header.queue_index.RelaxedInvalidate();
Austin Schuh20b2b082019-09-11 20:42:56 -07001095}
1096
Brian Silverman4f4e0612020-08-12 19:54:41 -07001097int LocklessQueue::Sender::buffer_index() const {
1098 ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_);
1099 // We can do a relaxed load on our sender because we're the only person
1100 // modifying it right now.
1101 const Index scratch_index = sender->scratch_index.RelaxedLoad();
1102 return scratch_index.message_index();
1103}
1104
Austin Schuh20b2b082019-09-11 20:42:56 -07001105LocklessQueue::ReadResult LocklessQueue::Read(
1106 uint32_t uint32_queue_index,
1107 ::aos::monotonic_clock::time_point *monotonic_sent_time,
Austin Schuhad154822019-12-27 15:45:13 -08001108 ::aos::realtime_clock::time_point *realtime_sent_time,
1109 ::aos::monotonic_clock::time_point *monotonic_remote_time,
1110 ::aos::realtime_clock::time_point *realtime_remote_time,
1111 uint32_t *remote_queue_index, size_t *length, char *data) {
Austin Schuh20b2b082019-09-11 20:42:56 -07001112 const size_t queue_size = memory_->queue_size();
1113
1114 // Build up the QueueIndex.
1115 const QueueIndex queue_index =
1116 QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index);
1117
1118 // Read the message stored at the requested location.
1119 Index mi = memory_->LoadIndex(queue_index);
1120 Message *m = memory_->GetMessage(mi);
1121
1122 while (true) {
1123 // We need to confirm that the data doesn't change while we are reading it.
1124 // Do that by first confirming that the message points to the queue index we
1125 // want.
1126 const QueueIndex starting_queue_index =
1127 m->header.queue_index.Load(queue_size);
1128 if (starting_queue_index != queue_index) {
1129 // If we found a message that is exactly 1 loop old, we just wrapped.
1130 if (starting_queue_index == queue_index.DecrementBy(queue_size)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -07001131 VLOG(3) << "Matches: " << std::hex << starting_queue_index.index()
1132 << ", " << queue_index.DecrementBy(queue_size).index();
Austin Schuh20b2b082019-09-11 20:42:56 -07001133 return ReadResult::NOTHING_NEW;
Brian Silverman177567e2020-08-12 19:51:33 -07001134 }
1135
1136 // Someone has re-used this message between when we pulled it out of the
1137 // queue and when we grabbed its index. It is pretty hard to deduce
1138 // what happened. Just try again.
1139 Message *const new_m = memory_->GetMessage(queue_index);
1140 if (m != new_m) {
1141 m = new_m;
1142 VLOG(3) << "Retrying, m doesn't match";
1143 continue;
1144 }
1145
1146 // We have confirmed that message still points to the same message. This
1147 // means that the message didn't get swapped out from under us, so
1148 // starting_queue_index is correct.
1149 //
1150 // Either we got too far behind (signaled by this being a valid
1151 // message), or this is one of the initial messages which are invalid.
1152 if (starting_queue_index.valid()) {
1153 VLOG(3) << "Too old. Tried for " << std::hex << queue_index.index()
1154 << ", got " << starting_queue_index.index() << ", behind by "
1155 << std::dec
1156 << (starting_queue_index.index() - queue_index.index());
1157 return ReadResult::TOO_OLD;
1158 }
1159
1160 VLOG(3) << "Initial";
1161
1162 // There isn't a valid message at this location.
1163 //
1164 // If someone asks for one of the messages within the first go around,
1165 // then they need to wait. They got ahead. Otherwise, they are
1166 // asking for something crazy, like something before the beginning of
1167 // the queue. Tell them that they are behind.
1168 if (uint32_queue_index < memory_->queue_size()) {
1169 VLOG(3) << "Near zero, " << std::hex << uint32_queue_index;
1170 return ReadResult::NOTHING_NEW;
Austin Schuh20b2b082019-09-11 20:42:56 -07001171 } else {
Brian Silverman177567e2020-08-12 19:51:33 -07001172 VLOG(3) << "Not near zero, " << std::hex << uint32_queue_index;
1173 return ReadResult::TOO_OLD;
Austin Schuh20b2b082019-09-11 20:42:56 -07001174 }
1175 }
Alex Perrycb7da4b2019-08-28 19:35:56 -07001176 VLOG(3) << "Eq: " << std::hex << starting_queue_index.index() << ", "
1177 << queue_index.index();
Austin Schuh20b2b082019-09-11 20:42:56 -07001178 break;
1179 }
1180
Alex Perrycb7da4b2019-08-28 19:35:56 -07001181 // Then read the data out. Copy it all out to be deterministic and so we can
1182 // make length be from either end.
Austin Schuh20b2b082019-09-11 20:42:56 -07001183 *monotonic_sent_time = m->header.monotonic_sent_time;
1184 *realtime_sent_time = m->header.realtime_sent_time;
Austin Schuhad154822019-12-27 15:45:13 -08001185 if (m->header.remote_queue_index == 0xffffffffu) {
1186 *remote_queue_index = queue_index.index();
1187 } else {
1188 *remote_queue_index = m->header.remote_queue_index;
1189 }
1190 *monotonic_remote_time = m->header.monotonic_remote_time;
1191 *realtime_remote_time = m->header.realtime_remote_time;
Brian Silverman6b8a3c32020-03-06 11:26:14 -08001192 if (data) {
1193 memcpy(data, m->data(memory_->message_data_size()), message_data_size());
1194 }
Austin Schuh20b2b082019-09-11 20:42:56 -07001195 *length = m->header.length;
1196
1197 // And finally, confirm that the message *still* points to the queue index we
1198 // want. This means it didn't change out from under us.
1199 // If something changed out from under us, we were reading it much too late in
1200 // it's lifetime.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001201 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -07001202 const QueueIndex final_queue_index = m->header.queue_index.Load(queue_size);
1203 if (final_queue_index != queue_index) {
Alex Perrycb7da4b2019-08-28 19:35:56 -07001204 VLOG(3) << "Changed out from under us. Reading " << std::hex
1205 << queue_index.index() << ", finished with "
1206 << final_queue_index.index() << ", delta: " << std::dec
1207 << (final_queue_index.index() - queue_index.index());
1208 return ReadResult::OVERWROTE;
Austin Schuh20b2b082019-09-11 20:42:56 -07001209 }
1210
1211 return ReadResult::GOOD;
1212}
1213
Alex Perrycb7da4b2019-08-28 19:35:56 -07001214size_t LocklessQueue::queue_size() const { return memory_->queue_size(); }
1215size_t LocklessQueue::message_data_size() const {
1216 return memory_->message_data_size();
1217}
1218
1219QueueIndex LocklessQueue::LatestQueueIndex() {
Austin Schuh20b2b082019-09-11 20:42:56 -07001220 const size_t queue_size = memory_->queue_size();
1221
1222 // There is only one interesting case. We need to know if the queue is empty.
1223 // That is done with a sentinel value. At worst, this will be off by one.
1224 const QueueIndex next_queue_index =
1225 memory_->next_queue_index.Load(queue_size);
1226 if (next_queue_index.valid()) {
1227 const QueueIndex current_queue_index = next_queue_index.DecrementBy(1u);
Alex Perrycb7da4b2019-08-28 19:35:56 -07001228 return current_queue_index;
Austin Schuh20b2b082019-09-11 20:42:56 -07001229 } else {
1230 return empty_queue_index();
1231 }
1232}
1233
1234namespace {
1235
1236// Prints out the mutex state. Not safe to use while the mutex is being
1237// changed.
1238::std::string PrintMutex(aos_mutex *mutex) {
1239 ::std::stringstream s;
1240 s << "aos_mutex(" << ::std::hex << mutex->futex;
1241
1242 if (mutex->futex != 0) {
1243 s << ":";
1244 if (mutex->futex & FUTEX_OWNER_DIED) {
1245 s << "FUTEX_OWNER_DIED|";
1246 }
1247 s << "tid=" << (mutex->futex & FUTEX_TID_MASK);
1248 }
1249
1250 s << ")";
1251 return s.str();
1252}
1253
1254} // namespace
1255
1256void PrintLocklessQueueMemory(LocklessQueueMemory *memory) {
1257 const size_t queue_size = memory->queue_size();
1258 ::std::cout << "LocklessQueueMemory (" << memory << ") {" << ::std::endl;
1259 ::std::cout << " aos_mutex queue_setup_lock = "
1260 << PrintMutex(&memory->queue_setup_lock) << ::std::endl;
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001261 ::std::cout << " bool initialized = " << memory->initialized << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -07001262 ::std::cout << " config {" << ::std::endl;
1263 ::std::cout << " size_t num_watchers = " << memory->config.num_watchers
1264 << ::std::endl;
1265 ::std::cout << " size_t num_senders = " << memory->config.num_senders
1266 << ::std::endl;
Brian Silverman177567e2020-08-12 19:51:33 -07001267 ::std::cout << " size_t num_pinners = " << memory->config.num_pinners
1268 << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -07001269 ::std::cout << " size_t queue_size = " << memory->config.queue_size
1270 << ::std::endl;
1271 ::std::cout << " size_t message_data_size = "
1272 << memory->config.message_data_size << ::std::endl;
1273
1274 ::std::cout << " AtomicQueueIndex next_queue_index = "
1275 << memory->next_queue_index.Load(queue_size).DebugString()
1276 << ::std::endl;
1277
Austin Schuh3328d132020-02-28 13:54:57 -08001278 ::std::cout << " uid_t uid = " << memory->uid << ::std::endl;
1279
Austin Schuh20b2b082019-09-11 20:42:56 -07001280 ::std::cout << " }" << ::std::endl;
1281 ::std::cout << " AtomicIndex queue[" << queue_size << "] {" << ::std::endl;
1282 for (size_t i = 0; i < queue_size; ++i) {
1283 ::std::cout << " [" << i << "] -> "
1284 << memory->GetQueue(i)->Load().DebugString() << ::std::endl;
1285 }
1286 ::std::cout << " }" << ::std::endl;
1287 ::std::cout << " Message messages[" << memory->num_messages() << "] {"
1288 << ::std::endl;
1289 for (size_t i = 0; i < memory->num_messages(); ++i) {
1290 Message *m = memory->GetMessage(Index(i, i));
Brian Silverman001f24d2020-08-12 19:33:20 -07001291 ::std::cout << " [" << i << "] -> Message 0x" << std::hex
1292 << (reinterpret_cast<uintptr_t>(
1293 memory->GetMessage(Index(i, i))) -
1294 reinterpret_cast<uintptr_t>(memory))
1295 << std::dec << " {" << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -07001296 ::std::cout << " Header {" << ::std::endl;
1297 ::std::cout << " AtomicQueueIndex queue_index = "
1298 << m->header.queue_index.Load(queue_size).DebugString()
1299 << ::std::endl;
Brian Silverman001f24d2020-08-12 19:33:20 -07001300 ::std::cout << " monotonic_clock::time_point monotonic_sent_time = "
1301 << m->header.monotonic_sent_time << " 0x" << std::hex
1302 << m->header.monotonic_sent_time.time_since_epoch().count()
1303 << std::dec << ::std::endl;
1304 ::std::cout << " realtime_clock::time_point realtime_sent_time = "
1305 << m->header.realtime_sent_time << " 0x" << std::hex
1306 << m->header.realtime_sent_time.time_since_epoch().count()
1307 << std::dec << ::std::endl;
1308 ::std::cout
1309 << " monotonic_clock::time_point monotonic_remote_time = "
1310 << m->header.monotonic_remote_time << " 0x" << std::hex
1311 << m->header.monotonic_remote_time.time_since_epoch().count()
1312 << std::dec << ::std::endl;
1313 ::std::cout << " realtime_clock::time_point realtime_remote_time = "
1314 << m->header.realtime_remote_time << " 0x" << std::hex
1315 << m->header.realtime_remote_time.time_since_epoch().count()
1316 << std::dec << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -07001317 ::std::cout << " size_t length = " << m->header.length
1318 << ::std::endl;
1319 ::std::cout << " }" << ::std::endl;
1320 ::std::cout << " data: {";
1321
Brian Silverman001f24d2020-08-12 19:33:20 -07001322 if (FLAGS_dump_lockless_queue_data) {
1323 const char *const m_data = m->data(memory->message_data_size());
1324 for (size_t j = 0; j < m->header.length; ++j) {
1325 char data = m_data[j];
1326 if (j != 0) {
1327 ::std::cout << " ";
1328 }
1329 if (::std::isprint(data)) {
1330 ::std::cout << ::std::setfill(' ') << ::std::setw(2) << ::std::hex
1331 << data;
1332 } else {
1333 ::std::cout << "0x" << ::std::setfill('0') << ::std::setw(2)
1334 << ::std::hex << (static_cast<unsigned>(data) & 0xff);
1335 }
Austin Schuh20b2b082019-09-11 20:42:56 -07001336 }
1337 }
1338 ::std::cout << ::std::setfill(' ') << ::std::dec << "}" << ::std::endl;
1339 ::std::cout << " }," << ::std::endl;
1340 }
1341 ::std::cout << " }" << ::std::endl;
1342
Alex Perrycb7da4b2019-08-28 19:35:56 -07001343 ::std::cout << " Sender senders[" << memory->num_senders() << "] {"
1344 << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -07001345 for (size_t i = 0; i < memory->num_senders(); ++i) {
1346 Sender *s = memory->GetSender(i);
1347 ::std::cout << " [" << i << "] -> Sender {" << ::std::endl;
1348 ::std::cout << " aos_mutex tid = " << PrintMutex(&s->tid)
1349 << ::std::endl;
1350 ::std::cout << " AtomicIndex scratch_index = "
1351 << s->scratch_index.Load().DebugString() << ::std::endl;
1352 ::std::cout << " AtomicIndex to_replace = "
1353 << s->to_replace.Load().DebugString() << ::std::endl;
1354 ::std::cout << " }" << ::std::endl;
1355 }
1356 ::std::cout << " }" << ::std::endl;
1357
Brian Silverman177567e2020-08-12 19:51:33 -07001358 ::std::cout << " Pinner pinners[" << memory->num_pinners() << "] {"
1359 << ::std::endl;
1360 for (size_t i = 0; i < memory->num_pinners(); ++i) {
1361 Pinner *p = memory->GetPinner(i);
1362 ::std::cout << " [" << i << "] -> Pinner {" << ::std::endl;
1363 ::std::cout << " aos_mutex tid = " << PrintMutex(&p->tid)
1364 << ::std::endl;
1365 ::std::cout << " AtomicIndex scratch_index = "
1366 << p->scratch_index.Load().DebugString() << ::std::endl;
1367 ::std::cout << " AtomicIndex pinned = "
1368 << p->pinned.Load(memory->queue_size()).DebugString()
1369 << ::std::endl;
1370 ::std::cout << " }" << ::std::endl;
1371 }
1372 ::std::cout << " }" << ::std::endl;
1373
Austin Schuh20b2b082019-09-11 20:42:56 -07001374 ::std::cout << " Watcher watchers[" << memory->num_watchers() << "] {"
1375 << ::std::endl;
1376 for (size_t i = 0; i < memory->num_watchers(); ++i) {
1377 Watcher *w = memory->GetWatcher(i);
1378 ::std::cout << " [" << i << "] -> Watcher {" << ::std::endl;
1379 ::std::cout << " aos_mutex tid = " << PrintMutex(&w->tid)
1380 << ::std::endl;
1381 ::std::cout << " pid_t pid = " << w->pid << ::std::endl;
1382 ::std::cout << " int priority = " << w->priority << ::std::endl;
1383 ::std::cout << " }" << ::std::endl;
1384 }
1385 ::std::cout << " }" << ::std::endl;
1386
1387 ::std::cout << "}" << ::std::endl;
1388}
1389
1390} // namespace ipc_lib
1391} // namespace aos