Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame^] | 1 | #include "aos/linux_code/ipc_lib/queue.h" |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <errno.h> |
| 6 | #include <assert.h> |
| 7 | |
| 8 | #include <memory> |
| 9 | |
| 10 | #include "aos/common/logging/logging.h" |
| 11 | #include "aos/common/type_traits.h" |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame^] | 12 | #include "aos/linux_code/ipc_lib/core_lib.h" |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 13 | |
| 14 | namespace aos { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 15 | namespace { |
| 16 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 17 | static_assert(shm_ok<RawQueue>::value, |
| 18 | "RawQueue instances go into shared memory"); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 19 | |
| 20 | const bool kReadDebug = false; |
| 21 | const bool kWriteDebug = false; |
| 22 | const bool kRefDebug = false; |
| 23 | const bool kFetchDebug = false; |
| 24 | |
| 25 | // The number of extra messages the pool associated with each queue will be able |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 26 | // to hold (for readers who are slow about freeing them or who leak one when |
| 27 | // they get killed). |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 28 | const int kExtraMessages = 20; |
| 29 | |
| 30 | } // namespace |
| 31 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 32 | const int RawQueue::kPeek; |
| 33 | const int RawQueue::kFromEnd; |
| 34 | const int RawQueue::kNonBlock; |
| 35 | const int RawQueue::kBlock; |
| 36 | const int RawQueue::kOverride; |
| 37 | |
| 38 | struct RawQueue::MessageHeader { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 39 | int ref_count; |
| 40 | int index; // in pool_ |
| 41 | static MessageHeader *Get(const void *msg) { |
Brian Silverman | 63cf241 | 2013-11-17 05:44:36 -0800 | [diff] [blame] | 42 | return reinterpret_cast<MessageHeader *>(__builtin_assume_aligned( |
| 43 | static_cast<uint8_t *>(const_cast<void *>(msg)) - sizeof(MessageHeader), |
| 44 | alignof(MessageHeader))); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 45 | } |
| 46 | void Swap(MessageHeader *other) { |
| 47 | MessageHeader temp; |
| 48 | memcpy(&temp, other, sizeof(temp)); |
| 49 | memcpy(other, this, sizeof(*other)); |
| 50 | memcpy(this, &temp, sizeof(*this)); |
| 51 | } |
| 52 | }; |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 53 | static_assert(shm_ok<RawQueue::MessageHeader>::value, "the whole point" |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 54 | " is to stick it in shared memory"); |
| 55 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 56 | struct RawQueue::ReadData { |
| 57 | bool writable_start; |
| 58 | }; |
| 59 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 60 | // TODO(brians) maybe do this with atomic integer instructions so it doesn't |
| 61 | // have to lock/unlock pool_lock_ |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 62 | void RawQueue::DecrementMessageReferenceCount(const void *msg) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 63 | MutexLocker locker(&pool_lock_); |
| 64 | MessageHeader *header = MessageHeader::Get(msg); |
| 65 | --header->ref_count; |
| 66 | assert(header->ref_count >= 0); |
| 67 | if (kRefDebug) { |
| 68 | printf("ref_dec_count: %p count=%d\n", msg, header->ref_count); |
| 69 | } |
| 70 | if (header->ref_count == 0) { |
| 71 | DoFreeMessage(msg); |
| 72 | } |
| 73 | } |
| 74 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 75 | RawQueue::RawQueue(const char *name, size_t length, int hash, int queue_length) |
| 76 | : readable_(&data_lock_), writable_(&data_lock_) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 77 | const size_t name_size = strlen(name) + 1; |
| 78 | char *temp = static_cast<char *>(shm_malloc(name_size)); |
| 79 | memcpy(temp, name, name_size); |
| 80 | name_ = temp; |
| 81 | length_ = length; |
| 82 | hash_ = hash; |
| 83 | queue_length_ = queue_length; |
| 84 | |
| 85 | next_ = NULL; |
| 86 | recycle_ = NULL; |
| 87 | |
| 88 | if (kFetchDebug) { |
| 89 | printf("initializing name=%s, length=%zd, hash=%d, queue_length=%d\n", |
| 90 | name, length, hash, queue_length); |
| 91 | } |
| 92 | |
| 93 | data_length_ = queue_length + 1; |
| 94 | if (data_length_ < 2) { // TODO(brians) when could this happen? |
| 95 | data_length_ = 2; |
| 96 | } |
| 97 | data_ = static_cast<void **>(shm_malloc(sizeof(void *) * data_length_)); |
| 98 | data_start_ = 0; |
| 99 | data_end_ = 0; |
| 100 | messages_ = 0; |
| 101 | |
| 102 | mem_length_ = queue_length + kExtraMessages; |
| 103 | pool_length_ = 0; |
| 104 | messages_used_ = 0; |
| 105 | msg_length_ = length + sizeof(MessageHeader); |
| 106 | pool_ = static_cast<MessageHeader **>( |
| 107 | shm_malloc(sizeof(MessageHeader *) * mem_length_)); |
| 108 | |
| 109 | if (kFetchDebug) { |
| 110 | printf("made queue %s\n", name); |
| 111 | } |
| 112 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 113 | RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash, |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 114 | int queue_length) { |
| 115 | if (kFetchDebug) { |
| 116 | printf("fetching queue %s\n", name); |
| 117 | } |
| 118 | if (mutex_lock(&global_core->mem_struct->queues.alloc_lock) != 0) { |
| 119 | return NULL; |
| 120 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 121 | RawQueue *current = static_cast<RawQueue *>( |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 122 | global_core->mem_struct->queues.queue_list); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 123 | if (current != NULL) { |
| 124 | while (true) { |
| 125 | // If we found a matching queue. |
| 126 | if (strcmp(current->name_, name) == 0 && current->length_ == length && |
| 127 | current->hash_ == hash && current->queue_length_ == queue_length) { |
| 128 | mutex_unlock(&global_core->mem_struct->queues.alloc_lock); |
| 129 | return current; |
| 130 | } else { |
| 131 | if (kFetchDebug) { |
| 132 | printf("rejected queue %s strcmp=%d target=%s\n", current->name_, |
| 133 | strcmp(current->name_, name), name); |
| 134 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 135 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 136 | // If this is the last one. |
| 137 | if (current->next_ == NULL) break; |
| 138 | current = current->next_; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 139 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 142 | RawQueue *r = new (shm_malloc(sizeof(RawQueue))) |
| 143 | RawQueue(name, length, hash, queue_length); |
| 144 | if (current == NULL) { // if we don't already have one |
| 145 | global_core->mem_struct->queues.queue_list = r; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 146 | } else { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 147 | current->next_ = r; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | mutex_unlock(&global_core->mem_struct->queues.alloc_lock); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 151 | return r; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 152 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 153 | RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash, |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 154 | int queue_length, |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 155 | int recycle_hash, int recycle_length, RawQueue **recycle) { |
| 156 | RawQueue *r = Fetch(name, length, hash, queue_length); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 157 | r->recycle_ = Fetch(name, length, recycle_hash, recycle_length); |
| 158 | if (r == r->recycle_) { |
| 159 | fprintf(stderr, "queue: r->recycle_(=%p) == r(=%p)\n", r->recycle_, r); |
| 160 | printf("see stderr\n"); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 161 | r->recycle_ = NULL; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 162 | abort(); |
| 163 | } |
| 164 | *recycle = r->recycle_; |
| 165 | return r; |
| 166 | } |
| 167 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 168 | void RawQueue::DoFreeMessage(const void *msg) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 169 | MessageHeader *header = MessageHeader::Get(msg); |
| 170 | if (pool_[header->index] != header) { // if something's messed up |
| 171 | fprintf(stderr, "queue: something is very very wrong with queue %p." |
| 172 | " pool_(=%p)[header->index(=%d)] != header(=%p)\n", |
| 173 | this, pool_, header->index, header); |
| 174 | printf("queue: see stderr\n"); |
| 175 | abort(); |
| 176 | } |
| 177 | if (kRefDebug) { |
| 178 | printf("ref free: %p\n", msg); |
| 179 | } |
| 180 | --messages_used_; |
| 181 | |
| 182 | if (recycle_ != NULL) { |
| 183 | void *const new_msg = recycle_->GetMessage(); |
| 184 | if (new_msg == NULL) { |
| 185 | fprintf(stderr, "queue: couldn't get a message" |
| 186 | " for recycle queue %p\n", recycle_); |
| 187 | } else { |
| 188 | // Take a message from recycle_ and switch its |
| 189 | // header with the one being freed, which effectively |
| 190 | // switches which queue each message belongs to. |
| 191 | MessageHeader *const new_header = MessageHeader::Get(new_msg); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 192 | // Also switch the messages between the pools. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 193 | pool_[header->index] = new_header; |
| 194 | { |
| 195 | MutexLocker locker(&recycle_->pool_lock_); |
| 196 | recycle_->pool_[new_header->index] = header; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 197 | // Swap the information in both headers. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 198 | header->Swap(new_header); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 199 | // Don't unlock the other pool until all of its messages are valid. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 200 | } |
| 201 | // use the header for new_msg which is now for this pool |
| 202 | header = new_header; |
| 203 | if (!recycle_->WriteMessage(const_cast<void *>(msg), kOverride)) { |
| 204 | fprintf(stderr, "queue: %p->WriteMessage(%p, kOverride) failed." |
| 205 | " aborting\n", recycle_, msg); |
| 206 | printf("see stderr\n"); |
| 207 | abort(); |
| 208 | } |
| 209 | msg = new_msg; |
| 210 | } |
| 211 | } |
| 212 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 213 | // Where the one we're freeing was. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 214 | int index = header->index; |
| 215 | header->index = -1; |
| 216 | if (index != messages_used_) { // if we're not freeing the one on the end |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 217 | // Put the last one where the one we're freeing was. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 218 | header = pool_[index] = pool_[messages_used_]; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 219 | // Put the one we're freeing at the end. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 220 | pool_[messages_used_] = MessageHeader::Get(msg); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 221 | // Update the former last one's index. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 222 | header->index = index; |
| 223 | } |
| 224 | } |
| 225 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 226 | bool RawQueue::WriteMessage(void *msg, int options) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 227 | if (kWriteDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 228 | printf("queue: %p->WriteMessage(%p, %x)\n", this, msg, options); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 229 | } |
| 230 | if (msg == NULL || msg < reinterpret_cast<void *>(global_core->mem_struct) || |
| 231 | msg > static_cast<void *>(( |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 232 | reinterpret_cast<char *>(global_core->mem_struct) + |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 233 | global_core->size))) { |
| 234 | fprintf(stderr, "queue: attempt to write bad message %p to %p. aborting\n", |
| 235 | msg, this); |
| 236 | printf("see stderr\n"); |
| 237 | abort(); |
| 238 | } |
| 239 | { |
| 240 | MutexLocker locker(&data_lock_); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 241 | bool writable_waited = false; |
| 242 | |
| 243 | int new_end; |
| 244 | while (true) { |
| 245 | new_end = (data_end_ + 1) % data_length_; |
| 246 | // If there is room in the queue right now. |
| 247 | if (new_end != data_start_) break; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 248 | if (options & kNonBlock) { |
| 249 | if (kWriteDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 250 | printf("queue: not blocking on %p. returning false\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 251 | } |
| 252 | return false; |
| 253 | } else if (options & kOverride) { |
| 254 | if (kWriteDebug) { |
| 255 | printf("queue: overriding on %p\n", this); |
| 256 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 257 | // Avoid leaking the message that we're going to overwrite. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 258 | DecrementMessageReferenceCount(data_[data_start_]); |
| 259 | data_start_ = (data_start_ + 1) % data_length_; |
| 260 | } else { // kBlock |
| 261 | if (kWriteDebug) { |
| 262 | printf("queue: going to wait for writable_ of %p\n", this); |
| 263 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 264 | writable_.Wait(); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 265 | writable_waited = true; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 266 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 267 | } |
| 268 | data_[data_end_] = msg; |
| 269 | ++messages_; |
| 270 | data_end_ = new_end; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 271 | |
| 272 | if (kWriteDebug) { |
| 273 | printf("queue: broadcasting to readable_ of %p\n", this); |
| 274 | } |
| 275 | readable_.Broadcast(); |
| 276 | |
| 277 | // If we got a signal on writable_ here and it's still writable, then we |
| 278 | // need to signal the next person in line (if any). |
| 279 | if (writable_waited && is_writable()) { |
| 280 | if (kWriteDebug) { |
| 281 | printf("queue: resignalling writable_ of %p\n", this); |
| 282 | } |
| 283 | writable_.Signal(); |
| 284 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 285 | } |
| 286 | if (kWriteDebug) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 287 | printf("queue: write returning true on queue %p\n", this); |
| 288 | } |
| 289 | return true; |
| 290 | } |
| 291 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 292 | void RawQueue::ReadCommonEnd(ReadData *read_data) { |
| 293 | if (is_writable()) { |
| 294 | if (kReadDebug) { |
| 295 | printf("queue: %ssignalling writable_ of %p\n", |
| 296 | read_data->writable_start ? "not " : "", this); |
| 297 | } |
| 298 | if (!read_data->writable_start) writable_.Signal(); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 301 | bool RawQueue::ReadCommonStart(int options, int *index, ReadData *read_data) { |
| 302 | read_data->writable_start = is_writable(); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 303 | while (data_start_ == data_end_ || ((index != NULL) && messages_ <= *index)) { |
| 304 | if (options & kNonBlock) { |
| 305 | if (kReadDebug) { |
| 306 | printf("queue: not going to block waiting on %p\n", this); |
| 307 | } |
| 308 | return false; |
| 309 | } else { // kBlock |
| 310 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 311 | printf("queue: going to wait for readable_ of %p\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 312 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 313 | // Wait for a message to become readable. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 314 | readable_.Wait(); |
| 315 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 316 | printf("queue: done waiting for readable_ of %p\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 317 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | if (kReadDebug) { |
| 321 | printf("queue: %p->read start=%d end=%d\n", this, data_start_, data_end_); |
| 322 | } |
| 323 | return true; |
| 324 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 325 | void *RawQueue::ReadPeek(int options, int start) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 326 | void *ret; |
| 327 | if (options & kFromEnd) { |
| 328 | int pos = data_end_ - 1; |
| 329 | if (pos < 0) { // if it needs to wrap |
| 330 | pos = data_length_ - 1; |
| 331 | } |
| 332 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 333 | printf("queue: %p reading from line %d: %d\n", this, __LINE__, pos); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 334 | } |
| 335 | ret = data_[pos]; |
| 336 | } else { |
| 337 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 338 | printf("queue: %p reading from line %d: %d\n", this, __LINE__, start); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 339 | } |
| 340 | ret = data_[start]; |
| 341 | } |
| 342 | MessageHeader *const header = MessageHeader::Get(ret); |
| 343 | ++header->ref_count; |
| 344 | if (kRefDebug) { |
| 345 | printf("ref inc count: %p\n", ret); |
| 346 | } |
| 347 | return ret; |
| 348 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 349 | const void *RawQueue::ReadMessage(int options) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 350 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 351 | printf("queue: %p->ReadMessage(%x)\n", this, options); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 352 | } |
| 353 | void *msg = NULL; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 354 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 355 | MutexLocker locker(&data_lock_); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 356 | |
| 357 | ReadData read_data; |
| 358 | if (!ReadCommonStart(options, NULL, &read_data)) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 359 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 360 | printf("queue: %p common returned false\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 361 | } |
| 362 | return NULL; |
| 363 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 364 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 365 | if (options & kPeek) { |
| 366 | msg = ReadPeek(options, data_start_); |
| 367 | } else { |
| 368 | if (options & kFromEnd) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 369 | while (true) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 370 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 371 | printf("queue: %p start of c2\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 372 | } |
| 373 | // This loop pulls each message out of the buffer. |
| 374 | const int pos = data_start_; |
| 375 | data_start_ = (data_start_ + 1) % data_length_; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 376 | // If this is the last one. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 377 | if (data_start_ == data_end_) { |
| 378 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 379 | printf("queue: %p reading from c2: %d\n", this, pos); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 380 | } |
| 381 | msg = data_[pos]; |
| 382 | break; |
| 383 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 384 | // This message is not going to be in the queue any more. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 385 | DecrementMessageReferenceCount(data_[pos]); |
| 386 | } |
| 387 | } else { |
| 388 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 389 | printf("queue: %p reading from d2: %d\n", this, data_start_); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 390 | } |
| 391 | msg = data_[data_start_]; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 392 | // TODO(brians): Doesn't this need to increment the ref count? |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 393 | data_start_ = (data_start_ + 1) % data_length_; |
| 394 | } |
| 395 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 396 | ReadCommonEnd(&read_data); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 397 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 398 | printf("queue: %p read returning %p\n", this, msg); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 399 | } |
| 400 | return msg; |
| 401 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 402 | const void *RawQueue::ReadMessageIndex(int options, int *index) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 403 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 404 | printf("queue: %p->ReadMessageIndex(%x, %p(*=%d))\n", |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 405 | this, options, index, *index); |
| 406 | } |
| 407 | void *msg = NULL; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 408 | |
| 409 | MutexLocker locker(&data_lock_); |
| 410 | |
| 411 | ReadData read_data; |
| 412 | if (!ReadCommonStart(options, index, &read_data)) { |
| 413 | if (kReadDebug) { |
| 414 | printf("queue: %p common returned false\n", this); |
| 415 | } |
| 416 | return NULL; |
| 417 | } |
| 418 | |
| 419 | // TODO(parker): Handle integer wrap on the index. |
| 420 | |
| 421 | // How many unread messages we have. |
| 422 | const int offset = messages_ - *index; |
| 423 | // Where we're going to start reading. |
| 424 | int my_start = data_end_ - offset; |
| 425 | if (my_start < 0) { // If we want to read off the end of the buffer. |
| 426 | // Unwrap it. |
| 427 | my_start += data_length_; |
| 428 | } |
| 429 | if (offset >= data_length_) { // If we're behind the available messages. |
| 430 | // Catch index up to the last available message. |
| 431 | *index += data_start_ - my_start; |
| 432 | // And that's the one we're going to read. |
| 433 | my_start = data_start_; |
| 434 | } |
| 435 | if (options & kPeek) { |
| 436 | msg = ReadPeek(options, my_start); |
| 437 | } else { |
| 438 | if (options & kFromEnd) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 439 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 440 | printf("queue: %p start of c1\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 441 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 442 | int pos = data_end_ - 1; |
| 443 | if (pos < 0) { // If it wrapped. |
| 444 | pos = data_length_ - 1; // Unwrap it. |
| 445 | } |
| 446 | if (kReadDebug) { |
| 447 | printf("queue: %p reading from c1: %d\n", this, pos); |
| 448 | } |
| 449 | msg = data_[pos]; |
| 450 | *index = messages_; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 451 | } else { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 452 | if (kReadDebug) { |
| 453 | printf("queue: %p reading from d1: %d\n", this, my_start); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 454 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 455 | msg = data_[my_start]; |
| 456 | ++(*index); |
| 457 | } |
| 458 | MessageHeader *const header = MessageHeader::Get(msg); |
| 459 | ++header->ref_count; |
| 460 | if (kRefDebug) { |
| 461 | printf("ref_inc_count: %p\n", msg); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 464 | ReadCommonEnd(&read_data); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 465 | return msg; |
| 466 | } |
| 467 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 468 | void *RawQueue::GetMessage() { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 469 | MutexLocker locker(&pool_lock_); |
| 470 | MessageHeader *header; |
| 471 | if (pool_length_ - messages_used_ > 0) { |
| 472 | header = pool_[messages_used_]; |
| 473 | } else { |
| 474 | if (pool_length_ >= mem_length_) { |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 475 | LOG(FATAL, "overused pool of queue %p\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 476 | } |
| 477 | header = pool_[pool_length_] = |
| 478 | static_cast<MessageHeader *>(shm_malloc(msg_length_)); |
| 479 | ++pool_length_; |
| 480 | } |
| 481 | void *msg = reinterpret_cast<uint8_t *>(header) + sizeof(MessageHeader); |
| 482 | header->ref_count = 1; |
| 483 | if (kRefDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 484 | printf("%p ref alloc: %p\n", this, msg); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 485 | } |
| 486 | header->index = messages_used_; |
| 487 | ++messages_used_; |
| 488 | return msg; |
| 489 | } |
| 490 | |
| 491 | } // namespace aos |