From d4cab35533fe5a5f8bf609d9209655ecff5ed749 Mon Sep 17 00:00:00 2001 From: Rodrigo Locatti Date: Tue, 22 Jun 2021 04:30:07 -0300 Subject: input_common: Fix data race on GC implementation --- src/input_common/gcadapter/gc_adapter.h | 46 ++++++++++++++------------------- 1 file changed, 19 insertions(+), 27 deletions(-) (limited to 'src/input_common/gcadapter/gc_adapter.h') diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index e5de5e94f..28dbcbe05 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -3,11 +3,14 @@ // Refer to the license.txt file included. #pragma once + #include #include #include +#include #include #include + #include "common/common_types.h" #include "common/threadsafe_queue.h" #include "input_common/main.h" @@ -18,6 +21,9 @@ struct libusb_device_handle; namespace GCAdapter { +class LibUSBContext; +class LibUSBDeviceHandle; + enum class PadButton { Undefined = 0x0000, ButtonLeft = 0x0001, @@ -63,11 +69,11 @@ struct GCPadStatus { }; struct GCController { - ControllerTypes type{}; - bool enable_vibration{}; - u8 rumble_amplitude{}; - u16 buttons{}; - PadButton last_button{}; + ControllerTypes type = ControllerTypes::None; + bool enable_vibration = false; + u8 rumble_amplitude = 0; + u16 buttons = 0; + PadButton last_button = PadButton::Undefined; std::array axis_values{}; std::array axis_origin{}; u8 reset_origin_counter{}; @@ -109,9 +115,9 @@ private: void UpdateStateAxes(std::size_t port, const AdapterPayload& adapter_payload); void UpdateVibrations(); - void AdapterInputThread(); + void AdapterInputThread(std::stop_token stop_token); - void AdapterScanThread(); + void AdapterScanThread(std::stop_token stop_token); bool IsPayloadCorrect(const AdapterPayload& adapter_payload, s32 payload_size); @@ -119,13 +125,7 @@ private: void SendVibrations(); /// For use in initialization, querying devices to find the adapter - void Setup(); - - /// Resets status of all GC controller devices to a disconnected state - void ResetDevices(); - - /// Resets status of device connected to a disconnected state - void ResetDevice(std::size_t port); + bool Setup(); /// Returns true if we successfully gain access to GC Adapter bool CheckDeviceAccess(); @@ -137,23 +137,15 @@ private: /// For shutting down, clear all data, join all threads, release usb void Reset(); - // Join all threads - void JoinThreads(); - - // Release usb handles - void ClearLibusbHandle(); - - libusb_device_handle* usb_adapter_handle = nullptr; + std::unique_ptr usb_adapter_handle; std::array pads; Common::SPSCQueue pad_queue; - std::thread adapter_input_thread; - std::thread adapter_scan_thread; - bool adapter_input_thread_running; - bool adapter_scan_thread_running; - bool restart_scan_thread; + std::jthread adapter_input_thread; + std::jthread adapter_scan_thread; + bool restart_scan_thread{}; - libusb_context* libusb_ctx; + std::unique_ptr libusb_ctx; u8 input_endpoint{0}; u8 output_endpoint{0}; -- cgit v1.2.3