blob: 67faf5b4c7fb7aa62b05288b97f4d0f744c0ed71 [file] [log] [blame]
Brian Silverman14fd0fb2014-01-14 21:42:01 -08001#include "aos/linux_code/ipc_lib/queue.h"
Brian Silvermana6d1b562013-09-01 14:39:39 -07002
3#include <stdio.h>
4#include <string.h>
5#include <errno.h>
6#include <assert.h>
7
8#include <memory>
Brian Silvermanc39e2bd2014-02-21 09:17:35 -08009#include <algorithm>
Brian Silvermana6d1b562013-09-01 14:39:39 -070010
11#include "aos/common/logging/logging.h"
12#include "aos/common/type_traits.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -080013#include "aos/linux_code/ipc_lib/core_lib.h"
Brian Silvermana6d1b562013-09-01 14:39:39 -070014
Brian Silverman227ad482014-03-23 11:21:32 -070015#undef assert
16#define assert(...)
17
Brian Silvermana6d1b562013-09-01 14:39:39 -070018namespace aos {
Brian Silvermana6d1b562013-09-01 14:39:39 -070019namespace {
20
Brian Silverman08661c72013-09-01 17:24:38 -070021static_assert(shm_ok<RawQueue>::value,
22 "RawQueue instances go into shared memory");
Brian Silvermana6d1b562013-09-01 14:39:39 -070023
24const bool kReadDebug = false;
25const bool kWriteDebug = false;
26const bool kRefDebug = false;
27const bool kFetchDebug = false;
Brian Silvermancd2d84c2014-03-13 23:30:58 -070028const bool kReadIndexDebug = false;
Brian Silvermana6d1b562013-09-01 14:39:39 -070029
30// The number of extra messages the pool associated with each queue will be able
Brian Silverman08661c72013-09-01 17:24:38 -070031// to hold (for readers who are slow about freeing them or who leak one when
32// they get killed).
Brian Silvermana6d1b562013-09-01 14:39:39 -070033const int kExtraMessages = 20;
34
35} // namespace
36
Brian Silverman08661c72013-09-01 17:24:38 -070037const int RawQueue::kPeek;
38const int RawQueue::kFromEnd;
39const int RawQueue::kNonBlock;
40const int RawQueue::kBlock;
41const int RawQueue::kOverride;
42
Brian Silverman430e7fa2014-03-21 16:58:33 -070043// This is what gets stuck in before each queue message in memory. It is always
44// allocated aligned to 8 bytes and its size has to maintain that alignment for
45// the message that follows immediately.
Brian Silverman08661c72013-09-01 17:24:38 -070046struct RawQueue::MessageHeader {
Brian Silvermanad290d82014-03-19 17:22:05 -070047 // This gets incremented and decremented with atomic instructions without any
48 // locks held.
Brian Silverman227ad482014-03-23 11:21:32 -070049 int32_t ref_count;
Brian Silvermanc2e04222014-03-22 12:43:44 -070050 MessageHeader *next;
Brian Silverman5f8c4922014-02-11 21:22:38 -080051 // Gets the message header immediately preceding msg.
Brian Silvermana6d1b562013-09-01 14:39:39 -070052 static MessageHeader *Get(const void *msg) {
Brian Silverman63cf2412013-11-17 05:44:36 -080053 return reinterpret_cast<MessageHeader *>(__builtin_assume_aligned(
54 static_cast<uint8_t *>(const_cast<void *>(msg)) - sizeof(MessageHeader),
55 alignof(MessageHeader)));
Brian Silvermana6d1b562013-09-01 14:39:39 -070056 }
Brian Silverman227ad482014-03-23 11:21:32 -070057 // Padding to make the total size 8 bytes if we have 4-byte pointers or bump
58 // it to 16 if a pointer is 8 bytes by itself.
Brian Silvermanc2e04222014-03-22 12:43:44 -070059#if __SIZEOF_POINTER__ == 8
Brian Silverman227ad482014-03-23 11:21:32 -070060 char padding[4];
Brian Silvermanc2e04222014-03-22 12:43:44 -070061#elif __SIZEOF_POINTER__ == 4
62 // No padding needed to get 8 byte total size.
63#else
64#error Unknown pointer size.
65#endif
Brian Silvermana6d1b562013-09-01 14:39:39 -070066};
Brian Silvermana6d1b562013-09-01 14:39:39 -070067
Brian Silverman08661c72013-09-01 17:24:38 -070068void RawQueue::DecrementMessageReferenceCount(const void *msg) {
Brian Silvermana6d1b562013-09-01 14:39:39 -070069 MessageHeader *header = MessageHeader::Get(msg);
Brian Silvermanad290d82014-03-19 17:22:05 -070070 __atomic_sub_fetch(&header->ref_count, 1, __ATOMIC_RELAXED);
Brian Silvermana6d1b562013-09-01 14:39:39 -070071 if (kRefDebug) {
Brian Silvermanc2e04222014-03-22 12:43:44 -070072 printf("%p ref dec count: %p count=%d\n", this, msg, header->ref_count);
Brian Silvermana6d1b562013-09-01 14:39:39 -070073 }
Brian Silvermanad290d82014-03-19 17:22:05 -070074
75 // The only way it should ever be 0 is if we were the last one to decrement,
76 // in which case nobody else should have it around to re-increment it or
77 // anything in the middle, so this is safe to do not atomically with the
78 // decrement.
Brian Silvermana6d1b562013-09-01 14:39:39 -070079 if (header->ref_count == 0) {
80 DoFreeMessage(msg);
Brian Silvermanad290d82014-03-19 17:22:05 -070081 } else {
82 assert(header->ref_count > 0);
Brian Silvermana6d1b562013-09-01 14:39:39 -070083 }
84}
85
Brian Silverman227ad482014-03-23 11:21:32 -070086inline void RawQueue::IncrementMessageReferenceCount(const void *msg) const {
Brian Silverman430e7fa2014-03-21 16:58:33 -070087 MessageHeader *const header = MessageHeader::Get(msg);
88 __atomic_add_fetch(&header->ref_count, 1, __ATOMIC_RELAXED);
89 if (kRefDebug) {
Brian Silvermanc2e04222014-03-22 12:43:44 -070090 printf("%p ref inc count: %p\n", this, msg);
Brian Silverman430e7fa2014-03-21 16:58:33 -070091 }
92}
93
Brian Silverman42d52372014-03-23 15:29:13 -070094inline void RawQueue::DoFreeMessage(const void *msg) {
95 MessageHeader *header = MessageHeader::Get(msg);
96 if (kRefDebug) {
97 printf("%p ref free to %p: %p\n", this, recycle_, msg);
98 }
99
100 if (__builtin_expect(recycle_ != nullptr, 0)) {
101 void *const new_msg = recycle_->GetMessage();
102 if (new_msg == nullptr) {
103 fprintf(stderr, "queue: couldn't get a message"
104 " for recycle queue %p\n", recycle_);
105 } else {
106 // Nobody else has a reference to the message at this point, so no need to
107 // be fancy about it.
108 ++header->ref_count;
109 if (!recycle_->WriteMessage(const_cast<void *>(msg), kOverride)) {
110 fprintf(stderr, "queue: %p->WriteMessage(%p, kOverride) failed."
111 " aborting\n", recycle_, msg);
112 printf("see stderr\n");
113 abort();
114 }
115 msg = new_msg;
116 header = MessageHeader::Get(new_msg);
117 }
118 }
119
120 // This works around GCC bug 60272 (fixed in 4.8.3).
121 // new_next should just get replaced with header->next (and the body of the
122 // loop should become empty).
123 // The bug is that the store to new_next after the compare/exchange is
124 // unconditional but it should only be if it fails, which could mean
125 // overwriting what somebody else who preempted us right then changed it to.
126 // TODO(brians): Get rid of this workaround once we get a new enough GCC.
127 MessageHeader *new_next = __atomic_load_n(&free_messages_, __ATOMIC_RELAXED);
128 do {
129 header->next = new_next;
130 } while (__builtin_expect(
131 !__atomic_compare_exchange_n(&free_messages_, &new_next, header, true,
132 __ATOMIC_RELEASE, __ATOMIC_RELAXED),
133 0));
134}
135
136void *RawQueue::GetMessage() {
137 // TODO(brians): Test this function.
138
139 MessageHeader *header = __atomic_load_n(&free_messages_, __ATOMIC_RELAXED);
140 do {
141 if (__builtin_expect(header == nullptr, 0)) {
142 LOG(FATAL, "overused pool of queue %p\n", this);
143 }
144 } while (__builtin_expect(
145 !__atomic_compare_exchange_n(&free_messages_, &header, header->next, true,
146 __ATOMIC_ACQ_REL, __ATOMIC_RELAXED),
147 0));
148 void *msg = reinterpret_cast<uint8_t *>(header + 1);
149 // It might be uninitialized, 0 from a previous use, or 1 from previously
150 // being recycled.
151 header->ref_count = 1;
152 static_assert(
153 __atomic_always_lock_free(sizeof(header->ref_count), &header->ref_count),
154 "we access this using not specifically atomic loads and stores");
155 if (kRefDebug) {
156 printf("%p ref alloc: %p\n", this, msg);
157 }
158 return msg;
159}
160
Brian Silverman08661c72013-09-01 17:24:38 -0700161RawQueue::RawQueue(const char *name, size_t length, int hash, int queue_length)
Brian Silverman5f8c4922014-02-11 21:22:38 -0800162 : readable_(&data_lock_), writable_(&data_lock_) {
Brian Silvermanc2e04222014-03-22 12:43:44 -0700163 static_assert(shm_ok<RawQueue::MessageHeader>::value,
164 "the whole point is to stick it in shared memory");
165 static_assert((sizeof(RawQueue::MessageHeader) % 8) == 0,
166 "need to revalidate size/alignent assumptions");
167
Brian Silverman227ad482014-03-23 11:21:32 -0700168 if (queue_length < 1) {
169 LOG(FATAL, "queue length %d needs to be at least 1\n", queue_length);
170 }
171
Brian Silvermana6d1b562013-09-01 14:39:39 -0700172 const size_t name_size = strlen(name) + 1;
173 char *temp = static_cast<char *>(shm_malloc(name_size));
174 memcpy(temp, name, name_size);
175 name_ = temp;
176 length_ = length;
177 hash_ = hash;
178 queue_length_ = queue_length;
179
180 next_ = NULL;
181 recycle_ = NULL;
182
183 if (kFetchDebug) {
184 printf("initializing name=%s, length=%zd, hash=%d, queue_length=%d\n",
185 name, length, hash, queue_length);
186 }
187
188 data_length_ = queue_length + 1;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700189 data_ = static_cast<void **>(shm_malloc(sizeof(void *) * data_length_));
190 data_start_ = 0;
191 data_end_ = 0;
192 messages_ = 0;
193
Brian Silvermana6d1b562013-09-01 14:39:39 -0700194 msg_length_ = length + sizeof(MessageHeader);
Brian Silvermanc2e04222014-03-22 12:43:44 -0700195
Brian Silverman227ad482014-03-23 11:21:32 -0700196 // Create all of the messages for the free list and stick them on.
197 {
198 MessageHeader *previous = nullptr;
199 for (int i = 0; i < queue_length + kExtraMessages; ++i) {
200 MessageHeader *const message =
201 static_cast<MessageHeader *>(shm_malloc(msg_length_));
202 free_messages_ = message;
203 message->next = previous;
204 previous = message;
205 }
Brian Silverman60eff202014-03-21 17:10:02 -0700206 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700207
208 if (kFetchDebug) {
209 printf("made queue %s\n", name);
210 }
211}
Brian Silverman42d52372014-03-23 15:29:13 -0700212
Brian Silverman08661c72013-09-01 17:24:38 -0700213RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700214 int queue_length) {
215 if (kFetchDebug) {
216 printf("fetching queue %s\n", name);
217 }
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800218 if (mutex_lock(&global_core->mem_struct->queues.lock) != 0) {
Brian Silverman227ad482014-03-23 11:21:32 -0700219 LOG(FATAL, "mutex_lock(%p) failed\n",
220 &global_core->mem_struct->queues.lock);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700221 }
Brian Silverman08661c72013-09-01 17:24:38 -0700222 RawQueue *current = static_cast<RawQueue *>(
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800223 global_core->mem_struct->queues.pointer);
Brian Silverman797e71e2013-09-06 17:29:39 -0700224 if (current != NULL) {
225 while (true) {
226 // If we found a matching queue.
227 if (strcmp(current->name_, name) == 0 && current->length_ == length &&
228 current->hash_ == hash && current->queue_length_ == queue_length) {
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800229 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700230 return current;
231 } else {
232 if (kFetchDebug) {
233 printf("rejected queue %s strcmp=%d target=%s\n", current->name_,
234 strcmp(current->name_, name), name);
235 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700236 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700237 // If this is the last one.
238 if (current->next_ == NULL) break;
239 current = current->next_;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700240 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700241 }
242
Brian Silverman797e71e2013-09-06 17:29:39 -0700243 RawQueue *r = new (shm_malloc(sizeof(RawQueue)))
244 RawQueue(name, length, hash, queue_length);
245 if (current == NULL) { // if we don't already have one
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800246 global_core->mem_struct->queues.pointer = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700247 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700248 current->next_ = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700249 }
250
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800251 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700252 return r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700253}
Brian Silverman42d52372014-03-23 15:29:13 -0700254
Brian Silverman08661c72013-09-01 17:24:38 -0700255RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700256 int queue_length,
Brian Silverman08661c72013-09-01 17:24:38 -0700257 int recycle_hash, int recycle_length, RawQueue **recycle) {
258 RawQueue *r = Fetch(name, length, hash, queue_length);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700259 r->recycle_ = Fetch(name, length, recycle_hash, recycle_length);
260 if (r == r->recycle_) {
261 fprintf(stderr, "queue: r->recycle_(=%p) == r(=%p)\n", r->recycle_, r);
262 printf("see stderr\n");
Brian Silverman797e71e2013-09-06 17:29:39 -0700263 r->recycle_ = NULL;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700264 abort();
265 }
266 *recycle = r->recycle_;
267 return r;
268}
269
Brian Silverman08661c72013-09-01 17:24:38 -0700270bool RawQueue::WriteMessage(void *msg, int options) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700271 // TODO(brians): Test this function.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700272 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700273 printf("queue: %p->WriteMessage(%p, %x)\n", this, msg, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700274 }
275 if (msg == NULL || msg < reinterpret_cast<void *>(global_core->mem_struct) ||
276 msg > static_cast<void *>((
Brian Silverman08661c72013-09-01 17:24:38 -0700277 reinterpret_cast<char *>(global_core->mem_struct) +
Brian Silvermana6d1b562013-09-01 14:39:39 -0700278 global_core->size))) {
279 fprintf(stderr, "queue: attempt to write bad message %p to %p. aborting\n",
280 msg, this);
281 printf("see stderr\n");
282 abort();
283 }
284 {
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) {
290 new_end = (data_end_ + 1) % data_length_;
291 // 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_]);
305 data_start_ = (data_start_ + 1) % data_length_;
306 } 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_;
412 data_start_ = (data_start_ + 1) % data_length_;
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 Silvermana6d1b562013-09-01 14:39:39 -0700433 data_start_ = (data_start_ + 1) % data_length_;
434 }
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