Expose a unique index for each event loop buffer
This can be helpful for indexing into other datastructures based on the
messages, by giving an identifier which will be unique as long as the
message is pinned.
Change-Id: I49ce18fba25a796005e64b40e5d1d5c55ca15543
diff --git a/aos/ipc_lib/lockless_queue.cc b/aos/ipc_lib/lockless_queue.cc
index 01e5f24..dad44bb 100644
--- a/aos/ipc_lib/lockless_queue.cc
+++ b/aos/ipc_lib/lockless_queue.cc
@@ -859,7 +859,7 @@
// This method doesn't mess with any scratch_index, so it doesn't have to worry
// about message ownership.
-bool LocklessQueue::Pinner::PinIndex(uint32_t uint32_queue_index) {
+int LocklessQueue::Pinner::PinIndex(uint32_t uint32_queue_index) {
const size_t queue_size = memory_->queue_size();
const QueueIndex queue_index =
QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index);
@@ -880,7 +880,7 @@
if (message_queue_index == queue_index) {
VLOG(3) << "Eq: " << std::hex << message_queue_index.index();
aos_compiler_memory_barrier();
- return true;
+ return message_index.message_index();
}
VLOG(3) << "Message reused: " << std::hex << message_queue_index.index()
<< ", " << queue_index.index();
@@ -890,7 +890,7 @@
// longer in the queue, so back that out now.
pinner->pinned.Invalidate();
VLOG(3) << "Unpinned: " << std::hex << queue_index.index();
- return false;
+ return -1;
}
size_t LocklessQueue::Pinner::size() const {
@@ -1094,6 +1094,14 @@
memory_->GetMessage(new_scratch)->header.queue_index.RelaxedInvalidate();
}
+int LocklessQueue::Sender::buffer_index() const {
+ ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_);
+ // We can do a relaxed load on our sender because we're the only person
+ // modifying it right now.
+ const Index scratch_index = sender->scratch_index.RelaxedLoad();
+ return scratch_index.message_index();
+}
+
LocklessQueue::ReadResult LocklessQueue::Read(
uint32_t uint32_queue_index,
::aos::monotonic_clock::time_point *monotonic_sent_time,
diff --git a/aos/ipc_lib/lockless_queue.h b/aos/ipc_lib/lockless_queue.h
index 5676c53..afa7ced 100644
--- a/aos/ipc_lib/lockless_queue.h
+++ b/aos/ipc_lib/lockless_queue.h
@@ -258,6 +258,8 @@
aos::realtime_clock::time_point *realtime_sent_time = nullptr,
uint32_t *queue_index = nullptr);
+ int buffer_index() const;
+
private:
friend class LocklessQueue;
@@ -298,9 +300,9 @@
// Attempts to pin the message at queue_index.
// Un-pins the previous message.
- // Returns true if it succeeds.
- // Returns false if that message is no longer in the queue.
- bool PinIndex(uint32_t queue_index);
+ // Returns the buffer index (non-negative) if it succeeds.
+ // Returns -1 if that message is no longer in the queue.
+ int PinIndex(uint32_t queue_index);
// Read at most size() bytes of data into the memory pointed to by Data().
// Note: calls to Data() are expensive enough that you should cache it.