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