Skip to content

Commit

Permalink
Merge pull request #10540 from nyanpasu64/fix-gcadapter-atomics
Browse files Browse the repository at this point in the history
Remove atomic usage and fix mutex locking in GCAdapter code
  • Loading branch information
JosJuice committed Apr 23, 2022
2 parents cb5e967 + b5a7ae5 commit e0afcb3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 4 additions & 3 deletions Source/Core/InputCommon/GCAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ static std::mutex s_mutex;
static u8 s_controller_payload[37];
static u8 s_controller_payload_swap[37];

static std::atomic<int> s_controller_payload_size = {0};
// Only access with s_mutex held!
static int s_controller_payload_size = {0};

static std::thread s_adapter_input_thread;
static std::thread s_adapter_output_thread;
Expand Down Expand Up @@ -112,7 +113,7 @@ static void Read()
{
std::lock_guard<std::mutex> lk(s_mutex);
std::swap(s_controller_payload_swap, s_controller_payload);
s_controller_payload_size.store(payload_size);
s_controller_payload_size = payload_size;
}

Common::YieldCPU();
Expand Down Expand Up @@ -478,7 +479,7 @@ GCPadStatus Input(int chan)
std::lock_guard<std::mutex> lk(s_mutex);
std::copy(std::begin(s_controller_payload), std::end(s_controller_payload),
std::begin(controller_payload_copy));
payload_size = s_controller_payload_size.load();
payload_size = s_controller_payload_size;
}

GCPadStatus pad = {};
Expand Down
8 changes: 4 additions & 4 deletions Source/Core/InputCommon/GCAdapter_Android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static u8 s_controller_rumble[4];
// Input handling
static std::mutex s_read_mutex;
static std::array<u8, 37> s_controller_payload;
static std::atomic<int> s_controller_payload_size{0};
static int s_controller_payload_size{0};

// Output handling
static std::mutex s_write_mutex;
Expand Down Expand Up @@ -164,7 +164,7 @@ static void Read()
{
std::lock_guard<std::mutex> lk(s_read_mutex);
std::copy(java_data, java_data + s_controller_payload.size(), s_controller_payload.begin());
s_controller_payload_size.store(read_size);
s_controller_payload_size = read_size;
}
env->ReleaseByteArrayElements(*java_controller_payload, java_data, 0);

Expand Down Expand Up @@ -284,7 +284,7 @@ GCPadStatus Input(int chan)
{
std::lock_guard<std::mutex> lk(s_read_mutex);
controller_payload_copy = s_controller_payload;
payload_size = s_controller_payload_size.load();
payload_size = s_controller_payload_size;
}

GCPadStatus pad = {};
Expand Down Expand Up @@ -406,7 +406,7 @@ void ResetRumble()
{
unsigned char rumble[5] = {0x11, 0, 0, 0, 0};
{
std::lock_guard<std::mutex> lk(s_read_mutex);
std::lock_guard<std::mutex> lk(s_write_mutex);
memcpy(s_controller_write_payload, rumble, 5);
s_controller_write_payload_size.store(5);
}
Expand Down

0 comments on commit e0afcb3

Please sign in to comment.