input_common: Fix data race on GC implementation

This commit is contained in:
Rodrigo Locatti
2021-06-22 04:30:07 -03:00
committed by german77
parent bd0e1d3a25
commit d4cab35533
2 changed files with 120 additions and 115 deletions

View File

@ -3,11 +3,14 @@
// Refer to the license.txt file included.
#pragma once
#include <algorithm>
#include <functional>
#include <mutex>
#include <stop_token>
#include <thread>
#include <unordered_map>
#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<s16, 6> axis_values{};
std::array<u8, 6> 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<LibUSBDeviceHandle> usb_adapter_handle;
std::array<GCController, 4> pads;
Common::SPSCQueue<GCPadStatus> 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<LibUSBContext> libusb_ctx;
u8 input_endpoint{0};
u8 output_endpoint{0};