blob: 9489265ef398085d769cc6dac24f018f99ed1db2 [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
Brian Silverman35109802014-04-09 14:31:53 -0700218 readable_waiting_ = false;
219
Brian Silvermana6d1b562013-09-01 14:39:39 -0700220 if (kFetchDebug) {
221 printf("made queue %s\n", name);
222 }
223}
Brian Silverman42d52372014-03-23 15:29:13 -0700224
Brian Silverman08661c72013-09-01 17:24:38 -0700225RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700226 int queue_length) {
227 if (kFetchDebug) {
228 printf("fetching queue %s\n", name);
229 }
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800230 if (mutex_lock(&global_core->mem_struct->queues.lock) != 0) {
Brian Silverman227ad482014-03-23 11:21:32 -0700231 LOG(FATAL, "mutex_lock(%p) failed\n",
232 &global_core->mem_struct->queues.lock);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700233 }
Brian Silverman08661c72013-09-01 17:24:38 -0700234 RawQueue *current = static_cast<RawQueue *>(
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800235 global_core->mem_struct->queues.pointer);
Brian Silverman797e71e2013-09-06 17:29:39 -0700236 if (current != NULL) {
237 while (true) {
238 // If we found a matching queue.
239 if (strcmp(current->name_, name) == 0 && current->length_ == length &&
240 current->hash_ == hash && current->queue_length_ == queue_length) {
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800241 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700242 return current;
243 } else {
244 if (kFetchDebug) {
245 printf("rejected queue %s strcmp=%d target=%s\n", current->name_,
246 strcmp(current->name_, name), name);
247 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700248 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700249 // If this is the last one.
250 if (current->next_ == NULL) break;
251 current = current->next_;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700252 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700253 }
254
Brian Silverman797e71e2013-09-06 17:29:39 -0700255 RawQueue *r = new (shm_malloc(sizeof(RawQueue)))
256 RawQueue(name, length, hash, queue_length);
257 if (current == NULL) { // if we don't already have one
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800258 global_core->mem_struct->queues.pointer = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700259 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700260 current->next_ = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700261 }
262
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800263 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700264 return r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700265}
Brian Silverman42d52372014-03-23 15:29:13 -0700266
Brian Silverman08661c72013-09-01 17:24:38 -0700267RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700268 int queue_length,
Brian Silverman08661c72013-09-01 17:24:38 -0700269 int recycle_hash, int recycle_length, RawQueue **recycle) {
270 RawQueue *r = Fetch(name, length, hash, queue_length);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700271 r->recycle_ = Fetch(name, length, recycle_hash, recycle_length);
272 if (r == r->recycle_) {
273 fprintf(stderr, "queue: r->recycle_(=%p) == r(=%p)\n", r->recycle_, r);
274 printf("see stderr\n");
Brian Silverman797e71e2013-09-06 17:29:39 -0700275 r->recycle_ = NULL;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700276 abort();
277 }
278 *recycle = r->recycle_;
279 return r;
280}
281
Brian Silverman08661c72013-09-01 17:24:38 -0700282bool RawQueue::WriteMessage(void *msg, int options) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700283 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700284 printf("queue: %p->WriteMessage(%p, %x)\n", this, msg, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700285 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700286 {
287 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700288 bool writable_waited = false;
289
290 int new_end;
291 while (true) {
Brian Silverman4d0789d2014-03-23 17:03:07 -0700292 new_end = index_add1(data_end_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700293 // If there is room in the queue right now.
294 if (new_end != data_start_) break;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700295 if (options & kNonBlock) {
296 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700297 printf("queue: not blocking on %p. returning false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700298 }
Brian Silverman358c49f2014-03-05 16:56:34 -0800299 DecrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700300 return false;
301 } else if (options & kOverride) {
302 if (kWriteDebug) {
303 printf("queue: overriding on %p\n", this);
304 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700305 // Avoid leaking the message that we're going to overwrite.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700306 DecrementMessageReferenceCount(data_[data_start_]);
Brian Silverman4d0789d2014-03-23 17:03:07 -0700307 data_start_ = index_add1(data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700308 } else { // kBlock
309 if (kWriteDebug) {
310 printf("queue: going to wait for writable_ of %p\n", this);
311 }
Brian Silverman08661c72013-09-01 17:24:38 -0700312 writable_.Wait();
Brian Silverman797e71e2013-09-06 17:29:39 -0700313 writable_waited = true;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700314 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700315 }
316 data_[data_end_] = msg;
317 ++messages_;
318 data_end_ = new_end;
Brian Silverman797e71e2013-09-06 17:29:39 -0700319
Brian Silverman35109802014-04-09 14:31:53 -0700320 if (readable_waiting_) {
321 if (kWriteDebug) {
322 printf("queue: broadcasting to readable_ of %p\n", this);
323 }
324 readable_waiting_ = false;
325 readable_.Broadcast();
326 } else if (kWriteDebug) {
327 printf("queue: skipping broadcast to readable_ of %p\n", this);
Brian Silverman797e71e2013-09-06 17:29:39 -0700328 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700329
330 // If we got a signal on writable_ here and it's still writable, then we
331 // need to signal the next person in line (if any).
332 if (writable_waited && is_writable()) {
333 if (kWriteDebug) {
334 printf("queue: resignalling writable_ of %p\n", this);
335 }
336 writable_.Signal();
337 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700338 }
339 if (kWriteDebug) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700340 printf("queue: write returning true on queue %p\n", this);
341 }
342 return true;
343}
344
Brian Silverman42d52372014-03-23 15:29:13 -0700345inline void RawQueue::ReadCommonEnd() {
Brian Silverman797e71e2013-09-06 17:29:39 -0700346 if (is_writable()) {
347 if (kReadDebug) {
348 printf("queue: %ssignalling writable_ of %p\n",
Brian Silverman42d52372014-03-23 15:29:13 -0700349 writable_start_ ? "not " : "", this);
Brian Silverman797e71e2013-09-06 17:29:39 -0700350 }
Brian Silverman42d52372014-03-23 15:29:13 -0700351 if (!writable_start_) writable_.Signal();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700352 }
353}
Brian Silverman227ad482014-03-23 11:21:32 -0700354
Brian Silverman42d52372014-03-23 15:29:13 -0700355bool RawQueue::ReadCommonStart(int options, int *index) {
356 writable_start_ = is_writable();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700357 while (data_start_ == data_end_ || ((index != NULL) && messages_ <= *index)) {
358 if (options & kNonBlock) {
359 if (kReadDebug) {
360 printf("queue: not going to block waiting on %p\n", this);
361 }
362 return false;
363 } else { // kBlock
364 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700365 printf("queue: going to wait for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700366 }
Brian Silverman35109802014-04-09 14:31:53 -0700367 readable_waiting_ = true;
Brian Silverman797e71e2013-09-06 17:29:39 -0700368 // Wait for a message to become readable.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700369 readable_.Wait();
370 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700371 printf("queue: done waiting for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700372 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700373 }
374 }
375 if (kReadDebug) {
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800376 printf("queue: %p->read(%p) start=%d end=%d\n", this, index, data_start_,
377 data_end_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700378 }
379 return true;
380}
Brian Silverman227ad482014-03-23 11:21:32 -0700381
382inline int RawQueue::LastMessageIndex() const {
383 int pos = data_end_ - 1;
384 if (pos < 0) { // If it wrapped around.
385 pos = data_length_ - 1;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700386 }
Brian Silverman227ad482014-03-23 11:21:32 -0700387 return pos;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700388}
Brian Silverman227ad482014-03-23 11:21:32 -0700389
Brian Silverman08661c72013-09-01 17:24:38 -0700390const void *RawQueue::ReadMessage(int options) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700391 // TODO(brians): Test this function.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700392 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700393 printf("queue: %p->ReadMessage(%x)\n", this, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700394 }
395 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700396
Brian Silvermana6d1b562013-09-01 14:39:39 -0700397 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700398
Brian Silverman42d52372014-03-23 15:29:13 -0700399 if (!ReadCommonStart(options, nullptr)) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700400 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700401 printf("queue: %p common returned false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700402 }
403 return NULL;
404 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700405
Brian Silverman227ad482014-03-23 11:21:32 -0700406 if (options & kFromEnd) {
407 if (options & kPeek) {
408 if (kReadDebug) {
409 printf("queue: %p shortcutting c2: %d\n", this, LastMessageIndex());
410 }
411 msg = data_[LastMessageIndex()];
412 IncrementMessageReferenceCount(msg);
413 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700414 while (true) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700415 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700416 printf("queue: %p start of c2\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700417 }
418 // This loop pulls each message out of the buffer.
419 const int pos = data_start_;
Brian Silverman4d0789d2014-03-23 17:03:07 -0700420 data_start_ = index_add1(data_start_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700421 // If this is the last one.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700422 if (data_start_ == data_end_) {
423 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700424 printf("queue: %p reading from c2: %d\n", this, pos);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700425 }
426 msg = data_[pos];
427 break;
428 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700429 // This message is not going to be in the queue any more.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700430 DecrementMessageReferenceCount(data_[pos]);
431 }
Brian Silverman227ad482014-03-23 11:21:32 -0700432 }
433 } else {
434 if (kReadDebug) {
435 printf("queue: %p reading from d2: %d\n", this, data_start_);
436 }
437 msg = data_[data_start_];
438 if (options & kPeek) {
439 IncrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700440 } else {
Brian Silverman4d0789d2014-03-23 17:03:07 -0700441 data_start_ = index_add1(data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700442 }
443 }
Brian Silverman42d52372014-03-23 15:29:13 -0700444 ReadCommonEnd();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700445 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700446 printf("queue: %p read returning %p\n", this, msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700447 }
448 return msg;
449}
Brian Silverman227ad482014-03-23 11:21:32 -0700450
Brian Silverman08661c72013-09-01 17:24:38 -0700451const void *RawQueue::ReadMessageIndex(int options, int *index) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700452 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700453 printf("queue: %p->ReadMessageIndex(%x, %p(*=%d))\n",
Brian Silvermana6d1b562013-09-01 14:39:39 -0700454 this, options, index, *index);
455 }
456 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700457
458 MutexLocker locker(&data_lock_);
459
Brian Silverman42d52372014-03-23 15:29:13 -0700460 if (!ReadCommonStart(options, index)) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700461 if (kReadDebug) {
462 printf("queue: %p common returned false\n", this);
463 }
464 return NULL;
465 }
466
467 // TODO(parker): Handle integer wrap on the index.
468
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700469 if (options & kFromEnd) {
Brian Silverman227ad482014-03-23 11:21:32 -0700470 if (kReadDebug) {
Brian Silverman227ad482014-03-23 11:21:32 -0700471 printf("queue: %p reading from c1: %d\n", this, LastMessageIndex());
472 }
473 msg = data_[LastMessageIndex()];
474 if (!(options & kPeek)) *index = messages_;
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800475 } else {
Brian Silverman227ad482014-03-23 11:21:32 -0700476 // Where we're going to start reading.
477 int my_start;
478
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700479 const int unread_messages = messages_ - *index;
480 assert(unread_messages > 0);
481 int current_messages = data_end_ - data_start_;
482 if (current_messages < 0) current_messages += data_length_;
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700483 if (kReadIndexDebug) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700484 printf("queue: %p start=%d end=%d current=%d\n",
485 this, data_start_, data_end_, current_messages);
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700486 }
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700487 assert(current_messages > 0);
488 // If we're behind the available messages.
489 if (unread_messages > current_messages) {
490 // Catch index up to the last available message.
491 *index = messages_ - current_messages;
492 // And that's the one we're going to read.
493 my_start = data_start_;
494 if (kReadIndexDebug) {
495 printf("queue: %p jumping ahead to message %d (have %d) (at %d)\n",
496 this, *index, messages_, data_start_);
497 }
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700498 } else {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700499 // Just start reading at the first available message that we haven't yet
500 // read.
501 my_start = data_end_ - unread_messages;
502 if (kReadIndexDebug) {
503 printf("queue: %p original read from %d\n", this, my_start);
504 }
505 if (data_start_ < data_end_) {
Brian Silverman42d52372014-03-23 15:29:13 -0700506 assert(my_start >= 0);
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700507 }
Brian Silverman42d52372014-03-23 15:29:13 -0700508 if (my_start < 0) my_start += data_length_;
Brian Silverman67e34f52014-03-13 15:52:57 -0700509 }
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800510
Brian Silverman227ad482014-03-23 11:21:32 -0700511 if (kReadDebug) {
512 printf("queue: %p reading from d1: %d\n", this, my_start);
Brian Silverman797e71e2013-09-06 17:29:39 -0700513 }
Brian Silverman227ad482014-03-23 11:21:32 -0700514 // We have to be either after the start or before the end, even if the queue
Brian Silverman42d52372014-03-23 15:29:13 -0700515 // is wrapped around (should be both if it's not).
Brian Silverman227ad482014-03-23 11:21:32 -0700516 assert((my_start >= data_start_) || (my_start < data_end_));
517 // More sanity checking.
518 assert((my_start >= 0) && (my_start < data_length_));
519 msg = data_[my_start];
520 if (!(options & kPeek)) ++(*index);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700521 }
Brian Silverman227ad482014-03-23 11:21:32 -0700522 IncrementMessageReferenceCount(msg);
523
Brian Silverman42d52372014-03-23 15:29:13 -0700524 ReadCommonEnd();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700525 return msg;
526}
527
Brian Silvermanc2e04222014-03-22 12:43:44 -0700528int RawQueue::FreeMessages() const {
529 int r = 0;
530 MessageHeader *header = free_messages_;
531 while (header != nullptr) {
532 ++r;
533 header = header->next;
534 }
535 return r;
536}
537
Brian Silvermanb3267322014-04-10 12:10:03 -0700538bool RawQueue::IsDebug() {
539#if QUEUE_DEBUG
540 return true;
541#else
542 return false;
543#endif
544}
545
Brian Silvermana6d1b562013-09-01 14:39:39 -0700546} // namespace aos