blob: 81af416cf5cb7b16ee0c3a626189d0fa303b495b [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 Silverman4d0789d2014-03-23 17:03:07 -070068inline int RawQueue::index_add1(int index) {
69 // Doing it this way instead of with % is more efficient on ARM.
70 int r = index + 1;
71 assert(index <= data_length_);
72 if (r == data_length_) {
73 return 0;
74 } else {
75 return r;
76 }
77}
78
Brian Silverman08661c72013-09-01 17:24:38 -070079void RawQueue::DecrementMessageReferenceCount(const void *msg) {
Brian Silvermana6d1b562013-09-01 14:39:39 -070080 MessageHeader *header = MessageHeader::Get(msg);
Brian Silvermanad290d82014-03-19 17:22:05 -070081 __atomic_sub_fetch(&header->ref_count, 1, __ATOMIC_RELAXED);
Brian Silvermana6d1b562013-09-01 14:39:39 -070082 if (kRefDebug) {
Brian Silvermanc2e04222014-03-22 12:43:44 -070083 printf("%p ref dec count: %p count=%d\n", this, msg, header->ref_count);
Brian Silvermana6d1b562013-09-01 14:39:39 -070084 }
Brian Silvermanad290d82014-03-19 17:22:05 -070085
86 // The only way it should ever be 0 is if we were the last one to decrement,
87 // in which case nobody else should have it around to re-increment it or
88 // anything in the middle, so this is safe to do not atomically with the
89 // decrement.
Brian Silvermana6d1b562013-09-01 14:39:39 -070090 if (header->ref_count == 0) {
91 DoFreeMessage(msg);
Brian Silvermanad290d82014-03-19 17:22:05 -070092 } else {
93 assert(header->ref_count > 0);
Brian Silvermana6d1b562013-09-01 14:39:39 -070094 }
95}
96
Brian Silverman227ad482014-03-23 11:21:32 -070097inline void RawQueue::IncrementMessageReferenceCount(const void *msg) const {
Brian Silverman430e7fa2014-03-21 16:58:33 -070098 MessageHeader *const header = MessageHeader::Get(msg);
99 __atomic_add_fetch(&header->ref_count, 1, __ATOMIC_RELAXED);
100 if (kRefDebug) {
Brian Silvermanc2e04222014-03-22 12:43:44 -0700101 printf("%p ref inc count: %p\n", this, msg);
Brian Silverman430e7fa2014-03-21 16:58:33 -0700102 }
103}
104
Brian Silverman42d52372014-03-23 15:29:13 -0700105inline void RawQueue::DoFreeMessage(const void *msg) {
106 MessageHeader *header = MessageHeader::Get(msg);
107 if (kRefDebug) {
108 printf("%p ref free to %p: %p\n", this, recycle_, msg);
109 }
110
111 if (__builtin_expect(recycle_ != nullptr, 0)) {
112 void *const new_msg = recycle_->GetMessage();
113 if (new_msg == nullptr) {
114 fprintf(stderr, "queue: couldn't get a message"
115 " for recycle queue %p\n", recycle_);
116 } else {
117 // Nobody else has a reference to the message at this point, so no need to
118 // be fancy about it.
119 ++header->ref_count;
120 if (!recycle_->WriteMessage(const_cast<void *>(msg), kOverride)) {
121 fprintf(stderr, "queue: %p->WriteMessage(%p, kOverride) failed."
122 " aborting\n", recycle_, msg);
123 printf("see stderr\n");
124 abort();
125 }
126 msg = new_msg;
127 header = MessageHeader::Get(new_msg);
128 }
129 }
130
131 // This works around GCC bug 60272 (fixed in 4.8.3).
132 // new_next should just get replaced with header->next (and the body of the
133 // loop should become empty).
134 // The bug is that the store to new_next after the compare/exchange is
135 // unconditional but it should only be if it fails, which could mean
136 // overwriting what somebody else who preempted us right then changed it to.
137 // TODO(brians): Get rid of this workaround once we get a new enough GCC.
138 MessageHeader *new_next = __atomic_load_n(&free_messages_, __ATOMIC_RELAXED);
139 do {
140 header->next = new_next;
141 } while (__builtin_expect(
142 !__atomic_compare_exchange_n(&free_messages_, &new_next, header, true,
143 __ATOMIC_RELEASE, __ATOMIC_RELAXED),
144 0));
145}
146
147void *RawQueue::GetMessage() {
148 // TODO(brians): Test this function.
149
150 MessageHeader *header = __atomic_load_n(&free_messages_, __ATOMIC_RELAXED);
151 do {
152 if (__builtin_expect(header == nullptr, 0)) {
153 LOG(FATAL, "overused pool of queue %p\n", this);
154 }
155 } while (__builtin_expect(
156 !__atomic_compare_exchange_n(&free_messages_, &header, header->next, true,
157 __ATOMIC_ACQ_REL, __ATOMIC_RELAXED),
158 0));
159 void *msg = reinterpret_cast<uint8_t *>(header + 1);
160 // It might be uninitialized, 0 from a previous use, or 1 from previously
161 // being recycled.
162 header->ref_count = 1;
163 static_assert(
164 __atomic_always_lock_free(sizeof(header->ref_count), &header->ref_count),
165 "we access this using not specifically atomic loads and stores");
166 if (kRefDebug) {
167 printf("%p ref alloc: %p\n", this, msg);
168 }
169 return msg;
170}
171
Brian Silverman08661c72013-09-01 17:24:38 -0700172RawQueue::RawQueue(const char *name, size_t length, int hash, int queue_length)
Brian Silverman5f8c4922014-02-11 21:22:38 -0800173 : readable_(&data_lock_), writable_(&data_lock_) {
Brian Silvermanc2e04222014-03-22 12:43:44 -0700174 static_assert(shm_ok<RawQueue::MessageHeader>::value,
175 "the whole point is to stick it in shared memory");
176 static_assert((sizeof(RawQueue::MessageHeader) % 8) == 0,
177 "need to revalidate size/alignent assumptions");
178
Brian Silverman227ad482014-03-23 11:21:32 -0700179 if (queue_length < 1) {
180 LOG(FATAL, "queue length %d needs to be at least 1\n", queue_length);
181 }
182
Brian Silvermana6d1b562013-09-01 14:39:39 -0700183 const size_t name_size = strlen(name) + 1;
184 char *temp = static_cast<char *>(shm_malloc(name_size));
185 memcpy(temp, name, name_size);
186 name_ = temp;
187 length_ = length;
188 hash_ = hash;
189 queue_length_ = queue_length;
190
191 next_ = NULL;
192 recycle_ = NULL;
193
194 if (kFetchDebug) {
195 printf("initializing name=%s, length=%zd, hash=%d, queue_length=%d\n",
196 name, length, hash, queue_length);
197 }
198
199 data_length_ = queue_length + 1;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700200 data_ = static_cast<void **>(shm_malloc(sizeof(void *) * data_length_));
201 data_start_ = 0;
202 data_end_ = 0;
203 messages_ = 0;
204
Brian Silvermana6d1b562013-09-01 14:39:39 -0700205 msg_length_ = length + sizeof(MessageHeader);
Brian Silvermanc2e04222014-03-22 12:43:44 -0700206
Brian Silverman227ad482014-03-23 11:21:32 -0700207 // Create all of the messages for the free list and stick them on.
208 {
209 MessageHeader *previous = nullptr;
210 for (int i = 0; i < queue_length + kExtraMessages; ++i) {
211 MessageHeader *const message =
212 static_cast<MessageHeader *>(shm_malloc(msg_length_));
213 free_messages_ = message;
214 message->next = previous;
215 previous = message;
216 }
Brian Silverman60eff202014-03-21 17:10:02 -0700217 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700218
219 if (kFetchDebug) {
220 printf("made queue %s\n", name);
221 }
222}
Brian Silverman42d52372014-03-23 15:29:13 -0700223
Brian Silverman08661c72013-09-01 17:24:38 -0700224RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700225 int queue_length) {
226 if (kFetchDebug) {
227 printf("fetching queue %s\n", name);
228 }
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800229 if (mutex_lock(&global_core->mem_struct->queues.lock) != 0) {
Brian Silverman227ad482014-03-23 11:21:32 -0700230 LOG(FATAL, "mutex_lock(%p) failed\n",
231 &global_core->mem_struct->queues.lock);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700232 }
Brian Silverman08661c72013-09-01 17:24:38 -0700233 RawQueue *current = static_cast<RawQueue *>(
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800234 global_core->mem_struct->queues.pointer);
Brian Silverman797e71e2013-09-06 17:29:39 -0700235 if (current != NULL) {
236 while (true) {
237 // If we found a matching queue.
238 if (strcmp(current->name_, name) == 0 && current->length_ == length &&
239 current->hash_ == hash && current->queue_length_ == queue_length) {
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800240 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700241 return current;
242 } else {
243 if (kFetchDebug) {
244 printf("rejected queue %s strcmp=%d target=%s\n", current->name_,
245 strcmp(current->name_, name), name);
246 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700247 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700248 // If this is the last one.
249 if (current->next_ == NULL) break;
250 current = current->next_;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700251 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700252 }
253
Brian Silverman797e71e2013-09-06 17:29:39 -0700254 RawQueue *r = new (shm_malloc(sizeof(RawQueue)))
255 RawQueue(name, length, hash, queue_length);
256 if (current == NULL) { // if we don't already have one
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800257 global_core->mem_struct->queues.pointer = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700258 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700259 current->next_ = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700260 }
261
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800262 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700263 return r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700264}
Brian Silverman42d52372014-03-23 15:29:13 -0700265
Brian Silverman08661c72013-09-01 17:24:38 -0700266RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700267 int queue_length,
Brian Silverman08661c72013-09-01 17:24:38 -0700268 int recycle_hash, int recycle_length, RawQueue **recycle) {
269 RawQueue *r = Fetch(name, length, hash, queue_length);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700270 r->recycle_ = Fetch(name, length, recycle_hash, recycle_length);
271 if (r == r->recycle_) {
272 fprintf(stderr, "queue: r->recycle_(=%p) == r(=%p)\n", r->recycle_, r);
273 printf("see stderr\n");
Brian Silverman797e71e2013-09-06 17:29:39 -0700274 r->recycle_ = NULL;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700275 abort();
276 }
277 *recycle = r->recycle_;
278 return r;
279}
280
Brian Silverman08661c72013-09-01 17:24:38 -0700281bool RawQueue::WriteMessage(void *msg, int options) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700282 // TODO(brians): Test this function.
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 }
286 if (msg == NULL || msg < reinterpret_cast<void *>(global_core->mem_struct) ||
287 msg > static_cast<void *>((
Brian Silverman08661c72013-09-01 17:24:38 -0700288 reinterpret_cast<char *>(global_core->mem_struct) +
Brian Silvermana6d1b562013-09-01 14:39:39 -0700289 global_core->size))) {
290 fprintf(stderr, "queue: attempt to write bad message %p to %p. aborting\n",
291 msg, this);
292 printf("see stderr\n");
293 abort();
294 }
295 {
296 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700297 bool writable_waited = false;
298
299 int new_end;
300 while (true) {
Brian Silverman4d0789d2014-03-23 17:03:07 -0700301 new_end = index_add1(data_end_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700302 // If there is room in the queue right now.
303 if (new_end != data_start_) break;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700304 if (options & kNonBlock) {
305 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700306 printf("queue: not blocking on %p. returning false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700307 }
Brian Silverman358c49f2014-03-05 16:56:34 -0800308 DecrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700309 return false;
310 } else if (options & kOverride) {
311 if (kWriteDebug) {
312 printf("queue: overriding on %p\n", this);
313 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700314 // Avoid leaking the message that we're going to overwrite.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700315 DecrementMessageReferenceCount(data_[data_start_]);
Brian Silverman4d0789d2014-03-23 17:03:07 -0700316 data_start_ = index_add1(data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700317 } else { // kBlock
318 if (kWriteDebug) {
319 printf("queue: going to wait for writable_ of %p\n", this);
320 }
Brian Silverman08661c72013-09-01 17:24:38 -0700321 writable_.Wait();
Brian Silverman797e71e2013-09-06 17:29:39 -0700322 writable_waited = true;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700323 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700324 }
325 data_[data_end_] = msg;
326 ++messages_;
327 data_end_ = new_end;
Brian Silverman797e71e2013-09-06 17:29:39 -0700328
329 if (kWriteDebug) {
330 printf("queue: broadcasting to readable_ of %p\n", this);
331 }
332 readable_.Broadcast();
333
334 // If we got a signal on writable_ here and it's still writable, then we
335 // need to signal the next person in line (if any).
336 if (writable_waited && is_writable()) {
337 if (kWriteDebug) {
338 printf("queue: resignalling writable_ of %p\n", this);
339 }
340 writable_.Signal();
341 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700342 }
343 if (kWriteDebug) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700344 printf("queue: write returning true on queue %p\n", this);
345 }
346 return true;
347}
348
Brian Silverman42d52372014-03-23 15:29:13 -0700349inline void RawQueue::ReadCommonEnd() {
Brian Silverman797e71e2013-09-06 17:29:39 -0700350 if (is_writable()) {
351 if (kReadDebug) {
352 printf("queue: %ssignalling writable_ of %p\n",
Brian Silverman42d52372014-03-23 15:29:13 -0700353 writable_start_ ? "not " : "", this);
Brian Silverman797e71e2013-09-06 17:29:39 -0700354 }
Brian Silverman42d52372014-03-23 15:29:13 -0700355 if (!writable_start_) writable_.Signal();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700356 }
357}
Brian Silverman227ad482014-03-23 11:21:32 -0700358
Brian Silverman42d52372014-03-23 15:29:13 -0700359bool RawQueue::ReadCommonStart(int options, int *index) {
360 writable_start_ = is_writable();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700361 while (data_start_ == data_end_ || ((index != NULL) && messages_ <= *index)) {
362 if (options & kNonBlock) {
363 if (kReadDebug) {
364 printf("queue: not going to block waiting on %p\n", this);
365 }
366 return false;
367 } else { // kBlock
368 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700369 printf("queue: going to wait for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700370 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700371 // Wait for a message to become readable.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700372 readable_.Wait();
373 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700374 printf("queue: done waiting for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700375 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700376 }
377 }
378 if (kReadDebug) {
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800379 printf("queue: %p->read(%p) start=%d end=%d\n", this, index, data_start_,
380 data_end_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700381 }
382 return true;
383}
Brian Silverman227ad482014-03-23 11:21:32 -0700384
385inline int RawQueue::LastMessageIndex() const {
386 int pos = data_end_ - 1;
387 if (pos < 0) { // If it wrapped around.
388 pos = data_length_ - 1;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700389 }
Brian Silverman227ad482014-03-23 11:21:32 -0700390 return pos;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700391}
Brian Silverman227ad482014-03-23 11:21:32 -0700392
Brian Silverman08661c72013-09-01 17:24:38 -0700393const void *RawQueue::ReadMessage(int options) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700394 // TODO(brians): Test this function.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700395 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700396 printf("queue: %p->ReadMessage(%x)\n", this, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700397 }
398 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700399
Brian Silvermana6d1b562013-09-01 14:39:39 -0700400 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700401
Brian Silverman42d52372014-03-23 15:29:13 -0700402 if (!ReadCommonStart(options, nullptr)) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700403 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700404 printf("queue: %p common returned false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700405 }
406 return NULL;
407 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700408
Brian Silverman227ad482014-03-23 11:21:32 -0700409 if (options & kFromEnd) {
410 if (options & kPeek) {
411 if (kReadDebug) {
412 printf("queue: %p shortcutting c2: %d\n", this, LastMessageIndex());
413 }
414 msg = data_[LastMessageIndex()];
415 IncrementMessageReferenceCount(msg);
416 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700417 while (true) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700418 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700419 printf("queue: %p start of c2\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700420 }
421 // This loop pulls each message out of the buffer.
422 const int pos = data_start_;
Brian Silverman4d0789d2014-03-23 17:03:07 -0700423 data_start_ = index_add1(data_start_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700424 // If this is the last one.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700425 if (data_start_ == data_end_) {
426 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700427 printf("queue: %p reading from c2: %d\n", this, pos);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700428 }
429 msg = data_[pos];
430 break;
431 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700432 // This message is not going to be in the queue any more.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700433 DecrementMessageReferenceCount(data_[pos]);
434 }
Brian Silverman227ad482014-03-23 11:21:32 -0700435 }
436 } else {
437 if (kReadDebug) {
438 printf("queue: %p reading from d2: %d\n", this, data_start_);
439 }
440 msg = data_[data_start_];
441 if (options & kPeek) {
442 IncrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700443 } else {
Brian Silverman4d0789d2014-03-23 17:03:07 -0700444 data_start_ = index_add1(data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700445 }
446 }
Brian Silverman42d52372014-03-23 15:29:13 -0700447 ReadCommonEnd();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700448 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700449 printf("queue: %p read returning %p\n", this, msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700450 }
451 return msg;
452}
Brian Silverman227ad482014-03-23 11:21:32 -0700453
Brian Silverman08661c72013-09-01 17:24:38 -0700454const void *RawQueue::ReadMessageIndex(int options, int *index) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700455 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700456 printf("queue: %p->ReadMessageIndex(%x, %p(*=%d))\n",
Brian Silvermana6d1b562013-09-01 14:39:39 -0700457 this, options, index, *index);
458 }
459 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700460
461 MutexLocker locker(&data_lock_);
462
Brian Silverman42d52372014-03-23 15:29:13 -0700463 if (!ReadCommonStart(options, index)) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700464 if (kReadDebug) {
465 printf("queue: %p common returned false\n", this);
466 }
467 return NULL;
468 }
469
470 // TODO(parker): Handle integer wrap on the index.
471
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700472 if (options & kFromEnd) {
Brian Silverman227ad482014-03-23 11:21:32 -0700473 if (kReadDebug) {
Brian Silverman227ad482014-03-23 11:21:32 -0700474 printf("queue: %p reading from c1: %d\n", this, LastMessageIndex());
475 }
476 msg = data_[LastMessageIndex()];
477 if (!(options & kPeek)) *index = messages_;
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800478 } else {
Brian Silverman227ad482014-03-23 11:21:32 -0700479 // Where we're going to start reading.
480 int my_start;
481
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700482 const int unread_messages = messages_ - *index;
483 assert(unread_messages > 0);
484 int current_messages = data_end_ - data_start_;
485 if (current_messages < 0) current_messages += data_length_;
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700486 if (kReadIndexDebug) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700487 printf("queue: %p start=%d end=%d current=%d\n",
488 this, data_start_, data_end_, current_messages);
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700489 }
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700490 assert(current_messages > 0);
491 // If we're behind the available messages.
492 if (unread_messages > current_messages) {
493 // Catch index up to the last available message.
494 *index = messages_ - current_messages;
495 // And that's the one we're going to read.
496 my_start = data_start_;
497 if (kReadIndexDebug) {
498 printf("queue: %p jumping ahead to message %d (have %d) (at %d)\n",
499 this, *index, messages_, data_start_);
500 }
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700501 } else {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700502 // Just start reading at the first available message that we haven't yet
503 // read.
504 my_start = data_end_ - unread_messages;
505 if (kReadIndexDebug) {
506 printf("queue: %p original read from %d\n", this, my_start);
507 }
508 if (data_start_ < data_end_) {
Brian Silverman42d52372014-03-23 15:29:13 -0700509 assert(my_start >= 0);
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700510 }
Brian Silverman42d52372014-03-23 15:29:13 -0700511 if (my_start < 0) my_start += data_length_;
Brian Silverman67e34f52014-03-13 15:52:57 -0700512 }
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800513
Brian Silverman227ad482014-03-23 11:21:32 -0700514 if (kReadDebug) {
515 printf("queue: %p reading from d1: %d\n", this, my_start);
Brian Silverman797e71e2013-09-06 17:29:39 -0700516 }
Brian Silverman227ad482014-03-23 11:21:32 -0700517 // We have to be either after the start or before the end, even if the queue
Brian Silverman42d52372014-03-23 15:29:13 -0700518 // is wrapped around (should be both if it's not).
Brian Silverman227ad482014-03-23 11:21:32 -0700519 assert((my_start >= data_start_) || (my_start < data_end_));
520 // More sanity checking.
521 assert((my_start >= 0) && (my_start < data_length_));
522 msg = data_[my_start];
523 if (!(options & kPeek)) ++(*index);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700524 }
Brian Silverman227ad482014-03-23 11:21:32 -0700525 IncrementMessageReferenceCount(msg);
526
Brian Silverman42d52372014-03-23 15:29:13 -0700527 ReadCommonEnd();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700528 return msg;
529}
530
Brian Silvermanc2e04222014-03-22 12:43:44 -0700531int RawQueue::FreeMessages() const {
532 int r = 0;
533 MessageHeader *header = free_messages_;
534 while (header != nullptr) {
535 ++r;
536 header = header->next;
537 }
538 return r;
539}
540
Brian Silvermana6d1b562013-09-01 14:39:39 -0700541} // namespace aos