blob: b92ceb92648d9139ca1071ab5e8c5183c26fb88a [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
15namespace aos {
Brian Silvermana6d1b562013-09-01 14:39:39 -070016namespace {
17
Brian Silverman08661c72013-09-01 17:24:38 -070018static_assert(shm_ok<RawQueue>::value,
19 "RawQueue instances go into shared memory");
Brian Silvermana6d1b562013-09-01 14:39:39 -070020
21const bool kReadDebug = false;
22const bool kWriteDebug = false;
23const bool kRefDebug = false;
24const bool kFetchDebug = false;
Brian Silvermancd2d84c2014-03-13 23:30:58 -070025const bool kReadIndexDebug = false;
Brian Silvermana6d1b562013-09-01 14:39:39 -070026
27// The number of extra messages the pool associated with each queue will be able
Brian Silverman08661c72013-09-01 17:24:38 -070028// to hold (for readers who are slow about freeing them or who leak one when
29// they get killed).
Brian Silvermana6d1b562013-09-01 14:39:39 -070030const int kExtraMessages = 20;
31
32} // namespace
33
Brian Silverman08661c72013-09-01 17:24:38 -070034const int RawQueue::kPeek;
35const int RawQueue::kFromEnd;
36const int RawQueue::kNonBlock;
37const int RawQueue::kBlock;
38const int RawQueue::kOverride;
39
Brian Silverman430e7fa2014-03-21 16:58:33 -070040// This is what gets stuck in before each queue message in memory. It is always
41// allocated aligned to 8 bytes and its size has to maintain that alignment for
42// the message that follows immediately.
Brian Silverman08661c72013-09-01 17:24:38 -070043struct RawQueue::MessageHeader {
Brian Silvermanad290d82014-03-19 17:22:05 -070044 // This gets incremented and decremented with atomic instructions without any
45 // locks held.
Brian Silvermana6d1b562013-09-01 14:39:39 -070046 int ref_count;
47 int index; // in pool_
Brian Silverman5f8c4922014-02-11 21:22:38 -080048 // Gets the message header immediately preceding msg.
Brian Silvermana6d1b562013-09-01 14:39:39 -070049 static MessageHeader *Get(const void *msg) {
Brian Silverman63cf2412013-11-17 05:44:36 -080050 return reinterpret_cast<MessageHeader *>(__builtin_assume_aligned(
51 static_cast<uint8_t *>(const_cast<void *>(msg)) - sizeof(MessageHeader),
52 alignof(MessageHeader)));
Brian Silvermana6d1b562013-09-01 14:39:39 -070053 }
54 void Swap(MessageHeader *other) {
55 MessageHeader temp;
56 memcpy(&temp, other, sizeof(temp));
57 memcpy(other, this, sizeof(*other));
58 memcpy(this, &temp, sizeof(*this));
59 }
60};
Brian Silverman5f8c4922014-02-11 21:22:38 -080061static_assert(shm_ok<RawQueue::MessageHeader>::value,
62 "the whole point is to stick it in shared memory");
Brian Silvermana6d1b562013-09-01 14:39:39 -070063
Brian Silverman797e71e2013-09-06 17:29:39 -070064struct RawQueue::ReadData {
65 bool writable_start;
66};
67
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) {
72 printf("ref_dec_count: %p count=%d\n", msg, header->ref_count);
73 }
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 Silverman430e7fa2014-03-21 16:58:33 -070086void RawQueue::IncrementMessageReferenceCount(const void *msg) const {
87 MessageHeader *const header = MessageHeader::Get(msg);
88 __atomic_add_fetch(&header->ref_count, 1, __ATOMIC_RELAXED);
89 if (kRefDebug) {
90 printf("ref inc count: %p\n", msg);
91 }
92}
93
Brian Silverman08661c72013-09-01 17:24:38 -070094RawQueue::RawQueue(const char *name, size_t length, int hash, int queue_length)
Brian Silverman5f8c4922014-02-11 21:22:38 -080095 : readable_(&data_lock_), writable_(&data_lock_) {
Brian Silvermana6d1b562013-09-01 14:39:39 -070096 const size_t name_size = strlen(name) + 1;
97 char *temp = static_cast<char *>(shm_malloc(name_size));
98 memcpy(temp, name, name_size);
99 name_ = temp;
100 length_ = length;
101 hash_ = hash;
102 queue_length_ = queue_length;
103
104 next_ = NULL;
105 recycle_ = NULL;
106
107 if (kFetchDebug) {
108 printf("initializing name=%s, length=%zd, hash=%d, queue_length=%d\n",
109 name, length, hash, queue_length);
110 }
111
112 data_length_ = queue_length + 1;
113 if (data_length_ < 2) { // TODO(brians) when could this happen?
114 data_length_ = 2;
115 }
116 data_ = static_cast<void **>(shm_malloc(sizeof(void *) * data_length_));
117 data_start_ = 0;
118 data_end_ = 0;
119 messages_ = 0;
120
Brian Silverman60eff202014-03-21 17:10:02 -0700121 pool_length_ = queue_length + kExtraMessages;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700122 messages_used_ = 0;
123 msg_length_ = length + sizeof(MessageHeader);
124 pool_ = static_cast<MessageHeader **>(
Brian Silverman60eff202014-03-21 17:10:02 -0700125 shm_malloc(sizeof(MessageHeader *) * pool_length_));
126 for (int i = 0; i < pool_length_; ++i) {
127 pool_[i] =
128 static_cast<MessageHeader *>(shm_malloc(msg_length_));
129 pool_[i]->index = i;
130 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700131
132 if (kFetchDebug) {
133 printf("made queue %s\n", name);
134 }
135}
Brian Silverman08661c72013-09-01 17:24:38 -0700136RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700137 int queue_length) {
138 if (kFetchDebug) {
139 printf("fetching queue %s\n", name);
140 }
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800141 if (mutex_lock(&global_core->mem_struct->queues.lock) != 0) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700142 return NULL;
143 }
Brian Silverman08661c72013-09-01 17:24:38 -0700144 RawQueue *current = static_cast<RawQueue *>(
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800145 global_core->mem_struct->queues.pointer);
Brian Silverman797e71e2013-09-06 17:29:39 -0700146 if (current != NULL) {
147 while (true) {
148 // If we found a matching queue.
149 if (strcmp(current->name_, name) == 0 && current->length_ == length &&
150 current->hash_ == hash && current->queue_length_ == queue_length) {
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800151 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700152 return current;
153 } else {
154 if (kFetchDebug) {
155 printf("rejected queue %s strcmp=%d target=%s\n", current->name_,
156 strcmp(current->name_, name), name);
157 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700158 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700159 // If this is the last one.
160 if (current->next_ == NULL) break;
161 current = current->next_;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700162 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700163 }
164
Brian Silverman797e71e2013-09-06 17:29:39 -0700165 RawQueue *r = new (shm_malloc(sizeof(RawQueue)))
166 RawQueue(name, length, hash, queue_length);
167 if (current == NULL) { // if we don't already have one
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800168 global_core->mem_struct->queues.pointer = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700169 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700170 current->next_ = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700171 }
172
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800173 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700174 return r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700175}
Brian Silverman08661c72013-09-01 17:24:38 -0700176RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700177 int queue_length,
Brian Silverman08661c72013-09-01 17:24:38 -0700178 int recycle_hash, int recycle_length, RawQueue **recycle) {
179 RawQueue *r = Fetch(name, length, hash, queue_length);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700180 r->recycle_ = Fetch(name, length, recycle_hash, recycle_length);
181 if (r == r->recycle_) {
182 fprintf(stderr, "queue: r->recycle_(=%p) == r(=%p)\n", r->recycle_, r);
183 printf("see stderr\n");
Brian Silverman797e71e2013-09-06 17:29:39 -0700184 r->recycle_ = NULL;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700185 abort();
186 }
187 *recycle = r->recycle_;
188 return r;
189}
190
Brian Silverman08661c72013-09-01 17:24:38 -0700191void RawQueue::DoFreeMessage(const void *msg) {
Brian Silverman430e7fa2014-03-21 16:58:33 -0700192 MutexLocker locker(&pool_lock_);
193
Brian Silvermana6d1b562013-09-01 14:39:39 -0700194 MessageHeader *header = MessageHeader::Get(msg);
195 if (pool_[header->index] != header) { // if something's messed up
196 fprintf(stderr, "queue: something is very very wrong with queue %p."
197 " pool_(=%p)[header->index(=%d)] != header(=%p)\n",
198 this, pool_, header->index, header);
199 printf("queue: see stderr\n");
200 abort();
201 }
202 if (kRefDebug) {
203 printf("ref free: %p\n", msg);
204 }
205 --messages_used_;
206
207 if (recycle_ != NULL) {
208 void *const new_msg = recycle_->GetMessage();
209 if (new_msg == NULL) {
210 fprintf(stderr, "queue: couldn't get a message"
211 " for recycle queue %p\n", recycle_);
212 } else {
213 // Take a message from recycle_ and switch its
214 // header with the one being freed, which effectively
215 // switches which queue each message belongs to.
216 MessageHeader *const new_header = MessageHeader::Get(new_msg);
Brian Silverman797e71e2013-09-06 17:29:39 -0700217 // Also switch the messages between the pools.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700218 pool_[header->index] = new_header;
219 {
220 MutexLocker locker(&recycle_->pool_lock_);
221 recycle_->pool_[new_header->index] = header;
Brian Silverman797e71e2013-09-06 17:29:39 -0700222 // Swap the information in both headers.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700223 header->Swap(new_header);
Brian Silverman797e71e2013-09-06 17:29:39 -0700224 // Don't unlock the other pool until all of its messages are valid.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700225 }
226 // use the header for new_msg which is now for this pool
227 header = new_header;
228 if (!recycle_->WriteMessage(const_cast<void *>(msg), kOverride)) {
229 fprintf(stderr, "queue: %p->WriteMessage(%p, kOverride) failed."
230 " aborting\n", recycle_, msg);
231 printf("see stderr\n");
232 abort();
233 }
234 msg = new_msg;
235 }
236 }
237
Brian Silverman430e7fa2014-03-21 16:58:33 -0700238 // If we're not freeing the one on the end.
239 if (header->index != messages_used_) {
240 // The one that is on the end before we change it.
241 MessageHeader *const other_header = pool_[messages_used_];
Brian Silverman797e71e2013-09-06 17:29:39 -0700242 // Put the last one where the one we're freeing was.
Brian Silverman430e7fa2014-03-21 16:58:33 -0700243 pool_[header->index] = other_header;
Brian Silverman797e71e2013-09-06 17:29:39 -0700244 // Update the former last one's index.
Brian Silverman430e7fa2014-03-21 16:58:33 -0700245 other_header->index = header->index;
246 // Put the one we're freeing at the end.
247 pool_[messages_used_] = header;
Brian Silverman1f3ba712014-03-21 17:01:55 -0700248 header->index = messages_used_;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700249 }
250}
251
Brian Silverman08661c72013-09-01 17:24:38 -0700252bool RawQueue::WriteMessage(void *msg, int options) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700253 // TODO(brians): Test this function.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700254 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700255 printf("queue: %p->WriteMessage(%p, %x)\n", this, msg, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700256 }
257 if (msg == NULL || msg < reinterpret_cast<void *>(global_core->mem_struct) ||
258 msg > static_cast<void *>((
Brian Silverman08661c72013-09-01 17:24:38 -0700259 reinterpret_cast<char *>(global_core->mem_struct) +
Brian Silvermana6d1b562013-09-01 14:39:39 -0700260 global_core->size))) {
261 fprintf(stderr, "queue: attempt to write bad message %p to %p. aborting\n",
262 msg, this);
263 printf("see stderr\n");
264 abort();
265 }
266 {
267 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700268 bool writable_waited = false;
269
270 int new_end;
271 while (true) {
272 new_end = (data_end_ + 1) % data_length_;
273 // If there is room in the queue right now.
274 if (new_end != data_start_) break;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700275 if (options & kNonBlock) {
276 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700277 printf("queue: not blocking on %p. returning false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700278 }
Brian Silverman358c49f2014-03-05 16:56:34 -0800279 DecrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700280 return false;
281 } else if (options & kOverride) {
282 if (kWriteDebug) {
283 printf("queue: overriding on %p\n", this);
284 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700285 // Avoid leaking the message that we're going to overwrite.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700286 DecrementMessageReferenceCount(data_[data_start_]);
287 data_start_ = (data_start_ + 1) % data_length_;
288 } else { // kBlock
289 if (kWriteDebug) {
290 printf("queue: going to wait for writable_ of %p\n", this);
291 }
Brian Silverman08661c72013-09-01 17:24:38 -0700292 writable_.Wait();
Brian Silverman797e71e2013-09-06 17:29:39 -0700293 writable_waited = true;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700294 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700295 }
296 data_[data_end_] = msg;
297 ++messages_;
298 data_end_ = new_end;
Brian Silverman797e71e2013-09-06 17:29:39 -0700299
300 if (kWriteDebug) {
301 printf("queue: broadcasting to readable_ of %p\n", this);
302 }
303 readable_.Broadcast();
304
305 // If we got a signal on writable_ here and it's still writable, then we
306 // need to signal the next person in line (if any).
307 if (writable_waited && is_writable()) {
308 if (kWriteDebug) {
309 printf("queue: resignalling writable_ of %p\n", this);
310 }
311 writable_.Signal();
312 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700313 }
314 if (kWriteDebug) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700315 printf("queue: write returning true on queue %p\n", this);
316 }
317 return true;
318}
319
Brian Silverman797e71e2013-09-06 17:29:39 -0700320void RawQueue::ReadCommonEnd(ReadData *read_data) {
321 if (is_writable()) {
322 if (kReadDebug) {
323 printf("queue: %ssignalling writable_ of %p\n",
324 read_data->writable_start ? "not " : "", this);
325 }
326 if (!read_data->writable_start) writable_.Signal();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700327 }
328}
Brian Silverman797e71e2013-09-06 17:29:39 -0700329bool RawQueue::ReadCommonStart(int options, int *index, ReadData *read_data) {
330 read_data->writable_start = is_writable();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700331 while (data_start_ == data_end_ || ((index != NULL) && messages_ <= *index)) {
332 if (options & kNonBlock) {
333 if (kReadDebug) {
334 printf("queue: not going to block waiting on %p\n", this);
335 }
336 return false;
337 } else { // kBlock
338 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700339 printf("queue: going to wait for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700340 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700341 // Wait for a message to become readable.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700342 readable_.Wait();
343 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700344 printf("queue: done waiting for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700345 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700346 }
347 }
348 if (kReadDebug) {
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800349 printf("queue: %p->read(%p) start=%d end=%d\n", this, index, data_start_,
350 data_end_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700351 }
352 return true;
353}
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700354void *RawQueue::ReadPeek(int options, int start) const {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700355 void *ret;
356 if (options & kFromEnd) {
357 int pos = data_end_ - 1;
358 if (pos < 0) { // if it needs to wrap
359 pos = data_length_ - 1;
360 }
361 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700362 printf("queue: %p reading from line %d: %d\n", this, __LINE__, pos);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700363 }
364 ret = data_[pos];
365 } else {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700366 assert(start != -1);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700367 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700368 printf("queue: %p reading from line %d: %d\n", this, __LINE__, start);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700369 }
370 ret = data_[start];
371 }
Brian Silverman430e7fa2014-03-21 16:58:33 -0700372 IncrementMessageReferenceCount(ret);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700373 return ret;
374}
Brian Silverman08661c72013-09-01 17:24:38 -0700375const void *RawQueue::ReadMessage(int options) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700376 // TODO(brians): Test this function.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700377 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700378 printf("queue: %p->ReadMessage(%x)\n", this, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700379 }
380 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700381
Brian Silvermana6d1b562013-09-01 14:39:39 -0700382 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700383
384 ReadData read_data;
385 if (!ReadCommonStart(options, NULL, &read_data)) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700386 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700387 printf("queue: %p common returned false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700388 }
389 return NULL;
390 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700391
Brian Silvermana6d1b562013-09-01 14:39:39 -0700392 if (options & kPeek) {
393 msg = ReadPeek(options, data_start_);
394 } else {
395 if (options & kFromEnd) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700396 while (true) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700397 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700398 printf("queue: %p start of c2\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700399 }
400 // This loop pulls each message out of the buffer.
401 const int pos = data_start_;
402 data_start_ = (data_start_ + 1) % data_length_;
Brian Silverman797e71e2013-09-06 17:29:39 -0700403 // If this is the last one.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700404 if (data_start_ == data_end_) {
405 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700406 printf("queue: %p reading from c2: %d\n", this, pos);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700407 }
408 msg = data_[pos];
409 break;
410 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700411 // This message is not going to be in the queue any more.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700412 DecrementMessageReferenceCount(data_[pos]);
413 }
414 } else {
415 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700416 printf("queue: %p reading from d2: %d\n", this, data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700417 }
418 msg = data_[data_start_];
419 data_start_ = (data_start_ + 1) % data_length_;
420 }
421 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700422 ReadCommonEnd(&read_data);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700423 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700424 printf("queue: %p read returning %p\n", this, msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700425 }
426 return msg;
427}
Brian Silverman08661c72013-09-01 17:24:38 -0700428const void *RawQueue::ReadMessageIndex(int options, int *index) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700429 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700430 printf("queue: %p->ReadMessageIndex(%x, %p(*=%d))\n",
Brian Silvermana6d1b562013-09-01 14:39:39 -0700431 this, options, index, *index);
432 }
433 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700434
435 MutexLocker locker(&data_lock_);
436
437 ReadData read_data;
438 if (!ReadCommonStart(options, index, &read_data)) {
439 if (kReadDebug) {
440 printf("queue: %p common returned false\n", this);
441 }
442 return NULL;
443 }
444
445 // TODO(parker): Handle integer wrap on the index.
446
Brian Silverman797e71e2013-09-06 17:29:39 -0700447 // Where we're going to start reading.
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800448 int my_start;
449
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700450 if (options & kFromEnd) {
451 my_start = -1;
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800452 } else {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700453 const int unread_messages = messages_ - *index;
454 assert(unread_messages > 0);
455 int current_messages = data_end_ - data_start_;
456 if (current_messages < 0) current_messages += data_length_;
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700457 if (kReadIndexDebug) {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700458 printf("queue: %p start=%d end=%d current=%d\n",
459 this, data_start_, data_end_, current_messages);
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700460 }
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700461 assert(current_messages > 0);
462 // If we're behind the available messages.
463 if (unread_messages > current_messages) {
464 // Catch index up to the last available message.
465 *index = messages_ - current_messages;
466 // And that's the one we're going to read.
467 my_start = data_start_;
468 if (kReadIndexDebug) {
469 printf("queue: %p jumping ahead to message %d (have %d) (at %d)\n",
470 this, *index, messages_, data_start_);
471 }
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700472 } else {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700473 // Just start reading at the first available message that we haven't yet
474 // read.
475 my_start = data_end_ - unread_messages;
476 if (kReadIndexDebug) {
477 printf("queue: %p original read from %d\n", this, my_start);
478 }
479 if (data_start_ < data_end_) {
480 assert(my_start >= data_start_);
481 } else {
482 if (my_start < 0) my_start += data_length_;
483 }
Brian Silverman67e34f52014-03-13 15:52:57 -0700484 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700485 }
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800486
Brian Silverman797e71e2013-09-06 17:29:39 -0700487 if (options & kPeek) {
488 msg = ReadPeek(options, my_start);
489 } else {
490 if (options & kFromEnd) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700491 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700492 printf("queue: %p start of c1\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700493 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700494 int pos = data_end_ - 1;
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700495 if (kReadIndexDebug) {
496 printf("queue: %p end pos start %d\n", this, pos);
497 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700498 if (pos < 0) { // If it wrapped.
499 pos = data_length_ - 1; // Unwrap it.
500 }
501 if (kReadDebug) {
502 printf("queue: %p reading from c1: %d\n", this, pos);
503 }
504 msg = data_[pos];
505 *index = messages_;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700506 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700507 if (kReadDebug) {
508 printf("queue: %p reading from d1: %d\n", this, my_start);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700509 }
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800510 // This assert checks that we're either within both endpoints (duh) or
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700511 // not between them (if the queue is wrapped around).
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800512 assert((my_start >= data_start_ && my_start < data_end_) ||
Brian Silvermancd2d84c2014-03-13 23:30:58 -0700513 ((my_start >= data_start_) == (my_start > data_end_)));
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700514 // More sanity checking.
515 assert((my_start >= 0) && (my_start < data_length_));
Brian Silverman797e71e2013-09-06 17:29:39 -0700516 msg = data_[my_start];
517 ++(*index);
518 }
Brian Silverman430e7fa2014-03-21 16:58:33 -0700519 IncrementMessageReferenceCount(msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700520 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700521 ReadCommonEnd(&read_data);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700522 return msg;
523}
524
Brian Silverman08661c72013-09-01 17:24:38 -0700525void *RawQueue::GetMessage() {
Brian Silvermaneb51cbb2014-03-14 22:57:08 -0700526 // TODO(brians): Test this function.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700527 MutexLocker locker(&pool_lock_);
Brian Silverman60eff202014-03-21 17:10:02 -0700528 MessageHeader *const header = pool_[messages_used_];
529 if (messages_used_ >= pool_length_) {
530 LOG(FATAL, "overused pool of queue %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700531 }
Brian Silverman60eff202014-03-21 17:10:02 -0700532 assert(header->index == messages_used_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700533 void *msg = reinterpret_cast<uint8_t *>(header) + sizeof(MessageHeader);
534 header->ref_count = 1;
Brian Silvermanad290d82014-03-19 17:22:05 -0700535 static_assert(
536 __atomic_always_lock_free(sizeof(header->ref_count), &header->ref_count),
537 "we access this using not specifically atomic loads and stores");
Brian Silvermana6d1b562013-09-01 14:39:39 -0700538 if (kRefDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700539 printf("%p ref alloc: %p\n", this, msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700540 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700541 ++messages_used_;
542 return msg;
543}
544
545} // namespace aos