blob: fb9c0b65afbb463f23ac76574e18d1ced01f4d3e [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
12#include "aos/init.h"
13#include "aos/ipc_lib/lockless_queue_memory.h"
14#include "aos/logging/logging.h"
15#include "aos/util/compiler_memory_barrier.h"
16
17namespace aos {
18namespace ipc_lib {
19
20namespace {
21
22constexpr bool kDebug = false;
23
24void GrabQueueSetupLockOrDie(LocklessQueueMemory *memory) {
25 const int result = mutex_grab(&(memory->queue_setup_lock));
26 CHECK(result == 0 || result == 1);
27}
28
29// This must be called under the queue_setup_lock.
30void Cleanup(LocklessQueueMemory *memory) {
31 const size_t num_senders = memory->num_senders();
32 const size_t queue_size = memory->queue_size();
33 const size_t num_messages = memory->num_messages();
34
35 // There are a large number of crazy cases here for how things can go wrong
36 // and how we have to recover. They either require us to keep extra track of
37 // what is going on, slowing down the send path, or require a large number of
38 // cases.
39 //
40 // The solution here is to not over-think it. This is running while not real
41 // time during construction. It is allowed to be slow. It will also very
42 // rarely trigger. There is a small uS window where process death is
43 // ambiguous.
44 //
45 // So, build up a list N long, where N is the number of messages. Search
46 // through the entire queue and the sender list (ignoring any dead senders),
47 // and mark down which ones we have seen. Once we have seen all the messages
48 // except the N dead senders, we know which messages are dead. Because the
49 // queue is active while we do this, it may take a couple of go arounds to see
50 // everything.
51
52 // Do the easy case. Find all senders who have died. See if they are either
53 // consistent already, or if they have copied over to_replace to the scratch
54 // index, but haven't cleared to_replace. Count them.
55 size_t valid_senders = 0;
56 for (size_t i = 0; i < num_senders; ++i) {
57 Sender *sender = memory->GetSender(i);
58 const uint32_t tid =
59 __atomic_load_n(&(sender->tid.futex), __ATOMIC_RELAXED);
60 if (tid & FUTEX_OWNER_DIED) {
61 if (kDebug) {
62 printf("Found an easy death for sender %zu\n", i);
63 }
64 const Index to_replace = sender->to_replace.RelaxedLoad();
65 const Index scratch_index = sender->scratch_index.Load();
66
67 // I find it easiest to think about this in terms of the set of observable
68 // states. The main code follows the following states:
69
70 // 1) scratch_index = xxx
71 // to_replace = invalid
72 // This is unambiguous. Already good.
73
74 // 2) scratch_index = xxx
75 // to_replace = yyy
76 // Very ambiguous. Is xxx or yyy the correct one? Need to either roll
77 // this forwards or backwards.
78
79 // 3) scratch_index = yyy
80 // to_replace = yyy
81 // We are in the act of moving to_replace to scratch_index, but didn't
82 // finish. Easy.
83
84 // 4) scratch_index = yyy
85 // to_replace = invalid
86 // Finished, but died. Looks like 1)
87
88 // Any cleanup code needs to follow the same set of states to be robust to
89 // death, so death can be restarted.
90
91 // Could be 2) or 3).
92 if (to_replace.valid()) {
93 // 3)
94 if (to_replace == scratch_index) {
95 // Just need to invalidate to_replace to finish.
96 sender->to_replace.Invalidate();
97
98 // And mark that we succeeded.
99 __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_SEQ_CST);
100 ++valid_senders;
101 }
102 } else {
103 // 1) or 4). Make sure we aren't corrupted and declare victory.
104 CHECK(scratch_index.valid());
105
106 __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_SEQ_CST);
107 ++valid_senders;
108 }
109 } else {
110 // Not dead.
111 ++valid_senders;
112 }
113 }
114
115 // If all the senders are (or were made) good, there is no need to do the hard
116 // case.
117 if (valid_senders == num_senders) {
118 return;
119 }
120
121 if (kDebug) {
122 printf("Starting hard cleanup\n");
123 }
124
125 size_t num_accounted_for = 0;
126 size_t num_missing = 0;
127 ::std::vector<bool> accounted_for(num_messages, false);
128
129 while ((num_accounted_for + num_missing) != num_messages) {
130 num_missing = 0;
131 for (size_t i = 0; i < num_senders; ++i) {
132 Sender *sender = memory->GetSender(i);
133 const uint32_t tid =
134 __atomic_load_n(&(sender->tid.futex), __ATOMIC_RELAXED);
135 if (tid & FUTEX_OWNER_DIED) {
136 ++num_missing;
137 } else {
138 const Index scratch_index = sender->scratch_index.RelaxedLoad();
139 if (!accounted_for[scratch_index.message_index()]) {
140 ++num_accounted_for;
141 }
142 accounted_for[scratch_index.message_index()] = true;
143 }
144 }
145
146 for (size_t i = 0; i < queue_size; ++i) {
147 const Index index = memory->GetQueue(i)->RelaxedLoad();
148 if (!accounted_for[index.message_index()]) {
149 ++num_accounted_for;
150 }
151 accounted_for[index.message_index()] = true;
152 }
153 }
154
155 while (num_missing != 0) {
156 const size_t starting_num_missing = num_missing;
157 for (size_t i = 0; i < num_senders; ++i) {
158 Sender *sender = memory->GetSender(i);
159 const uint32_t tid =
160 __atomic_load_n(&(sender->tid.futex), __ATOMIC_RELAXED);
161 if (tid & FUTEX_OWNER_DIED) {
162 const Index scratch_index = sender->scratch_index.RelaxedLoad();
163 const Index to_replace = sender->to_replace.RelaxedLoad();
164
165 // Candidate.
166 CHECK_LE(to_replace.message_index(), accounted_for.size());
167 if (accounted_for[to_replace.message_index()]) {
168 if (kDebug) {
169 printf("Sender %zu died, to_replace is already accounted for\n", i);
170 }
171 // If both are accounted for, we are corrupt...
172 CHECK(!accounted_for[scratch_index.message_index()]);
173
174 // to_replace is already accounted for. This means that we didn't
175 // atomically insert scratch_index into the queue yet. So
176 // invalidate to_replace.
177 sender->to_replace.Invalidate();
178
179 // And then mark this sender clean.
180 __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_SEQ_CST);
181
182 // And account for scratch_index.
183 accounted_for[scratch_index.message_index()] = true;
184 --num_missing;
185 ++num_accounted_for;
186 } else if (accounted_for[scratch_index.message_index()]) {
187 if (kDebug) {
188 printf("Sender %zu died, scratch_index is already accounted for\n", i);
189 }
190 // scratch_index is accounted for. That means we did the insert,
191 // but didn't record it.
192 CHECK(to_replace.valid());
193 // Finish the transaction. Copy to_replace, then clear it.
194
195 sender->scratch_index.Store(to_replace);
196 sender->to_replace.Invalidate();
197
198 // And then mark this sender clean.
199 __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_SEQ_CST);
200
201 // And account for to_replace.
202 accounted_for[to_replace.message_index()] = true;
203 --num_missing;
204 ++num_accounted_for;
205 } else {
206 if (kDebug) {
207 printf("Sender %zu died, neither is accounted for\n", i);
208 }
209 // Ambiguous. There will be an unambiguous one somewhere that we
210 // can do first.
211 }
212 }
213 }
214 // CHECK that we are making progress.
215 CHECK_NE(num_missing, starting_num_missing);
216 }
217}
218
219// Exposes rt_tgsigqueueinfo so we can send the signal *just* to the target
220// thread.
221int rt_tgsigqueueinfo(pid_t tgid, pid_t tid, int sig, siginfo_t *si) {
222 return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, si);
223}
224
225} // namespace
226
227size_t LocklessQueueMemorySize(LocklessQueueConfiguration config) {
228 // Round up the message size so following data is double aligned. That should
229 // be overkill for most platforms. And the checks below confirms it.
230 config.message_data_size = (config.message_data_size + 7) & ~0x7;
231
232 // As we build up the size, confirm that everything is aligned to the
233 // alignment requirements of the type.
234 size_t size = sizeof(LocklessQueueMemory);
235 CHECK_EQ(size & (alignof(LocklessQueueMemory) - 1), 0u);
236
237 CHECK_EQ(size & (alignof(AtomicIndex) - 1), 0u);
238 size += LocklessQueueMemory::SizeOfQueue(config);
239
240 CHECK_EQ(size & (alignof(Message) - 1), 0u);
241 size += LocklessQueueMemory::SizeOfMessages(config);
242
243 CHECK_EQ(size & (alignof(Watcher) - 1), 0u);
244 size += LocklessQueueMemory::SizeOfWatchers(config);
245
246 CHECK_EQ(size & (alignof(Sender) - 1), 0u);
247 size += LocklessQueueMemory::SizeOfSenders(config);
248
249 return size;
250}
251
252LocklessQueueMemory *InitializeLocklessQueueMemory(
253 LocklessQueueMemory *memory, LocklessQueueConfiguration config) {
254 // Everything should be zero initialized already. So we just need to fill
255 // everything out properly.
256
257 // Grab the mutex. We don't care if the previous reader died. We are going
258 // to check everything anyways.
259 GrabQueueSetupLockOrDie(memory);
260
261 if (!memory->initialized) {
262 // TODO(austin): Check these for out of bounds.
263 memory->config.num_watchers = config.num_watchers;
264 memory->config.num_senders = config.num_senders;
265 memory->config.queue_size = config.queue_size;
266 // Round up to the nearest double word bytes.
267 memory->config.message_data_size = (config.message_data_size + 7) & ~0x7;
268
269 const size_t num_messages = memory->num_messages();
270 // There need to be at most MaxMessages() messages allocated.
271 CHECK_LE(num_messages, Index::MaxMessages());
272
273 for (size_t i = 0; i < num_messages; ++i) {
274 memory->GetMessage(Index(QueueIndex::Zero(memory->queue_size()), i))
275 ->header.queue_index.Invalidate();
276 }
277
278 for (size_t i = 0; i < memory->queue_size(); ++i) {
279 // Make the initial counter be the furthest away number. That means that
280 // index 0 should be 0xffff, 1 should be 0, etc.
281 memory->GetQueue(i)->Store(Index(QueueIndex::Zero(memory->queue_size())
282 .IncrementBy(i)
283 .DecrementBy(memory->queue_size()),
284 i));
285 }
286
287 memory->next_queue_index.Invalidate();
288
289 for (size_t i = 0; i < memory->num_senders(); ++i) {
290 ::aos::ipc_lib::Sender *s = memory->GetSender(i);
291 s->scratch_index.Store(Index(0xffff, i + memory->queue_size()));
292 s->to_replace.RelaxedInvalidate();
293 }
294
295 // Signal everything is done. This needs to be done last, so if we die, we
296 // redo initialization.
297 // This is a full atomic (probably overkill), but this is at initialization
298 // time, so it is cheap.
299 memory->initialized.store(true);
300 }
301
302 mutex_unlock(&(memory->queue_setup_lock));
303 return memory;
304}
305
306LocklessQueue::LocklessQueue(LocklessQueueMemory *memory,
307 LocklessQueueConfiguration config)
308 : memory_(InitializeLocklessQueueMemory(memory, config)),
309 watcher_copy_(memory_->num_watchers()),
310 pid_(getpid()),
311 uid_(getuid()) {}
312
313LocklessQueue::~LocklessQueue() {
314 CHECK_EQ(watcher_index_, -1);
315
316 GrabQueueSetupLockOrDie(memory_);
317 const int num_watchers = memory_->num_watchers();
318 // Cleanup is cheap. Go for it anyways.
319
320 // And confirm that nothing is owned by us.
321 for (int i = 0; i < num_watchers; ++i) {
322 CHECK(!mutex_islocked(&(memory_->GetWatcher(i)->tid)));
323 }
324 mutex_unlock(&(memory_->queue_setup_lock));
325}
326
327size_t LocklessQueue::QueueSize() const { return memory_->queue_size(); }
328
329bool LocklessQueue::RegisterWakeup(int priority) {
330 // TODO(austin): Make sure signal coalescing is turned on. We don't need
331 // duplicates. That will improve performance under high load.
332
333 // Since everything is self consistent, all we need to do is make sure nobody
334 // else is running. Someone dying will get caught in the generic consistency
335 // check.
336 GrabQueueSetupLockOrDie(memory_);
337 const int num_watchers = memory_->num_watchers();
338
339 // Now, find the first empty watcher and grab it.
340 CHECK_EQ(watcher_index_, -1);
341 for (int i = 0; i < num_watchers; ++i) {
342 const uint32_t tid =
343 __atomic_load_n(&(memory_->GetWatcher(i)->tid.futex), __ATOMIC_RELAXED);
344 if (tid == 0 || tid & FUTEX_OWNER_DIED) {
345 watcher_index_ = i;
346 break;
347 }
348 }
349
350 // Bail if we failed to find an open slot.
351 if (watcher_index_ == -1) {
352 mutex_unlock(&(memory_->queue_setup_lock));
353 return false;
354 }
355
356 Watcher *w = memory_->GetWatcher(watcher_index_);
357
358 w->pid = getpid();
359 w->priority = priority;
360
361 // Grabbing a mutex is a compiler and memory barrier, so nothing before will
362 // get rearranged afterwords.
363 //
364 // Since everything is done under the queue_setup_lock, this should always
365 // return immediately.
366 const int result = mutex_grab(&(w->tid));
367
368 mutex_unlock(&(memory_->queue_setup_lock));
369
370 // We should either get the lock, or the previous owner should have died.
371 // Anything else is a pretty serious error.
372 return result == 0 || result == 1;
373}
374
375void LocklessQueue::UnregisterWakeup() {
376 // Since everything is self consistent, all we need to do is make sure nobody
377 // else is running. Someone dying will get caught in the generic consistency
378 // check.
379 GrabQueueSetupLockOrDie(memory_);
380
381 // Make sure we are registered.
382 CHECK_NE(watcher_index_, -1);
383
384 // Make sure we still own the slot we are supposed to.
385 CHECK(mutex_islocked(&(memory_->GetWatcher(watcher_index_)->tid)));
386
387 // The act of unlocking invalidates the entry. Invalidate it.
388 mutex_unlock(&(memory_->GetWatcher(watcher_index_)->tid));
389 // And internally forget the slot.
390 watcher_index_ = -1;
391
392 mutex_unlock(&(memory_->queue_setup_lock));
393}
394
395int LocklessQueue::Wakeup(const int current_priority) {
396 const size_t num_watchers = memory_->num_watchers();
397
398 CHECK_EQ(watcher_copy_.size(), num_watchers);
399
400 // Grab a copy so it won't change out from underneath us, and we can sort it
401 // nicely in C++.
402 // Do note that there is still a window where the process can die *after* we
403 // read everything. We will still PI boost and send a signal to the thread in
404 // question. There is no way without pidfd's to close this window, and
405 // creating a pidfd is likely not RT.
406 for (size_t i = 0; i < num_watchers; ++i) {
407 Watcher *w = memory_->GetWatcher(i);
408 // Start by reading the tid. This needs to be atomic to force it to come first.
409 watcher_copy_[i].tid = __atomic_load_n(&(w->tid.futex), __ATOMIC_SEQ_CST);
410 watcher_copy_[i].pid = w->pid;
411 watcher_copy_[i].priority = w->priority;
412
413 // Use a priority of -1 to mean an invalid entry to make sorting easier.
414 if (watcher_copy_[i].tid & FUTEX_OWNER_DIED || watcher_copy_[i].tid == 0) {
415 watcher_copy_[i].priority = -1;
416 } else if (watcher_copy_[i].tid !=
417 static_cast<pid_t>(
418 __atomic_load_n(&(w->tid.futex), __ATOMIC_SEQ_CST))) {
419 // Confirm that the watcher hasn't been re-used and modified while we read
420 // it. If it has, mark it invalid again.
421 watcher_copy_[i].priority = -1;
422 watcher_copy_[i].tid = 0;
423 }
424 }
425
426 // Now sort.
427 ::std::sort(watcher_copy_.begin(), watcher_copy_.end(),
428 [](const WatcherCopy &a, const WatcherCopy &b) {
429 return a.priority > b.priority;
430 });
431
432 int count = 0;
433 if (watcher_copy_[0].priority != -1) {
434 const int max_priority =
435 ::std::max(current_priority, watcher_copy_[0].priority);
436 // Boost if we are RT and there is a higher priority sender out there.
437 // Otherwise we might run into priority inversions.
438 if (max_priority > current_priority && current_priority > 0) {
439 SetCurrentThreadRealtimePriority(max_priority);
440 }
441
442 // Build up the siginfo to send.
443 siginfo_t uinfo;
444 memset(&uinfo, 0, sizeof(uinfo));
445
446 uinfo.si_code = SI_QUEUE;
447 uinfo.si_pid = pid_;
448 uinfo.si_uid = uid_;
449 uinfo.si_value.sival_int = 0;
450
451 for (const WatcherCopy &watcher_copy : watcher_copy_) {
452 // The first -1 priority means we are at the end of the valid list.
453 if (watcher_copy.priority == -1) {
454 break;
455 }
456
457 // Send the signal. Target just the thread that sent it so that we can
458 // support multiple watchers in a process (when someone creates multiple
459 // event loops in different threads).
460 rt_tgsigqueueinfo(watcher_copy.pid, watcher_copy.tid, kWakeupSignal,
461 &uinfo);
462
463 ++count;
464 }
465
466 // Drop back down if we were boosted.
467 if (max_priority > current_priority && current_priority > 0) {
468 SetCurrentThreadRealtimePriority(current_priority);
469 }
470 }
471
472 return count;
473}
474
475LocklessQueue::Sender::Sender(LocklessQueueMemory *memory) : memory_(memory) {
476 GrabQueueSetupLockOrDie(memory_);
477
478 // Since we already have the lock, go ahead and try cleaning up.
479 Cleanup(memory_);
480
481 const int num_senders = memory_->num_senders();
482
483 for (int i = 0; i < num_senders; ++i) {
484 ::aos::ipc_lib::Sender *s = memory->GetSender(i);
485 const uint32_t tid = __atomic_load_n(&(s->tid.futex), __ATOMIC_RELAXED);
486 if (tid == 0) {
487 sender_index_ = i;
488 break;
489 }
490 }
491
492 if (sender_index_ == -1) {
493 LOG(FATAL, "Too many senders\n");
494 }
495
496 ::aos::ipc_lib::Sender *s = memory_->GetSender(sender_index_);
497
498 // Atomically grab the mutex. This signals that we are alive. If the
499 // previous owner died, we don't care, and want to grab the mutex anyways.
500 const int result = mutex_grab(&(s->tid));
501 CHECK(result == 0 || result == 1);
502
503 mutex_unlock(&(memory->queue_setup_lock));
504}
505
506LocklessQueue::Sender::~Sender() {
507 if (memory_ != nullptr) {
508 mutex_unlock(&(memory_->GetSender(sender_index_)->tid));
509 }
510}
511
512LocklessQueue::Sender LocklessQueue::MakeSender() {
513 return LocklessQueue::Sender(memory_);
514}
515
516QueueIndex ZeroOrValid(QueueIndex index) {
517 if (!index.valid()) {
518 return index.Clear();
519 }
520 return index;
521}
522
523void LocklessQueue::Sender::Send(const char *data, size_t length) {
524 const size_t queue_size = memory_->queue_size();
525 CHECK_LE(length, memory_->message_data_size());
526
527 ::aos::ipc_lib::Sender *sender = memory_->GetSender(sender_index_);
528 Index scratch_index = sender->scratch_index.RelaxedLoad();
529 Message *message = memory_->GetMessage(scratch_index);
530
531 message->header.queue_index.Invalidate();
532
533 message->header.length = length;
534 memcpy(&message->data[0], data, length);
535
536 while (true) {
537 const QueueIndex actual_next_queue_index =
538 memory_->next_queue_index.Load(queue_size);
539 const QueueIndex next_queue_index = ZeroOrValid(actual_next_queue_index);
540
541 const QueueIndex incremented_queue_index = next_queue_index.Increment();
542
543 // TODO(austin): I think we can drop the barrier off this.
544 const Index to_replace = memory_->LoadIndex(next_queue_index);
545
546 const QueueIndex decremented_queue_index =
547 next_queue_index.DecrementBy(queue_size);
548
549 // See if we got beat. If we did, try to atomically update
550 // next_queue_index in case the previous writer failed and retry.
551 if (!to_replace.IsPlausible(decremented_queue_index)) {
552 // We don't care about the result. It will either succeed, or we got
553 // beat in fixing it and just need to give up and try again. If we got
554 // beat multiple times, the only way progress can be made is if the queue
555 // is updated as well. This means that if we retry reading
556 // next_queue_index, we will be at most off by one and can retry.
557 //
558 // Both require no further action from us.
559 //
560 // TODO(austin): If we are having fairness issues under contention, we
561 // could have a mode bit in next_queue_index, and could use a lock or some
562 // other form of PI boosting to let the higher priority task win.
563 memory_->next_queue_index.CompareAndExchangeStrong(
564 actual_next_queue_index, incremented_queue_index);
565
566 if (kDebug) {
567 printf("We were beat. Try again. Was %x, is %x\n", to_replace.get(),
568 decremented_queue_index.index());
569 }
570 continue;
571 }
572
573 // Confirm that the message is what it should be.
574 {
575 // We just need this to be atomic and after the index has been calculated
576 // and before we exchange the index back in. Both of those will be strong
577 // barriers, so this is fine.
578 const QueueIndex previous_index =
579 memory_->GetMessage(to_replace)
580 ->header.queue_index.RelaxedLoad(queue_size);
581 if (previous_index != decremented_queue_index && previous_index.valid()) {
582 // Retry.
583 if (kDebug) {
584 printf(
585 "Something fishy happened, queue index doesn't match. Retrying. "
586 " Previous index was %x, should be %x\n",
587 previous_index.index(), decremented_queue_index.index());
588 }
589 continue;
590 }
591 }
592
593 message->header.monotonic_sent_time = ::aos::monotonic_clock::now();
594 message->header.realtime_sent_time = ::aos::realtime_clock::now();
595
596 // Before we are fully done filling out the message, update the Sender state
597 // with the new index to write. This re-uses the barrier for the
598 // queue_index store.
599 const Index index_to_write(next_queue_index,
600 scratch_index.message_index());
601
602 sender->scratch_index.RelaxedStore(index_to_write);
603
604 message->header.queue_index.Store(next_queue_index);
605
606 // The message is now filled out, and we have a confirmed slot to store
607 // into.
608 //
609 // Start by writing down what we are going to pull out of the queue. This
610 // was Invalid before now.
611 sender->to_replace.RelaxedStore(to_replace);
612
613 // Then exchange the next index into the queue.
614 if (!memory_->GetQueue(next_queue_index.Wrapped())
615 ->CompareAndExchangeStrong(to_replace, index_to_write)) {
616 // Aw, didn't succeed. Retry.
617 sender->to_replace.RelaxedInvalidate();
618 if (kDebug) {
619 printf("Failed to wrap into queue\n");
620 }
621 continue;
622 }
623
624 // Then update next_queue_index to save the next user some computation time.
625 memory_->next_queue_index.CompareAndExchangeStrong(actual_next_queue_index,
626 incremented_queue_index);
627
628 // Now update the scratch space and record that we succeeded.
629 sender->scratch_index.Store(to_replace);
630 // And then clear out the entry used to replace. This just needs to be
631 // atomic. It can't be moved above the store because that is a full
632 // barrier, but delaying it until later will only affect things if something
633 // died.
634 sender->to_replace.RelaxedInvalidate();
635 break;
636 }
637}
638
639LocklessQueue::ReadResult LocklessQueue::Read(
640 uint32_t uint32_queue_index,
641 ::aos::monotonic_clock::time_point *monotonic_sent_time,
642 ::aos::realtime_clock::time_point *realtime_sent_time, size_t *length,
643 char *data) {
644 const size_t queue_size = memory_->queue_size();
645
646 // Build up the QueueIndex.
647 const QueueIndex queue_index =
648 QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index);
649
650 // Read the message stored at the requested location.
651 Index mi = memory_->LoadIndex(queue_index);
652 Message *m = memory_->GetMessage(mi);
653
654 while (true) {
655 // We need to confirm that the data doesn't change while we are reading it.
656 // Do that by first confirming that the message points to the queue index we
657 // want.
658 const QueueIndex starting_queue_index =
659 m->header.queue_index.Load(queue_size);
660 if (starting_queue_index != queue_index) {
661 // If we found a message that is exactly 1 loop old, we just wrapped.
662 if (starting_queue_index == queue_index.DecrementBy(queue_size)) {
663 if (kDebug) {
664 printf("Matches: %x, %x\n", starting_queue_index.index(),
665 queue_index.DecrementBy(queue_size).index());
666 }
667 return ReadResult::NOTHING_NEW;
668 } else {
669 // Someone has re-used this message between when we pulled it out of the
670 // queue and when we grabbed its index. It is pretty hard to deduce
671 // what happened. Just try again.
672 Message *new_m = memory_->GetMessage(queue_index);
673 if (m != new_m) {
674 m = new_m;
675 if (kDebug) {
676 printf("Retrying, m doesn't match\n");
677 }
678 continue;
679 }
680
681 // We have confirmed that message still points to the same message. This
682 // means that the message didn't get swapped out from under us, so
683 // starting_queue_index is correct.
684 //
685 // Either we got too far behind (signaled by this being a valid
686 // message), or this is one of the initial messages which are invalid.
687 if (starting_queue_index.valid()) {
688 if (kDebug) {
689 printf("Too old. Tried for %x, got %x, behind by %d\n",
690 queue_index.index(), starting_queue_index.index(),
691 starting_queue_index.index() - queue_index.index());
692 }
693 return ReadResult::TOO_OLD;
694 }
695
696 if (kDebug) {
697 printf("Initial\n");
698 }
699
700 // There isn't a valid message at this location.
701 //
702 // If someone asks for one of the messages within the first go around,
703 // then they need to wait. They got ahead. Otherwise, they are
704 // asking for something crazy, like something before the beginning of
705 // the queue. Tell them that they are behind.
706 if (uint32_queue_index < memory_->queue_size()) {
707 if (kDebug) {
708 printf("Near zero, %x\n", uint32_queue_index);
709 }
710 return ReadResult::NOTHING_NEW;
711 } else {
712 if (kDebug) {
713 printf("not near zero, %x\n", uint32_queue_index);
714 }
715 return ReadResult::TOO_OLD;
716 }
717 }
718 }
719 if (kDebug) {
720 printf("Eq: %x, %x\n", starting_queue_index.index(), queue_index.index());
721 }
722 break;
723 }
724
725 // Then read the data out.
726 *monotonic_sent_time = m->header.monotonic_sent_time;
727 *realtime_sent_time = m->header.realtime_sent_time;
728 memcpy(data, &m->data[0], m->header.length);
729 *length = m->header.length;
730
731 // And finally, confirm that the message *still* points to the queue index we
732 // want. This means it didn't change out from under us.
733 // If something changed out from under us, we were reading it much too late in
734 // it's lifetime.
735 const QueueIndex final_queue_index = m->header.queue_index.Load(queue_size);
736 if (final_queue_index != queue_index) {
737 if (kDebug) {
738 printf(
739 "Changed out from under us. Reading %x, finished with %x, delta: "
740 "%d\n",
741 queue_index.index(), final_queue_index.index(),
742 final_queue_index.index() - queue_index.index());
743 }
744 return ReadResult::TOO_OLD;
745 }
746
747 return ReadResult::GOOD;
748}
749
750uint32_t LocklessQueue::LatestQueueIndex() {
751 const size_t queue_size = memory_->queue_size();
752
753 // There is only one interesting case. We need to know if the queue is empty.
754 // That is done with a sentinel value. At worst, this will be off by one.
755 const QueueIndex next_queue_index =
756 memory_->next_queue_index.Load(queue_size);
757 if (next_queue_index.valid()) {
758 const QueueIndex current_queue_index = next_queue_index.DecrementBy(1u);
759 return current_queue_index.index();
760 } else {
761 return empty_queue_index();
762 }
763}
764
765namespace {
766
767// Prints out the mutex state. Not safe to use while the mutex is being
768// changed.
769::std::string PrintMutex(aos_mutex *mutex) {
770 ::std::stringstream s;
771 s << "aos_mutex(" << ::std::hex << mutex->futex;
772
773 if (mutex->futex != 0) {
774 s << ":";
775 if (mutex->futex & FUTEX_OWNER_DIED) {
776 s << "FUTEX_OWNER_DIED|";
777 }
778 s << "tid=" << (mutex->futex & FUTEX_TID_MASK);
779 }
780
781 s << ")";
782 return s.str();
783}
784
785} // namespace
786
787void PrintLocklessQueueMemory(LocklessQueueMemory *memory) {
788 const size_t queue_size = memory->queue_size();
789 ::std::cout << "LocklessQueueMemory (" << memory << ") {" << ::std::endl;
790 ::std::cout << " aos_mutex queue_setup_lock = "
791 << PrintMutex(&memory->queue_setup_lock) << ::std::endl;
792 ::std::cout << " ::std::atomic<bool> initialized = " << memory->initialized
793 << ::std::endl;
794 ::std::cout << " config {" << ::std::endl;
795 ::std::cout << " size_t num_watchers = " << memory->config.num_watchers
796 << ::std::endl;
797 ::std::cout << " size_t num_senders = " << memory->config.num_senders
798 << ::std::endl;
799 ::std::cout << " size_t queue_size = " << memory->config.queue_size
800 << ::std::endl;
801 ::std::cout << " size_t message_data_size = "
802 << memory->config.message_data_size << ::std::endl;
803
804 ::std::cout << " AtomicQueueIndex next_queue_index = "
805 << memory->next_queue_index.Load(queue_size).DebugString()
806 << ::std::endl;
807
808 ::std::cout << " }" << ::std::endl;
809 ::std::cout << " AtomicIndex queue[" << queue_size << "] {" << ::std::endl;
810 for (size_t i = 0; i < queue_size; ++i) {
811 ::std::cout << " [" << i << "] -> "
812 << memory->GetQueue(i)->Load().DebugString() << ::std::endl;
813 }
814 ::std::cout << " }" << ::std::endl;
815 ::std::cout << " Message messages[" << memory->num_messages() << "] {"
816 << ::std::endl;
817 for (size_t i = 0; i < memory->num_messages(); ++i) {
818 Message *m = memory->GetMessage(Index(i, i));
819 ::std::cout << " [" << i << "] -> Message {" << ::std::endl;
820 ::std::cout << " Header {" << ::std::endl;
821 ::std::cout << " AtomicQueueIndex queue_index = "
822 << m->header.queue_index.Load(queue_size).DebugString()
823 << ::std::endl;
824 ::std::cout << " size_t length = " << m->header.length
825 << ::std::endl;
826 ::std::cout << " }" << ::std::endl;
827 ::std::cout << " data: {";
828
829 for (size_t j = 0; j < m->header.length; ++j) {
830 char data = m->data[j];
831 if (j != 0) {
832 ::std::cout << " ";
833 }
834 if (::std::isprint(data)) {
835 ::std::cout << ::std::setfill(' ') << ::std::setw(2) << ::std::hex
836 << data;
837 } else {
838 ::std::cout << "0x" << ::std::setfill('0') << ::std::setw(2)
839 << ::std::hex << (static_cast<unsigned>(data) & 0xff);
840 }
841 }
842 ::std::cout << ::std::setfill(' ') << ::std::dec << "}" << ::std::endl;
843 ::std::cout << " }," << ::std::endl;
844 }
845 ::std::cout << " }" << ::std::endl;
846
847 ::std::cout << " Sender senders[" << memory->num_senders() << "] {" << ::std::endl;
848 for (size_t i = 0; i < memory->num_senders(); ++i) {
849 Sender *s = memory->GetSender(i);
850 ::std::cout << " [" << i << "] -> Sender {" << ::std::endl;
851 ::std::cout << " aos_mutex tid = " << PrintMutex(&s->tid)
852 << ::std::endl;
853 ::std::cout << " AtomicIndex scratch_index = "
854 << s->scratch_index.Load().DebugString() << ::std::endl;
855 ::std::cout << " AtomicIndex to_replace = "
856 << s->to_replace.Load().DebugString() << ::std::endl;
857 ::std::cout << " }" << ::std::endl;
858 }
859 ::std::cout << " }" << ::std::endl;
860
861 ::std::cout << " Watcher watchers[" << memory->num_watchers() << "] {"
862 << ::std::endl;
863 for (size_t i = 0; i < memory->num_watchers(); ++i) {
864 Watcher *w = memory->GetWatcher(i);
865 ::std::cout << " [" << i << "] -> Watcher {" << ::std::endl;
866 ::std::cout << " aos_mutex tid = " << PrintMutex(&w->tid)
867 << ::std::endl;
868 ::std::cout << " pid_t pid = " << w->pid << ::std::endl;
869 ::std::cout << " int priority = " << w->priority << ::std::endl;
870 ::std::cout << " }" << ::std::endl;
871 }
872 ::std::cout << " }" << ::std::endl;
873
874 ::std::cout << "}" << ::std::endl;
875}
876
877} // namespace ipc_lib
878} // namespace aos