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