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> |
Brian Silverman | c39e2bd | 2014-02-21 09:17:35 -0800 | [diff] [blame^] | 9 | #include <algorithm> |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 10 | |
| 11 | #include "aos/common/logging/logging.h" |
| 12 | #include "aos/common/type_traits.h" |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 13 | #include "aos/linux_code/ipc_lib/core_lib.h" |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 14 | |
| 15 | namespace aos { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 16 | namespace { |
| 17 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 18 | static_assert(shm_ok<RawQueue>::value, |
| 19 | "RawQueue instances go into shared memory"); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 20 | |
| 21 | const bool kReadDebug = false; |
| 22 | const bool kWriteDebug = false; |
| 23 | const bool kRefDebug = false; |
| 24 | const bool kFetchDebug = false; |
| 25 | |
| 26 | // 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] | 27 | // to hold (for readers who are slow about freeing them or who leak one when |
| 28 | // they get killed). |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 29 | const int kExtraMessages = 20; |
| 30 | |
| 31 | } // namespace |
| 32 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 33 | const int RawQueue::kPeek; |
| 34 | const int RawQueue::kFromEnd; |
| 35 | const int RawQueue::kNonBlock; |
| 36 | const int RawQueue::kBlock; |
| 37 | const int RawQueue::kOverride; |
| 38 | |
| 39 | struct RawQueue::MessageHeader { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 40 | int ref_count; |
| 41 | int index; // in pool_ |
Brian Silverman | 5f8c492 | 2014-02-11 21:22:38 -0800 | [diff] [blame] | 42 | // Gets the message header immediately preceding msg. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 43 | static MessageHeader *Get(const void *msg) { |
Brian Silverman | 63cf241 | 2013-11-17 05:44:36 -0800 | [diff] [blame] | 44 | return reinterpret_cast<MessageHeader *>(__builtin_assume_aligned( |
| 45 | static_cast<uint8_t *>(const_cast<void *>(msg)) - sizeof(MessageHeader), |
| 46 | alignof(MessageHeader))); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 47 | } |
| 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 Silverman | 5f8c492 | 2014-02-11 21:22:38 -0800 | [diff] [blame] | 55 | static_assert(shm_ok<RawQueue::MessageHeader>::value, |
| 56 | "the whole point is to stick it in shared memory"); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 57 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 58 | struct RawQueue::ReadData { |
| 59 | bool writable_start; |
| 60 | }; |
| 61 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 62 | // TODO(brians) maybe do this with atomic integer instructions so it doesn't |
| 63 | // have to lock/unlock pool_lock_ |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 64 | void RawQueue::DecrementMessageReferenceCount(const void *msg) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 65 | 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 Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 77 | RawQueue::RawQueue(const char *name, size_t length, int hash, int queue_length) |
Brian Silverman | 5f8c492 | 2014-02-11 21:22:38 -0800 | [diff] [blame] | 78 | : readable_(&data_lock_), writable_(&data_lock_) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 79 | 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 Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 115 | RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash, |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 116 | int queue_length) { |
| 117 | if (kFetchDebug) { |
| 118 | printf("fetching queue %s\n", name); |
| 119 | } |
Brian Silverman | 4aeac5f | 2014-02-11 22:17:07 -0800 | [diff] [blame] | 120 | if (mutex_lock(&global_core->mem_struct->queues.lock) != 0) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 121 | return NULL; |
| 122 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 123 | RawQueue *current = static_cast<RawQueue *>( |
Brian Silverman | 4aeac5f | 2014-02-11 22:17:07 -0800 | [diff] [blame] | 124 | global_core->mem_struct->queues.pointer); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 125 | 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 Silverman | 4aeac5f | 2014-02-11 22:17:07 -0800 | [diff] [blame] | 130 | mutex_unlock(&global_core->mem_struct->queues.lock); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 131 | 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 Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 137 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 138 | // If this is the last one. |
| 139 | if (current->next_ == NULL) break; |
| 140 | current = current->next_; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 141 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 144 | 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 Silverman | 4aeac5f | 2014-02-11 22:17:07 -0800 | [diff] [blame] | 147 | global_core->mem_struct->queues.pointer = r; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 148 | } else { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 149 | current->next_ = r; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Brian Silverman | 4aeac5f | 2014-02-11 22:17:07 -0800 | [diff] [blame] | 152 | mutex_unlock(&global_core->mem_struct->queues.lock); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 153 | return r; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 154 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 155 | RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash, |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 156 | int queue_length, |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 157 | int recycle_hash, int recycle_length, RawQueue **recycle) { |
| 158 | RawQueue *r = Fetch(name, length, hash, queue_length); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 159 | 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 163 | r->recycle_ = NULL; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 164 | abort(); |
| 165 | } |
| 166 | *recycle = r->recycle_; |
| 167 | return r; |
| 168 | } |
| 169 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 170 | void RawQueue::DoFreeMessage(const void *msg) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 171 | 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 194 | // Also switch the messages between the pools. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 195 | pool_[header->index] = new_header; |
| 196 | { |
| 197 | MutexLocker locker(&recycle_->pool_lock_); |
| 198 | recycle_->pool_[new_header->index] = header; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 199 | // Swap the information in both headers. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 200 | header->Swap(new_header); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 201 | // 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] | 202 | } |
| 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 215 | // Where the one we're freeing was. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 216 | int index = header->index; |
| 217 | header->index = -1; |
| 218 | 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] | 219 | // Put the last one where the one we're freeing was. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 220 | header = pool_[index] = pool_[messages_used_]; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 221 | // Put the one we're freeing at the end. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 222 | pool_[messages_used_] = MessageHeader::Get(msg); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 223 | // Update the former last one's index. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 224 | header->index = index; |
| 225 | } |
| 226 | } |
| 227 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 228 | bool RawQueue::WriteMessage(void *msg, int options) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 229 | if (kWriteDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 230 | printf("queue: %p->WriteMessage(%p, %x)\n", this, msg, options); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 231 | } |
| 232 | if (msg == NULL || msg < reinterpret_cast<void *>(global_core->mem_struct) || |
| 233 | msg > static_cast<void *>(( |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 234 | reinterpret_cast<char *>(global_core->mem_struct) + |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 235 | 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 243 | 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 Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 250 | if (options & kNonBlock) { |
| 251 | if (kWriteDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 252 | printf("queue: not blocking on %p. returning false\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 253 | } |
| 254 | return false; |
| 255 | } else if (options & kOverride) { |
| 256 | if (kWriteDebug) { |
| 257 | printf("queue: overriding on %p\n", this); |
| 258 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 259 | // Avoid leaking the message that we're going to overwrite. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 260 | 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 Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 266 | writable_.Wait(); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 267 | writable_waited = true; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 268 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 269 | } |
| 270 | data_[data_end_] = msg; |
| 271 | ++messages_; |
| 272 | data_end_ = new_end; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 273 | |
| 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 Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 287 | } |
| 288 | if (kWriteDebug) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 289 | printf("queue: write returning true on queue %p\n", this); |
| 290 | } |
| 291 | return true; |
| 292 | } |
| 293 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 294 | void 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 Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 303 | bool RawQueue::ReadCommonStart(int options, int *index, ReadData *read_data) { |
| 304 | read_data->writable_start = is_writable(); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 305 | 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 313 | printf("queue: going to wait for readable_ of %p\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 314 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 315 | // Wait for a message to become readable. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 316 | readable_.Wait(); |
| 317 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 318 | printf("queue: done waiting for readable_ of %p\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 319 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | if (kReadDebug) { |
Brian Silverman | c39e2bd | 2014-02-21 09:17:35 -0800 | [diff] [blame^] | 323 | printf("queue: %p->read(%p) start=%d end=%d\n", this, index, data_start_, |
| 324 | data_end_); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 325 | } |
| 326 | return true; |
| 327 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 328 | void *RawQueue::ReadPeek(int options, int start) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 329 | 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 336 | printf("queue: %p reading from line %d: %d\n", this, __LINE__, pos); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 337 | } |
| 338 | ret = data_[pos]; |
| 339 | } else { |
| 340 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 341 | printf("queue: %p reading from line %d: %d\n", this, __LINE__, start); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 342 | } |
| 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 Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 352 | const void *RawQueue::ReadMessage(int options) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 353 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 354 | printf("queue: %p->ReadMessage(%x)\n", this, options); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 355 | } |
| 356 | void *msg = NULL; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 357 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 358 | MutexLocker locker(&data_lock_); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 359 | |
| 360 | ReadData read_data; |
| 361 | if (!ReadCommonStart(options, NULL, &read_data)) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 362 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 363 | printf("queue: %p common returned false\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 364 | } |
| 365 | return NULL; |
| 366 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 367 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 368 | if (options & kPeek) { |
| 369 | msg = ReadPeek(options, data_start_); |
| 370 | } else { |
| 371 | if (options & kFromEnd) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 372 | while (true) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 373 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 374 | printf("queue: %p start of c2\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 375 | } |
| 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 379 | // If this is the last one. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 380 | if (data_start_ == data_end_) { |
| 381 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 382 | printf("queue: %p reading from c2: %d\n", this, pos); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 383 | } |
| 384 | msg = data_[pos]; |
| 385 | break; |
| 386 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 387 | // This message is not going to be in the queue any more. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 388 | DecrementMessageReferenceCount(data_[pos]); |
| 389 | } |
| 390 | } else { |
| 391 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 392 | printf("queue: %p reading from d2: %d\n", this, data_start_); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 393 | } |
| 394 | msg = data_[data_start_]; |
| 395 | data_start_ = (data_start_ + 1) % data_length_; |
| 396 | } |
| 397 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 398 | ReadCommonEnd(&read_data); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 399 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 400 | printf("queue: %p read returning %p\n", this, msg); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 401 | } |
| 402 | return msg; |
| 403 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 404 | const void *RawQueue::ReadMessageIndex(int options, int *index) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 405 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 406 | printf("queue: %p->ReadMessageIndex(%x, %p(*=%d))\n", |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 407 | this, options, index, *index); |
| 408 | } |
| 409 | void *msg = NULL; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 410 | |
| 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 423 | // Where we're going to start reading. |
Brian Silverman | c39e2bd | 2014-02-21 09:17:35 -0800 | [diff] [blame^] | 424 | 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 429 | // Catch index up to the last available message. |
Brian Silverman | c39e2bd | 2014-02-21 09:17:35 -0800 | [diff] [blame^] | 430 | *index = messages_ - current_messages; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 431 | // And that's the one we're going to read. |
| 432 | my_start = data_start_; |
Brian Silverman | c39e2bd | 2014-02-21 09:17:35 -0800 | [diff] [blame^] | 433 | } 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 437 | } |
Brian Silverman | c39e2bd | 2014-02-21 09:17:35 -0800 | [diff] [blame^] | 438 | |
| 439 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 440 | if (options & kPeek) { |
| 441 | msg = ReadPeek(options, my_start); |
| 442 | } else { |
| 443 | if (options & kFromEnd) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 444 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 445 | printf("queue: %p start of c1\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 446 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 447 | 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 Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 456 | } else { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 457 | if (kReadDebug) { |
| 458 | printf("queue: %p reading from d1: %d\n", this, my_start); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 459 | } |
Brian Silverman | c39e2bd | 2014-02-21 09:17:35 -0800 | [diff] [blame^] | 460 | // 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 464 | 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 Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 473 | ReadCommonEnd(&read_data); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 474 | return msg; |
| 475 | } |
| 476 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 477 | void *RawQueue::GetMessage() { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 478 | 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 Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 484 | LOG(FATAL, "overused pool of queue %p\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 485 | } |
| 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 Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 493 | printf("%p ref alloc: %p\n", this, msg); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 494 | } |
| 495 | header->index = messages_used_; |
| 496 | ++messages_used_; |
| 497 | return msg; |
| 498 | } |
| 499 | |
| 500 | } // namespace aos |