Skip to content

Commit

Permalink
InputCommon/ControllerInterface: Make devices mutex recursive so Remo…
Browse files Browse the repository at this point in the history
…veDevice can be used within UpdateInput.
  • Loading branch information
jordan-woyak committed Feb 17, 2020
1 parent 5af2081 commit 4176cc7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void ControllerInterface::RefreshDevices()
return;

{
std::lock_guard<std::mutex> lk(m_devices_mutex);
std::lock_guard lk(m_devices_mutex);
m_devices.clear();
}

Expand Down Expand Up @@ -131,6 +131,7 @@ void ControllerInterface::RefreshDevices()
#ifdef CIFACE_USE_DUALSHOCKUDPCLIENT
ciface::DualShockUDPClient::PopulateDevices();
#endif
ciface::Wiimote::PopulateDevices();

m_is_populating_devices = false;
InvokeDevicesChangedCallbacks();
Expand All @@ -146,7 +147,7 @@ void ControllerInterface::Shutdown()
m_is_init = false;

{
std::lock_guard<std::mutex> lk(m_devices_mutex);
std::lock_guard lk(m_devices_mutex);

for (const auto& d : m_devices)
{
Expand Down Expand Up @@ -193,7 +194,7 @@ void ControllerInterface::AddDevice(std::shared_ptr<ciface::Core::Device> device
return;

{
std::lock_guard<std::mutex> lk(m_devices_mutex);
std::lock_guard lk(m_devices_mutex);

const auto is_id_in_use = [&device, this](int id) {
return std::any_of(m_devices.begin(), m_devices.end(), [&device, &id](const auto& d) {
Expand Down Expand Up @@ -229,7 +230,7 @@ void ControllerInterface::AddDevice(std::shared_ptr<ciface::Core::Device> device
void ControllerInterface::RemoveDevice(std::function<bool(const ciface::Core::Device*)> callback)
{
{
std::lock_guard<std::mutex> lk(m_devices_mutex);
std::lock_guard lk(m_devices_mutex);
auto it = std::remove_if(m_devices.begin(), m_devices.end(), [&callback](const auto& dev) {
if (callback(dev.get()))
{
Expand All @@ -251,7 +252,7 @@ void ControllerInterface::UpdateInput()
// Don't block the UI or CPU thread (to avoid a short but noticeable frame drop)
if (m_devices_mutex.try_lock())
{
std::lock_guard<std::mutex> lk(m_devices_mutex, std::adopt_lock);
std::lock_guard lk(m_devices_mutex, std::adopt_lock);
for (const auto& d : m_devices)
d->UpdateInput();
}
Expand Down
8 changes: 4 additions & 4 deletions Source/Core/InputCommon/ControllerInterface/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ bool DeviceQualifier::operator!=(const DeviceQualifier& devq) const

std::shared_ptr<Device> DeviceContainer::FindDevice(const DeviceQualifier& devq) const
{
std::lock_guard<std::mutex> lk(m_devices_mutex);
std::lock_guard lk(m_devices_mutex);
for (const auto& d : m_devices)
{
if (devq == d.get())
Expand All @@ -192,7 +192,7 @@ std::shared_ptr<Device> DeviceContainer::FindDevice(const DeviceQualifier& devq)

std::vector<std::string> DeviceContainer::GetAllDeviceStrings() const
{
std::lock_guard<std::mutex> lk(m_devices_mutex);
std::lock_guard lk(m_devices_mutex);

std::vector<std::string> device_strings;
DeviceQualifier device_qualifier;
Expand All @@ -208,7 +208,7 @@ std::vector<std::string> DeviceContainer::GetAllDeviceStrings() const

std::string DeviceContainer::GetDefaultDeviceString() const
{
std::lock_guard<std::mutex> lk(m_devices_mutex);
std::lock_guard lk(m_devices_mutex);
if (m_devices.empty())
return "";

Expand All @@ -226,7 +226,7 @@ Device::Input* DeviceContainer::FindInput(std::string_view name, const Device* d
return inp;
}

std::lock_guard<std::mutex> lk(m_devices_mutex);
std::lock_guard lk(m_devices_mutex);
for (const auto& d : m_devices)
{
Device::Input* const i = d->FindInput(name);
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/InputCommon/ControllerInterface/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class DeviceContainer
DetectInput(u32 wait_ms, const std::vector<std::string>& device_strings) const;

protected:
mutable std::mutex m_devices_mutex;
mutable std::recursive_mutex m_devices_mutex;
std::vector<std::shared_ptr<Device>> m_devices;
};
} // namespace Core
Expand Down

0 comments on commit 4176cc7

Please sign in to comment.