blob: 6c8b046ad8d9518aa21348b435f3ee69a968a850 [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;
25
26// The number of extra messages the pool associated with each queue will be able
Brian Silverman08661c72013-09-01 17:24:38 -070027// to hold (for readers who are slow about freeing them or who leak one when
28// they get killed).
Brian Silvermana6d1b562013-09-01 14:39:39 -070029const int kExtraMessages = 20;
30
31} // namespace
32
Brian Silverman08661c72013-09-01 17:24:38 -070033const int RawQueue::kPeek;
34const int RawQueue::kFromEnd;
35const int RawQueue::kNonBlock;
36const int RawQueue::kBlock;
37const int RawQueue::kOverride;
38
39struct RawQueue::MessageHeader {
Brian Silvermana6d1b562013-09-01 14:39:39 -070040 int ref_count;
41 int index; // in pool_
Brian Silverman5f8c4922014-02-11 21:22:38 -080042 // Gets the message header immediately preceding msg.
Brian Silvermana6d1b562013-09-01 14:39:39 -070043 static MessageHeader *Get(const void *msg) {
Brian Silverman63cf2412013-11-17 05:44:36 -080044 return reinterpret_cast<MessageHeader *>(__builtin_assume_aligned(
45 static_cast<uint8_t *>(const_cast<void *>(msg)) - sizeof(MessageHeader),
46 alignof(MessageHeader)));
Brian Silvermana6d1b562013-09-01 14:39:39 -070047 }
48 void Swap(MessageHeader *other) {
49 MessageHeader temp;
50 memcpy(&temp, other, sizeof(temp));
51 memcpy(other, this, sizeof(*other));
52 memcpy(this, &temp, sizeof(*this));
53 }
54};
Brian Silverman5f8c4922014-02-11 21:22:38 -080055static_assert(shm_ok<RawQueue::MessageHeader>::value,
56 "the whole point is to stick it in shared memory");
Brian Silvermana6d1b562013-09-01 14:39:39 -070057
Brian Silverman797e71e2013-09-06 17:29:39 -070058struct RawQueue::ReadData {
59 bool writable_start;
60};
61
Brian Silvermana6d1b562013-09-01 14:39:39 -070062// TODO(brians) maybe do this with atomic integer instructions so it doesn't
63// have to lock/unlock pool_lock_
Brian Silverman08661c72013-09-01 17:24:38 -070064void RawQueue::DecrementMessageReferenceCount(const void *msg) {
Brian Silvermana6d1b562013-09-01 14:39:39 -070065 MutexLocker locker(&pool_lock_);
66 MessageHeader *header = MessageHeader::Get(msg);
67 --header->ref_count;
68 assert(header->ref_count >= 0);
69 if (kRefDebug) {
70 printf("ref_dec_count: %p count=%d\n", msg, header->ref_count);
71 }
72 if (header->ref_count == 0) {
73 DoFreeMessage(msg);
74 }
75}
76
Brian Silverman08661c72013-09-01 17:24:38 -070077RawQueue::RawQueue(const char *name, size_t length, int hash, int queue_length)
Brian Silverman5f8c4922014-02-11 21:22:38 -080078 : readable_(&data_lock_), writable_(&data_lock_) {
Brian Silvermana6d1b562013-09-01 14:39:39 -070079 const size_t name_size = strlen(name) + 1;
80 char *temp = static_cast<char *>(shm_malloc(name_size));
81 memcpy(temp, name, name_size);
82 name_ = temp;
83 length_ = length;
84 hash_ = hash;
85 queue_length_ = queue_length;
86
87 next_ = NULL;
88 recycle_ = NULL;
89
90 if (kFetchDebug) {
91 printf("initializing name=%s, length=%zd, hash=%d, queue_length=%d\n",
92 name, length, hash, queue_length);
93 }
94
95 data_length_ = queue_length + 1;
96 if (data_length_ < 2) { // TODO(brians) when could this happen?
97 data_length_ = 2;
98 }
99 data_ = static_cast<void **>(shm_malloc(sizeof(void *) * data_length_));
100 data_start_ = 0;
101 data_end_ = 0;
102 messages_ = 0;
103
104 mem_length_ = queue_length + kExtraMessages;
105 pool_length_ = 0;
106 messages_used_ = 0;
107 msg_length_ = length + sizeof(MessageHeader);
108 pool_ = static_cast<MessageHeader **>(
109 shm_malloc(sizeof(MessageHeader *) * mem_length_));
110
111 if (kFetchDebug) {
112 printf("made queue %s\n", name);
113 }
114}
Brian Silverman08661c72013-09-01 17:24:38 -0700115RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700116 int queue_length) {
117 if (kFetchDebug) {
118 printf("fetching queue %s\n", name);
119 }
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800120 if (mutex_lock(&global_core->mem_struct->queues.lock) != 0) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700121 return NULL;
122 }
Brian Silverman08661c72013-09-01 17:24:38 -0700123 RawQueue *current = static_cast<RawQueue *>(
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800124 global_core->mem_struct->queues.pointer);
Brian Silverman797e71e2013-09-06 17:29:39 -0700125 if (current != NULL) {
126 while (true) {
127 // If we found a matching queue.
128 if (strcmp(current->name_, name) == 0 && current->length_ == length &&
129 current->hash_ == hash && current->queue_length_ == queue_length) {
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800130 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700131 return current;
132 } else {
133 if (kFetchDebug) {
134 printf("rejected queue %s strcmp=%d target=%s\n", current->name_,
135 strcmp(current->name_, name), name);
136 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700137 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700138 // If this is the last one.
139 if (current->next_ == NULL) break;
140 current = current->next_;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700141 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700142 }
143
Brian Silverman797e71e2013-09-06 17:29:39 -0700144 RawQueue *r = new (shm_malloc(sizeof(RawQueue)))
145 RawQueue(name, length, hash, queue_length);
146 if (current == NULL) { // if we don't already have one
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800147 global_core->mem_struct->queues.pointer = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700148 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700149 current->next_ = r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700150 }
151
Brian Silverman4aeac5f2014-02-11 22:17:07 -0800152 mutex_unlock(&global_core->mem_struct->queues.lock);
Brian Silverman797e71e2013-09-06 17:29:39 -0700153 return r;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700154}
Brian Silverman08661c72013-09-01 17:24:38 -0700155RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash,
Brian Silvermana6d1b562013-09-01 14:39:39 -0700156 int queue_length,
Brian Silverman08661c72013-09-01 17:24:38 -0700157 int recycle_hash, int recycle_length, RawQueue **recycle) {
158 RawQueue *r = Fetch(name, length, hash, queue_length);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700159 r->recycle_ = Fetch(name, length, recycle_hash, recycle_length);
160 if (r == r->recycle_) {
161 fprintf(stderr, "queue: r->recycle_(=%p) == r(=%p)\n", r->recycle_, r);
162 printf("see stderr\n");
Brian Silverman797e71e2013-09-06 17:29:39 -0700163 r->recycle_ = NULL;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700164 abort();
165 }
166 *recycle = r->recycle_;
167 return r;
168}
169
Brian Silverman08661c72013-09-01 17:24:38 -0700170void RawQueue::DoFreeMessage(const void *msg) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700171 MessageHeader *header = MessageHeader::Get(msg);
172 if (pool_[header->index] != header) { // if something's messed up
173 fprintf(stderr, "queue: something is very very wrong with queue %p."
174 " pool_(=%p)[header->index(=%d)] != header(=%p)\n",
175 this, pool_, header->index, header);
176 printf("queue: see stderr\n");
177 abort();
178 }
179 if (kRefDebug) {
180 printf("ref free: %p\n", msg);
181 }
182 --messages_used_;
183
184 if (recycle_ != NULL) {
185 void *const new_msg = recycle_->GetMessage();
186 if (new_msg == NULL) {
187 fprintf(stderr, "queue: couldn't get a message"
188 " for recycle queue %p\n", recycle_);
189 } else {
190 // Take a message from recycle_ and switch its
191 // header with the one being freed, which effectively
192 // switches which queue each message belongs to.
193 MessageHeader *const new_header = MessageHeader::Get(new_msg);
Brian Silverman797e71e2013-09-06 17:29:39 -0700194 // Also switch the messages between the pools.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700195 pool_[header->index] = new_header;
196 {
197 MutexLocker locker(&recycle_->pool_lock_);
198 recycle_->pool_[new_header->index] = header;
Brian Silverman797e71e2013-09-06 17:29:39 -0700199 // Swap the information in both headers.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700200 header->Swap(new_header);
Brian Silverman797e71e2013-09-06 17:29:39 -0700201 // Don't unlock the other pool until all of its messages are valid.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700202 }
203 // use the header for new_msg which is now for this pool
204 header = new_header;
205 if (!recycle_->WriteMessage(const_cast<void *>(msg), kOverride)) {
206 fprintf(stderr, "queue: %p->WriteMessage(%p, kOverride) failed."
207 " aborting\n", recycle_, msg);
208 printf("see stderr\n");
209 abort();
210 }
211 msg = new_msg;
212 }
213 }
214
Brian Silverman797e71e2013-09-06 17:29:39 -0700215 // Where the one we're freeing was.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700216 int index = header->index;
217 header->index = -1;
218 if (index != messages_used_) { // if we're not freeing the one on the end
Brian Silverman797e71e2013-09-06 17:29:39 -0700219 // Put the last one where the one we're freeing was.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700220 header = pool_[index] = pool_[messages_used_];
Brian Silverman797e71e2013-09-06 17:29:39 -0700221 // Put the one we're freeing at the end.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700222 pool_[messages_used_] = MessageHeader::Get(msg);
Brian Silverman797e71e2013-09-06 17:29:39 -0700223 // Update the former last one's index.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700224 header->index = index;
225 }
226}
227
Brian Silverman08661c72013-09-01 17:24:38 -0700228bool RawQueue::WriteMessage(void *msg, int options) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700229 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700230 printf("queue: %p->WriteMessage(%p, %x)\n", this, msg, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700231 }
232 if (msg == NULL || msg < reinterpret_cast<void *>(global_core->mem_struct) ||
233 msg > static_cast<void *>((
Brian Silverman08661c72013-09-01 17:24:38 -0700234 reinterpret_cast<char *>(global_core->mem_struct) +
Brian Silvermana6d1b562013-09-01 14:39:39 -0700235 global_core->size))) {
236 fprintf(stderr, "queue: attempt to write bad message %p to %p. aborting\n",
237 msg, this);
238 printf("see stderr\n");
239 abort();
240 }
241 {
242 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700243 bool writable_waited = false;
244
245 int new_end;
246 while (true) {
247 new_end = (data_end_ + 1) % data_length_;
248 // If there is room in the queue right now.
249 if (new_end != data_start_) break;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700250 if (options & kNonBlock) {
251 if (kWriteDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700252 printf("queue: not blocking on %p. returning false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700253 }
254 return false;
255 } else if (options & kOverride) {
256 if (kWriteDebug) {
257 printf("queue: overriding on %p\n", this);
258 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700259 // Avoid leaking the message that we're going to overwrite.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700260 DecrementMessageReferenceCount(data_[data_start_]);
261 data_start_ = (data_start_ + 1) % data_length_;
262 } else { // kBlock
263 if (kWriteDebug) {
264 printf("queue: going to wait for writable_ of %p\n", this);
265 }
Brian Silverman08661c72013-09-01 17:24:38 -0700266 writable_.Wait();
Brian Silverman797e71e2013-09-06 17:29:39 -0700267 writable_waited = true;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700268 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700269 }
270 data_[data_end_] = msg;
271 ++messages_;
272 data_end_ = new_end;
Brian Silverman797e71e2013-09-06 17:29:39 -0700273
274 if (kWriteDebug) {
275 printf("queue: broadcasting to readable_ of %p\n", this);
276 }
277 readable_.Broadcast();
278
279 // If we got a signal on writable_ here and it's still writable, then we
280 // need to signal the next person in line (if any).
281 if (writable_waited && is_writable()) {
282 if (kWriteDebug) {
283 printf("queue: resignalling writable_ of %p\n", this);
284 }
285 writable_.Signal();
286 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700287 }
288 if (kWriteDebug) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700289 printf("queue: write returning true on queue %p\n", this);
290 }
291 return true;
292}
293
Brian Silverman797e71e2013-09-06 17:29:39 -0700294void RawQueue::ReadCommonEnd(ReadData *read_data) {
295 if (is_writable()) {
296 if (kReadDebug) {
297 printf("queue: %ssignalling writable_ of %p\n",
298 read_data->writable_start ? "not " : "", this);
299 }
300 if (!read_data->writable_start) writable_.Signal();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700301 }
302}
Brian Silverman797e71e2013-09-06 17:29:39 -0700303bool RawQueue::ReadCommonStart(int options, int *index, ReadData *read_data) {
304 read_data->writable_start = is_writable();
Brian Silvermana6d1b562013-09-01 14:39:39 -0700305 while (data_start_ == data_end_ || ((index != NULL) && messages_ <= *index)) {
306 if (options & kNonBlock) {
307 if (kReadDebug) {
308 printf("queue: not going to block waiting on %p\n", this);
309 }
310 return false;
311 } else { // kBlock
312 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700313 printf("queue: going to wait for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700314 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700315 // Wait for a message to become readable.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700316 readable_.Wait();
317 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700318 printf("queue: done waiting for readable_ of %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700319 }
Brian Silvermana6d1b562013-09-01 14:39:39 -0700320 }
321 }
322 if (kReadDebug) {
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800323 printf("queue: %p->read(%p) start=%d end=%d\n", this, index, data_start_,
324 data_end_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700325 }
326 return true;
327}
Brian Silverman08661c72013-09-01 17:24:38 -0700328void *RawQueue::ReadPeek(int options, int start) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700329 void *ret;
330 if (options & kFromEnd) {
331 int pos = data_end_ - 1;
332 if (pos < 0) { // if it needs to wrap
333 pos = data_length_ - 1;
334 }
335 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700336 printf("queue: %p reading from line %d: %d\n", this, __LINE__, pos);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700337 }
338 ret = data_[pos];
339 } else {
340 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700341 printf("queue: %p reading from line %d: %d\n", this, __LINE__, start);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700342 }
343 ret = data_[start];
344 }
345 MessageHeader *const header = MessageHeader::Get(ret);
346 ++header->ref_count;
347 if (kRefDebug) {
348 printf("ref inc count: %p\n", ret);
349 }
350 return ret;
351}
Brian Silverman08661c72013-09-01 17:24:38 -0700352const void *RawQueue::ReadMessage(int options) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700353 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700354 printf("queue: %p->ReadMessage(%x)\n", this, options);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700355 }
356 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700357
Brian Silvermana6d1b562013-09-01 14:39:39 -0700358 MutexLocker locker(&data_lock_);
Brian Silverman797e71e2013-09-06 17:29:39 -0700359
360 ReadData read_data;
361 if (!ReadCommonStart(options, NULL, &read_data)) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700362 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700363 printf("queue: %p common returned false\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700364 }
365 return NULL;
366 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700367
Brian Silvermana6d1b562013-09-01 14:39:39 -0700368 if (options & kPeek) {
369 msg = ReadPeek(options, data_start_);
370 } else {
371 if (options & kFromEnd) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700372 while (true) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700373 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700374 printf("queue: %p start of c2\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700375 }
376 // This loop pulls each message out of the buffer.
377 const int pos = data_start_;
378 data_start_ = (data_start_ + 1) % data_length_;
Brian Silverman797e71e2013-09-06 17:29:39 -0700379 // If this is the last one.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700380 if (data_start_ == data_end_) {
381 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700382 printf("queue: %p reading from c2: %d\n", this, pos);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700383 }
384 msg = data_[pos];
385 break;
386 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700387 // This message is not going to be in the queue any more.
Brian Silvermana6d1b562013-09-01 14:39:39 -0700388 DecrementMessageReferenceCount(data_[pos]);
389 }
390 } else {
391 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700392 printf("queue: %p reading from d2: %d\n", this, data_start_);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700393 }
394 msg = data_[data_start_];
395 data_start_ = (data_start_ + 1) % data_length_;
396 }
397 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700398 ReadCommonEnd(&read_data);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700399 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700400 printf("queue: %p read returning %p\n", this, msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700401 }
402 return msg;
403}
Brian Silverman08661c72013-09-01 17:24:38 -0700404const void *RawQueue::ReadMessageIndex(int options, int *index) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700405 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700406 printf("queue: %p->ReadMessageIndex(%x, %p(*=%d))\n",
Brian Silvermana6d1b562013-09-01 14:39:39 -0700407 this, options, index, *index);
408 }
409 void *msg = NULL;
Brian Silverman797e71e2013-09-06 17:29:39 -0700410
411 MutexLocker locker(&data_lock_);
412
413 ReadData read_data;
414 if (!ReadCommonStart(options, index, &read_data)) {
415 if (kReadDebug) {
416 printf("queue: %p common returned false\n", this);
417 }
418 return NULL;
419 }
420
421 // TODO(parker): Handle integer wrap on the index.
422
Brian Silverman797e71e2013-09-06 17:29:39 -0700423 // Where we're going to start reading.
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800424 int my_start;
425
426 const int unread_messages = messages_ - *index;
427 const int current_messages = ::std::abs(data_start_ - data_end_);
428 if (unread_messages > current_messages) { // If we're behind the available messages.
Brian Silverman797e71e2013-09-06 17:29:39 -0700429 // Catch index up to the last available message.
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800430 *index = messages_ - current_messages;
Brian Silverman797e71e2013-09-06 17:29:39 -0700431 // And that's the one we're going to read.
432 my_start = data_start_;
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800433 } else {
434 // Just start reading at the first available message that we haven't yet
435 // read.
436 my_start = (data_start_ + unread_messages - 1) % data_length_;
Brian Silverman797e71e2013-09-06 17:29:39 -0700437 }
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800438
439
Brian Silverman797e71e2013-09-06 17:29:39 -0700440 if (options & kPeek) {
441 msg = ReadPeek(options, my_start);
442 } else {
443 if (options & kFromEnd) {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700444 if (kReadDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700445 printf("queue: %p start of c1\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700446 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700447 int pos = data_end_ - 1;
448 if (pos < 0) { // If it wrapped.
449 pos = data_length_ - 1; // Unwrap it.
450 }
451 if (kReadDebug) {
452 printf("queue: %p reading from c1: %d\n", this, pos);
453 }
454 msg = data_[pos];
455 *index = messages_;
Brian Silvermana6d1b562013-09-01 14:39:39 -0700456 } else {
Brian Silverman797e71e2013-09-06 17:29:39 -0700457 if (kReadDebug) {
458 printf("queue: %p reading from d1: %d\n", this, my_start);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700459 }
Brian Silvermanc39e2bd2014-02-21 09:17:35 -0800460 // This assert checks that we're either within both endpoints (duh) or
461 // outside of both of them (if the queue is wrapped around).
462 assert((my_start >= data_start_ && my_start < data_end_) ||
463 (my_start > data_end_ && my_start <= data_start_));
Brian Silverman797e71e2013-09-06 17:29:39 -0700464 msg = data_[my_start];
465 ++(*index);
466 }
467 MessageHeader *const header = MessageHeader::Get(msg);
468 ++header->ref_count;
469 if (kRefDebug) {
470 printf("ref_inc_count: %p\n", msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700471 }
472 }
Brian Silverman797e71e2013-09-06 17:29:39 -0700473 ReadCommonEnd(&read_data);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700474 return msg;
475}
476
Brian Silverman08661c72013-09-01 17:24:38 -0700477void *RawQueue::GetMessage() {
Brian Silvermana6d1b562013-09-01 14:39:39 -0700478 MutexLocker locker(&pool_lock_);
479 MessageHeader *header;
480 if (pool_length_ - messages_used_ > 0) {
481 header = pool_[messages_used_];
482 } else {
483 if (pool_length_ >= mem_length_) {
Brian Silverman08661c72013-09-01 17:24:38 -0700484 LOG(FATAL, "overused pool of queue %p\n", this);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700485 }
486 header = pool_[pool_length_] =
487 static_cast<MessageHeader *>(shm_malloc(msg_length_));
488 ++pool_length_;
489 }
490 void *msg = reinterpret_cast<uint8_t *>(header) + sizeof(MessageHeader);
491 header->ref_count = 1;
492 if (kRefDebug) {
Brian Silverman797e71e2013-09-06 17:29:39 -0700493 printf("%p ref alloc: %p\n", this, msg);
Brian Silvermana6d1b562013-09-01 14:39:39 -0700494 }
495 header->index = messages_used_;
496 ++messages_used_;
497 return msg;
498}
499
500} // namespace aos