input_common: Replace lock_guard with scoped_lock

This commit is contained in:
Merry 2022-04-07 19:32:09 +01:00
parent 6a071c42d2
commit 159ae5e47c
2 changed files with 29 additions and 29 deletions

View file

@ -62,7 +62,7 @@ public:
bool UpdateMotion(SDL_ControllerSensorEvent event) { bool UpdateMotion(SDL_ControllerSensorEvent event) {
constexpr float gravity_constant = 9.80665f; constexpr float gravity_constant = 9.80665f;
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
const u64 time_difference = event.timestamp - last_motion_update; const u64 time_difference = event.timestamp - last_motion_update;
last_motion_update = event.timestamp; last_motion_update = event.timestamp;
switch (event.sensor) { switch (event.sensor) {
@ -241,7 +241,7 @@ private:
}; };
std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickByGUID(const std::string& guid, int port) { std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickByGUID(const std::string& guid, int port) {
std::lock_guard lock{joystick_map_mutex}; std::scoped_lock lock{joystick_map_mutex};
const auto it = joystick_map.find(guid); const auto it = joystick_map.find(guid);
if (it != joystick_map.end()) { if (it != joystick_map.end()) {
@ -263,7 +263,7 @@ std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickBySDLID(SDL_JoystickID sdl
auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id); auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id);
const std::string guid = GetGUID(sdl_joystick); const std::string guid = GetGUID(sdl_joystick);
std::lock_guard lock{joystick_map_mutex}; std::scoped_lock lock{joystick_map_mutex};
const auto map_it = joystick_map.find(guid); const auto map_it = joystick_map.find(guid);
if (map_it == joystick_map.end()) { if (map_it == joystick_map.end()) {
@ -297,7 +297,7 @@ void SDLDriver::InitJoystick(int joystick_index) {
const std::string guid = GetGUID(sdl_joystick); const std::string guid = GetGUID(sdl_joystick);
std::lock_guard lock{joystick_map_mutex}; std::scoped_lock lock{joystick_map_mutex};
if (joystick_map.find(guid) == joystick_map.end()) { if (joystick_map.find(guid) == joystick_map.end()) {
auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_gamecontroller); auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_gamecontroller);
PreSetController(joystick->GetPadIdentifier()); PreSetController(joystick->GetPadIdentifier());
@ -326,7 +326,7 @@ void SDLDriver::InitJoystick(int joystick_index) {
void SDLDriver::CloseJoystick(SDL_Joystick* sdl_joystick) { void SDLDriver::CloseJoystick(SDL_Joystick* sdl_joystick) {
const std::string guid = GetGUID(sdl_joystick); const std::string guid = GetGUID(sdl_joystick);
std::lock_guard lock{joystick_map_mutex}; std::scoped_lock lock{joystick_map_mutex};
// This call to guid is safe since the joystick is guaranteed to be in the map // This call to guid is safe since the joystick is guaranteed to be in the map
const auto& joystick_guid_list = joystick_map[guid]; const auto& joystick_guid_list = joystick_map[guid];
const auto joystick_it = std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), const auto joystick_it = std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(),
@ -392,7 +392,7 @@ void SDLDriver::HandleGameControllerEvent(const SDL_Event& event) {
} }
void SDLDriver::CloseJoysticks() { void SDLDriver::CloseJoysticks() {
std::lock_guard lock{joystick_map_mutex}; std::scoped_lock lock{joystick_map_mutex};
joystick_map.clear(); joystick_map.clear();
} }

View file

@ -8,37 +8,37 @@
namespace InputCommon { namespace InputCommon {
void InputEngine::PreSetController(const PadIdentifier& identifier) { void InputEngine::PreSetController(const PadIdentifier& identifier) {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
controller_list.try_emplace(identifier); controller_list.try_emplace(identifier);
} }
void InputEngine::PreSetButton(const PadIdentifier& identifier, int button) { void InputEngine::PreSetButton(const PadIdentifier& identifier, int button) {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
ControllerData& controller = controller_list.at(identifier); ControllerData& controller = controller_list.at(identifier);
controller.buttons.try_emplace(button, false); controller.buttons.try_emplace(button, false);
} }
void InputEngine::PreSetHatButton(const PadIdentifier& identifier, int button) { void InputEngine::PreSetHatButton(const PadIdentifier& identifier, int button) {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
ControllerData& controller = controller_list.at(identifier); ControllerData& controller = controller_list.at(identifier);
controller.hat_buttons.try_emplace(button, u8{0}); controller.hat_buttons.try_emplace(button, u8{0});
} }
void InputEngine::PreSetAxis(const PadIdentifier& identifier, int axis) { void InputEngine::PreSetAxis(const PadIdentifier& identifier, int axis) {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
ControllerData& controller = controller_list.at(identifier); ControllerData& controller = controller_list.at(identifier);
controller.axes.try_emplace(axis, 0.0f); controller.axes.try_emplace(axis, 0.0f);
} }
void InputEngine::PreSetMotion(const PadIdentifier& identifier, int motion) { void InputEngine::PreSetMotion(const PadIdentifier& identifier, int motion) {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
ControllerData& controller = controller_list.at(identifier); ControllerData& controller = controller_list.at(identifier);
controller.motions.try_emplace(motion); controller.motions.try_emplace(motion);
} }
void InputEngine::SetButton(const PadIdentifier& identifier, int button, bool value) { void InputEngine::SetButton(const PadIdentifier& identifier, int button, bool value) {
{ {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
ControllerData& controller = controller_list.at(identifier); ControllerData& controller = controller_list.at(identifier);
if (!configuring) { if (!configuring) {
controller.buttons.insert_or_assign(button, value); controller.buttons.insert_or_assign(button, value);
@ -49,7 +49,7 @@ void InputEngine::SetButton(const PadIdentifier& identifier, int button, bool va
void InputEngine::SetHatButton(const PadIdentifier& identifier, int button, u8 value) { void InputEngine::SetHatButton(const PadIdentifier& identifier, int button, u8 value) {
{ {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
ControllerData& controller = controller_list.at(identifier); ControllerData& controller = controller_list.at(identifier);
if (!configuring) { if (!configuring) {
controller.hat_buttons.insert_or_assign(button, value); controller.hat_buttons.insert_or_assign(button, value);
@ -60,7 +60,7 @@ void InputEngine::SetHatButton(const PadIdentifier& identifier, int button, u8 v
void InputEngine::SetAxis(const PadIdentifier& identifier, int axis, f32 value) { void InputEngine::SetAxis(const PadIdentifier& identifier, int axis, f32 value) {
{ {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
ControllerData& controller = controller_list.at(identifier); ControllerData& controller = controller_list.at(identifier);
if (!configuring) { if (!configuring) {
controller.axes.insert_or_assign(axis, value); controller.axes.insert_or_assign(axis, value);
@ -71,7 +71,7 @@ void InputEngine::SetAxis(const PadIdentifier& identifier, int axis, f32 value)
void InputEngine::SetBattery(const PadIdentifier& identifier, Common::Input::BatteryLevel value) { void InputEngine::SetBattery(const PadIdentifier& identifier, Common::Input::BatteryLevel value) {
{ {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
ControllerData& controller = controller_list.at(identifier); ControllerData& controller = controller_list.at(identifier);
if (!configuring) { if (!configuring) {
controller.battery = value; controller.battery = value;
@ -82,7 +82,7 @@ void InputEngine::SetBattery(const PadIdentifier& identifier, Common::Input::Bat
void InputEngine::SetMotion(const PadIdentifier& identifier, int motion, const BasicMotion& value) { void InputEngine::SetMotion(const PadIdentifier& identifier, int motion, const BasicMotion& value) {
{ {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
ControllerData& controller = controller_list.at(identifier); ControllerData& controller = controller_list.at(identifier);
if (!configuring) { if (!configuring) {
controller.motions.insert_or_assign(motion, value); controller.motions.insert_or_assign(motion, value);
@ -92,7 +92,7 @@ void InputEngine::SetMotion(const PadIdentifier& identifier, int motion, const B
} }
bool InputEngine::GetButton(const PadIdentifier& identifier, int button) const { bool InputEngine::GetButton(const PadIdentifier& identifier, int button) const {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
const auto controller_iter = controller_list.find(identifier); const auto controller_iter = controller_list.find(identifier);
if (controller_iter == controller_list.cend()) { if (controller_iter == controller_list.cend()) {
LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
@ -109,7 +109,7 @@ bool InputEngine::GetButton(const PadIdentifier& identifier, int button) const {
} }
bool InputEngine::GetHatButton(const PadIdentifier& identifier, int button, u8 direction) const { bool InputEngine::GetHatButton(const PadIdentifier& identifier, int button, u8 direction) const {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
const auto controller_iter = controller_list.find(identifier); const auto controller_iter = controller_list.find(identifier);
if (controller_iter == controller_list.cend()) { if (controller_iter == controller_list.cend()) {
LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
@ -126,7 +126,7 @@ bool InputEngine::GetHatButton(const PadIdentifier& identifier, int button, u8 d
} }
f32 InputEngine::GetAxis(const PadIdentifier& identifier, int axis) const { f32 InputEngine::GetAxis(const PadIdentifier& identifier, int axis) const {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
const auto controller_iter = controller_list.find(identifier); const auto controller_iter = controller_list.find(identifier);
if (controller_iter == controller_list.cend()) { if (controller_iter == controller_list.cend()) {
LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
@ -143,7 +143,7 @@ f32 InputEngine::GetAxis(const PadIdentifier& identifier, int axis) const {
} }
Common::Input::BatteryLevel InputEngine::GetBattery(const PadIdentifier& identifier) const { Common::Input::BatteryLevel InputEngine::GetBattery(const PadIdentifier& identifier) const {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
const auto controller_iter = controller_list.find(identifier); const auto controller_iter = controller_list.find(identifier);
if (controller_iter == controller_list.cend()) { if (controller_iter == controller_list.cend()) {
LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
@ -155,7 +155,7 @@ Common::Input::BatteryLevel InputEngine::GetBattery(const PadIdentifier& identif
} }
BasicMotion InputEngine::GetMotion(const PadIdentifier& identifier, int motion) const { BasicMotion InputEngine::GetMotion(const PadIdentifier& identifier, int motion) const {
std::lock_guard lock{mutex}; std::scoped_lock lock{mutex};
const auto controller_iter = controller_list.find(identifier); const auto controller_iter = controller_list.find(identifier);
if (controller_iter == controller_list.cend()) { if (controller_iter == controller_list.cend()) {
LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
@ -186,7 +186,7 @@ void InputEngine::ResetAnalogState() {
} }
void InputEngine::TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value) { void InputEngine::TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value) {
std::lock_guard lock{mutex_callback}; std::scoped_lock lock{mutex_callback};
for (const auto& poller_pair : callback_list) { for (const auto& poller_pair : callback_list) {
const InputIdentifier& poller = poller_pair.second; const InputIdentifier& poller = poller_pair.second;
if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Button, button)) { if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Button, button)) {
@ -214,7 +214,7 @@ void InputEngine::TriggerOnButtonChange(const PadIdentifier& identifier, int but
} }
void InputEngine::TriggerOnHatButtonChange(const PadIdentifier& identifier, int button, u8 value) { void InputEngine::TriggerOnHatButtonChange(const PadIdentifier& identifier, int button, u8 value) {
std::lock_guard lock{mutex_callback}; std::scoped_lock lock{mutex_callback};
for (const auto& poller_pair : callback_list) { for (const auto& poller_pair : callback_list) {
const InputIdentifier& poller = poller_pair.second; const InputIdentifier& poller = poller_pair.second;
if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::HatButton, button)) { if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::HatButton, button)) {
@ -243,7 +243,7 @@ void InputEngine::TriggerOnHatButtonChange(const PadIdentifier& identifier, int
} }
void InputEngine::TriggerOnAxisChange(const PadIdentifier& identifier, int axis, f32 value) { void InputEngine::TriggerOnAxisChange(const PadIdentifier& identifier, int axis, f32 value) {
std::lock_guard lock{mutex_callback}; std::scoped_lock lock{mutex_callback};
for (const auto& poller_pair : callback_list) { for (const auto& poller_pair : callback_list) {
const InputIdentifier& poller = poller_pair.second; const InputIdentifier& poller = poller_pair.second;
if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Analog, axis)) { if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Analog, axis)) {
@ -270,7 +270,7 @@ void InputEngine::TriggerOnAxisChange(const PadIdentifier& identifier, int axis,
void InputEngine::TriggerOnBatteryChange(const PadIdentifier& identifier, void InputEngine::TriggerOnBatteryChange(const PadIdentifier& identifier,
[[maybe_unused]] Common::Input::BatteryLevel value) { [[maybe_unused]] Common::Input::BatteryLevel value) {
std::lock_guard lock{mutex_callback}; std::scoped_lock lock{mutex_callback};
for (const auto& poller_pair : callback_list) { for (const auto& poller_pair : callback_list) {
const InputIdentifier& poller = poller_pair.second; const InputIdentifier& poller = poller_pair.second;
if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Battery, 0)) { if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Battery, 0)) {
@ -284,7 +284,7 @@ void InputEngine::TriggerOnBatteryChange(const PadIdentifier& identifier,
void InputEngine::TriggerOnMotionChange(const PadIdentifier& identifier, int motion, void InputEngine::TriggerOnMotionChange(const PadIdentifier& identifier, int motion,
const BasicMotion& value) { const BasicMotion& value) {
std::lock_guard lock{mutex_callback}; std::scoped_lock lock{mutex_callback};
for (const auto& poller_pair : callback_list) { for (const auto& poller_pair : callback_list) {
const InputIdentifier& poller = poller_pair.second; const InputIdentifier& poller = poller_pair.second;
if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Motion, motion)) { if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Motion, motion)) {
@ -346,18 +346,18 @@ const std::string& InputEngine::GetEngineName() const {
} }
int InputEngine::SetCallback(InputIdentifier input_identifier) { int InputEngine::SetCallback(InputIdentifier input_identifier) {
std::lock_guard lock{mutex_callback}; std::scoped_lock lock{mutex_callback};
callback_list.insert_or_assign(last_callback_key, std::move(input_identifier)); callback_list.insert_or_assign(last_callback_key, std::move(input_identifier));
return last_callback_key++; return last_callback_key++;
} }
void InputEngine::SetMappingCallback(MappingCallback callback) { void InputEngine::SetMappingCallback(MappingCallback callback) {
std::lock_guard lock{mutex_callback}; std::scoped_lock lock{mutex_callback};
mapping_callback = std::move(callback); mapping_callback = std::move(callback);
} }
void InputEngine::DeleteCallback(int key) { void InputEngine::DeleteCallback(int key) {
std::lock_guard lock{mutex_callback}; std::scoped_lock lock{mutex_callback};
const auto& iterator = callback_list.find(key); const auto& iterator = callback_list.find(key);
if (iterator == callback_list.end()) { if (iterator == callback_list.end()) {
LOG_ERROR(Input, "Tried to delete non-existent callback {}", key); LOG_ERROR(Input, "Tried to delete non-existent callback {}", key);