Brian Silverman | 9eaf91a | 2014-03-24 16:37:44 -0700 | [diff] [blame] | 1 | #if !QUEUE_DEBUG |
| 2 | #define NDEBUG |
| 3 | #endif |
| 4 | |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 5 | #include "aos/linux_code/ipc_lib/queue.h" |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include <errno.h> |
| 10 | #include <assert.h> |
| 11 | |
| 12 | #include <memory> |
Brian Silverman | c39e2bd | 2014-02-21 09:17:35 -0800 | [diff] [blame] | 13 | #include <algorithm> |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 14 | |
| 15 | #include "aos/common/logging/logging.h" |
| 16 | #include "aos/common/type_traits.h" |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 17 | #include "aos/linux_code/ipc_lib/core_lib.h" |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 18 | |
| 19 | namespace aos { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 20 | namespace { |
| 21 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 22 | static_assert(shm_ok<RawQueue>::value, |
| 23 | "RawQueue instances go into shared memory"); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 24 | |
| 25 | const bool kReadDebug = false; |
Brian Silverman | bad7c8a | 2014-03-26 20:45:18 -0700 | [diff] [blame] | 26 | const bool kWriteDebug = false; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 27 | const bool kRefDebug = false; |
| 28 | const bool kFetchDebug = false; |
Brian Silverman | cd2d84c | 2014-03-13 23:30:58 -0700 | [diff] [blame] | 29 | const bool kReadIndexDebug = false; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 30 | |
| 31 | // 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] | 32 | // to hold (for readers who are slow about freeing them or who leak one when |
| 33 | // they get killed). |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 34 | const int kExtraMessages = 20; |
| 35 | |
| 36 | } // namespace |
| 37 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 38 | const int RawQueue::kPeek; |
| 39 | const int RawQueue::kFromEnd; |
| 40 | const int RawQueue::kNonBlock; |
| 41 | const int RawQueue::kBlock; |
| 42 | const int RawQueue::kOverride; |
| 43 | |
Brian Silverman | 430e7fa | 2014-03-21 16:58:33 -0700 | [diff] [blame] | 44 | // This is what gets stuck in before each queue message in memory. It is always |
| 45 | // allocated aligned to 8 bytes and its size has to maintain that alignment for |
| 46 | // the message that follows immediately. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 47 | struct RawQueue::MessageHeader { |
Brian Silverman | ad290d8 | 2014-03-19 17:22:05 -0700 | [diff] [blame] | 48 | // This gets incremented and decremented with atomic instructions without any |
| 49 | // locks held. |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 50 | int32_t ref_count; |
Brian Silverman | c2e0422 | 2014-03-22 12:43:44 -0700 | [diff] [blame] | 51 | MessageHeader *next; |
Brian Silverman | 5f8c492 | 2014-02-11 21:22:38 -0800 | [diff] [blame] | 52 | // Gets the message header immediately preceding msg. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 53 | static MessageHeader *Get(const void *msg) { |
Brian Silverman | 63cf241 | 2013-11-17 05:44:36 -0800 | [diff] [blame] | 54 | return reinterpret_cast<MessageHeader *>(__builtin_assume_aligned( |
| 55 | static_cast<uint8_t *>(const_cast<void *>(msg)) - sizeof(MessageHeader), |
| 56 | alignof(MessageHeader))); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 57 | } |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 58 | // Padding to make the total size 8 bytes if we have 4-byte pointers or bump |
| 59 | // it to 16 if a pointer is 8 bytes by itself. |
Brian Silverman | c2e0422 | 2014-03-22 12:43:44 -0700 | [diff] [blame] | 60 | #if __SIZEOF_POINTER__ == 8 |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 61 | char padding[4]; |
Brian Silverman | c2e0422 | 2014-03-22 12:43:44 -0700 | [diff] [blame] | 62 | #elif __SIZEOF_POINTER__ == 4 |
| 63 | // No padding needed to get 8 byte total size. |
| 64 | #else |
| 65 | #error Unknown pointer size. |
| 66 | #endif |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 67 | }; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 68 | |
Brian Silverman | 4d0789d | 2014-03-23 17:03:07 -0700 | [diff] [blame] | 69 | inline int RawQueue::index_add1(int index) { |
| 70 | // Doing it this way instead of with % is more efficient on ARM. |
| 71 | int r = index + 1; |
| 72 | assert(index <= data_length_); |
| 73 | if (r == data_length_) { |
| 74 | return 0; |
| 75 | } else { |
| 76 | return r; |
| 77 | } |
| 78 | } |
| 79 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 80 | void RawQueue::DecrementMessageReferenceCount(const void *msg) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 81 | MessageHeader *header = MessageHeader::Get(msg); |
Brian Silverman | ad290d8 | 2014-03-19 17:22:05 -0700 | [diff] [blame] | 82 | __atomic_sub_fetch(&header->ref_count, 1, __ATOMIC_RELAXED); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 83 | if (kRefDebug) { |
Brian Silverman | c2e0422 | 2014-03-22 12:43:44 -0700 | [diff] [blame] | 84 | printf("%p ref dec count: %p count=%d\n", this, msg, header->ref_count); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 85 | } |
Brian Silverman | ad290d8 | 2014-03-19 17:22:05 -0700 | [diff] [blame] | 86 | |
| 87 | // The only way it should ever be 0 is if we were the last one to decrement, |
| 88 | // in which case nobody else should have it around to re-increment it or |
| 89 | // anything in the middle, so this is safe to do not atomically with the |
| 90 | // decrement. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 91 | if (header->ref_count == 0) { |
| 92 | DoFreeMessage(msg); |
Brian Silverman | ad290d8 | 2014-03-19 17:22:05 -0700 | [diff] [blame] | 93 | } else { |
| 94 | assert(header->ref_count > 0); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 98 | inline void RawQueue::IncrementMessageReferenceCount(const void *msg) const { |
Brian Silverman | 430e7fa | 2014-03-21 16:58:33 -0700 | [diff] [blame] | 99 | MessageHeader *const header = MessageHeader::Get(msg); |
| 100 | __atomic_add_fetch(&header->ref_count, 1, __ATOMIC_RELAXED); |
| 101 | if (kRefDebug) { |
Brian Silverman | c2e0422 | 2014-03-22 12:43:44 -0700 | [diff] [blame] | 102 | printf("%p ref inc count: %p\n", this, msg); |
Brian Silverman | 430e7fa | 2014-03-21 16:58:33 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 106 | inline void RawQueue::DoFreeMessage(const void *msg) { |
| 107 | MessageHeader *header = MessageHeader::Get(msg); |
| 108 | if (kRefDebug) { |
| 109 | printf("%p ref free to %p: %p\n", this, recycle_, msg); |
| 110 | } |
| 111 | |
| 112 | if (__builtin_expect(recycle_ != nullptr, 0)) { |
| 113 | void *const new_msg = recycle_->GetMessage(); |
| 114 | if (new_msg == nullptr) { |
| 115 | fprintf(stderr, "queue: couldn't get a message" |
| 116 | " for recycle queue %p\n", recycle_); |
| 117 | } else { |
| 118 | // Nobody else has a reference to the message at this point, so no need to |
| 119 | // be fancy about it. |
| 120 | ++header->ref_count; |
| 121 | if (!recycle_->WriteMessage(const_cast<void *>(msg), kOverride)) { |
| 122 | fprintf(stderr, "queue: %p->WriteMessage(%p, kOverride) failed." |
| 123 | " aborting\n", recycle_, msg); |
| 124 | printf("see stderr\n"); |
| 125 | abort(); |
| 126 | } |
| 127 | msg = new_msg; |
| 128 | header = MessageHeader::Get(new_msg); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // This works around GCC bug 60272 (fixed in 4.8.3). |
| 133 | // new_next should just get replaced with header->next (and the body of the |
| 134 | // loop should become empty). |
| 135 | // The bug is that the store to new_next after the compare/exchange is |
| 136 | // unconditional but it should only be if it fails, which could mean |
| 137 | // overwriting what somebody else who preempted us right then changed it to. |
| 138 | // TODO(brians): Get rid of this workaround once we get a new enough GCC. |
| 139 | MessageHeader *new_next = __atomic_load_n(&free_messages_, __ATOMIC_RELAXED); |
| 140 | do { |
| 141 | header->next = new_next; |
| 142 | } while (__builtin_expect( |
| 143 | !__atomic_compare_exchange_n(&free_messages_, &new_next, header, true, |
| 144 | __ATOMIC_RELEASE, __ATOMIC_RELAXED), |
| 145 | 0)); |
| 146 | } |
| 147 | |
| 148 | void *RawQueue::GetMessage() { |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 149 | MessageHeader *header = __atomic_load_n(&free_messages_, __ATOMIC_RELAXED); |
| 150 | do { |
| 151 | if (__builtin_expect(header == nullptr, 0)) { |
| 152 | LOG(FATAL, "overused pool of queue %p\n", this); |
| 153 | } |
| 154 | } while (__builtin_expect( |
| 155 | !__atomic_compare_exchange_n(&free_messages_, &header, header->next, true, |
| 156 | __ATOMIC_ACQ_REL, __ATOMIC_RELAXED), |
| 157 | 0)); |
| 158 | void *msg = reinterpret_cast<uint8_t *>(header + 1); |
| 159 | // It might be uninitialized, 0 from a previous use, or 1 from previously |
| 160 | // being recycled. |
| 161 | header->ref_count = 1; |
| 162 | static_assert( |
| 163 | __atomic_always_lock_free(sizeof(header->ref_count), &header->ref_count), |
| 164 | "we access this using not specifically atomic loads and stores"); |
| 165 | if (kRefDebug) { |
| 166 | printf("%p ref alloc: %p\n", this, msg); |
| 167 | } |
| 168 | return msg; |
| 169 | } |
| 170 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 171 | 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] | 172 | : readable_(&data_lock_), writable_(&data_lock_) { |
Brian Silverman | c2e0422 | 2014-03-22 12:43:44 -0700 | [diff] [blame] | 173 | static_assert(shm_ok<RawQueue::MessageHeader>::value, |
| 174 | "the whole point is to stick it in shared memory"); |
| 175 | static_assert((sizeof(RawQueue::MessageHeader) % 8) == 0, |
| 176 | "need to revalidate size/alignent assumptions"); |
| 177 | |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 178 | if (queue_length < 1) { |
| 179 | LOG(FATAL, "queue length %d needs to be at least 1\n", queue_length); |
| 180 | } |
| 181 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 182 | const size_t name_size = strlen(name) + 1; |
| 183 | char *temp = static_cast<char *>(shm_malloc(name_size)); |
| 184 | memcpy(temp, name, name_size); |
| 185 | name_ = temp; |
| 186 | length_ = length; |
| 187 | hash_ = hash; |
| 188 | queue_length_ = queue_length; |
| 189 | |
| 190 | next_ = NULL; |
| 191 | recycle_ = NULL; |
| 192 | |
| 193 | if (kFetchDebug) { |
| 194 | printf("initializing name=%s, length=%zd, hash=%d, queue_length=%d\n", |
| 195 | name, length, hash, queue_length); |
| 196 | } |
| 197 | |
| 198 | data_length_ = queue_length + 1; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 199 | data_ = static_cast<void **>(shm_malloc(sizeof(void *) * data_length_)); |
| 200 | data_start_ = 0; |
| 201 | data_end_ = 0; |
| 202 | messages_ = 0; |
| 203 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 204 | msg_length_ = length + sizeof(MessageHeader); |
Brian Silverman | c2e0422 | 2014-03-22 12:43:44 -0700 | [diff] [blame] | 205 | |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 206 | // Create all of the messages for the free list and stick them on. |
| 207 | { |
| 208 | MessageHeader *previous = nullptr; |
| 209 | for (int i = 0; i < queue_length + kExtraMessages; ++i) { |
| 210 | MessageHeader *const message = |
| 211 | static_cast<MessageHeader *>(shm_malloc(msg_length_)); |
| 212 | free_messages_ = message; |
| 213 | message->next = previous; |
| 214 | previous = message; |
| 215 | } |
Brian Silverman | 60eff20 | 2014-03-21 17:10:02 -0700 | [diff] [blame] | 216 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 217 | |
Brian Silverman | 3510980 | 2014-04-09 14:31:53 -0700 | [diff] [blame] | 218 | readable_waiting_ = false; |
| 219 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 220 | if (kFetchDebug) { |
| 221 | printf("made queue %s\n", name); |
| 222 | } |
| 223 | } |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 224 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 225 | RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash, |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 226 | int queue_length) { |
| 227 | if (kFetchDebug) { |
| 228 | printf("fetching queue %s\n", name); |
| 229 | } |
Brian Silverman | 4aeac5f | 2014-02-11 22:17:07 -0800 | [diff] [blame] | 230 | if (mutex_lock(&global_core->mem_struct->queues.lock) != 0) { |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 231 | LOG(FATAL, "mutex_lock(%p) failed\n", |
| 232 | &global_core->mem_struct->queues.lock); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 233 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 234 | RawQueue *current = static_cast<RawQueue *>( |
Brian Silverman | 4aeac5f | 2014-02-11 22:17:07 -0800 | [diff] [blame] | 235 | global_core->mem_struct->queues.pointer); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 236 | if (current != NULL) { |
| 237 | while (true) { |
| 238 | // If we found a matching queue. |
| 239 | if (strcmp(current->name_, name) == 0 && current->length_ == length && |
| 240 | current->hash_ == hash && current->queue_length_ == queue_length) { |
Brian Silverman | 4aeac5f | 2014-02-11 22:17:07 -0800 | [diff] [blame] | 241 | mutex_unlock(&global_core->mem_struct->queues.lock); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 242 | return current; |
| 243 | } else { |
| 244 | if (kFetchDebug) { |
| 245 | printf("rejected queue %s strcmp=%d target=%s\n", current->name_, |
| 246 | strcmp(current->name_, name), name); |
| 247 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 248 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 249 | // If this is the last one. |
| 250 | if (current->next_ == NULL) break; |
| 251 | current = current->next_; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 252 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 255 | RawQueue *r = new (shm_malloc(sizeof(RawQueue))) |
| 256 | RawQueue(name, length, hash, queue_length); |
| 257 | if (current == NULL) { // if we don't already have one |
Brian Silverman | 4aeac5f | 2014-02-11 22:17:07 -0800 | [diff] [blame] | 258 | global_core->mem_struct->queues.pointer = r; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 259 | } else { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 260 | current->next_ = r; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Brian Silverman | 4aeac5f | 2014-02-11 22:17:07 -0800 | [diff] [blame] | 263 | mutex_unlock(&global_core->mem_struct->queues.lock); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 264 | return r; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 265 | } |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 266 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 267 | RawQueue *RawQueue::Fetch(const char *name, size_t length, int hash, |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 268 | int queue_length, |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 269 | int recycle_hash, int recycle_length, RawQueue **recycle) { |
| 270 | RawQueue *r = Fetch(name, length, hash, queue_length); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 271 | r->recycle_ = Fetch(name, length, recycle_hash, recycle_length); |
| 272 | if (r == r->recycle_) { |
| 273 | fprintf(stderr, "queue: r->recycle_(=%p) == r(=%p)\n", r->recycle_, r); |
| 274 | printf("see stderr\n"); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 275 | r->recycle_ = NULL; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 276 | abort(); |
| 277 | } |
| 278 | *recycle = r->recycle_; |
| 279 | return r; |
| 280 | } |
| 281 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 282 | bool RawQueue::WriteMessage(void *msg, int options) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 283 | if (kWriteDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 284 | printf("queue: %p->WriteMessage(%p, %x)\n", this, msg, options); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 285 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 286 | { |
| 287 | MutexLocker locker(&data_lock_); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 288 | bool writable_waited = false; |
| 289 | |
| 290 | int new_end; |
| 291 | while (true) { |
Brian Silverman | 4d0789d | 2014-03-23 17:03:07 -0700 | [diff] [blame] | 292 | new_end = index_add1(data_end_); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 293 | // If there is room in the queue right now. |
| 294 | if (new_end != data_start_) break; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 295 | if (options & kNonBlock) { |
| 296 | if (kWriteDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 297 | printf("queue: not blocking on %p. returning false\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 298 | } |
Brian Silverman | 358c49f | 2014-03-05 16:56:34 -0800 | [diff] [blame] | 299 | DecrementMessageReferenceCount(msg); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 300 | return false; |
| 301 | } else if (options & kOverride) { |
| 302 | if (kWriteDebug) { |
| 303 | printf("queue: overriding on %p\n", this); |
| 304 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 305 | // Avoid leaking the message that we're going to overwrite. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 306 | DecrementMessageReferenceCount(data_[data_start_]); |
Brian Silverman | 4d0789d | 2014-03-23 17:03:07 -0700 | [diff] [blame] | 307 | data_start_ = index_add1(data_start_); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 308 | } else { // kBlock |
| 309 | if (kWriteDebug) { |
| 310 | printf("queue: going to wait for writable_ of %p\n", this); |
| 311 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 312 | writable_.Wait(); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 313 | writable_waited = true; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 314 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 315 | } |
| 316 | data_[data_end_] = msg; |
| 317 | ++messages_; |
| 318 | data_end_ = new_end; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 319 | |
Brian Silverman | 3510980 | 2014-04-09 14:31:53 -0700 | [diff] [blame] | 320 | if (readable_waiting_) { |
| 321 | if (kWriteDebug) { |
| 322 | printf("queue: broadcasting to readable_ of %p\n", this); |
| 323 | } |
| 324 | readable_waiting_ = false; |
| 325 | readable_.Broadcast(); |
| 326 | } else if (kWriteDebug) { |
| 327 | printf("queue: skipping broadcast to readable_ of %p\n", this); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 328 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 329 | |
| 330 | // If we got a signal on writable_ here and it's still writable, then we |
| 331 | // need to signal the next person in line (if any). |
| 332 | if (writable_waited && is_writable()) { |
| 333 | if (kWriteDebug) { |
| 334 | printf("queue: resignalling writable_ of %p\n", this); |
| 335 | } |
| 336 | writable_.Signal(); |
| 337 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 338 | } |
| 339 | if (kWriteDebug) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 340 | printf("queue: write returning true on queue %p\n", this); |
| 341 | } |
| 342 | return true; |
| 343 | } |
| 344 | |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 345 | inline void RawQueue::ReadCommonEnd() { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 346 | if (is_writable()) { |
| 347 | if (kReadDebug) { |
| 348 | printf("queue: %ssignalling writable_ of %p\n", |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 349 | writable_start_ ? "not " : "", this); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 350 | } |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 351 | if (!writable_start_) writable_.Signal(); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 354 | |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 355 | bool RawQueue::ReadCommonStart(int options, int *index) { |
| 356 | writable_start_ = is_writable(); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 357 | while (data_start_ == data_end_ || ((index != NULL) && messages_ <= *index)) { |
| 358 | if (options & kNonBlock) { |
| 359 | if (kReadDebug) { |
| 360 | printf("queue: not going to block waiting on %p\n", this); |
| 361 | } |
| 362 | return false; |
| 363 | } else { // kBlock |
| 364 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 365 | printf("queue: going to wait for readable_ of %p\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 366 | } |
Brian Silverman | 3510980 | 2014-04-09 14:31:53 -0700 | [diff] [blame] | 367 | readable_waiting_ = true; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 368 | // Wait for a message to become readable. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 369 | readable_.Wait(); |
| 370 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 371 | printf("queue: done waiting for readable_ of %p\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 372 | } |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | if (kReadDebug) { |
Brian Silverman | c39e2bd | 2014-02-21 09:17:35 -0800 | [diff] [blame] | 376 | printf("queue: %p->read(%p) start=%d end=%d\n", this, index, data_start_, |
| 377 | data_end_); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 378 | } |
| 379 | return true; |
| 380 | } |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 381 | |
| 382 | inline int RawQueue::LastMessageIndex() const { |
| 383 | int pos = data_end_ - 1; |
| 384 | if (pos < 0) { // If it wrapped around. |
| 385 | pos = data_length_ - 1; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 386 | } |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 387 | return pos; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 388 | } |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 389 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 390 | const void *RawQueue::ReadMessage(int options) { |
Brian Silverman | eb51cbb | 2014-03-14 22:57:08 -0700 | [diff] [blame] | 391 | // TODO(brians): Test this function. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 392 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 393 | printf("queue: %p->ReadMessage(%x)\n", this, options); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 394 | } |
| 395 | void *msg = NULL; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 396 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 397 | MutexLocker locker(&data_lock_); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 398 | |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 399 | if (!ReadCommonStart(options, nullptr)) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 400 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 401 | printf("queue: %p common returned false\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 402 | } |
| 403 | return NULL; |
| 404 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 405 | |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 406 | if (options & kFromEnd) { |
| 407 | if (options & kPeek) { |
| 408 | if (kReadDebug) { |
| 409 | printf("queue: %p shortcutting c2: %d\n", this, LastMessageIndex()); |
| 410 | } |
| 411 | msg = data_[LastMessageIndex()]; |
| 412 | IncrementMessageReferenceCount(msg); |
| 413 | } else { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 414 | while (true) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 415 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 416 | printf("queue: %p start of c2\n", this); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 417 | } |
| 418 | // This loop pulls each message out of the buffer. |
| 419 | const int pos = data_start_; |
Brian Silverman | 4d0789d | 2014-03-23 17:03:07 -0700 | [diff] [blame] | 420 | data_start_ = index_add1(data_start_); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 421 | // If this is the last one. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 422 | if (data_start_ == data_end_) { |
| 423 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 424 | printf("queue: %p reading from c2: %d\n", this, pos); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 425 | } |
| 426 | msg = data_[pos]; |
| 427 | break; |
| 428 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 429 | // This message is not going to be in the queue any more. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 430 | DecrementMessageReferenceCount(data_[pos]); |
| 431 | } |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 432 | } |
| 433 | } else { |
| 434 | if (kReadDebug) { |
| 435 | printf("queue: %p reading from d2: %d\n", this, data_start_); |
| 436 | } |
| 437 | msg = data_[data_start_]; |
| 438 | if (options & kPeek) { |
| 439 | IncrementMessageReferenceCount(msg); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 440 | } else { |
Brian Silverman | 4d0789d | 2014-03-23 17:03:07 -0700 | [diff] [blame] | 441 | data_start_ = index_add1(data_start_); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 444 | ReadCommonEnd(); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 445 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 446 | printf("queue: %p read returning %p\n", this, msg); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 447 | } |
| 448 | return msg; |
| 449 | } |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 450 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 451 | const void *RawQueue::ReadMessageIndex(int options, int *index) { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 452 | if (kReadDebug) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 453 | printf("queue: %p->ReadMessageIndex(%x, %p(*=%d))\n", |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 454 | this, options, index, *index); |
| 455 | } |
| 456 | void *msg = NULL; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 457 | |
| 458 | MutexLocker locker(&data_lock_); |
| 459 | |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 460 | if (!ReadCommonStart(options, index)) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 461 | if (kReadDebug) { |
| 462 | printf("queue: %p common returned false\n", this); |
| 463 | } |
| 464 | return NULL; |
| 465 | } |
| 466 | |
| 467 | // TODO(parker): Handle integer wrap on the index. |
| 468 | |
Brian Silverman | eb51cbb | 2014-03-14 22:57:08 -0700 | [diff] [blame] | 469 | if (options & kFromEnd) { |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 470 | if (kReadDebug) { |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 471 | printf("queue: %p reading from c1: %d\n", this, LastMessageIndex()); |
| 472 | } |
| 473 | msg = data_[LastMessageIndex()]; |
| 474 | if (!(options & kPeek)) *index = messages_; |
Brian Silverman | c39e2bd | 2014-02-21 09:17:35 -0800 | [diff] [blame] | 475 | } else { |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 476 | // Where we're going to start reading. |
| 477 | int my_start; |
| 478 | |
Brian Silverman | eb51cbb | 2014-03-14 22:57:08 -0700 | [diff] [blame] | 479 | const int unread_messages = messages_ - *index; |
| 480 | assert(unread_messages > 0); |
| 481 | int current_messages = data_end_ - data_start_; |
| 482 | if (current_messages < 0) current_messages += data_length_; |
Brian Silverman | cd2d84c | 2014-03-13 23:30:58 -0700 | [diff] [blame] | 483 | if (kReadIndexDebug) { |
Brian Silverman | eb51cbb | 2014-03-14 22:57:08 -0700 | [diff] [blame] | 484 | printf("queue: %p start=%d end=%d current=%d\n", |
| 485 | this, data_start_, data_end_, current_messages); |
Brian Silverman | cd2d84c | 2014-03-13 23:30:58 -0700 | [diff] [blame] | 486 | } |
Brian Silverman | eb51cbb | 2014-03-14 22:57:08 -0700 | [diff] [blame] | 487 | assert(current_messages > 0); |
| 488 | // If we're behind the available messages. |
| 489 | if (unread_messages > current_messages) { |
| 490 | // Catch index up to the last available message. |
| 491 | *index = messages_ - current_messages; |
| 492 | // And that's the one we're going to read. |
| 493 | my_start = data_start_; |
| 494 | if (kReadIndexDebug) { |
| 495 | printf("queue: %p jumping ahead to message %d (have %d) (at %d)\n", |
| 496 | this, *index, messages_, data_start_); |
| 497 | } |
Brian Silverman | cd2d84c | 2014-03-13 23:30:58 -0700 | [diff] [blame] | 498 | } else { |
Brian Silverman | eb51cbb | 2014-03-14 22:57:08 -0700 | [diff] [blame] | 499 | // Just start reading at the first available message that we haven't yet |
| 500 | // read. |
| 501 | my_start = data_end_ - unread_messages; |
| 502 | if (kReadIndexDebug) { |
| 503 | printf("queue: %p original read from %d\n", this, my_start); |
| 504 | } |
| 505 | if (data_start_ < data_end_) { |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 506 | assert(my_start >= 0); |
Brian Silverman | eb51cbb | 2014-03-14 22:57:08 -0700 | [diff] [blame] | 507 | } |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 508 | if (my_start < 0) my_start += data_length_; |
Brian Silverman | 67e34f5 | 2014-03-13 15:52:57 -0700 | [diff] [blame] | 509 | } |
Brian Silverman | c39e2bd | 2014-02-21 09:17:35 -0800 | [diff] [blame] | 510 | |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 511 | if (kReadDebug) { |
| 512 | printf("queue: %p reading from d1: %d\n", this, my_start); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 513 | } |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 514 | // We have to be either after the start or before the end, even if the queue |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 515 | // is wrapped around (should be both if it's not). |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 516 | assert((my_start >= data_start_) || (my_start < data_end_)); |
| 517 | // More sanity checking. |
| 518 | assert((my_start >= 0) && (my_start < data_length_)); |
| 519 | msg = data_[my_start]; |
| 520 | if (!(options & kPeek)) ++(*index); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 521 | } |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 522 | IncrementMessageReferenceCount(msg); |
| 523 | |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 524 | ReadCommonEnd(); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 525 | return msg; |
| 526 | } |
| 527 | |
Brian Silverman | c2e0422 | 2014-03-22 12:43:44 -0700 | [diff] [blame] | 528 | int RawQueue::FreeMessages() const { |
| 529 | int r = 0; |
| 530 | MessageHeader *header = free_messages_; |
| 531 | while (header != nullptr) { |
| 532 | ++r; |
| 533 | header = header->next; |
| 534 | } |
| 535 | return r; |
| 536 | } |
| 537 | |
Brian Silverman | b326732 | 2014-04-10 12:10:03 -0700 | [diff] [blame] | 538 | bool RawQueue::IsDebug() { |
| 539 | #if QUEUE_DEBUG |
| 540 | return true; |
| 541 | #else |
| 542 | return false; |
| 543 | #endif |
| 544 | } |
| 545 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 546 | } // namespace aos |