blob: ab117d146279ef864dfa42c36dd6b1969dcaedf2 [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;
26const bool kWriteDebug = false;
27const 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() {
149 // TODO(brians): Test this function.
150
151 MessageHeader *header = __atomic_load_n(&free_messages_, __ATOMIC_RELAXED);
152 do {
153 if (__builtin_expect(header == nullptr, 0)) {
154 LOG(FATAL, "overused pool of queue %p\n", this);
155 }
156 } while (__builtin_expect(
157 !__atomic_compare_exchange_n(&free_messages_, &header, header->next, true,
158 __ATOMIC_ACQ_REL, __ATOMIC_RELAXED),
159 0));
160 void *msg = reinterpret_cast<uint8_t *>(header + 1);
161 // It might be uninitialized, 0 from a previous use, or 1 from previously
162 // being recycled.
163 header->ref_count = 1;
164 static_assert(
165 __atomic_always_lock_free(sizeof(header->ref_count), &header->ref_count),
166 "we access this using not specifically atomic loads and stores");
167 if (kRefDebug) {
168 printf("%p ref alloc: %p\n", this, msg);
169 }
170 return msg;
171}
172
Brian Silverman08661c72013-09-01 17:24:38 -0700173RawQueue::RawQueue(const char *name, size_t length, int hash, int queue_length)
Brian Silverman5f8c4922014-02-11 21:22:38 -0800174 : readable_(&data_lock_), writable_(&data_lock_) {
Brian Silvermanc2e04222014-03-22 12:43:44 -0700175 static_assert(shm_ok<RawQueue::MessageHeader>::value,
176 "the whole point is to stick it in shared memory");
177 static_assert((sizeof(RawQueue::MessageHeader) % 8) == 0,
178 "need to revalidate size/alignent assumptions");
179
Brian Silverman227ad482014-03-23 11:21:32 -0700180 if (queue_length < 1) {
181 LOG(FATAL, "queue length %d needs to be at least 1\n", queue_length);
182 }
183
Brian Silvermana6d1b562013-09-01 14:39:39 -0700184 const size_t name_size = strlen(name) + 1;
185 char *temp = static_cast<char *>(shm_malloc(name_size));
186 memcpy(temp, name, name_size);
187 name_ = temp;
188 length_ = length;
189 hash_ = hash;
190 queue_length_ = queue_length;
191
192 next_ = NULL;
193 recycle_ = NULL;
194
195 if (kFetchDebug) {
196 printf("initializing name=%s, length=%zd, hash=%d, queue_length=%d\n",
197 name, length, hash, queue_length);
198 }
199
200 data_length_ = queue_length + 1;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700201 data_ = static_cast<void **>(shm_malloc(sizeof(void *) * data_length_));
202 data_start_ = 0;
203 data_end_ = 0;
204 messages_ = 0;
205
Brian Silvermana6d1b562013-09-01 14:39:39 -0700206 msg_length_ = length + sizeof(MessageHeader);
Brian Silvermanc2e04222014-03-22 12:43:44 -0700207
Brian Silverman227ad482014-03-23 11:21:32 -0700208 // Create all of the messages for the free list and stick them on.
209 {
210 MessageHeader *previous = nullptr;
211 for (int i = 0; i < queue_length + kExtraMessages; ++i) {
212 MessageHeader *const message =
213 static_cast<MessageHeader *>(shm_malloc(msg_length_));
214 free_messages_ = message;
215 message->next = previous;
216 previous = message;
217 }
Brian Silverman60eff202014-03-21 17:10:02 -0700218 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700219
220 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 Silvermaneb51cbb2014-03-14 22:57:08 -0700283 // TODO(brians): Test this function.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700284 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700285 printf("queue: %p->WriteMessage(%p, %x)\n", this, msg, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700286 }
287 if (msg == NULL || msg < reinterpret_cast<void *>(global_core->mem_struct) ||
288 msg > static_cast<void *>((
Brian Silverman08661c72013-09-01 17:24:38 -0700289 reinterpret_cast<char *>(global_core->mem_struct) +
Brian Silvermana6d1b562013-09-01 14:39:39 -0700290 global_core->size))) {
291 fprintf(stderr, "queue: attempt to write bad message %p to %p. aborting\n",
292 msg, this);
293 printf("see stderr\n");
294 abort();
295 }
296 {
297 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700298 bool writable_waited = false;
299
300 int new_end;
301 while (true) {
Brian Silverman4d0789d2014-03-23 17:03:07 -0700302 new_end = index_add1(data_end_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700303 // If there is room in the queue right now.
304 if (new_end != data_start_) break;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700305 if (options & kNonBlock) {
306 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700307 printf("queue: not blocking on %p. returning false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700308 }
Brian Silverman358c49f2014-03-05 16:56:34 -0800309 DecrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700310 return false;
311 } else if (options & kOverride) {
312 if (kWriteDebug) {
313 printf("queue: overriding on %p\n", this);
314 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700315 // Avoid leaking the message that we're going to overwrite.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700316 DecrementMessageReferenceCount(data_[data_start_]);
Brian Silverman4d0789d2014-03-23 17:03:07 -0700317 data_start_ = index_add1(data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700318 } else { // kBlock
319 if (kWriteDebug) {
320 printf("queue: going to wait for writable_ of %p\n", this);
321 }
Brian Silverman08661c72013-09-01 17:24:38 -0700322 writable_.Wait();
Brian Silverman797e71e2013-09-06 17:29:39 -0700323 writable_waited = true;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700324 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700325 }
326 data_[data_end_] = msg;
327 ++messages_;
328 data_end_ = new_end;
Brian Silverman797e71e2013-09-06 17:29:39 -0700329
330 if (kWriteDebug) {
331 printf("queue: broadcasting to readable_ of %p\n", this);
332 }
333 readable_.Broadcast();
334
335 // If we got a signal on writable_ here and it's still writable, then we
336 // need to signal the next person in line (if any).
337 if (writable_waited && is_writable()) {
338 if (kWriteDebug) {
339 printf("queue: resignalling writable_ of %p\n", this);
340 }
341 writable_.Signal();
342 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700343 }
344 if (kWriteDebug) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700345 printf("queue: write returning true on queue %p\n", this);
346 }
347 return true;
348}
349
Brian Silverman42d52372014-03-23 15:29:13 -0700350inline void RawQueue::ReadCommonEnd() {
Brian Silverman797e71e2013-09-06 17:29:39 -0700351 if (is_writable()) {
352 if (kReadDebug) {
353 printf("queue: %ssignalling writable_ of %p\n",
Brian Silverman42d52372014-03-23 15:29:13 -0700354 writable_start_ ? "not " : "", this);
Brian Silverman797e71e2013-09-06 17:29:39 -0700355 }
Brian Silverman42d52372014-03-23 15:29:13 -0700356 if (!writable_start_) writable_.Signal();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700357 }
358}
Brian Silverman227ad482014-03-23 11:21:32 -0700359
Brian Silverman42d52372014-03-23 15:29:13 -0700360bool RawQueue::ReadCommonStart(int options, int *index) {
361 writable_start_ = is_writable();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700362 while (data_start_ == data_end_ || ((index != NULL) && messages_ <= *index)) {
363 if (options & kNonBlock) {
364 if (kReadDebug) {
365 printf("queue: not going to block waiting on %p\n", this);
366 }
367 return false;
368 } else { // kBlock
369 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700370 printf("queue: going to wait for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700371 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700372 // Wait for a message to become readable.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700373 readable_.Wait();
374 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700375 printf("queue: done waiting for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700376 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700377 }
378 }
379 if (kReadDebug) {
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800380 printf("queue: %p->read(%p) start=%d end=%d\n", this, index, data_start_,
381 data_end_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700382 }
383 return true;
384}
Brian Silverman227ad482014-03-23 11:21:32 -0700385
386inline int RawQueue::LastMessageIndex() const {
387 int pos = data_end_ - 1;
388 if (pos < 0) { // If it wrapped around.
389 pos = data_length_ - 1;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700390 }
Brian Silverman227ad482014-03-23 11:21:32 -0700391 return pos;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700392}
Brian Silverman227ad482014-03-23 11:21:32 -0700393
Brian Silverman08661c72013-09-01 17:24:38 -0700394const void *RawQueue::ReadMessage(int options) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700395 // TODO(brians): Test this function.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700396 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700397 printf("queue: %p->ReadMessage(%x)\n", this, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700398 }
399 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700400
Brian Silvermana6d1b562013-09-01 14:39:39 -0700401 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700402
Brian Silverman42d52372014-03-23 15:29:13 -0700403 if (!ReadCommonStart(options, nullptr)) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700404 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700405 printf("queue: %p common returned false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700406 }
407 return NULL;
408 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700409
Brian Silverman227ad482014-03-23 11:21:32 -0700410 if (options & kFromEnd) {
411 if (options & kPeek) {
412 if (kReadDebug) {
413 printf("queue: %p shortcutting c2: %d\n", this, LastMessageIndex());
414 }
415 msg = data_[LastMessageIndex()];
416 IncrementMessageReferenceCount(msg);
417 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700418 while (true) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700419 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700420 printf("queue: %p start of c2\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700421 }
422 // This loop pulls each message out of the buffer.
423 const int pos = data_start_;
Brian Silverman4d0789d2014-03-23 17:03:07 -0700424 data_start_ = index_add1(data_start_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700425 // If this is the last one.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700426 if (data_start_ == data_end_) {
427 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700428 printf("queue: %p reading from c2: %d\n", this, pos);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700429 }
430 msg = data_[pos];
431 break;
432 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700433 // This message is not going to be in the queue any more.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700434 DecrementMessageReferenceCount(data_[pos]);
435 }
Brian Silverman227ad482014-03-23 11:21:32 -0700436 }
437 } else {
438 if (kReadDebug) {
439 printf("queue: %p reading from d2: %d\n", this, data_start_);
440 }
441 msg = data_[data_start_];
442 if (options & kPeek) {
443 IncrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700444 } else {
Brian Silverman4d0789d2014-03-23 17:03:07 -0700445 data_start_ = index_add1(data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700446 }
447 }
Brian Silverman42d52372014-03-23 15:29:13 -0700448 ReadCommonEnd();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700449 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700450 printf("queue: %p read returning %p\n", this, msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700451 }
452 return msg;
453}
Brian Silverman227ad482014-03-23 11:21:32 -0700454
Brian Silverman08661c72013-09-01 17:24:38 -0700455const void *RawQueue::ReadMessageIndex(int options, int *index) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700456 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700457 printf("queue: %p->ReadMessageIndex(%x, %p(*=%d))\n",
Brian Silvermana6d1b562013-09-01 14:39:39 -0700458 this, options, index, *index);
459 }
460 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700461
462 MutexLocker locker(&data_lock_);
463
Brian Silverman42d52372014-03-23 15:29:13 -0700464 if (!ReadCommonStart(options, index)) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700465 if (kReadDebug) {
466 printf("queue: %p common returned false\n", this);
467 }
468 return NULL;
469 }
470
471 // TODO(parker): Handle integer wrap on the index.
472
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700473 if (options & kFromEnd) {
Brian Silverman227ad482014-03-23 11:21:32 -0700474 if (kReadDebug) {
Brian Silverman227ad482014-03-23 11:21:32 -0700475 printf("queue: %p reading from c1: %d\n", this, LastMessageIndex());
476 }
477 msg = data_[LastMessageIndex()];
478 if (!(options & kPeek)) *index = messages_;
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800479 } else {
Brian Silverman227ad482014-03-23 11:21:32 -0700480 // Where we're going to start reading.
481 int my_start;
482
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700483 const int unread_messages = messages_ - *index;
484 assert(unread_messages > 0);
485 int current_messages = data_end_ - data_start_;
486 if (current_messages < 0) current_messages += data_length_;
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700487 if (kReadIndexDebug) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700488 printf("queue: %p start=%d end=%d current=%d\n",
489 this, data_start_, data_end_, current_messages);
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700490 }
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700491 assert(current_messages > 0);
492 // If we're behind the available messages.
493 if (unread_messages > current_messages) {
494 // Catch index up to the last available message.
495 *index = messages_ - current_messages;
496 // And that's the one we're going to read.
497 my_start = data_start_;
498 if (kReadIndexDebug) {
499 printf("queue: %p jumping ahead to message %d (have %d) (at %d)\n",
500 this, *index, messages_, data_start_);
501 }
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700502 } else {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700503 // Just start reading at the first available message that we haven't yet
504 // read.
505 my_start = data_end_ - unread_messages;
506 if (kReadIndexDebug) {
507 printf("queue: %p original read from %d\n", this, my_start);
508 }
509 if (data_start_ < data_end_) {
Brian Silverman42d52372014-03-23 15:29:13 -0700510 assert(my_start >= 0);
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700511 }
Brian Silverman42d52372014-03-23 15:29:13 -0700512 if (my_start < 0) my_start += data_length_;
Brian Silverman67e34f52014-03-13 15:52:57 -0700513 }
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800514
Brian Silverman227ad482014-03-23 11:21:32 -0700515 if (kReadDebug) {
516 printf("queue: %p reading from d1: %d\n", this, my_start);
Brian Silverman797e71e2013-09-06 17:29:39 -0700517 }
Brian Silverman227ad482014-03-23 11:21:32 -0700518 // We have to be either after the start or before the end, even if the queue
Brian Silverman42d52372014-03-23 15:29:13 -0700519 // is wrapped around (should be both if it's not).
Brian Silverman227ad482014-03-23 11:21:32 -0700520 assert((my_start >= data_start_) || (my_start < data_end_));
521 // More sanity checking.
522 assert((my_start >= 0) && (my_start < data_length_));
523 msg = data_[my_start];
524 if (!(options & kPeek)) ++(*index);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700525 }
Brian Silverman227ad482014-03-23 11:21:32 -0700526 IncrementMessageReferenceCount(msg);
527
Brian Silverman42d52372014-03-23 15:29:13 -0700528 ReadCommonEnd();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700529 return msg;
530}
531
Brian Silvermanc2e04222014-03-22 12:43:44 -0700532int RawQueue::FreeMessages() const {
533 int r = 0;
534 MessageHeader *header = free_messages_;
535 while (header != nullptr) {
536 ++r;
537 header = header->next;
538 }
539 return r;
540}
541
Brian Silvermana6d1b562013-09-01 14:39:39 -0700542} // namespace aos