blob: 8513e329d17047a47c04f9c21990b48b55efeb6f [file] [log] [blame]
Brian Silverman9eaf91a2014-03-24 16:37:44 -07001#if !QUEUE_DEBUG
2#define NDEBUG
3#endif
4
Brian Silverman14fd0fb2014-01-14 21:42:01 -08005#include "aos/linux_code/ipc_lib/queue.h"
Brian Silvermana6d1b562013-09-01 14:39:39 -07006
7#include <stdio.h>
8#include <string.h>
9#include <errno.h>
10#include <assert.h>
11
12#include <memory>
Brian Silvermanc39e2bd2014-02-21 09:17:35 -080013#include <algorithm>
Brian Silvermana6d1b562013-09-01 14:39:39 -070014
15#include "aos/common/logging/logging.h"
16#include "aos/common/type_traits.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -080017#include "aos/linux_code/ipc_lib/core_lib.h"
Brian Silvermana6d1b562013-09-01 14:39:39 -070018
19namespace aos {
Brian Silvermana6d1b562013-09-01 14:39:39 -070020namespace {
21
Brian Silverman08661c72013-09-01 17:24:38 -070022static_assert(shm_ok<RawQueue>::value,
23 "RawQueue instances go into shared memory");
Brian Silvermana6d1b562013-09-01 14:39:39 -070024
25const bool kReadDebug = false;
Brian Silvermanbad7c8a2014-03-26 20:45:18 -070026const bool kWriteDebug = false;
Brian Silvermana6d1b562013-09-01 14:39:39 -070027const bool kRefDebug = false;
28const bool kFetchDebug = false;
Brian Silvermancd2d84c2014-03-13 23:30:58 -070029const bool kReadIndexDebug = false;
Brian Silvermana6d1b562013-09-01 14:39:39 -070030
31// The number of extra messages the pool associated with each queue will be able
Brian Silverman08661c72013-09-01 17:24:38 -070032// to hold (for readers who are slow about freeing them or who leak one when
33// they get killed).
Brian Silvermana6d1b562013-09-01 14:39:39 -070034const int kExtraMessages = 20;
35
36} // namespace
37
Brian Silverman08661c72013-09-01 17:24:38 -070038const int RawQueue::kPeek;
39const int RawQueue::kFromEnd;
40const int RawQueue::kNonBlock;
41const int RawQueue::kBlock;
42const int RawQueue::kOverride;
43
Brian Silverman430e7fa2014-03-21 16:58:33 -070044// This is what gets stuck in before each queue message in memory. It is always
45// allocated aligned to 8 bytes and its size has to maintain that alignment for
46// the message that follows immediately.
Brian Silverman08661c72013-09-01 17:24:38 -070047struct RawQueue::MessageHeader {
Brian Silvermanad290d82014-03-19 17:22:05 -070048 // This gets incremented and decremented with atomic instructions without any
49 // locks held.
Brian Silverman227ad482014-03-23 11:21:32 -070050 int32_t ref_count;
Brian Silvermanc2e04222014-03-22 12:43:44 -070051 MessageHeader *next;
Brian Silverman5f8c4922014-02-11 21:22:38 -080052 // Gets the message header immediately preceding msg.
Brian Silvermana6d1b562013-09-01 14:39:39 -070053 static MessageHeader *Get(const void *msg) {
Brian Silverman63cf2412013-11-17 05:44:36 -080054 return reinterpret_cast<MessageHeader *>(__builtin_assume_aligned(
55 static_cast<uint8_t *>(const_cast<void *>(msg)) - sizeof(MessageHeader),
56 alignof(MessageHeader)));
Brian Silvermana6d1b562013-09-01 14:39:39 -070057 }
Brian Silverman227ad482014-03-23 11:21:32 -070058 // Padding to make the total size 8 bytes if we have 4-byte pointers or bump
59 // it to 16 if a pointer is 8 bytes by itself.
Brian Silvermanc2e04222014-03-22 12:43:44 -070060#if __SIZEOF_POINTER__ == 8
Brian Silverman227ad482014-03-23 11:21:32 -070061 char padding[4];
Brian Silvermanc2e04222014-03-22 12:43:44 -070062#elif __SIZEOF_POINTER__ == 4
63 // No padding needed to get 8 byte total size.
64#else
65#error Unknown pointer size.
66#endif
Brian Silvermana6d1b562013-09-01 14:39:39 -070067};
Brian Silvermana6d1b562013-09-01 14:39:39 -070068
Brian Silverman4d0789d2014-03-23 17:03:07 -070069inline int RawQueue::index_add1(int index) {
70 // Doing it this way instead of with % is more efficient on ARM.
71 int r = index + 1;
72 assert(index <= data_length_);
73 if (r == data_length_) {
74 return 0;
75 } else {
76 return r;
77 }
78}
79
Brian Silverman08661c72013-09-01 17:24:38 -070080void RawQueue::DecrementMessageReferenceCount(const void *msg) {
Brian Silvermana6d1b562013-09-01 14:39:39 -070081 MessageHeader *header = MessageHeader::Get(msg);
Brian Silvermanad290d82014-03-19 17:22:05 -070082 __atomic_sub_fetch(&header->ref_count, 1, __ATOMIC_RELAXED);
Brian Silvermana6d1b562013-09-01 14:39:39 -070083 if (kRefDebug) {
Brian Silvermanc2e04222014-03-22 12:43:44 -070084 printf("%p ref dec count: %p count=%d\n", this, msg, header->ref_count);
Brian Silvermana6d1b562013-09-01 14:39:39 -070085 }
Brian Silvermanad290d82014-03-19 17:22:05 -070086
87 // The only way it should ever be 0 is if we were the last one to decrement,
88 // in which case nobody else should have it around to re-increment it or
89 // anything in the middle, so this is safe to do not atomically with the
90 // decrement.
Brian Silvermana6d1b562013-09-01 14:39:39 -070091 if (header->ref_count == 0) {
92 DoFreeMessage(msg);
Brian Silvermanad290d82014-03-19 17:22:05 -070093 } else {
94 assert(header->ref_count > 0);
Brian Silvermana6d1b562013-09-01 14:39:39 -070095 }
96}
97
Brian Silverman227ad482014-03-23 11:21:32 -070098inline void RawQueue::IncrementMessageReferenceCount(const void *msg) const {
Brian Silverman430e7fa2014-03-21 16:58:33 -070099 MessageHeader *const header = MessageHeader::Get(msg);
100 __atomic_add_fetch(&header->ref_count, 1, __ATOMIC_RELAXED);
101 if (kRefDebug) {
Brian Silvermanc2e04222014-03-22 12:43:44 -0700102 printf("%p ref inc count: %p\n", this, msg);
Brian Silverman430e7fa2014-03-21 16:58:33 -0700103 }
104}
105
Brian Silverman42d52372014-03-23 15:29:13 -0700106inline void RawQueue::DoFreeMessage(const void *msg) {
107 MessageHeader *header = MessageHeader::Get(msg);
108 if (kRefDebug) {
109 printf("%p ref free to %p: %p\n", this, recycle_, msg);
110 }
111
112 if (__builtin_expect(recycle_ != nullptr, 0)) {
113 void *const new_msg = recycle_->GetMessage();
114 if (new_msg == nullptr) {
115 fprintf(stderr, "queue: couldn't get a message"
116 " for recycle queue %p\n", recycle_);
117 } else {
118 // Nobody else has a reference to the message at this point, so no need to
119 // be fancy about it.
120 ++header->ref_count;
121 if (!recycle_->WriteMessage(const_cast<void *>(msg), kOverride)) {
122 fprintf(stderr, "queue: %p->WriteMessage(%p, kOverride) failed."
123 " aborting\n", recycle_, msg);
124 printf("see stderr\n");
125 abort();
126 }
127 msg = new_msg;
128 header = MessageHeader::Get(new_msg);
129 }
130 }
131
132 // This works around GCC bug 60272 (fixed in 4.8.3).
133 // new_next should just get replaced with header->next (and the body of the
134 // loop should become empty).
135 // The bug is that the store to new_next after the compare/exchange is
136 // unconditional but it should only be if it fails, which could mean
137 // overwriting what somebody else who preempted us right then changed it to.
138 // TODO(brians): Get rid of this workaround once we get a new enough GCC.
139 MessageHeader *new_next = __atomic_load_n(&free_messages_, __ATOMIC_RELAXED);
140 do {
141 header->next = new_next;
142 } while (__builtin_expect(
143 !__atomic_compare_exchange_n(&free_messages_, &new_next, header, true,
144 __ATOMIC_RELEASE, __ATOMIC_RELAXED),
145 0));
146}
147
148void *RawQueue::GetMessage() {
Brian Silverman42d52372014-03-23 15:29:13 -0700149 MessageHeader *header = __atomic_load_n(&free_messages_, __ATOMIC_RELAXED);
150 do {
151 if (__builtin_expect(header == nullptr, 0)) {
152 LOG(FATAL, "overused pool of queue %p\n", this);
153 }
154 } while (__builtin_expect(
155 !__atomic_compare_exchange_n(&free_messages_, &header, header->next, true,
156 __ATOMIC_ACQ_REL, __ATOMIC_RELAXED),
157 0));
158 void *msg = reinterpret_cast<uint8_t *>(header + 1);
159 // It might be uninitialized, 0 from a previous use, or 1 from previously
160 // being recycled.
161 header->ref_count = 1;
162 static_assert(
163 __atomic_always_lock_free(sizeof(header->ref_count), &header->ref_count),
164 "we access this using not specifically atomic loads and stores");
165 if (kRefDebug) {
166 printf("%p ref alloc: %p\n", this, msg);
167 }
168 return msg;
169}
170
Brian Silverman08661c72013-09-01 17:24:38 -0700171RawQueue::RawQueue(const char *name, size_t length, int hash, int queue_length)
Brian Silverman5f8c4922014-02-11 21:22:38 -0800172 : readable_(&data_lock_), writable_(&data_lock_) {
Brian Silvermanc2e04222014-03-22 12:43:44 -0700173 static_assert(shm_ok<RawQueue::MessageHeader>::value,
174 "the whole point is to stick it in shared memory");
175 static_assert((sizeof(RawQueue::MessageHeader) % 8) == 0,
176 "need to revalidate size/alignent assumptions");
177
Brian Silverman227ad482014-03-23 11:21:32 -0700178 if (queue_length < 1) {
179 LOG(FATAL, "queue length %d needs to be at least 1\n", queue_length);
180 }
181
Brian Silvermana6d1b562013-09-01 14:39:39 -0700182 const size_t name_size = strlen(name) + 1;
183 char *temp = static_cast<char *>(shm_malloc(name_size));
184 memcpy(temp, name, name_size);
185 name_ = temp;
186 length_ = length;
187 hash_ = hash;
188 queue_length_ = queue_length;
189
190 next_ = NULL;
191 recycle_ = NULL;
192
193 if (kFetchDebug) {
194 printf("initializing name=%s, length=%zd, hash=%d, queue_length=%d\n",
195 name, length, hash, queue_length);
196 }
197
198 data_length_ = queue_length + 1;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700199 data_ = static_cast<void **>(shm_malloc(sizeof(void *) * data_length_));
200 data_start_ = 0;
201 data_end_ = 0;
202 messages_ = 0;
203
Brian Silvermana6d1b562013-09-01 14:39:39 -0700204 msg_length_ = length + sizeof(MessageHeader);
Brian Silvermanc2e04222014-03-22 12:43:44 -0700205
Brian Silverman227ad482014-03-23 11:21:32 -0700206 // Create all of the messages for the free list and stick them on.
207 {
208 MessageHeader *previous = nullptr;
209 for (int i = 0; i < queue_length + kExtraMessages; ++i) {
210 MessageHeader *const message =
211 static_cast<MessageHeader *>(shm_malloc(msg_length_));
212 free_messages_ = message;
213 message->next = previous;
214 previous = message;
215 }
Brian Silverman60eff202014-03-21 17:10:02 -0700216 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700217
218 if (kFetchDebug) {
219 printf("made queue %s\n", name);
220 }
221}
Brian Silverman42d52372014-03-23 15:29:13 -0700222
Brian Silverman08661c72013-09-01 17:24:38 -0700223RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700224 int queue_length) {
225 if (kFetchDebug) {
226 printf("fetching queue %s\n", name);
227 }
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800228 if (mutex_lock(&global_core->mem_struct->queues.lock) != 0) {
Brian Silverman227ad482014-03-23 11:21:32 -0700229 LOG(FATAL, "mutex_lock(%p) failed\n",
230 &global_core->mem_struct->queues.lock);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700231 }
Brian Silverman08661c72013-09-01 17:24:38 -0700232 RawQueue *current = static_cast<RawQueue *>(
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800233 global_core->mem_struct->queues.pointer);
Brian Silverman797e71e2013-09-06 17:29:39 -0700234 if (current != NULL) {
235 while (true) {
236 // If we found a matching queue.
237 if (strcmp(current->name_, name) == 0 && current->length_ == length &&
238 current->hash_ == hash && current->queue_length_ == queue_length) {
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800239 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700240 return current;
241 } else {
242 if (kFetchDebug) {
243 printf("rejected queue %s strcmp=%d target=%s\n", current->name_,
244 strcmp(current->name_, name), name);
245 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700246 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700247 // If this is the last one.
248 if (current->next_ == NULL) break;
249 current = current->next_;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700250 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700251 }
252
Brian Silverman797e71e2013-09-06 17:29:39 -0700253 RawQueue *r = new (shm_malloc(sizeof(RawQueue)))
254 RawQueue(name, length, hash, queue_length);
255 if (current == NULL) { // if we don't already have one
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800256 global_core->mem_struct->queues.pointer = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700257 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700258 current->next_ = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700259 }
260
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800261 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700262 return r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700263}
Brian Silverman42d52372014-03-23 15:29:13 -0700264
Brian Silverman08661c72013-09-01 17:24:38 -0700265RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700266 int queue_length,
Brian Silverman08661c72013-09-01 17:24:38 -0700267 int recycle_hash, int recycle_length, RawQueue **recycle) {
268 RawQueue *r = Fetch(name, length, hash, queue_length);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700269 r->recycle_ = Fetch(name, length, recycle_hash, recycle_length);
270 if (r == r->recycle_) {
271 fprintf(stderr, "queue: r->recycle_(=%p) == r(=%p)\n", r->recycle_, r);
272 printf("see stderr\n");
Brian Silverman797e71e2013-09-06 17:29:39 -0700273 r->recycle_ = NULL;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700274 abort();
275 }
276 *recycle = r->recycle_;
277 return r;
278}
279
Brian Silverman08661c72013-09-01 17:24:38 -0700280bool RawQueue::WriteMessage(void *msg, int options) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700281 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700282 printf("queue: %p->WriteMessage(%p, %x)\n", this, msg, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700283 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700284 {
285 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700286 bool writable_waited = false;
287
288 int new_end;
289 while (true) {
Brian Silverman4d0789d2014-03-23 17:03:07 -0700290 new_end = index_add1(data_end_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700291 // If there is room in the queue right now.
292 if (new_end != data_start_) break;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700293 if (options & kNonBlock) {
294 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700295 printf("queue: not blocking on %p. returning false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700296 }
Brian Silverman358c49f2014-03-05 16:56:34 -0800297 DecrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700298 return false;
299 } else if (options & kOverride) {
300 if (kWriteDebug) {
301 printf("queue: overriding on %p\n", this);
302 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700303 // Avoid leaking the message that we're going to overwrite.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700304 DecrementMessageReferenceCount(data_[data_start_]);
Brian Silverman4d0789d2014-03-23 17:03:07 -0700305 data_start_ = index_add1(data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700306 } else { // kBlock
307 if (kWriteDebug) {
308 printf("queue: going to wait for writable_ of %p\n", this);
309 }
Brian Silverman08661c72013-09-01 17:24:38 -0700310 writable_.Wait();
Brian Silverman797e71e2013-09-06 17:29:39 -0700311 writable_waited = true;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700312 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700313 }
314 data_[data_end_] = msg;
315 ++messages_;
316 data_end_ = new_end;
Brian Silverman797e71e2013-09-06 17:29:39 -0700317
318 if (kWriteDebug) {
319 printf("queue: broadcasting to readable_ of %p\n", this);
320 }
321 readable_.Broadcast();
322
323 // If we got a signal on writable_ here and it's still writable, then we
324 // need to signal the next person in line (if any).
325 if (writable_waited && is_writable()) {
326 if (kWriteDebug) {
327 printf("queue: resignalling writable_ of %p\n", this);
328 }
329 writable_.Signal();
330 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700331 }
332 if (kWriteDebug) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700333 printf("queue: write returning true on queue %p\n", this);
334 }
335 return true;
336}
337
Brian Silverman42d52372014-03-23 15:29:13 -0700338inline void RawQueue::ReadCommonEnd() {
Brian Silverman797e71e2013-09-06 17:29:39 -0700339 if (is_writable()) {
340 if (kReadDebug) {
341 printf("queue: %ssignalling writable_ of %p\n",
Brian Silverman42d52372014-03-23 15:29:13 -0700342 writable_start_ ? "not " : "", this);
Brian Silverman797e71e2013-09-06 17:29:39 -0700343 }
Brian Silverman42d52372014-03-23 15:29:13 -0700344 if (!writable_start_) writable_.Signal();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700345 }
346}
Brian Silverman227ad482014-03-23 11:21:32 -0700347
Brian Silverman42d52372014-03-23 15:29:13 -0700348bool RawQueue::ReadCommonStart(int options, int *index) {
349 writable_start_ = is_writable();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700350 while (data_start_ == data_end_ || ((index != NULL) && messages_ <= *index)) {
351 if (options & kNonBlock) {
352 if (kReadDebug) {
353 printf("queue: not going to block waiting on %p\n", this);
354 }
355 return false;
356 } else { // kBlock
357 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700358 printf("queue: going to wait for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700359 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700360 // Wait for a message to become readable.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700361 readable_.Wait();
362 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700363 printf("queue: done waiting for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700364 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700365 }
366 }
367 if (kReadDebug) {
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800368 printf("queue: %p->read(%p) start=%d end=%d\n", this, index, data_start_,
369 data_end_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700370 }
371 return true;
372}
Brian Silverman227ad482014-03-23 11:21:32 -0700373
374inline int RawQueue::LastMessageIndex() const {
375 int pos = data_end_ - 1;
376 if (pos < 0) { // If it wrapped around.
377 pos = data_length_ - 1;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700378 }
Brian Silverman227ad482014-03-23 11:21:32 -0700379 return pos;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700380}
Brian Silverman227ad482014-03-23 11:21:32 -0700381
Brian Silverman08661c72013-09-01 17:24:38 -0700382const void *RawQueue::ReadMessage(int options) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700383 // TODO(brians): Test this function.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700384 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700385 printf("queue: %p->ReadMessage(%x)\n", this, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700386 }
387 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700388
Brian Silvermana6d1b562013-09-01 14:39:39 -0700389 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700390
Brian Silverman42d52372014-03-23 15:29:13 -0700391 if (!ReadCommonStart(options, nullptr)) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700392 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700393 printf("queue: %p common returned false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700394 }
395 return NULL;
396 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700397
Brian Silverman227ad482014-03-23 11:21:32 -0700398 if (options & kFromEnd) {
399 if (options & kPeek) {
400 if (kReadDebug) {
401 printf("queue: %p shortcutting c2: %d\n", this, LastMessageIndex());
402 }
403 msg = data_[LastMessageIndex()];
404 IncrementMessageReferenceCount(msg);
405 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700406 while (true) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700407 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700408 printf("queue: %p start of c2\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700409 }
410 // This loop pulls each message out of the buffer.
411 const int pos = data_start_;
Brian Silverman4d0789d2014-03-23 17:03:07 -0700412 data_start_ = index_add1(data_start_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700413 // If this is the last one.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700414 if (data_start_ == data_end_) {
415 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700416 printf("queue: %p reading from c2: %d\n", this, pos);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700417 }
418 msg = data_[pos];
419 break;
420 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700421 // This message is not going to be in the queue any more.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700422 DecrementMessageReferenceCount(data_[pos]);
423 }
Brian Silverman227ad482014-03-23 11:21:32 -0700424 }
425 } else {
426 if (kReadDebug) {
427 printf("queue: %p reading from d2: %d\n", this, data_start_);
428 }
429 msg = data_[data_start_];
430 if (options & kPeek) {
431 IncrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700432 } else {
Brian Silverman4d0789d2014-03-23 17:03:07 -0700433 data_start_ = index_add1(data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700434 }
435 }
Brian Silverman42d52372014-03-23 15:29:13 -0700436 ReadCommonEnd();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700437 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700438 printf("queue: %p read returning %p\n", this, msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700439 }
440 return msg;
441}
Brian Silverman227ad482014-03-23 11:21:32 -0700442
Brian Silverman08661c72013-09-01 17:24:38 -0700443const void *RawQueue::ReadMessageIndex(int options, int *index) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700444 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700445 printf("queue: %p->ReadMessageIndex(%x, %p(*=%d))\n",
Brian Silvermana6d1b562013-09-01 14:39:39 -0700446 this, options, index, *index);
447 }
448 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700449
450 MutexLocker locker(&data_lock_);
451
Brian Silverman42d52372014-03-23 15:29:13 -0700452 if (!ReadCommonStart(options, index)) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700453 if (kReadDebug) {
454 printf("queue: %p common returned false\n", this);
455 }
456 return NULL;
457 }
458
459 // TODO(parker): Handle integer wrap on the index.
460
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700461 if (options & kFromEnd) {
Brian Silverman227ad482014-03-23 11:21:32 -0700462 if (kReadDebug) {
Brian Silverman227ad482014-03-23 11:21:32 -0700463 printf("queue: %p reading from c1: %d\n", this, LastMessageIndex());
464 }
465 msg = data_[LastMessageIndex()];
466 if (!(options & kPeek)) *index = messages_;
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800467 } else {
Brian Silverman227ad482014-03-23 11:21:32 -0700468 // Where we're going to start reading.
469 int my_start;
470
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700471 const int unread_messages = messages_ - *index;
472 assert(unread_messages > 0);
473 int current_messages = data_end_ - data_start_;
474 if (current_messages < 0) current_messages += data_length_;
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700475 if (kReadIndexDebug) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700476 printf("queue: %p start=%d end=%d current=%d\n",
477 this, data_start_, data_end_, current_messages);
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700478 }
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700479 assert(current_messages > 0);
480 // If we're behind the available messages.
481 if (unread_messages > current_messages) {
482 // Catch index up to the last available message.
483 *index = messages_ - current_messages;
484 // And that's the one we're going to read.
485 my_start = data_start_;
486 if (kReadIndexDebug) {
487 printf("queue: %p jumping ahead to message %d (have %d) (at %d)\n",
488 this, *index, messages_, data_start_);
489 }
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700490 } else {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700491 // Just start reading at the first available message that we haven't yet
492 // read.
493 my_start = data_end_ - unread_messages;
494 if (kReadIndexDebug) {
495 printf("queue: %p original read from %d\n", this, my_start);
496 }
497 if (data_start_ < data_end_) {
Brian Silverman42d52372014-03-23 15:29:13 -0700498 assert(my_start >= 0);
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700499 }
Brian Silverman42d52372014-03-23 15:29:13 -0700500 if (my_start < 0) my_start += data_length_;
Brian Silverman67e34f52014-03-13 15:52:57 -0700501 }
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800502
Brian Silverman227ad482014-03-23 11:21:32 -0700503 if (kReadDebug) {
504 printf("queue: %p reading from d1: %d\n", this, my_start);
Brian Silverman797e71e2013-09-06 17:29:39 -0700505 }
Brian Silverman227ad482014-03-23 11:21:32 -0700506 // We have to be either after the start or before the end, even if the queue
Brian Silverman42d52372014-03-23 15:29:13 -0700507 // is wrapped around (should be both if it's not).
Brian Silverman227ad482014-03-23 11:21:32 -0700508 assert((my_start >= data_start_) || (my_start < data_end_));
509 // More sanity checking.
510 assert((my_start >= 0) && (my_start < data_length_));
511 msg = data_[my_start];
512 if (!(options & kPeek)) ++(*index);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700513 }
Brian Silverman227ad482014-03-23 11:21:32 -0700514 IncrementMessageReferenceCount(msg);
515
Brian Silverman42d52372014-03-23 15:29:13 -0700516 ReadCommonEnd();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700517 return msg;
518}
519
Brian Silvermanc2e04222014-03-22 12:43:44 -0700520int RawQueue::FreeMessages() const {
521 int r = 0;
522 MessageHeader *header = free_messages_;
523 while (header != nullptr) {
524 ++r;
525 header = header->next;
526 }
527 return r;
528}
529
Brian Silvermana6d1b562013-09-01 14:39:39 -0700530} // namespace aos