Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1 | #include "aos/ipc_lib/lockless_queue.h" |
| 2 | |
| 3 | #include <linux/futex.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <syscall.h> |
| 6 | #include <unistd.h> |
| 7 | #include <algorithm> |
| 8 | #include <iomanip> |
| 9 | #include <iostream> |
| 10 | #include <sstream> |
| 11 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 12 | #include "aos/ipc_lib/lockless_queue_memory.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | #include "aos/realtime.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 14 | #include "aos/util/compiler_memory_barrier.h" |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame^] | 15 | #include "gflags/gflags.h" |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 16 | #include "glog/logging.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 17 | |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame^] | 18 | DEFINE_bool(dump_lockless_queue_data, false, |
| 19 | "If true, print the data out when dumping the queue."); |
| 20 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 21 | namespace aos { |
| 22 | namespace ipc_lib { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 25 | class GrabQueueSetupLockOrDie { |
| 26 | public: |
| 27 | GrabQueueSetupLockOrDie(LocklessQueueMemory *memory) : memory_(memory) { |
| 28 | const int result = mutex_grab(&(memory->queue_setup_lock)); |
| 29 | CHECK(result == 0 || result == 1) << ": " << result; |
| 30 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 31 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 32 | ~GrabQueueSetupLockOrDie() { mutex_unlock(&(memory_->queue_setup_lock)); } |
| 33 | |
| 34 | GrabQueueSetupLockOrDie(const GrabQueueSetupLockOrDie &) = delete; |
| 35 | GrabQueueSetupLockOrDie &operator=(const GrabQueueSetupLockOrDie &) = delete; |
| 36 | |
| 37 | private: |
| 38 | LocklessQueueMemory *const memory_; |
| 39 | }; |
| 40 | |
| 41 | void Cleanup(LocklessQueueMemory *memory, const GrabQueueSetupLockOrDie &) { |
| 42 | // Make sure we start looking at shared memory fresh right now. We'll handle |
| 43 | // people dying partway through by either cleaning up after them or not, but |
| 44 | // we want to ensure we clean up after anybody who has already died when we |
| 45 | // start. |
| 46 | aos_compiler_memory_barrier(); |
| 47 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 48 | const size_t num_senders = memory->num_senders(); |
| 49 | const size_t queue_size = memory->queue_size(); |
| 50 | const size_t num_messages = memory->num_messages(); |
| 51 | |
| 52 | // There are a large number of crazy cases here for how things can go wrong |
| 53 | // and how we have to recover. They either require us to keep extra track of |
| 54 | // what is going on, slowing down the send path, or require a large number of |
| 55 | // cases. |
| 56 | // |
| 57 | // The solution here is to not over-think it. This is running while not real |
| 58 | // time during construction. It is allowed to be slow. It will also very |
| 59 | // rarely trigger. There is a small uS window where process death is |
| 60 | // ambiguous. |
| 61 | // |
| 62 | // So, build up a list N long, where N is the number of messages. Search |
| 63 | // through the entire queue and the sender list (ignoring any dead senders), |
| 64 | // and mark down which ones we have seen. Once we have seen all the messages |
| 65 | // except the N dead senders, we know which messages are dead. Because the |
| 66 | // queue is active while we do this, it may take a couple of go arounds to see |
| 67 | // everything. |
| 68 | |
| 69 | // Do the easy case. Find all senders who have died. See if they are either |
| 70 | // consistent already, or if they have copied over to_replace to the scratch |
| 71 | // index, but haven't cleared to_replace. Count them. |
| 72 | size_t valid_senders = 0; |
| 73 | for (size_t i = 0; i < num_senders; ++i) { |
| 74 | Sender *sender = memory->GetSender(i); |
| 75 | const uint32_t tid = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 76 | __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 77 | if (tid & FUTEX_OWNER_DIED) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 78 | VLOG(3) << "Found an easy death for sender " << i; |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 79 | // We can do a relaxed load here because we're the only person touching |
| 80 | // this sender at this point. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 81 | const Index to_replace = sender->to_replace.RelaxedLoad(); |
| 82 | const Index scratch_index = sender->scratch_index.Load(); |
| 83 | |
| 84 | // I find it easiest to think about this in terms of the set of observable |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 85 | // states. The main code progresses through the following states: |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 86 | |
| 87 | // 1) scratch_index = xxx |
| 88 | // to_replace = invalid |
| 89 | // This is unambiguous. Already good. |
| 90 | |
| 91 | // 2) scratch_index = xxx |
| 92 | // to_replace = yyy |
| 93 | // Very ambiguous. Is xxx or yyy the correct one? Need to either roll |
| 94 | // this forwards or backwards. |
| 95 | |
| 96 | // 3) scratch_index = yyy |
| 97 | // to_replace = yyy |
| 98 | // We are in the act of moving to_replace to scratch_index, but didn't |
| 99 | // finish. Easy. |
| 100 | |
| 101 | // 4) scratch_index = yyy |
| 102 | // to_replace = invalid |
| 103 | // Finished, but died. Looks like 1) |
| 104 | |
| 105 | // Any cleanup code needs to follow the same set of states to be robust to |
| 106 | // death, so death can be restarted. |
| 107 | |
| 108 | // Could be 2) or 3). |
| 109 | if (to_replace.valid()) { |
| 110 | // 3) |
| 111 | if (to_replace == scratch_index) { |
| 112 | // Just need to invalidate to_replace to finish. |
| 113 | sender->to_replace.Invalidate(); |
| 114 | |
| 115 | // And mark that we succeeded. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 116 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 117 | ++valid_senders; |
| 118 | } |
| 119 | } else { |
| 120 | // 1) or 4). Make sure we aren't corrupted and declare victory. |
| 121 | CHECK(scratch_index.valid()); |
| 122 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 123 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 124 | ++valid_senders; |
| 125 | } |
| 126 | } else { |
| 127 | // Not dead. |
| 128 | ++valid_senders; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // If all the senders are (or were made) good, there is no need to do the hard |
| 133 | // case. |
| 134 | if (valid_senders == num_senders) { |
| 135 | return; |
| 136 | } |
| 137 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 138 | VLOG(3) << "Starting hard cleanup"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 139 | |
| 140 | size_t num_accounted_for = 0; |
| 141 | size_t num_missing = 0; |
| 142 | ::std::vector<bool> accounted_for(num_messages, false); |
| 143 | |
| 144 | while ((num_accounted_for + num_missing) != num_messages) { |
| 145 | num_missing = 0; |
| 146 | for (size_t i = 0; i < num_senders; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 147 | Sender *const sender = memory->GetSender(i); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 148 | const uint32_t tid = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 149 | __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 150 | if (tid & FUTEX_OWNER_DIED) { |
| 151 | ++num_missing; |
| 152 | } else { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 153 | // We can do a relaxed load here because we're the only person touching |
| 154 | // this sender at this point, if it matters. If it's not a dead sender, |
| 155 | // then any message it every has will already be accounted for, so this |
| 156 | // will always be a NOP. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 157 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 158 | if (!accounted_for[scratch_index.message_index()]) { |
| 159 | ++num_accounted_for; |
| 160 | } |
| 161 | accounted_for[scratch_index.message_index()] = true; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | for (size_t i = 0; i < queue_size; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 166 | // Same logic as above for scratch_index applies here too. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 167 | const Index index = memory->GetQueue(i)->RelaxedLoad(); |
| 168 | if (!accounted_for[index.message_index()]) { |
| 169 | ++num_accounted_for; |
| 170 | } |
| 171 | accounted_for[index.message_index()] = true; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | while (num_missing != 0) { |
| 176 | const size_t starting_num_missing = num_missing; |
| 177 | for (size_t i = 0; i < num_senders; ++i) { |
| 178 | Sender *sender = memory->GetSender(i); |
| 179 | const uint32_t tid = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 180 | __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 181 | if (tid & FUTEX_OWNER_DIED) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 182 | // We can do relaxed loads here because we're the only person touching |
| 183 | // this sender at this point. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 184 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 185 | const Index to_replace = sender->to_replace.RelaxedLoad(); |
| 186 | |
| 187 | // Candidate. |
Brian Silverman | ba3b04d | 2020-07-02 19:44:37 -0700 | [diff] [blame] | 188 | if (to_replace.valid()) { |
| 189 | CHECK_LE(to_replace.message_index(), accounted_for.size()); |
| 190 | } |
| 191 | if (scratch_index.valid()) { |
| 192 | CHECK_LE(scratch_index.message_index(), accounted_for.size()); |
| 193 | } |
| 194 | if (!to_replace.valid() || accounted_for[to_replace.message_index()]) { |
| 195 | CHECK(scratch_index.valid()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 196 | VLOG(3) << "Sender " << i |
| 197 | << " died, to_replace is already accounted for"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 198 | // If both are accounted for, we are corrupt... |
| 199 | CHECK(!accounted_for[scratch_index.message_index()]); |
| 200 | |
| 201 | // to_replace is already accounted for. This means that we didn't |
| 202 | // atomically insert scratch_index into the queue yet. So |
| 203 | // invalidate to_replace. |
| 204 | sender->to_replace.Invalidate(); |
| 205 | |
| 206 | // And then mark this sender clean. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 207 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 208 | |
| 209 | // And account for scratch_index. |
| 210 | accounted_for[scratch_index.message_index()] = true; |
| 211 | --num_missing; |
| 212 | ++num_accounted_for; |
Brian Silverman | ba3b04d | 2020-07-02 19:44:37 -0700 | [diff] [blame] | 213 | } else if (!scratch_index.valid() || |
| 214 | accounted_for[scratch_index.message_index()]) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 215 | VLOG(3) << "Sender " << i |
| 216 | << " died, scratch_index is already accounted for"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 217 | // scratch_index is accounted for. That means we did the insert, |
| 218 | // but didn't record it. |
| 219 | CHECK(to_replace.valid()); |
| 220 | // Finish the transaction. Copy to_replace, then clear it. |
| 221 | |
| 222 | sender->scratch_index.Store(to_replace); |
| 223 | sender->to_replace.Invalidate(); |
| 224 | |
| 225 | // And then mark this sender clean. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 226 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 227 | |
| 228 | // And account for to_replace. |
| 229 | accounted_for[to_replace.message_index()] = true; |
| 230 | --num_missing; |
| 231 | ++num_accounted_for; |
| 232 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 233 | VLOG(3) << "Sender " << i << " died, neither is accounted for"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 234 | // Ambiguous. There will be an unambiguous one somewhere that we |
| 235 | // can do first. |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | // CHECK that we are making progress. |
| 240 | CHECK_NE(num_missing, starting_num_missing); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // Exposes rt_tgsigqueueinfo so we can send the signal *just* to the target |
| 245 | // thread. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 246 | // TODO(Brian): Do directly in assembly for armhf at least for efficiency. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 247 | int rt_tgsigqueueinfo(pid_t tgid, pid_t tid, int sig, siginfo_t *si) { |
| 248 | return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, si); |
| 249 | } |
| 250 | |
| 251 | } // namespace |
| 252 | |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 253 | size_t LocklessQueueConfiguration::message_size() const { |
| 254 | // Round up the message size so following data is aligned appropriately. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 255 | return LocklessQueueMemory::AlignmentRoundUp(message_data_size + |
| 256 | (kChannelDataAlignment - 1)) + |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 257 | sizeof(Message); |
| 258 | } |
| 259 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 260 | size_t LocklessQueueMemorySize(LocklessQueueConfiguration config) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 261 | // Round up the message size so following data is aligned appropriately. |
| 262 | config.message_data_size = |
| 263 | LocklessQueueMemory::AlignmentRoundUp(config.message_data_size); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 264 | |
| 265 | // As we build up the size, confirm that everything is aligned to the |
| 266 | // alignment requirements of the type. |
| 267 | size_t size = sizeof(LocklessQueueMemory); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 268 | CHECK_EQ(size % alignof(LocklessQueueMemory), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 269 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 270 | CHECK_EQ(size % alignof(AtomicIndex), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 271 | size += LocklessQueueMemory::SizeOfQueue(config); |
| 272 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 273 | CHECK_EQ(size % alignof(Message), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 274 | size += LocklessQueueMemory::SizeOfMessages(config); |
| 275 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 276 | CHECK_EQ(size % alignof(Watcher), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 277 | size += LocklessQueueMemory::SizeOfWatchers(config); |
| 278 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 279 | CHECK_EQ(size % alignof(Sender), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 280 | size += LocklessQueueMemory::SizeOfSenders(config); |
| 281 | |
| 282 | return size; |
| 283 | } |
| 284 | |
| 285 | LocklessQueueMemory *InitializeLocklessQueueMemory( |
| 286 | LocklessQueueMemory *memory, LocklessQueueConfiguration config) { |
| 287 | // Everything should be zero initialized already. So we just need to fill |
| 288 | // everything out properly. |
| 289 | |
Brian Silverman | c57ff0a | 2020-04-28 16:45:13 -0700 | [diff] [blame] | 290 | // This is the UID we will use for checking signal-sending permission |
| 291 | // compatibility. |
| 292 | // |
| 293 | // The manpage says: |
| 294 | // For a process to have permission to send a signal, it must either be |
| 295 | // privileged [...], or the real or effective user ID of the sending process |
| 296 | // must equal the real or saved set-user-ID of the target process. |
| 297 | // |
| 298 | // Processes typically initialize a queue in random order as they start up. |
| 299 | // This means we need an algorithm for verifying all processes have |
| 300 | // permissions to send each other signals which gives the same answer no |
| 301 | // matter what order they attach in. We would also like to avoid maintaining a |
| 302 | // shared list of the UIDs of all processes. |
| 303 | // |
| 304 | // To do this while still giving sufficient flexibility for all current use |
| 305 | // cases, we track a single UID for the queue. All processes with a matching |
| 306 | // euid+suid must have this UID. Any processes with distinct euid/suid must |
| 307 | // instead have a matching ruid. This guarantees signals can be sent between |
| 308 | // all processes attached to the queue. |
| 309 | // |
| 310 | // In particular, this allows a process to change only its euid (to interact |
| 311 | // with a queue) while still maintaining privileges via its ruid. However, it |
| 312 | // can only use privileges in ways that do not require changing the euid back, |
| 313 | // because while the euid is different it will not be able to receive signals. |
| 314 | // We can't actually verify that, but we can sanity check that things are |
| 315 | // valid when the queue is initialized. |
| 316 | |
| 317 | uid_t uid; |
| 318 | { |
| 319 | uid_t ruid, euid, suid; |
| 320 | PCHECK(getresuid(&ruid, &euid, &suid) == 0); |
| 321 | // If these are equal, then use them, even if that's different from the real |
| 322 | // UID. This allows processes to keep a real UID of 0 (to have permissions |
| 323 | // to perform system-level changes) while still being able to communicate |
| 324 | // with processes running unprivileged as a distinct user. |
| 325 | if (euid == suid) { |
| 326 | uid = euid; |
| 327 | VLOG(1) << "Using euid==suid " << uid; |
| 328 | } else { |
| 329 | uid = ruid; |
| 330 | VLOG(1) << "Using ruid " << ruid; |
| 331 | } |
| 332 | } |
| 333 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 334 | // Grab the mutex. We don't care if the previous reader died. We are going |
| 335 | // to check everything anyways. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 336 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 337 | |
| 338 | if (!memory->initialized) { |
| 339 | // TODO(austin): Check these for out of bounds. |
| 340 | memory->config.num_watchers = config.num_watchers; |
| 341 | memory->config.num_senders = config.num_senders; |
| 342 | memory->config.queue_size = config.queue_size; |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 343 | memory->config.message_data_size = config.message_data_size; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 344 | |
| 345 | const size_t num_messages = memory->num_messages(); |
| 346 | // There need to be at most MaxMessages() messages allocated. |
| 347 | CHECK_LE(num_messages, Index::MaxMessages()); |
| 348 | |
| 349 | for (size_t i = 0; i < num_messages; ++i) { |
| 350 | memory->GetMessage(Index(QueueIndex::Zero(memory->queue_size()), i)) |
| 351 | ->header.queue_index.Invalidate(); |
| 352 | } |
| 353 | |
| 354 | for (size_t i = 0; i < memory->queue_size(); ++i) { |
| 355 | // Make the initial counter be the furthest away number. That means that |
| 356 | // index 0 should be 0xffff, 1 should be 0, etc. |
| 357 | memory->GetQueue(i)->Store(Index(QueueIndex::Zero(memory->queue_size()) |
| 358 | .IncrementBy(i) |
| 359 | .DecrementBy(memory->queue_size()), |
| 360 | i)); |
| 361 | } |
| 362 | |
| 363 | memory->next_queue_index.Invalidate(); |
Brian Silverman | c57ff0a | 2020-04-28 16:45:13 -0700 | [diff] [blame] | 364 | memory->uid = uid; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 365 | |
| 366 | for (size_t i = 0; i < memory->num_senders(); ++i) { |
| 367 | ::aos::ipc_lib::Sender *s = memory->GetSender(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 368 | // Nobody else can possibly be touching these because we haven't set |
| 369 | // initialized to true yet. |
| 370 | s->scratch_index.RelaxedStore(Index(0xffff, i + memory->queue_size())); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 371 | s->to_replace.RelaxedInvalidate(); |
| 372 | } |
| 373 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 374 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 375 | // Signal everything is done. This needs to be done last, so if we die, we |
| 376 | // redo initialization. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 377 | memory->initialized = true; |
Austin Schuh | 3328d13 | 2020-02-28 13:54:57 -0800 | [diff] [blame] | 378 | } else { |
Brian Silverman | c57ff0a | 2020-04-28 16:45:13 -0700 | [diff] [blame] | 379 | CHECK_EQ(uid, memory->uid) << ": UIDs must match for all processes"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 382 | return memory; |
| 383 | } |
| 384 | |
| 385 | LocklessQueue::LocklessQueue(LocklessQueueMemory *memory, |
| 386 | LocklessQueueConfiguration config) |
| 387 | : memory_(InitializeLocklessQueueMemory(memory, config)), |
| 388 | watcher_copy_(memory_->num_watchers()), |
| 389 | pid_(getpid()), |
| 390 | uid_(getuid()) {} |
| 391 | |
| 392 | LocklessQueue::~LocklessQueue() { |
| 393 | CHECK_EQ(watcher_index_, -1); |
| 394 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 395 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 396 | const int num_watchers = memory_->num_watchers(); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 397 | // Cleanup is cheap. The next user will do it anyways, so no need for us to do |
| 398 | // anything right now. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 399 | |
| 400 | // And confirm that nothing is owned by us. |
| 401 | for (int i = 0; i < num_watchers; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 402 | CHECK(!death_notification_is_held(&(memory_->GetWatcher(i)->tid))); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 403 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | size_t LocklessQueue::QueueSize() const { return memory_->queue_size(); } |
| 407 | |
| 408 | bool LocklessQueue::RegisterWakeup(int priority) { |
| 409 | // TODO(austin): Make sure signal coalescing is turned on. We don't need |
| 410 | // duplicates. That will improve performance under high load. |
| 411 | |
| 412 | // Since everything is self consistent, all we need to do is make sure nobody |
| 413 | // else is running. Someone dying will get caught in the generic consistency |
| 414 | // check. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 415 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 416 | const int num_watchers = memory_->num_watchers(); |
| 417 | |
| 418 | // Now, find the first empty watcher and grab it. |
| 419 | CHECK_EQ(watcher_index_, -1); |
| 420 | for (int i = 0; i < num_watchers; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 421 | // If we see a slot the kernel has marked as dead, everything we do reusing |
| 422 | // it needs to happen-after whatever that process did before dying. |
Brian Silverman | 2484eea | 2019-12-21 16:48:46 -0800 | [diff] [blame] | 423 | auto *const futex = &(memory_->GetWatcher(i)->tid.futex); |
| 424 | const uint32_t tid = __atomic_load_n(futex, __ATOMIC_ACQUIRE); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 425 | if (tid == 0 || (tid & FUTEX_OWNER_DIED)) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 426 | watcher_index_ = i; |
Brian Silverman | 2484eea | 2019-12-21 16:48:46 -0800 | [diff] [blame] | 427 | // Relaxed is OK here because we're the only task going to touch it |
| 428 | // between here and the write in death_notification_init below (other |
| 429 | // recovery is blocked by us holding the setup lock). |
| 430 | __atomic_store_n(futex, 0, __ATOMIC_RELAXED); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 431 | break; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // Bail if we failed to find an open slot. |
| 436 | if (watcher_index_ == -1) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 437 | return false; |
| 438 | } |
| 439 | |
| 440 | Watcher *w = memory_->GetWatcher(watcher_index_); |
| 441 | |
| 442 | w->pid = getpid(); |
| 443 | w->priority = priority; |
| 444 | |
| 445 | // Grabbing a mutex is a compiler and memory barrier, so nothing before will |
| 446 | // get rearranged afterwords. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 447 | death_notification_init(&(w->tid)); |
| 448 | return true; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | void LocklessQueue::UnregisterWakeup() { |
| 452 | // Since everything is self consistent, all we need to do is make sure nobody |
| 453 | // else is running. Someone dying will get caught in the generic consistency |
| 454 | // check. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 455 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 456 | |
| 457 | // Make sure we are registered. |
| 458 | CHECK_NE(watcher_index_, -1); |
| 459 | |
| 460 | // Make sure we still own the slot we are supposed to. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 461 | CHECK( |
| 462 | death_notification_is_held(&(memory_->GetWatcher(watcher_index_)->tid))); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 463 | |
| 464 | // The act of unlocking invalidates the entry. Invalidate it. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 465 | death_notification_release(&(memory_->GetWatcher(watcher_index_)->tid)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 466 | // And internally forget the slot. |
| 467 | watcher_index_ = -1; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | int LocklessQueue::Wakeup(const int current_priority) { |
| 471 | const size_t num_watchers = memory_->num_watchers(); |
| 472 | |
| 473 | CHECK_EQ(watcher_copy_.size(), num_watchers); |
| 474 | |
| 475 | // Grab a copy so it won't change out from underneath us, and we can sort it |
| 476 | // nicely in C++. |
| 477 | // Do note that there is still a window where the process can die *after* we |
| 478 | // read everything. We will still PI boost and send a signal to the thread in |
| 479 | // question. There is no way without pidfd's to close this window, and |
| 480 | // creating a pidfd is likely not RT. |
| 481 | for (size_t i = 0; i < num_watchers; ++i) { |
| 482 | Watcher *w = memory_->GetWatcher(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 483 | watcher_copy_[i].tid = __atomic_load_n(&(w->tid.futex), __ATOMIC_RELAXED); |
| 484 | // Force the load of the TID to come first. |
| 485 | aos_compiler_memory_barrier(); |
| 486 | watcher_copy_[i].pid = w->pid.load(std::memory_order_relaxed); |
| 487 | watcher_copy_[i].priority = w->priority.load(std::memory_order_relaxed); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 488 | |
| 489 | // Use a priority of -1 to mean an invalid entry to make sorting easier. |
| 490 | if (watcher_copy_[i].tid & FUTEX_OWNER_DIED || watcher_copy_[i].tid == 0) { |
| 491 | watcher_copy_[i].priority = -1; |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 492 | } else { |
| 493 | // Ensure all of this happens after we're done looking at the pid+priority |
| 494 | // in shared memory. |
| 495 | aos_compiler_memory_barrier(); |
| 496 | if (watcher_copy_[i].tid != static_cast<pid_t>(__atomic_load_n( |
| 497 | &(w->tid.futex), __ATOMIC_RELAXED))) { |
| 498 | // Confirm that the watcher hasn't been re-used and modified while we |
| 499 | // read it. If it has, mark it invalid again. |
| 500 | watcher_copy_[i].priority = -1; |
| 501 | watcher_copy_[i].tid = 0; |
| 502 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
| 506 | // Now sort. |
| 507 | ::std::sort(watcher_copy_.begin(), watcher_copy_.end(), |
| 508 | [](const WatcherCopy &a, const WatcherCopy &b) { |
| 509 | return a.priority > b.priority; |
| 510 | }); |
| 511 | |
| 512 | int count = 0; |
| 513 | if (watcher_copy_[0].priority != -1) { |
| 514 | const int max_priority = |
| 515 | ::std::max(current_priority, watcher_copy_[0].priority); |
| 516 | // Boost if we are RT and there is a higher priority sender out there. |
| 517 | // Otherwise we might run into priority inversions. |
| 518 | if (max_priority > current_priority && current_priority > 0) { |
| 519 | SetCurrentThreadRealtimePriority(max_priority); |
| 520 | } |
| 521 | |
| 522 | // Build up the siginfo to send. |
| 523 | siginfo_t uinfo; |
| 524 | memset(&uinfo, 0, sizeof(uinfo)); |
| 525 | |
| 526 | uinfo.si_code = SI_QUEUE; |
| 527 | uinfo.si_pid = pid_; |
| 528 | uinfo.si_uid = uid_; |
| 529 | uinfo.si_value.sival_int = 0; |
| 530 | |
| 531 | for (const WatcherCopy &watcher_copy : watcher_copy_) { |
| 532 | // The first -1 priority means we are at the end of the valid list. |
| 533 | if (watcher_copy.priority == -1) { |
| 534 | break; |
| 535 | } |
| 536 | |
| 537 | // Send the signal. Target just the thread that sent it so that we can |
| 538 | // support multiple watchers in a process (when someone creates multiple |
| 539 | // event loops in different threads). |
| 540 | rt_tgsigqueueinfo(watcher_copy.pid, watcher_copy.tid, kWakeupSignal, |
| 541 | &uinfo); |
| 542 | |
| 543 | ++count; |
| 544 | } |
| 545 | |
| 546 | // Drop back down if we were boosted. |
| 547 | if (max_priority > current_priority && current_priority > 0) { |
| 548 | SetCurrentThreadRealtimePriority(current_priority); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | return count; |
| 553 | } |
| 554 | |
| 555 | LocklessQueue::Sender::Sender(LocklessQueueMemory *memory) : memory_(memory) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 556 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 557 | |
| 558 | // Since we already have the lock, go ahead and try cleaning up. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 559 | Cleanup(memory_, grab_queue_setup_lock); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 560 | |
| 561 | const int num_senders = memory_->num_senders(); |
| 562 | |
| 563 | for (int i = 0; i < num_senders; ++i) { |
| 564 | ::aos::ipc_lib::Sender *s = memory->GetSender(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 565 | // This doesn't need synchronization because we're the only process doing |
| 566 | // initialization right now, and nobody else will be touching senders which |
| 567 | // we're interested in. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 568 | const uint32_t tid = __atomic_load_n(&(s->tid.futex), __ATOMIC_RELAXED); |
| 569 | if (tid == 0) { |
| 570 | sender_index_ = i; |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (sender_index_ == -1) { |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 576 | VLOG(1) << "Too many senders, starting to bail."; |
| 577 | return; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | ::aos::ipc_lib::Sender *s = memory_->GetSender(sender_index_); |
| 581 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 582 | // Indicate that we are now alive by taking over the slot. If the previous |
| 583 | // owner died, we still want to do this. |
| 584 | death_notification_init(&(s->tid)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | LocklessQueue::Sender::~Sender() { |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 588 | if (valid()) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 589 | death_notification_release(&(memory_->GetSender(sender_index_)->tid)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 593 | std::optional<LocklessQueue::Sender> LocklessQueue::MakeSender() { |
| 594 | LocklessQueue::Sender result = LocklessQueue::Sender(memory_); |
| 595 | if (result.valid()) { |
| 596 | return std::move(result); |
| 597 | } else { |
| 598 | return std::nullopt; |
| 599 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | QueueIndex ZeroOrValid(QueueIndex index) { |
| 603 | if (!index.valid()) { |
| 604 | return index.Clear(); |
| 605 | } |
| 606 | return index; |
| 607 | } |
| 608 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 609 | size_t LocklessQueue::Sender::size() { return memory_->message_data_size(); } |
| 610 | |
| 611 | void *LocklessQueue::Sender::Data() { |
| 612 | ::aos::ipc_lib::Sender *sender = memory_->GetSender(sender_index_); |
| 613 | Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 614 | Message *message = memory_->GetMessage(scratch_index); |
| 615 | message->header.queue_index.Invalidate(); |
| 616 | |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 617 | return message->data(memory_->message_data_size()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 620 | void LocklessQueue::Sender::Send( |
| 621 | const char *data, size_t length, |
| 622 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 623 | aos::realtime_clock::time_point realtime_remote_time, |
| 624 | uint32_t remote_queue_index, |
| 625 | aos::monotonic_clock::time_point *monotonic_sent_time, |
| 626 | aos::realtime_clock::time_point *realtime_sent_time, |
| 627 | uint32_t *queue_index) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 628 | CHECK_LE(length, size()); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 629 | // Flatbuffers write from the back of the buffer to the front. If we are |
| 630 | // going to write an explicit chunk of memory into the buffer, we need to |
| 631 | // adhere to this convention and place it at the end. |
| 632 | memcpy((reinterpret_cast<char *>(Data()) + size() - length), data, length); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 633 | Send(length, monotonic_remote_time, realtime_remote_time, remote_queue_index, |
| 634 | monotonic_sent_time, realtime_sent_time, queue_index); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 637 | void LocklessQueue::Sender::Send( |
| 638 | size_t length, aos::monotonic_clock::time_point monotonic_remote_time, |
| 639 | aos::realtime_clock::time_point realtime_remote_time, |
| 640 | uint32_t remote_queue_index, |
| 641 | aos::monotonic_clock::time_point *monotonic_sent_time, |
| 642 | aos::realtime_clock::time_point *realtime_sent_time, |
| 643 | uint32_t *queue_index) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 644 | const size_t queue_size = memory_->queue_size(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 645 | CHECK_LE(length, size()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 646 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 647 | ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_); |
| 648 | // We can do a relaxed load on our sender because we're the only person |
| 649 | // modifying it right now. |
| 650 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 651 | Message *const message = memory_->GetMessage(scratch_index); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 652 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 653 | message->header.length = length; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 654 | // Pass these through. Any alternative behavior can be implemented out a |
| 655 | // layer. |
| 656 | message->header.remote_queue_index = remote_queue_index; |
| 657 | message->header.monotonic_remote_time = monotonic_remote_time; |
| 658 | message->header.realtime_remote_time = realtime_remote_time; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 659 | |
| 660 | while (true) { |
| 661 | const QueueIndex actual_next_queue_index = |
| 662 | memory_->next_queue_index.Load(queue_size); |
| 663 | const QueueIndex next_queue_index = ZeroOrValid(actual_next_queue_index); |
| 664 | |
| 665 | const QueueIndex incremented_queue_index = next_queue_index.Increment(); |
| 666 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 667 | // This needs to synchronize with whoever the previous writer at this |
| 668 | // location was. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 669 | const Index to_replace = memory_->LoadIndex(next_queue_index); |
| 670 | |
| 671 | const QueueIndex decremented_queue_index = |
| 672 | next_queue_index.DecrementBy(queue_size); |
| 673 | |
| 674 | // See if we got beat. If we did, try to atomically update |
| 675 | // next_queue_index in case the previous writer failed and retry. |
| 676 | if (!to_replace.IsPlausible(decremented_queue_index)) { |
| 677 | // We don't care about the result. It will either succeed, or we got |
| 678 | // beat in fixing it and just need to give up and try again. If we got |
| 679 | // beat multiple times, the only way progress can be made is if the queue |
| 680 | // is updated as well. This means that if we retry reading |
| 681 | // next_queue_index, we will be at most off by one and can retry. |
| 682 | // |
| 683 | // Both require no further action from us. |
| 684 | // |
| 685 | // TODO(austin): If we are having fairness issues under contention, we |
| 686 | // could have a mode bit in next_queue_index, and could use a lock or some |
| 687 | // other form of PI boosting to let the higher priority task win. |
| 688 | memory_->next_queue_index.CompareAndExchangeStrong( |
| 689 | actual_next_queue_index, incremented_queue_index); |
| 690 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 691 | VLOG(3) << "We were beat. Try again. Was " << std::hex |
| 692 | << to_replace.get() << ", is " << decremented_queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 693 | continue; |
| 694 | } |
| 695 | |
| 696 | // Confirm that the message is what it should be. |
| 697 | { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 698 | const QueueIndex previous_index = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 699 | memory_->GetMessage(to_replace)->header.queue_index.Load(queue_size); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 700 | if (previous_index != decremented_queue_index && previous_index.valid()) { |
| 701 | // Retry. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 702 | VLOG(3) << "Something fishy happened, queue index doesn't match. " |
| 703 | "Retrying. Previous index was " |
| 704 | << std::hex << previous_index.index() << ", should be " |
| 705 | << decremented_queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 706 | continue; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | message->header.monotonic_sent_time = ::aos::monotonic_clock::now(); |
| 711 | message->header.realtime_sent_time = ::aos::realtime_clock::now(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 712 | if (monotonic_sent_time != nullptr) { |
| 713 | *monotonic_sent_time = message->header.monotonic_sent_time; |
| 714 | } |
| 715 | if (realtime_sent_time != nullptr) { |
| 716 | *realtime_sent_time = message->header.realtime_sent_time; |
| 717 | } |
| 718 | if (queue_index != nullptr) { |
| 719 | *queue_index = next_queue_index.index(); |
| 720 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 721 | |
| 722 | // Before we are fully done filling out the message, update the Sender state |
| 723 | // with the new index to write. This re-uses the barrier for the |
| 724 | // queue_index store. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 725 | const Index index_to_write(next_queue_index, scratch_index.message_index()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 726 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 727 | aos_compiler_memory_barrier(); |
| 728 | // We're the only person who cares about our scratch index, besides somebody |
| 729 | // cleaning up after us. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 730 | sender->scratch_index.RelaxedStore(index_to_write); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 731 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 732 | |
| 733 | message->header.queue_index.Store(next_queue_index); |
| 734 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 735 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 736 | // The message is now filled out, and we have a confirmed slot to store |
| 737 | // into. |
| 738 | // |
| 739 | // Start by writing down what we are going to pull out of the queue. This |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 740 | // was Invalid before now. Only person who will read this is whoever cleans |
| 741 | // up after us, so no synchronization necessary. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 742 | sender->to_replace.RelaxedStore(to_replace); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 743 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 744 | |
| 745 | // Then exchange the next index into the queue. |
| 746 | if (!memory_->GetQueue(next_queue_index.Wrapped()) |
| 747 | ->CompareAndExchangeStrong(to_replace, index_to_write)) { |
| 748 | // Aw, didn't succeed. Retry. |
| 749 | sender->to_replace.RelaxedInvalidate(); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 750 | aos_compiler_memory_barrier(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 751 | VLOG(3) << "Failed to wrap into queue"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 752 | continue; |
| 753 | } |
| 754 | |
| 755 | // Then update next_queue_index to save the next user some computation time. |
| 756 | memory_->next_queue_index.CompareAndExchangeStrong(actual_next_queue_index, |
| 757 | incremented_queue_index); |
| 758 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 759 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 760 | // Now update the scratch space and record that we succeeded. |
| 761 | sender->scratch_index.Store(to_replace); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 762 | aos_compiler_memory_barrier(); |
| 763 | // And then record that we succeeded, but definitely after the above store. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 764 | sender->to_replace.RelaxedInvalidate(); |
| 765 | break; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | LocklessQueue::ReadResult LocklessQueue::Read( |
| 770 | uint32_t uint32_queue_index, |
| 771 | ::aos::monotonic_clock::time_point *monotonic_sent_time, |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 772 | ::aos::realtime_clock::time_point *realtime_sent_time, |
| 773 | ::aos::monotonic_clock::time_point *monotonic_remote_time, |
| 774 | ::aos::realtime_clock::time_point *realtime_remote_time, |
| 775 | uint32_t *remote_queue_index, size_t *length, char *data) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 776 | const size_t queue_size = memory_->queue_size(); |
| 777 | |
| 778 | // Build up the QueueIndex. |
| 779 | const QueueIndex queue_index = |
| 780 | QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index); |
| 781 | |
| 782 | // Read the message stored at the requested location. |
| 783 | Index mi = memory_->LoadIndex(queue_index); |
| 784 | Message *m = memory_->GetMessage(mi); |
| 785 | |
| 786 | while (true) { |
| 787 | // We need to confirm that the data doesn't change while we are reading it. |
| 788 | // Do that by first confirming that the message points to the queue index we |
| 789 | // want. |
| 790 | const QueueIndex starting_queue_index = |
| 791 | m->header.queue_index.Load(queue_size); |
| 792 | if (starting_queue_index != queue_index) { |
| 793 | // If we found a message that is exactly 1 loop old, we just wrapped. |
| 794 | if (starting_queue_index == queue_index.DecrementBy(queue_size)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 795 | VLOG(3) << "Matches: " << std::hex << starting_queue_index.index() |
| 796 | << ", " << queue_index.DecrementBy(queue_size).index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 797 | return ReadResult::NOTHING_NEW; |
| 798 | } else { |
| 799 | // Someone has re-used this message between when we pulled it out of the |
| 800 | // queue and when we grabbed its index. It is pretty hard to deduce |
| 801 | // what happened. Just try again. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 802 | Message *const new_m = memory_->GetMessage(queue_index); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 803 | if (m != new_m) { |
| 804 | m = new_m; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 805 | VLOG(3) << "Retrying, m doesn't match"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 806 | continue; |
| 807 | } |
| 808 | |
| 809 | // We have confirmed that message still points to the same message. This |
| 810 | // means that the message didn't get swapped out from under us, so |
| 811 | // starting_queue_index is correct. |
| 812 | // |
| 813 | // Either we got too far behind (signaled by this being a valid |
| 814 | // message), or this is one of the initial messages which are invalid. |
| 815 | if (starting_queue_index.valid()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 816 | VLOG(3) << "Too old. Tried for " << std::hex << queue_index.index() |
| 817 | << ", got " << starting_queue_index.index() << ", behind by " |
| 818 | << std::dec |
| 819 | << (starting_queue_index.index() - queue_index.index()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 820 | return ReadResult::TOO_OLD; |
| 821 | } |
| 822 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 823 | VLOG(3) << "Initial"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 824 | |
| 825 | // There isn't a valid message at this location. |
| 826 | // |
| 827 | // If someone asks for one of the messages within the first go around, |
| 828 | // then they need to wait. They got ahead. Otherwise, they are |
| 829 | // asking for something crazy, like something before the beginning of |
| 830 | // the queue. Tell them that they are behind. |
| 831 | if (uint32_queue_index < memory_->queue_size()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 832 | VLOG(3) << "Near zero, " << std::hex << uint32_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 833 | return ReadResult::NOTHING_NEW; |
| 834 | } else { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 835 | VLOG(3) << "Not near zero, " << std::hex << uint32_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 836 | return ReadResult::TOO_OLD; |
| 837 | } |
| 838 | } |
| 839 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 840 | VLOG(3) << "Eq: " << std::hex << starting_queue_index.index() << ", " |
| 841 | << queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 842 | break; |
| 843 | } |
| 844 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 845 | // Then read the data out. Copy it all out to be deterministic and so we can |
| 846 | // make length be from either end. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 847 | *monotonic_sent_time = m->header.monotonic_sent_time; |
| 848 | *realtime_sent_time = m->header.realtime_sent_time; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 849 | if (m->header.remote_queue_index == 0xffffffffu) { |
| 850 | *remote_queue_index = queue_index.index(); |
| 851 | } else { |
| 852 | *remote_queue_index = m->header.remote_queue_index; |
| 853 | } |
| 854 | *monotonic_remote_time = m->header.monotonic_remote_time; |
| 855 | *realtime_remote_time = m->header.realtime_remote_time; |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 856 | if (data) { |
| 857 | memcpy(data, m->data(memory_->message_data_size()), message_data_size()); |
| 858 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 859 | *length = m->header.length; |
| 860 | |
| 861 | // And finally, confirm that the message *still* points to the queue index we |
| 862 | // want. This means it didn't change out from under us. |
| 863 | // If something changed out from under us, we were reading it much too late in |
| 864 | // it's lifetime. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 865 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 866 | const QueueIndex final_queue_index = m->header.queue_index.Load(queue_size); |
| 867 | if (final_queue_index != queue_index) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 868 | VLOG(3) << "Changed out from under us. Reading " << std::hex |
| 869 | << queue_index.index() << ", finished with " |
| 870 | << final_queue_index.index() << ", delta: " << std::dec |
| 871 | << (final_queue_index.index() - queue_index.index()); |
| 872 | return ReadResult::OVERWROTE; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | return ReadResult::GOOD; |
| 876 | } |
| 877 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 878 | size_t LocklessQueue::queue_size() const { return memory_->queue_size(); } |
| 879 | size_t LocklessQueue::message_data_size() const { |
| 880 | return memory_->message_data_size(); |
| 881 | } |
| 882 | |
| 883 | QueueIndex LocklessQueue::LatestQueueIndex() { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 884 | const size_t queue_size = memory_->queue_size(); |
| 885 | |
| 886 | // There is only one interesting case. We need to know if the queue is empty. |
| 887 | // That is done with a sentinel value. At worst, this will be off by one. |
| 888 | const QueueIndex next_queue_index = |
| 889 | memory_->next_queue_index.Load(queue_size); |
| 890 | if (next_queue_index.valid()) { |
| 891 | const QueueIndex current_queue_index = next_queue_index.DecrementBy(1u); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 892 | return current_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 893 | } else { |
| 894 | return empty_queue_index(); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | namespace { |
| 899 | |
| 900 | // Prints out the mutex state. Not safe to use while the mutex is being |
| 901 | // changed. |
| 902 | ::std::string PrintMutex(aos_mutex *mutex) { |
| 903 | ::std::stringstream s; |
| 904 | s << "aos_mutex(" << ::std::hex << mutex->futex; |
| 905 | |
| 906 | if (mutex->futex != 0) { |
| 907 | s << ":"; |
| 908 | if (mutex->futex & FUTEX_OWNER_DIED) { |
| 909 | s << "FUTEX_OWNER_DIED|"; |
| 910 | } |
| 911 | s << "tid=" << (mutex->futex & FUTEX_TID_MASK); |
| 912 | } |
| 913 | |
| 914 | s << ")"; |
| 915 | return s.str(); |
| 916 | } |
| 917 | |
| 918 | } // namespace |
| 919 | |
| 920 | void PrintLocklessQueueMemory(LocklessQueueMemory *memory) { |
| 921 | const size_t queue_size = memory->queue_size(); |
| 922 | ::std::cout << "LocklessQueueMemory (" << memory << ") {" << ::std::endl; |
| 923 | ::std::cout << " aos_mutex queue_setup_lock = " |
| 924 | << PrintMutex(&memory->queue_setup_lock) << ::std::endl; |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 925 | ::std::cout << " bool initialized = " << memory->initialized << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 926 | ::std::cout << " config {" << ::std::endl; |
| 927 | ::std::cout << " size_t num_watchers = " << memory->config.num_watchers |
| 928 | << ::std::endl; |
| 929 | ::std::cout << " size_t num_senders = " << memory->config.num_senders |
| 930 | << ::std::endl; |
| 931 | ::std::cout << " size_t queue_size = " << memory->config.queue_size |
| 932 | << ::std::endl; |
| 933 | ::std::cout << " size_t message_data_size = " |
| 934 | << memory->config.message_data_size << ::std::endl; |
| 935 | |
| 936 | ::std::cout << " AtomicQueueIndex next_queue_index = " |
| 937 | << memory->next_queue_index.Load(queue_size).DebugString() |
| 938 | << ::std::endl; |
| 939 | |
Austin Schuh | 3328d13 | 2020-02-28 13:54:57 -0800 | [diff] [blame] | 940 | ::std::cout << " uid_t uid = " << memory->uid << ::std::endl; |
| 941 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 942 | ::std::cout << " }" << ::std::endl; |
| 943 | ::std::cout << " AtomicIndex queue[" << queue_size << "] {" << ::std::endl; |
| 944 | for (size_t i = 0; i < queue_size; ++i) { |
| 945 | ::std::cout << " [" << i << "] -> " |
| 946 | << memory->GetQueue(i)->Load().DebugString() << ::std::endl; |
| 947 | } |
| 948 | ::std::cout << " }" << ::std::endl; |
| 949 | ::std::cout << " Message messages[" << memory->num_messages() << "] {" |
| 950 | << ::std::endl; |
| 951 | for (size_t i = 0; i < memory->num_messages(); ++i) { |
| 952 | Message *m = memory->GetMessage(Index(i, i)); |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame^] | 953 | ::std::cout << " [" << i << "] -> Message 0x" << std::hex |
| 954 | << (reinterpret_cast<uintptr_t>( |
| 955 | memory->GetMessage(Index(i, i))) - |
| 956 | reinterpret_cast<uintptr_t>(memory)) |
| 957 | << std::dec << " {" << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 958 | ::std::cout << " Header {" << ::std::endl; |
| 959 | ::std::cout << " AtomicQueueIndex queue_index = " |
| 960 | << m->header.queue_index.Load(queue_size).DebugString() |
| 961 | << ::std::endl; |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame^] | 962 | ::std::cout << " monotonic_clock::time_point monotonic_sent_time = " |
| 963 | << m->header.monotonic_sent_time << " 0x" << std::hex |
| 964 | << m->header.monotonic_sent_time.time_since_epoch().count() |
| 965 | << std::dec << ::std::endl; |
| 966 | ::std::cout << " realtime_clock::time_point realtime_sent_time = " |
| 967 | << m->header.realtime_sent_time << " 0x" << std::hex |
| 968 | << m->header.realtime_sent_time.time_since_epoch().count() |
| 969 | << std::dec << ::std::endl; |
| 970 | ::std::cout |
| 971 | << " monotonic_clock::time_point monotonic_remote_time = " |
| 972 | << m->header.monotonic_remote_time << " 0x" << std::hex |
| 973 | << m->header.monotonic_remote_time.time_since_epoch().count() |
| 974 | << std::dec << ::std::endl; |
| 975 | ::std::cout << " realtime_clock::time_point realtime_remote_time = " |
| 976 | << m->header.realtime_remote_time << " 0x" << std::hex |
| 977 | << m->header.realtime_remote_time.time_since_epoch().count() |
| 978 | << std::dec << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 979 | ::std::cout << " size_t length = " << m->header.length |
| 980 | << ::std::endl; |
| 981 | ::std::cout << " }" << ::std::endl; |
| 982 | ::std::cout << " data: {"; |
| 983 | |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame^] | 984 | if (FLAGS_dump_lockless_queue_data) { |
| 985 | const char *const m_data = m->data(memory->message_data_size()); |
| 986 | for (size_t j = 0; j < m->header.length; ++j) { |
| 987 | char data = m_data[j]; |
| 988 | if (j != 0) { |
| 989 | ::std::cout << " "; |
| 990 | } |
| 991 | if (::std::isprint(data)) { |
| 992 | ::std::cout << ::std::setfill(' ') << ::std::setw(2) << ::std::hex |
| 993 | << data; |
| 994 | } else { |
| 995 | ::std::cout << "0x" << ::std::setfill('0') << ::std::setw(2) |
| 996 | << ::std::hex << (static_cast<unsigned>(data) & 0xff); |
| 997 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | ::std::cout << ::std::setfill(' ') << ::std::dec << "}" << ::std::endl; |
| 1001 | ::std::cout << " }," << ::std::endl; |
| 1002 | } |
| 1003 | ::std::cout << " }" << ::std::endl; |
| 1004 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1005 | ::std::cout << " Sender senders[" << memory->num_senders() << "] {" |
| 1006 | << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1007 | for (size_t i = 0; i < memory->num_senders(); ++i) { |
| 1008 | Sender *s = memory->GetSender(i); |
| 1009 | ::std::cout << " [" << i << "] -> Sender {" << ::std::endl; |
| 1010 | ::std::cout << " aos_mutex tid = " << PrintMutex(&s->tid) |
| 1011 | << ::std::endl; |
| 1012 | ::std::cout << " AtomicIndex scratch_index = " |
| 1013 | << s->scratch_index.Load().DebugString() << ::std::endl; |
| 1014 | ::std::cout << " AtomicIndex to_replace = " |
| 1015 | << s->to_replace.Load().DebugString() << ::std::endl; |
| 1016 | ::std::cout << " }" << ::std::endl; |
| 1017 | } |
| 1018 | ::std::cout << " }" << ::std::endl; |
| 1019 | |
| 1020 | ::std::cout << " Watcher watchers[" << memory->num_watchers() << "] {" |
| 1021 | << ::std::endl; |
| 1022 | for (size_t i = 0; i < memory->num_watchers(); ++i) { |
| 1023 | Watcher *w = memory->GetWatcher(i); |
| 1024 | ::std::cout << " [" << i << "] -> Watcher {" << ::std::endl; |
| 1025 | ::std::cout << " aos_mutex tid = " << PrintMutex(&w->tid) |
| 1026 | << ::std::endl; |
| 1027 | ::std::cout << " pid_t pid = " << w->pid << ::std::endl; |
| 1028 | ::std::cout << " int priority = " << w->priority << ::std::endl; |
| 1029 | ::std::cout << " }" << ::std::endl; |
| 1030 | } |
| 1031 | ::std::cout << " }" << ::std::endl; |
| 1032 | |
| 1033 | ::std::cout << "}" << ::std::endl; |
| 1034 | } |
| 1035 | |
| 1036 | } // namespace ipc_lib |
| 1037 | } // namespace aos |