From eb5ae94c242af3da03513029d7a957fd34cfd640 Mon Sep 17 00:00:00 2001 From: Bevan Weiss Date: Tue, 4 Aug 2020 20:42:27 +1000 Subject: [PATCH] sys_usbd tidy ups Tidy up fake transfer iterator handling. erase invalidates all iterators including the current iterator (i.e. 'it'), given precedence ordering this was UB prior to C++17. Splitting out to use return iterator from erase seems cleaner. Also added some additional info to usb debug message to potentially help with #8666, and used the atomic (dev_counter) less often --- rpcs3/Emu/Cell/lv2/sys_usbd.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/rpcs3/Emu/Cell/lv2/sys_usbd.cpp b/rpcs3/Emu/Cell/lv2/sys_usbd.cpp index 7ff5639181..e88598f4f4 100644 --- a/rpcs3/Emu/Cell/lv2/sys_usbd.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_usbd.cpp @@ -253,23 +253,26 @@ void usb_handler_thread::operator()() u64 timestamp = get_system_time() - Emu.GetPauseTime(); - for (auto it = fake_transfers.begin(); it != fake_transfers.end(); it++) + for (auto it = fake_transfers.begin(); it != fake_transfers.end();) { auto transfer = *it; ASSERT(transfer->busy && transfer->fake); if (transfer->expected_time > timestamp) + { + ++it; continue; + } + transfer->result = transfer->expected_result; transfer->count = transfer->expected_count; transfer->fake = false; transfer->busy = false; - fake_transfers.erase(it--); - send_message(SYS_USBD_TRANSFER_COMPLETE, transfer->transfer_id); + it = fake_transfers.erase(it); // if we've processed this, then we erase this entry (replacing the iterator with the new reference) } } @@ -387,14 +390,13 @@ void usb_handler_thread::check_devices_vs_ldds() continue; } - sys_usbd.success("Ldd device matchup for <%s>", ldd.name); - dev->read_descriptors(); + dev->assigned_number = dev_counter++; // assign current dev_counter, and atomically increment - dev->assigned_number = dev_counter; - handled_devices.emplace(dev_counter, std::pair(UsbInternalDevice{0x00, dev_counter, 0x02, 0x40}, dev)); - send_message(SYS_USBD_ATTACH, dev_counter); - dev_counter++; + sys_usbd.success("Ldd device matchup for <%s>, assigned as handled_device=0x%x", ldd.name, dev->assigned_number); + + handled_devices.emplace(dev->assigned_number, std::pair(UsbInternalDevice{0x00, narrow(dev->assigned_number), 0x02, 0x40}, dev)); + send_message(SYS_USBD_ATTACH, dev->assigned_number); } } }