blob: 148deedc7e4c96b3487b4c1659e0a532dc38e3c7 [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 Silvermanc2e04222014-03-22 12:43:44 -070048 MessageHeader *next;
Brian Silvermane8337b72014-04-27 19:52:19 -070049
Brian Silverman5f8c4922014-02-11 21:22:38 -080050 // Gets the message header immediately preceding msg.
Brian Silvermana6d1b562013-09-01 14:39:39 -070051 static MessageHeader *Get(const void *msg) {
Brian Silverman63cf2412013-11-17 05:44:36 -080052 return reinterpret_cast<MessageHeader *>(__builtin_assume_aligned(
53 static_cast<uint8_t *>(const_cast<void *>(msg)) - sizeof(MessageHeader),
54 alignof(MessageHeader)));
Brian Silvermana6d1b562013-09-01 14:39:39 -070055 }
Brian Silvermane8337b72014-04-27 19:52:19 -070056
57 int32_t ref_count() const {
58 return __atomic_load_n(&ref_count_, __ATOMIC_RELAXED);
59 }
60 void set_ref_count(int32_t val) {
61 __atomic_store_n(&ref_count_, val, __ATOMIC_RELAXED);
62 }
63
64 void ref_count_sub() {
65 // TODO(brians): Take the #ifdef out once clang can handle the
66 // __atomic_*_fetch variants which could be more efficient.
67#ifdef __clang__
68 __atomic_fetch_sub(&ref_count_, 1, __ATOMIC_RELAXED);
69#else
70 __atomic_sub_fetch(&ref_count_, 1, __ATOMIC_RELAXED);
71#endif
72 }
73 void ref_count_add() {
74#ifdef __clang__
75 __atomic_fetch_add(&ref_count_, 1, __ATOMIC_RELAXED);
76#else
77 __atomic_add_fetch(&ref_count_, 1, __ATOMIC_RELAXED);
78#endif
79 }
80
81 private:
82 // This gets accessed with atomic instructions without any
83 // locks held by various member functions.
84 int32_t ref_count_;
85
Brian Silverman227ad482014-03-23 11:21:32 -070086 // Padding to make the total size 8 bytes if we have 4-byte pointers or bump
87 // it to 16 if a pointer is 8 bytes by itself.
Brian Silvermanc2e04222014-03-22 12:43:44 -070088#if __SIZEOF_POINTER__ == 8
Brian Silverman227ad482014-03-23 11:21:32 -070089 char padding[4];
Brian Silvermanc2e04222014-03-22 12:43:44 -070090#elif __SIZEOF_POINTER__ == 4
91 // No padding needed to get 8 byte total size.
92#else
93#error Unknown pointer size.
94#endif
Brian Silvermana6d1b562013-09-01 14:39:39 -070095};
Brian Silvermana6d1b562013-09-01 14:39:39 -070096
Brian Silverman4d0789d2014-03-23 17:03:07 -070097inline int RawQueue::index_add1(int index) {
98 // Doing it this way instead of with % is more efficient on ARM.
99 int r = index + 1;
100 assert(index <= data_length_);
101 if (r == data_length_) {
102 return 0;
103 } else {
104 return r;
105 }
106}
107
Brian Silverman08661c72013-09-01 17:24:38 -0700108void RawQueue::DecrementMessageReferenceCount(const void *msg) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700109 MessageHeader *header = MessageHeader::Get(msg);
Brian Silvermane8337b72014-04-27 19:52:19 -0700110 header->ref_count_sub();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700111 if (kRefDebug) {
Brian Silvermane8337b72014-04-27 19:52:19 -0700112 printf("%p ref dec count: %p count=%d\n", this, msg, header->ref_count());
Brian Silvermana6d1b562013-09-01 14:39:39 -0700113 }
Brian Silvermanad290d82014-03-19 17:22:05 -0700114
115 // The only way it should ever be 0 is if we were the last one to decrement,
116 // in which case nobody else should have it around to re-increment it or
117 // anything in the middle, so this is safe to do not atomically with the
118 // decrement.
Brian Silvermane8337b72014-04-27 19:52:19 -0700119 if (header->ref_count() == 0) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700120 DoFreeMessage(msg);
Brian Silvermanad290d82014-03-19 17:22:05 -0700121 } else {
Brian Silvermane8337b72014-04-27 19:52:19 -0700122 assert(header->ref_count() > 0);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700123 }
124}
125
Brian Silverman227ad482014-03-23 11:21:32 -0700126inline void RawQueue::IncrementMessageReferenceCount(const void *msg) const {
Brian Silverman430e7fa2014-03-21 16:58:33 -0700127 MessageHeader *const header = MessageHeader::Get(msg);
Brian Silvermane8337b72014-04-27 19:52:19 -0700128 header->ref_count_add();
Brian Silverman430e7fa2014-03-21 16:58:33 -0700129 if (kRefDebug) {
Brian Silvermanc2e04222014-03-22 12:43:44 -0700130 printf("%p ref inc count: %p\n", this, msg);
Brian Silverman430e7fa2014-03-21 16:58:33 -0700131 }
132}
133
Brian Silverman42d52372014-03-23 15:29:13 -0700134inline void RawQueue::DoFreeMessage(const void *msg) {
135 MessageHeader *header = MessageHeader::Get(msg);
136 if (kRefDebug) {
137 printf("%p ref free to %p: %p\n", this, recycle_, msg);
138 }
139
140 if (__builtin_expect(recycle_ != nullptr, 0)) {
141 void *const new_msg = recycle_->GetMessage();
142 if (new_msg == nullptr) {
143 fprintf(stderr, "queue: couldn't get a message"
144 " for recycle queue %p\n", recycle_);
145 } else {
Brian Silvermane8337b72014-04-27 19:52:19 -0700146 header->ref_count_add();
Brian Silverman42d52372014-03-23 15:29:13 -0700147 if (!recycle_->WriteMessage(const_cast<void *>(msg), kOverride)) {
148 fprintf(stderr, "queue: %p->WriteMessage(%p, kOverride) failed."
149 " aborting\n", recycle_, msg);
150 printf("see stderr\n");
151 abort();
152 }
153 msg = new_msg;
154 header = MessageHeader::Get(new_msg);
155 }
156 }
157
158 // This works around GCC bug 60272 (fixed in 4.8.3).
159 // new_next should just get replaced with header->next (and the body of the
160 // loop should become empty).
161 // The bug is that the store to new_next after the compare/exchange is
162 // unconditional but it should only be if it fails, which could mean
163 // overwriting what somebody else who preempted us right then changed it to.
164 // TODO(brians): Get rid of this workaround once we get a new enough GCC.
165 MessageHeader *new_next = __atomic_load_n(&free_messages_, __ATOMIC_RELAXED);
166 do {
167 header->next = new_next;
168 } while (__builtin_expect(
169 !__atomic_compare_exchange_n(&free_messages_, &new_next, header, true,
170 __ATOMIC_RELEASE, __ATOMIC_RELAXED),
171 0));
172}
173
174void *RawQueue::GetMessage() {
Brian Silverman42d52372014-03-23 15:29:13 -0700175 MessageHeader *header = __atomic_load_n(&free_messages_, __ATOMIC_RELAXED);
176 do {
177 if (__builtin_expect(header == nullptr, 0)) {
178 LOG(FATAL, "overused pool of queue %p\n", this);
179 }
180 } while (__builtin_expect(
181 !__atomic_compare_exchange_n(&free_messages_, &header, header->next, true,
182 __ATOMIC_ACQ_REL, __ATOMIC_RELAXED),
183 0));
184 void *msg = reinterpret_cast<uint8_t *>(header + 1);
185 // It might be uninitialized, 0 from a previous use, or 1 from previously
186 // being recycled.
Brian Silvermane8337b72014-04-27 19:52:19 -0700187 header->set_ref_count(1);
Brian Silverman42d52372014-03-23 15:29:13 -0700188 if (kRefDebug) {
189 printf("%p ref alloc: %p\n", this, msg);
190 }
191 return msg;
192}
193
Brian Silverman08661c72013-09-01 17:24:38 -0700194RawQueue::RawQueue(const char *name, size_t length, int hash, int queue_length)
Brian Silverman5f8c4922014-02-11 21:22:38 -0800195 : readable_(&data_lock_), writable_(&data_lock_) {
Brian Silvermanc2e04222014-03-22 12:43:44 -0700196 static_assert(shm_ok<RawQueue::MessageHeader>::value,
197 "the whole point is to stick it in shared memory");
198 static_assert((sizeof(RawQueue::MessageHeader) % 8) == 0,
199 "need to revalidate size/alignent assumptions");
200
Brian Silverman227ad482014-03-23 11:21:32 -0700201 if (queue_length < 1) {
202 LOG(FATAL, "queue length %d needs to be at least 1\n", queue_length);
203 }
204
Brian Silvermana6d1b562013-09-01 14:39:39 -0700205 const size_t name_size = strlen(name) + 1;
206 char *temp = static_cast<char *>(shm_malloc(name_size));
207 memcpy(temp, name, name_size);
208 name_ = temp;
209 length_ = length;
210 hash_ = hash;
211 queue_length_ = queue_length;
212
213 next_ = NULL;
214 recycle_ = NULL;
215
216 if (kFetchDebug) {
217 printf("initializing name=%s, length=%zd, hash=%d, queue_length=%d\n",
218 name, length, hash, queue_length);
219 }
220
221 data_length_ = queue_length + 1;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700222 data_ = static_cast<void **>(shm_malloc(sizeof(void *) * data_length_));
223 data_start_ = 0;
224 data_end_ = 0;
225 messages_ = 0;
226
Brian Silvermana6d1b562013-09-01 14:39:39 -0700227 msg_length_ = length + sizeof(MessageHeader);
Brian Silvermanc2e04222014-03-22 12:43:44 -0700228
Brian Silverman227ad482014-03-23 11:21:32 -0700229 // Create all of the messages for the free list and stick them on.
230 {
231 MessageHeader *previous = nullptr;
232 for (int i = 0; i < queue_length + kExtraMessages; ++i) {
233 MessageHeader *const message =
234 static_cast<MessageHeader *>(shm_malloc(msg_length_));
235 free_messages_ = message;
236 message->next = previous;
237 previous = message;
238 }
Brian Silverman60eff202014-03-21 17:10:02 -0700239 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700240
Brian Silverman35109802014-04-09 14:31:53 -0700241 readable_waiting_ = false;
242
Brian Silvermana6d1b562013-09-01 14:39:39 -0700243 if (kFetchDebug) {
244 printf("made queue %s\n", name);
245 }
246}
Brian Silverman42d52372014-03-23 15:29:13 -0700247
Brian Silverman08661c72013-09-01 17:24:38 -0700248RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700249 int queue_length) {
250 if (kFetchDebug) {
251 printf("fetching queue %s\n", name);
252 }
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800253 if (mutex_lock(&global_core->mem_struct->queues.lock) != 0) {
Brian Silverman227ad482014-03-23 11:21:32 -0700254 LOG(FATAL, "mutex_lock(%p) failed\n",
255 &global_core->mem_struct->queues.lock);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700256 }
Brian Silverman08661c72013-09-01 17:24:38 -0700257 RawQueue *current = static_cast<RawQueue *>(
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800258 global_core->mem_struct->queues.pointer);
Brian Silverman797e71e2013-09-06 17:29:39 -0700259 if (current != NULL) {
260 while (true) {
261 // If we found a matching queue.
262 if (strcmp(current->name_, name) == 0 && current->length_ == length &&
263 current->hash_ == hash && current->queue_length_ == queue_length) {
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800264 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700265 return current;
266 } else {
267 if (kFetchDebug) {
268 printf("rejected queue %s strcmp=%d target=%s\n", current->name_,
269 strcmp(current->name_, name), name);
270 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700271 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700272 // If this is the last one.
273 if (current->next_ == NULL) break;
274 current = current->next_;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700275 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700276 }
277
Brian Silverman797e71e2013-09-06 17:29:39 -0700278 RawQueue *r = new (shm_malloc(sizeof(RawQueue)))
279 RawQueue(name, length, hash, queue_length);
280 if (current == NULL) { // if we don't already have one
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800281 global_core->mem_struct->queues.pointer = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700282 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700283 current->next_ = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700284 }
285
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800286 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700287 return r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700288}
Brian Silverman42d52372014-03-23 15:29:13 -0700289
Brian Silverman08661c72013-09-01 17:24:38 -0700290RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700291 int queue_length,
Brian Silverman08661c72013-09-01 17:24:38 -0700292 int recycle_hash, int recycle_length, RawQueue **recycle) {
293 RawQueue *r = Fetch(name, length, hash, queue_length);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700294 r->recycle_ = Fetch(name, length, recycle_hash, recycle_length);
295 if (r == r->recycle_) {
296 fprintf(stderr, "queue: r->recycle_(=%p) == r(=%p)\n", r->recycle_, r);
297 printf("see stderr\n");
Brian Silverman797e71e2013-09-06 17:29:39 -0700298 r->recycle_ = NULL;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700299 abort();
300 }
301 *recycle = r->recycle_;
302 return r;
303}
304
Brian Silverman08661c72013-09-01 17:24:38 -0700305bool RawQueue::WriteMessage(void *msg, int options) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700306 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700307 printf("queue: %p->WriteMessage(%p, %x)\n", this, msg, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700308 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700309 {
310 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700311 bool writable_waited = false;
312
313 int new_end;
314 while (true) {
Brian Silverman4d0789d2014-03-23 17:03:07 -0700315 new_end = index_add1(data_end_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700316 // If there is room in the queue right now.
317 if (new_end != data_start_) break;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700318 if (options & kNonBlock) {
319 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700320 printf("queue: not blocking on %p. returning false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700321 }
Brian Silverman358c49f2014-03-05 16:56:34 -0800322 DecrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700323 return false;
324 } else if (options & kOverride) {
325 if (kWriteDebug) {
326 printf("queue: overriding on %p\n", this);
327 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700328 // Avoid leaking the message that we're going to overwrite.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700329 DecrementMessageReferenceCount(data_[data_start_]);
Brian Silverman4d0789d2014-03-23 17:03:07 -0700330 data_start_ = index_add1(data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700331 } else { // kBlock
332 if (kWriteDebug) {
333 printf("queue: going to wait for writable_ of %p\n", this);
334 }
Brian Silverman08661c72013-09-01 17:24:38 -0700335 writable_.Wait();
Brian Silverman797e71e2013-09-06 17:29:39 -0700336 writable_waited = true;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700337 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700338 }
339 data_[data_end_] = msg;
340 ++messages_;
341 data_end_ = new_end;
Brian Silverman797e71e2013-09-06 17:29:39 -0700342
Brian Silverman35109802014-04-09 14:31:53 -0700343 if (readable_waiting_) {
344 if (kWriteDebug) {
345 printf("queue: broadcasting to readable_ of %p\n", this);
346 }
347 readable_waiting_ = false;
348 readable_.Broadcast();
349 } else if (kWriteDebug) {
350 printf("queue: skipping broadcast to readable_ of %p\n", this);
Brian Silverman797e71e2013-09-06 17:29:39 -0700351 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700352
353 // If we got a signal on writable_ here and it's still writable, then we
354 // need to signal the next person in line (if any).
355 if (writable_waited && is_writable()) {
356 if (kWriteDebug) {
357 printf("queue: resignalling writable_ of %p\n", this);
358 }
359 writable_.Signal();
360 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700361 }
362 if (kWriteDebug) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700363 printf("queue: write returning true on queue %p\n", this);
364 }
365 return true;
366}
367
Brian Silverman42d52372014-03-23 15:29:13 -0700368inline void RawQueue::ReadCommonEnd() {
Brian Silverman797e71e2013-09-06 17:29:39 -0700369 if (is_writable()) {
370 if (kReadDebug) {
371 printf("queue: %ssignalling writable_ of %p\n",
Brian Silverman42d52372014-03-23 15:29:13 -0700372 writable_start_ ? "not " : "", this);
Brian Silverman797e71e2013-09-06 17:29:39 -0700373 }
Brian Silverman42d52372014-03-23 15:29:13 -0700374 if (!writable_start_) writable_.Signal();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700375 }
376}
Brian Silverman227ad482014-03-23 11:21:32 -0700377
Brian Silverman42d52372014-03-23 15:29:13 -0700378bool RawQueue::ReadCommonStart(int options, int *index) {
379 writable_start_ = is_writable();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700380 while (data_start_ == data_end_ || ((index != NULL) && messages_ <= *index)) {
381 if (options & kNonBlock) {
382 if (kReadDebug) {
383 printf("queue: not going to block waiting on %p\n", this);
384 }
385 return false;
386 } else { // kBlock
387 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700388 printf("queue: going to wait for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700389 }
Brian Silverman35109802014-04-09 14:31:53 -0700390 readable_waiting_ = true;
Brian Silverman797e71e2013-09-06 17:29:39 -0700391 // Wait for a message to become readable.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700392 readable_.Wait();
393 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700394 printf("queue: done waiting for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700395 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700396 }
397 }
398 if (kReadDebug) {
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800399 printf("queue: %p->read(%p) start=%d end=%d\n", this, index, data_start_,
400 data_end_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700401 }
402 return true;
403}
Brian Silverman227ad482014-03-23 11:21:32 -0700404
405inline int RawQueue::LastMessageIndex() const {
406 int pos = data_end_ - 1;
407 if (pos < 0) { // If it wrapped around.
408 pos = data_length_ - 1;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700409 }
Brian Silverman227ad482014-03-23 11:21:32 -0700410 return pos;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700411}
Brian Silverman227ad482014-03-23 11:21:32 -0700412
Brian Silverman08661c72013-09-01 17:24:38 -0700413const void *RawQueue::ReadMessage(int options) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700414 // TODO(brians): Test this function.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700415 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700416 printf("queue: %p->ReadMessage(%x)\n", this, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700417 }
418 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700419
Brian Silvermana6d1b562013-09-01 14:39:39 -0700420 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700421
Brian Silverman42d52372014-03-23 15:29:13 -0700422 if (!ReadCommonStart(options, nullptr)) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700423 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700424 printf("queue: %p common returned false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700425 }
426 return NULL;
427 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700428
Brian Silverman227ad482014-03-23 11:21:32 -0700429 if (options & kFromEnd) {
430 if (options & kPeek) {
431 if (kReadDebug) {
432 printf("queue: %p shortcutting c2: %d\n", this, LastMessageIndex());
433 }
434 msg = data_[LastMessageIndex()];
435 IncrementMessageReferenceCount(msg);
436 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700437 while (true) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700438 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700439 printf("queue: %p start of c2\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700440 }
441 // This loop pulls each message out of the buffer.
442 const int pos = data_start_;
Brian Silverman4d0789d2014-03-23 17:03:07 -0700443 data_start_ = index_add1(data_start_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700444 // If this is the last one.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700445 if (data_start_ == data_end_) {
446 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700447 printf("queue: %p reading from c2: %d\n", this, pos);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700448 }
449 msg = data_[pos];
450 break;
451 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700452 // This message is not going to be in the queue any more.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700453 DecrementMessageReferenceCount(data_[pos]);
454 }
Brian Silverman227ad482014-03-23 11:21:32 -0700455 }
456 } else {
457 if (kReadDebug) {
458 printf("queue: %p reading from d2: %d\n", this, data_start_);
459 }
460 msg = data_[data_start_];
461 if (options & kPeek) {
462 IncrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700463 } else {
Brian Silverman4d0789d2014-03-23 17:03:07 -0700464 data_start_ = index_add1(data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700465 }
466 }
Brian Silverman42d52372014-03-23 15:29:13 -0700467 ReadCommonEnd();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700468 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700469 printf("queue: %p read returning %p\n", this, msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700470 }
471 return msg;
472}
Brian Silverman227ad482014-03-23 11:21:32 -0700473
Brian Silverman08661c72013-09-01 17:24:38 -0700474const void *RawQueue::ReadMessageIndex(int options, int *index) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700475 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700476 printf("queue: %p->ReadMessageIndex(%x, %p(*=%d))\n",
Brian Silvermana6d1b562013-09-01 14:39:39 -0700477 this, options, index, *index);
478 }
479 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700480
481 MutexLocker locker(&data_lock_);
482
Brian Silverman42d52372014-03-23 15:29:13 -0700483 if (!ReadCommonStart(options, index)) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700484 if (kReadDebug) {
485 printf("queue: %p common returned false\n", this);
486 }
487 return NULL;
488 }
489
490 // TODO(parker): Handle integer wrap on the index.
491
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700492 if (options & kFromEnd) {
Brian Silverman227ad482014-03-23 11:21:32 -0700493 if (kReadDebug) {
Brian Silverman227ad482014-03-23 11:21:32 -0700494 printf("queue: %p reading from c1: %d\n", this, LastMessageIndex());
495 }
496 msg = data_[LastMessageIndex()];
497 if (!(options & kPeek)) *index = messages_;
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800498 } else {
Brian Silverman227ad482014-03-23 11:21:32 -0700499 // Where we're going to start reading.
500 int my_start;
501
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700502 const int unread_messages = messages_ - *index;
503 assert(unread_messages > 0);
504 int current_messages = data_end_ - data_start_;
505 if (current_messages < 0) current_messages += data_length_;
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700506 if (kReadIndexDebug) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700507 printf("queue: %p start=%d end=%d current=%d\n",
508 this, data_start_, data_end_, current_messages);
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700509 }
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700510 assert(current_messages > 0);
511 // If we're behind the available messages.
512 if (unread_messages > current_messages) {
513 // Catch index up to the last available message.
514 *index = messages_ - current_messages;
515 // And that's the one we're going to read.
516 my_start = data_start_;
517 if (kReadIndexDebug) {
518 printf("queue: %p jumping ahead to message %d (have %d) (at %d)\n",
519 this, *index, messages_, data_start_);
520 }
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700521 } else {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700522 // Just start reading at the first available message that we haven't yet
523 // read.
524 my_start = data_end_ - unread_messages;
525 if (kReadIndexDebug) {
526 printf("queue: %p original read from %d\n", this, my_start);
527 }
528 if (data_start_ < data_end_) {
Brian Silverman42d52372014-03-23 15:29:13 -0700529 assert(my_start >= 0);
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700530 }
Brian Silverman42d52372014-03-23 15:29:13 -0700531 if (my_start < 0) my_start += data_length_;
Brian Silverman67e34f52014-03-13 15:52:57 -0700532 }
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800533
Brian Silverman227ad482014-03-23 11:21:32 -0700534 if (kReadDebug) {
535 printf("queue: %p reading from d1: %d\n", this, my_start);
Brian Silverman797e71e2013-09-06 17:29:39 -0700536 }
Brian Silverman227ad482014-03-23 11:21:32 -0700537 // We have to be either after the start or before the end, even if the queue
Brian Silverman42d52372014-03-23 15:29:13 -0700538 // is wrapped around (should be both if it's not).
Brian Silverman227ad482014-03-23 11:21:32 -0700539 assert((my_start >= data_start_) || (my_start < data_end_));
540 // More sanity checking.
541 assert((my_start >= 0) && (my_start < data_length_));
542 msg = data_[my_start];
543 if (!(options & kPeek)) ++(*index);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700544 }
Brian Silverman227ad482014-03-23 11:21:32 -0700545 IncrementMessageReferenceCount(msg);
546
Brian Silverman42d52372014-03-23 15:29:13 -0700547 ReadCommonEnd();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700548 return msg;
549}
550
Brian Silvermanc2e04222014-03-22 12:43:44 -0700551int RawQueue::FreeMessages() const {
552 int r = 0;
553 MessageHeader *header = free_messages_;
554 while (header != nullptr) {
555 ++r;
556 header = header->next;
557 }
558 return r;
559}
560
Brian Silvermanb3267322014-04-10 12:10:03 -0700561bool RawQueue::IsDebug() {
562#if QUEUE_DEBUG
563 return true;
564#else
565 return false;
566#endif
567}
568
Brian Silvermana6d1b562013-09-01 14:39:39 -0700569} // namespace aos