blob: 92191929ffd76bf0173cd15a5281517b8d1ba4fa [file] [log] [blame]
Brian Silvermanaac705c2014-05-01 18:55:34 -07001#if !AOS_DEBUG
Brian Silverman9eaf91a2014-03-24 16:37:44 -07002#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
Brian Silvermana6d1b562013-09-01 14:39:39 -070015#include "aos/common/type_traits.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -080016#include "aos/linux_code/ipc_lib/core_lib.h"
Brian Silvermana6d1b562013-09-01 14:39:39 -070017
18namespace 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;
Brian Silvermanbad7c8a2014-03-26 20:45:18 -070025const bool kWriteDebug = false;
Brian Silvermana6d1b562013-09-01 14:39:39 -070026const 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 Silverman7faaec72014-05-26 16:25:38 -070037constexpr Options<RawQueue>::Option RawQueue::kPeek;
38constexpr Options<RawQueue>::Option RawQueue::kFromEnd;
39constexpr Options<RawQueue>::Option RawQueue::kNonBlock;
40constexpr Options<RawQueue>::Option RawQueue::kBlock;
41constexpr Options<RawQueue>::Option RawQueue::kOverride;
Brian Silverman08661c72013-09-01 17:24:38 -070042
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 Silvermanc2e04222014-03-22 12:43:44 -070047 MessageHeader *next;
Brian Silvermane8337b72014-04-27 19:52:19 -070048
Brian Silverman5f8c4922014-02-11 21:22:38 -080049 // Gets the message header immediately preceding msg.
Brian Silvermana6d1b562013-09-01 14:39:39 -070050 static MessageHeader *Get(const void *msg) {
Brian Silverman63cf2412013-11-17 05:44:36 -080051 return reinterpret_cast<MessageHeader *>(__builtin_assume_aligned(
52 static_cast<uint8_t *>(const_cast<void *>(msg)) - sizeof(MessageHeader),
53 alignof(MessageHeader)));
Brian Silvermana6d1b562013-09-01 14:39:39 -070054 }
Brian Silvermane8337b72014-04-27 19:52:19 -070055
56 int32_t ref_count() const {
57 return __atomic_load_n(&ref_count_, __ATOMIC_RELAXED);
58 }
59 void set_ref_count(int32_t val) {
60 __atomic_store_n(&ref_count_, val, __ATOMIC_RELAXED);
61 }
62
63 void ref_count_sub() {
Brian Silvermane8337b72014-04-27 19:52:19 -070064 __atomic_sub_fetch(&ref_count_, 1, __ATOMIC_RELAXED);
Brian Silvermane8337b72014-04-27 19:52:19 -070065 }
66 void ref_count_add() {
Brian Silvermane8337b72014-04-27 19:52:19 -070067 __atomic_add_fetch(&ref_count_, 1, __ATOMIC_RELAXED);
Brian Silvermane8337b72014-04-27 19:52:19 -070068 }
69
70 private:
71 // This gets accessed with atomic instructions without any
72 // locks held by various member functions.
73 int32_t ref_count_;
74
Brian Silverman227ad482014-03-23 11:21:32 -070075 // Padding to make the total size 8 bytes if we have 4-byte pointers or bump
76 // it to 16 if a pointer is 8 bytes by itself.
Brian Silvermanc2e04222014-03-22 12:43:44 -070077#if __SIZEOF_POINTER__ == 8
Brian Silverman4b09fce2014-04-27 19:58:14 -070078#ifdef __clang__
79 // Clang is smart enough to realize this is unused, but GCC doesn't like the
80 // attribute here...
81 __attribute__((unused))
82#endif
Brian Silverman227ad482014-03-23 11:21:32 -070083 char padding[4];
Brian Silvermanc2e04222014-03-22 12:43:44 -070084#elif __SIZEOF_POINTER__ == 4
85 // No padding needed to get 8 byte total size.
86#else
87#error Unknown pointer size.
88#endif
Brian Silvermana6d1b562013-09-01 14:39:39 -070089};
Brian Silvermana6d1b562013-09-01 14:39:39 -070090
Brian Silverman4d0789d2014-03-23 17:03:07 -070091inline int RawQueue::index_add1(int index) {
92 // Doing it this way instead of with % is more efficient on ARM.
93 int r = index + 1;
94 assert(index <= data_length_);
95 if (r == data_length_) {
96 return 0;
97 } else {
98 return r;
99 }
100}
101
Brian Silverman08661c72013-09-01 17:24:38 -0700102void RawQueue::DecrementMessageReferenceCount(const void *msg) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700103 MessageHeader *header = MessageHeader::Get(msg);
Brian Silvermane8337b72014-04-27 19:52:19 -0700104 header->ref_count_sub();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700105 if (kRefDebug) {
Brian Silvermane8337b72014-04-27 19:52:19 -0700106 printf("%p ref dec count: %p count=%d\n", this, msg, header->ref_count());
Brian Silvermana6d1b562013-09-01 14:39:39 -0700107 }
Brian Silvermanad290d82014-03-19 17:22:05 -0700108
109 // The only way it should ever be 0 is if we were the last one to decrement,
110 // in which case nobody else should have it around to re-increment it or
111 // anything in the middle, so this is safe to do not atomically with the
112 // decrement.
Brian Silvermane8337b72014-04-27 19:52:19 -0700113 if (header->ref_count() == 0) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700114 DoFreeMessage(msg);
Brian Silvermanad290d82014-03-19 17:22:05 -0700115 } else {
Brian Silvermane8337b72014-04-27 19:52:19 -0700116 assert(header->ref_count() > 0);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700117 }
118}
119
Brian Silverman227ad482014-03-23 11:21:32 -0700120inline void RawQueue::IncrementMessageReferenceCount(const void *msg) const {
Brian Silverman430e7fa2014-03-21 16:58:33 -0700121 MessageHeader *const header = MessageHeader::Get(msg);
Brian Silvermane8337b72014-04-27 19:52:19 -0700122 header->ref_count_add();
Brian Silverman430e7fa2014-03-21 16:58:33 -0700123 if (kRefDebug) {
Brian Silvermanc2e04222014-03-22 12:43:44 -0700124 printf("%p ref inc count: %p\n", this, msg);
Brian Silverman430e7fa2014-03-21 16:58:33 -0700125 }
126}
127
Brian Silverman42d52372014-03-23 15:29:13 -0700128inline void RawQueue::DoFreeMessage(const void *msg) {
129 MessageHeader *header = MessageHeader::Get(msg);
130 if (kRefDebug) {
131 printf("%p ref free to %p: %p\n", this, recycle_, msg);
132 }
133
134 if (__builtin_expect(recycle_ != nullptr, 0)) {
135 void *const new_msg = recycle_->GetMessage();
136 if (new_msg == nullptr) {
137 fprintf(stderr, "queue: couldn't get a message"
138 " for recycle queue %p\n", recycle_);
139 } else {
Brian Silvermane8337b72014-04-27 19:52:19 -0700140 header->ref_count_add();
Brian Silverman42d52372014-03-23 15:29:13 -0700141 if (!recycle_->WriteMessage(const_cast<void *>(msg), kOverride)) {
142 fprintf(stderr, "queue: %p->WriteMessage(%p, kOverride) failed."
143 " aborting\n", recycle_, msg);
144 printf("see stderr\n");
145 abort();
146 }
147 msg = new_msg;
148 header = MessageHeader::Get(new_msg);
149 }
150 }
151
152 // This works around GCC bug 60272 (fixed in 4.8.3).
153 // new_next should just get replaced with header->next (and the body of the
154 // loop should become empty).
155 // The bug is that the store to new_next after the compare/exchange is
156 // unconditional but it should only be if it fails, which could mean
157 // overwriting what somebody else who preempted us right then changed it to.
158 // TODO(brians): Get rid of this workaround once we get a new enough GCC.
159 MessageHeader *new_next = __atomic_load_n(&free_messages_, __ATOMIC_RELAXED);
160 do {
161 header->next = new_next;
162 } while (__builtin_expect(
163 !__atomic_compare_exchange_n(&free_messages_, &new_next, header, true,
164 __ATOMIC_RELEASE, __ATOMIC_RELAXED),
165 0));
166}
167
168void *RawQueue::GetMessage() {
Brian Silverman42d52372014-03-23 15:29:13 -0700169 MessageHeader *header = __atomic_load_n(&free_messages_, __ATOMIC_RELAXED);
170 do {
171 if (__builtin_expect(header == nullptr, 0)) {
172 LOG(FATAL, "overused pool of queue %p\n", this);
173 }
174 } while (__builtin_expect(
175 !__atomic_compare_exchange_n(&free_messages_, &header, header->next, true,
176 __ATOMIC_ACQ_REL, __ATOMIC_RELAXED),
177 0));
178 void *msg = reinterpret_cast<uint8_t *>(header + 1);
179 // It might be uninitialized, 0 from a previous use, or 1 from previously
180 // being recycled.
Brian Silvermane8337b72014-04-27 19:52:19 -0700181 header->set_ref_count(1);
Brian Silverman42d52372014-03-23 15:29:13 -0700182 if (kRefDebug) {
183 printf("%p ref alloc: %p\n", this, msg);
184 }
185 return msg;
186}
187
Brian Silverman08661c72013-09-01 17:24:38 -0700188RawQueue::RawQueue(const char *name, size_t length, int hash, int queue_length)
Brian Silverman5f8c4922014-02-11 21:22:38 -0800189 : readable_(&data_lock_), writable_(&data_lock_) {
Brian Silvermanc2e04222014-03-22 12:43:44 -0700190 static_assert(shm_ok<RawQueue::MessageHeader>::value,
191 "the whole point is to stick it in shared memory");
192 static_assert((sizeof(RawQueue::MessageHeader) % 8) == 0,
193 "need to revalidate size/alignent assumptions");
194
Brian Silverman227ad482014-03-23 11:21:32 -0700195 if (queue_length < 1) {
196 LOG(FATAL, "queue length %d needs to be at least 1\n", queue_length);
197 }
198
Brian Silvermana6d1b562013-09-01 14:39:39 -0700199 const size_t name_size = strlen(name) + 1;
200 char *temp = static_cast<char *>(shm_malloc(name_size));
201 memcpy(temp, name, name_size);
202 name_ = temp;
203 length_ = length;
204 hash_ = hash;
205 queue_length_ = queue_length;
206
207 next_ = NULL;
208 recycle_ = NULL;
209
210 if (kFetchDebug) {
211 printf("initializing name=%s, length=%zd, hash=%d, queue_length=%d\n",
212 name, length, hash, queue_length);
213 }
214
215 data_length_ = queue_length + 1;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700216 data_ = static_cast<void **>(shm_malloc(sizeof(void *) * data_length_));
217 data_start_ = 0;
218 data_end_ = 0;
219 messages_ = 0;
220
Brian Silvermana6d1b562013-09-01 14:39:39 -0700221 msg_length_ = length + sizeof(MessageHeader);
Brian Silvermanc2e04222014-03-22 12:43:44 -0700222
Brian Silverman227ad482014-03-23 11:21:32 -0700223 // Create all of the messages for the free list and stick them on.
224 {
225 MessageHeader *previous = nullptr;
226 for (int i = 0; i < queue_length + kExtraMessages; ++i) {
227 MessageHeader *const message =
228 static_cast<MessageHeader *>(shm_malloc(msg_length_));
229 free_messages_ = message;
230 message->next = previous;
231 previous = message;
232 }
Brian Silverman60eff202014-03-21 17:10:02 -0700233 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700234
Brian Silverman35109802014-04-09 14:31:53 -0700235 readable_waiting_ = false;
236
Brian Silvermana6d1b562013-09-01 14:39:39 -0700237 if (kFetchDebug) {
238 printf("made queue %s\n", name);
239 }
240}
Brian Silverman42d52372014-03-23 15:29:13 -0700241
Brian Silverman08661c72013-09-01 17:24:38 -0700242RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700243 int queue_length) {
244 if (kFetchDebug) {
245 printf("fetching queue %s\n", name);
246 }
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800247 if (mutex_lock(&global_core->mem_struct->queues.lock) != 0) {
Brian Silverman227ad482014-03-23 11:21:32 -0700248 LOG(FATAL, "mutex_lock(%p) failed\n",
249 &global_core->mem_struct->queues.lock);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700250 }
Brian Silverman08661c72013-09-01 17:24:38 -0700251 RawQueue *current = static_cast<RawQueue *>(
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800252 global_core->mem_struct->queues.pointer);
Brian Silverman797e71e2013-09-06 17:29:39 -0700253 if (current != NULL) {
254 while (true) {
255 // If we found a matching queue.
256 if (strcmp(current->name_, name) == 0 && current->length_ == length &&
257 current->hash_ == hash && current->queue_length_ == queue_length) {
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800258 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700259 return current;
260 } else {
261 if (kFetchDebug) {
262 printf("rejected queue %s strcmp=%d target=%s\n", current->name_,
263 strcmp(current->name_, name), name);
264 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700265 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700266 // If this is the last one.
267 if (current->next_ == NULL) break;
268 current = current->next_;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700269 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700270 }
271
Brian Silverman797e71e2013-09-06 17:29:39 -0700272 RawQueue *r = new (shm_malloc(sizeof(RawQueue)))
273 RawQueue(name, length, hash, queue_length);
274 if (current == NULL) { // if we don't already have one
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800275 global_core->mem_struct->queues.pointer = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700276 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700277 current->next_ = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700278 }
279
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800280 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700281 return r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700282}
Brian Silverman42d52372014-03-23 15:29:13 -0700283
Brian Silverman08661c72013-09-01 17:24:38 -0700284RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700285 int queue_length,
Brian Silverman08661c72013-09-01 17:24:38 -0700286 int recycle_hash, int recycle_length, RawQueue **recycle) {
287 RawQueue *r = Fetch(name, length, hash, queue_length);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700288 r->recycle_ = Fetch(name, length, recycle_hash, recycle_length);
289 if (r == r->recycle_) {
290 fprintf(stderr, "queue: r->recycle_(=%p) == r(=%p)\n", r->recycle_, r);
291 printf("see stderr\n");
Brian Silverman797e71e2013-09-06 17:29:39 -0700292 r->recycle_ = NULL;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700293 abort();
294 }
295 *recycle = r->recycle_;
296 return r;
297}
298
Brian Silverman7faaec72014-05-26 16:25:38 -0700299bool RawQueue::DoWriteMessage(void *msg, Options<RawQueue> options) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700300 if (kWriteDebug) {
Brian Silverman7faaec72014-05-26 16:25:38 -0700301 printf("queue: %p->WriteMessage(%p, %x)\n", this, msg, options.printable());
Brian Silvermana6d1b562013-09-01 14:39:39 -0700302 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700303 {
Brian Silvermandc1eb272014-08-19 14:25:59 -0400304 IPCMutexLocker locker(&data_lock_);
305 CHECK(!locker.owner_died());
Brian Silverman797e71e2013-09-06 17:29:39 -0700306 bool writable_waited = false;
307
308 int new_end;
309 while (true) {
Brian Silverman4d0789d2014-03-23 17:03:07 -0700310 new_end = index_add1(data_end_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700311 // If there is room in the queue right now.
312 if (new_end != data_start_) break;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700313 if (options & kNonBlock) {
314 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700315 printf("queue: not blocking on %p. returning false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700316 }
Brian Silverman358c49f2014-03-05 16:56:34 -0800317 DecrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700318 return false;
319 } else if (options & kOverride) {
320 if (kWriteDebug) {
321 printf("queue: overriding on %p\n", this);
322 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700323 // Avoid leaking the message that we're going to overwrite.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700324 DecrementMessageReferenceCount(data_[data_start_]);
Brian Silverman4d0789d2014-03-23 17:03:07 -0700325 data_start_ = index_add1(data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700326 } else { // kBlock
Brian Silverman7faaec72014-05-26 16:25:38 -0700327 assert(options & kBlock);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700328 if (kWriteDebug) {
329 printf("queue: going to wait for writable_ of %p\n", this);
330 }
Brian Silvermandc1eb272014-08-19 14:25:59 -0400331 CHECK(!writable_.Wait());
Brian Silverman797e71e2013-09-06 17:29:39 -0700332 writable_waited = true;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700333 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700334 }
335 data_[data_end_] = msg;
336 ++messages_;
337 data_end_ = new_end;
Brian Silverman797e71e2013-09-06 17:29:39 -0700338
Brian Silverman35109802014-04-09 14:31:53 -0700339 if (readable_waiting_) {
340 if (kWriteDebug) {
341 printf("queue: broadcasting to readable_ of %p\n", this);
342 }
343 readable_waiting_ = false;
344 readable_.Broadcast();
345 } else if (kWriteDebug) {
346 printf("queue: skipping broadcast to readable_ of %p\n", this);
Brian Silverman797e71e2013-09-06 17:29:39 -0700347 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700348
349 // If we got a signal on writable_ here and it's still writable, then we
350 // need to signal the next person in line (if any).
351 if (writable_waited && is_writable()) {
352 if (kWriteDebug) {
353 printf("queue: resignalling writable_ of %p\n", this);
354 }
355 writable_.Signal();
356 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700357 }
358 if (kWriteDebug) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700359 printf("queue: write returning true on queue %p\n", this);
360 }
361 return true;
362}
363
Brian Silverman42d52372014-03-23 15:29:13 -0700364inline void RawQueue::ReadCommonEnd() {
Brian Silverman797e71e2013-09-06 17:29:39 -0700365 if (is_writable()) {
366 if (kReadDebug) {
367 printf("queue: %ssignalling writable_ of %p\n",
Brian Silverman42d52372014-03-23 15:29:13 -0700368 writable_start_ ? "not " : "", this);
Brian Silverman797e71e2013-09-06 17:29:39 -0700369 }
Brian Silverman42d52372014-03-23 15:29:13 -0700370 if (!writable_start_) writable_.Signal();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700371 }
372}
Brian Silverman227ad482014-03-23 11:21:32 -0700373
Brian Silverman7faaec72014-05-26 16:25:38 -0700374bool RawQueue::ReadCommonStart(Options<RawQueue> options, int *index) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700375 while (data_start_ == data_end_ || ((index != NULL) && messages_ <= *index)) {
376 if (options & kNonBlock) {
377 if (kReadDebug) {
378 printf("queue: not going to block waiting on %p\n", this);
379 }
380 return false;
381 } else { // kBlock
Brian Silverman7faaec72014-05-26 16:25:38 -0700382 assert(options & kBlock);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700383 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700384 printf("queue: going to wait for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700385 }
Brian Silverman35109802014-04-09 14:31:53 -0700386 readable_waiting_ = true;
Brian Silverman797e71e2013-09-06 17:29:39 -0700387 // Wait for a message to become readable.
Brian Silvermandc1eb272014-08-19 14:25:59 -0400388 CHECK(!readable_.Wait());
Brian Silvermana6d1b562013-09-01 14:39:39 -0700389 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700390 printf("queue: done waiting for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700391 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700392 }
393 }
Brian Silverman9c9f1982014-05-24 12:01:49 -0700394 // We have to check down here because we might have unlocked the mutex while
395 // Wait()ing above so this value might have changed.
396 writable_start_ = is_writable();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700397 if (kReadDebug) {
Brian Silverman9c9f1982014-05-24 12:01:49 -0700398 printf("queue: %p->read(%p) start=%d end=%d writable_start=%d\n",
399 this, index, data_start_, data_end_, writable_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700400 }
401 return true;
402}
Brian Silverman227ad482014-03-23 11:21:32 -0700403
404inline int RawQueue::LastMessageIndex() const {
405 int pos = data_end_ - 1;
406 if (pos < 0) { // If it wrapped around.
407 pos = data_length_ - 1;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700408 }
Brian Silverman227ad482014-03-23 11:21:32 -0700409 return pos;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700410}
Brian Silverman227ad482014-03-23 11:21:32 -0700411
Brian Silverman7faaec72014-05-26 16:25:38 -0700412const void *RawQueue::DoReadMessage(Options<RawQueue> options) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700413 // TODO(brians): Test this function.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700414 if (kReadDebug) {
Brian Silverman7faaec72014-05-26 16:25:38 -0700415 printf("queue: %p->ReadMessage(%x)\n", this, options.printable());
Brian Silvermana6d1b562013-09-01 14:39:39 -0700416 }
417 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700418
Brian Silvermandc1eb272014-08-19 14:25:59 -0400419 IPCMutexLocker locker(&data_lock_);
420 CHECK(!locker.owner_died());
Brian Silverman797e71e2013-09-06 17:29:39 -0700421
Brian Silverman42d52372014-03-23 15:29:13 -0700422 if (!ReadCommonStart(options, nullptr)) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700423 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700424 printf("queue: %p common returned false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700425 }
426 return NULL;
427 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700428
Brian Silverman227ad482014-03-23 11:21:32 -0700429 if (options & kFromEnd) {
430 if (options & kPeek) {
431 if (kReadDebug) {
432 printf("queue: %p shortcutting c2: %d\n", this, LastMessageIndex());
433 }
434 msg = data_[LastMessageIndex()];
435 IncrementMessageReferenceCount(msg);
436 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700437 while (true) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700438 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700439 printf("queue: %p start of c2\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700440 }
441 // This loop pulls each message out of the buffer.
442 const int pos = data_start_;
Brian Silverman4d0789d2014-03-23 17:03:07 -0700443 data_start_ = index_add1(data_start_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700444 // If this is the last one.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700445 if (data_start_ == data_end_) {
446 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700447 printf("queue: %p reading from c2: %d\n", this, pos);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700448 }
449 msg = data_[pos];
450 break;
451 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700452 // This message is not going to be in the queue any more.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700453 DecrementMessageReferenceCount(data_[pos]);
454 }
Brian Silverman227ad482014-03-23 11:21:32 -0700455 }
456 } else {
457 if (kReadDebug) {
458 printf("queue: %p reading from d2: %d\n", this, data_start_);
459 }
460 msg = data_[data_start_];
461 if (options & kPeek) {
462 IncrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700463 } else {
Brian Silverman4d0789d2014-03-23 17:03:07 -0700464 data_start_ = index_add1(data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700465 }
466 }
Brian Silverman42d52372014-03-23 15:29:13 -0700467 ReadCommonEnd();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700468 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700469 printf("queue: %p read returning %p\n", this, msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700470 }
471 return msg;
472}
Brian Silverman227ad482014-03-23 11:21:32 -0700473
Brian Silverman7faaec72014-05-26 16:25:38 -0700474const void *RawQueue::DoReadMessageIndex(Options<RawQueue> options,
475 int *index) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700476 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700477 printf("queue: %p->ReadMessageIndex(%x, %p(*=%d))\n",
Brian Silverman7faaec72014-05-26 16:25:38 -0700478 this, options.printable(), index, *index);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700479 }
480 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700481
Brian Silvermandc1eb272014-08-19 14:25:59 -0400482 IPCMutexLocker locker(&data_lock_);
483 CHECK(!locker.owner_died());
Brian Silverman797e71e2013-09-06 17:29:39 -0700484
Brian Silverman42d52372014-03-23 15:29:13 -0700485 if (!ReadCommonStart(options, index)) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700486 if (kReadDebug) {
487 printf("queue: %p common returned false\n", this);
488 }
489 return NULL;
490 }
491
492 // TODO(parker): Handle integer wrap on the index.
493
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700494 if (options & kFromEnd) {
Brian Silverman227ad482014-03-23 11:21:32 -0700495 if (kReadDebug) {
Brian Silverman227ad482014-03-23 11:21:32 -0700496 printf("queue: %p reading from c1: %d\n", this, LastMessageIndex());
497 }
498 msg = data_[LastMessageIndex()];
Brian Silverman7faaec72014-05-26 16:25:38 -0700499
500 // We'd skip this if we had kPeek, but kPeek | kFromEnd isn't valid for
501 // reading with an index.
502 *index = messages_;
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800503 } else {
Brian Silverman227ad482014-03-23 11:21:32 -0700504 // Where we're going to start reading.
505 int my_start;
506
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700507 const int unread_messages = messages_ - *index;
508 assert(unread_messages > 0);
509 int current_messages = data_end_ - data_start_;
510 if (current_messages < 0) current_messages += data_length_;
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700511 if (kReadIndexDebug) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700512 printf("queue: %p start=%d end=%d current=%d\n",
513 this, data_start_, data_end_, current_messages);
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700514 }
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700515 assert(current_messages > 0);
516 // If we're behind the available messages.
517 if (unread_messages > current_messages) {
518 // Catch index up to the last available message.
519 *index = messages_ - current_messages;
520 // And that's the one we're going to read.
521 my_start = data_start_;
522 if (kReadIndexDebug) {
523 printf("queue: %p jumping ahead to message %d (have %d) (at %d)\n",
524 this, *index, messages_, data_start_);
525 }
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700526 } else {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700527 // Just start reading at the first available message that we haven't yet
528 // read.
529 my_start = data_end_ - unread_messages;
530 if (kReadIndexDebug) {
531 printf("queue: %p original read from %d\n", this, my_start);
532 }
533 if (data_start_ < data_end_) {
Brian Silverman42d52372014-03-23 15:29:13 -0700534 assert(my_start >= 0);
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700535 }
Brian Silverman42d52372014-03-23 15:29:13 -0700536 if (my_start < 0) my_start += data_length_;
Brian Silverman67e34f52014-03-13 15:52:57 -0700537 }
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800538
Brian Silverman227ad482014-03-23 11:21:32 -0700539 if (kReadDebug) {
540 printf("queue: %p reading from d1: %d\n", this, my_start);
Brian Silverman797e71e2013-09-06 17:29:39 -0700541 }
Brian Silverman227ad482014-03-23 11:21:32 -0700542 // We have to be either after the start or before the end, even if the queue
Brian Silverman42d52372014-03-23 15:29:13 -0700543 // is wrapped around (should be both if it's not).
Brian Silverman227ad482014-03-23 11:21:32 -0700544 assert((my_start >= data_start_) || (my_start < data_end_));
545 // More sanity checking.
546 assert((my_start >= 0) && (my_start < data_length_));
547 msg = data_[my_start];
548 if (!(options & kPeek)) ++(*index);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700549 }
Brian Silverman227ad482014-03-23 11:21:32 -0700550 IncrementMessageReferenceCount(msg);
551
Brian Silverman42d52372014-03-23 15:29:13 -0700552 ReadCommonEnd();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700553 return msg;
554}
555
Brian Silvermanc2e04222014-03-22 12:43:44 -0700556int RawQueue::FreeMessages() const {
557 int r = 0;
558 MessageHeader *header = free_messages_;
559 while (header != nullptr) {
560 ++r;
561 header = header->next;
562 }
563 return r;
564}
565
Brian Silvermana6d1b562013-09-01 14:39:39 -0700566} // namespace aos