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