summaryrefslogtreecommitdiff
path: root/src/core/hid/emulated_devices.cpp
diff options
context:
space:
mode:
authorGravatar german772022-04-02 00:02:31 -0600
committerGravatar Narr the Reg2022-04-07 13:18:03 -0500
commitfa5277ecdb363f22f31ad4d0b981e12dde4eed0e (patch)
treef8614e39d71c50c363a618945232af2a3e017627 /src/core/hid/emulated_devices.cpp
parentMerge pull request #8164 from liamwhite/jit-stub (diff)
downloadyuzu-fa5277ecdb363f22f31ad4d0b981e12dde4eed0e.tar.gz
yuzu-fa5277ecdb363f22f31ad4d0b981e12dde4eed0e.tar.xz
yuzu-fa5277ecdb363f22f31ad4d0b981e12dde4eed0e.zip
core: hid: Reduce the amount of dataraces
Diffstat (limited to 'src/core/hid/emulated_devices.cpp')
-rw-r--r--src/core/hid/emulated_devices.cpp33
1 files changed, 26 insertions, 7 deletions
diff --git a/src/core/hid/emulated_devices.cpp b/src/core/hid/emulated_devices.cpp
index 708480f2d..f899f8ac0 100644
--- a/src/core/hid/emulated_devices.cpp
+++ b/src/core/hid/emulated_devices.cpp
@@ -169,7 +169,7 @@ void EmulatedDevices::SetKeyboardButton(const Common::Input::CallbackStatus& cal
169 if (index >= device_status.keyboard_values.size()) { 169 if (index >= device_status.keyboard_values.size()) {
170 return; 170 return;
171 } 171 }
172 std::lock_guard lock{mutex}; 172 std::unique_lock lock{mutex};
173 bool value_changed = false; 173 bool value_changed = false;
174 const auto new_status = TransformToButton(callback); 174 const auto new_status = TransformToButton(callback);
175 auto& current_status = device_status.keyboard_values[index]; 175 auto& current_status = device_status.keyboard_values[index];
@@ -201,6 +201,7 @@ void EmulatedDevices::SetKeyboardButton(const Common::Input::CallbackStatus& cal
201 } 201 }
202 202
203 if (is_configuring) { 203 if (is_configuring) {
204 lock.unlock();
204 TriggerOnChange(DeviceTriggerType::Keyboard); 205 TriggerOnChange(DeviceTriggerType::Keyboard);
205 return; 206 return;
206 } 207 }
@@ -208,6 +209,7 @@ void EmulatedDevices::SetKeyboardButton(const Common::Input::CallbackStatus& cal
208 // Index should be converted from NativeKeyboard to KeyboardKeyIndex 209 // Index should be converted from NativeKeyboard to KeyboardKeyIndex
209 UpdateKey(index, current_status.value); 210 UpdateKey(index, current_status.value);
210 211
212 lock.unlock();
211 TriggerOnChange(DeviceTriggerType::Keyboard); 213 TriggerOnChange(DeviceTriggerType::Keyboard);
212} 214}
213 215
@@ -227,7 +229,7 @@ void EmulatedDevices::SetKeyboardModifier(const Common::Input::CallbackStatus& c
227 if (index >= device_status.keyboard_moddifier_values.size()) { 229 if (index >= device_status.keyboard_moddifier_values.size()) {
228 return; 230 return;
229 } 231 }
230 std::lock_guard lock{mutex}; 232 std::unique_lock lock{mutex};
231 bool value_changed = false; 233 bool value_changed = false;
232 const auto new_status = TransformToButton(callback); 234 const auto new_status = TransformToButton(callback);
233 auto& current_status = device_status.keyboard_moddifier_values[index]; 235 auto& current_status = device_status.keyboard_moddifier_values[index];
@@ -259,6 +261,7 @@ void EmulatedDevices::SetKeyboardModifier(const Common::Input::CallbackStatus& c
259 } 261 }
260 262
261 if (is_configuring) { 263 if (is_configuring) {
264 lock.unlock();
262 TriggerOnChange(DeviceTriggerType::KeyboardModdifier); 265 TriggerOnChange(DeviceTriggerType::KeyboardModdifier);
263 return; 266 return;
264 } 267 }
@@ -289,6 +292,7 @@ void EmulatedDevices::SetKeyboardModifier(const Common::Input::CallbackStatus& c
289 break; 292 break;
290 } 293 }
291 294
295 lock.unlock();
292 TriggerOnChange(DeviceTriggerType::KeyboardModdifier); 296 TriggerOnChange(DeviceTriggerType::KeyboardModdifier);
293} 297}
294 298
@@ -297,7 +301,7 @@ void EmulatedDevices::SetMouseButton(const Common::Input::CallbackStatus& callba
297 if (index >= device_status.mouse_button_values.size()) { 301 if (index >= device_status.mouse_button_values.size()) {
298 return; 302 return;
299 } 303 }
300 std::lock_guard lock{mutex}; 304 std::unique_lock lock{mutex};
301 bool value_changed = false; 305 bool value_changed = false;
302 const auto new_status = TransformToButton(callback); 306 const auto new_status = TransformToButton(callback);
303 auto& current_status = device_status.mouse_button_values[index]; 307 auto& current_status = device_status.mouse_button_values[index];
@@ -329,6 +333,7 @@ void EmulatedDevices::SetMouseButton(const Common::Input::CallbackStatus& callba
329 } 333 }
330 334
331 if (is_configuring) { 335 if (is_configuring) {
336 lock.unlock();
332 TriggerOnChange(DeviceTriggerType::Mouse); 337 TriggerOnChange(DeviceTriggerType::Mouse);
333 return; 338 return;
334 } 339 }
@@ -351,6 +356,7 @@ void EmulatedDevices::SetMouseButton(const Common::Input::CallbackStatus& callba
351 break; 356 break;
352 } 357 }
353 358
359 lock.unlock();
354 TriggerOnChange(DeviceTriggerType::Mouse); 360 TriggerOnChange(DeviceTriggerType::Mouse);
355} 361}
356 362
@@ -359,13 +365,14 @@ void EmulatedDevices::SetMouseAnalog(const Common::Input::CallbackStatus& callba
359 if (index >= device_status.mouse_analog_values.size()) { 365 if (index >= device_status.mouse_analog_values.size()) {
360 return; 366 return;
361 } 367 }
362 std::lock_guard lock{mutex}; 368 std::unique_lock lock{mutex};
363 const auto analog_value = TransformToAnalog(callback); 369 const auto analog_value = TransformToAnalog(callback);
364 370
365 device_status.mouse_analog_values[index] = analog_value; 371 device_status.mouse_analog_values[index] = analog_value;
366 372
367 if (is_configuring) { 373 if (is_configuring) {
368 device_status.mouse_position_state = {}; 374 device_status.mouse_position_state = {};
375 lock.unlock();
369 TriggerOnChange(DeviceTriggerType::Mouse); 376 TriggerOnChange(DeviceTriggerType::Mouse);
370 return; 377 return;
371 } 378 }
@@ -379,17 +386,19 @@ void EmulatedDevices::SetMouseAnalog(const Common::Input::CallbackStatus& callba
379 break; 386 break;
380 } 387 }
381 388
389 lock.unlock();
382 TriggerOnChange(DeviceTriggerType::Mouse); 390 TriggerOnChange(DeviceTriggerType::Mouse);
383} 391}
384 392
385void EmulatedDevices::SetMouseStick(const Common::Input::CallbackStatus& callback) { 393void EmulatedDevices::SetMouseStick(const Common::Input::CallbackStatus& callback) {
386 std::lock_guard lock{mutex}; 394 std::unique_lock lock{mutex};
387 const auto touch_value = TransformToTouch(callback); 395 const auto touch_value = TransformToTouch(callback);
388 396
389 device_status.mouse_stick_value = touch_value; 397 device_status.mouse_stick_value = touch_value;
390 398
391 if (is_configuring) { 399 if (is_configuring) {
392 device_status.mouse_position_state = {}; 400 device_status.mouse_position_state = {};
401 lock.unlock();
393 TriggerOnChange(DeviceTriggerType::Mouse); 402 TriggerOnChange(DeviceTriggerType::Mouse);
394 return; 403 return;
395 } 404 }
@@ -397,42 +406,52 @@ void EmulatedDevices::SetMouseStick(const Common::Input::CallbackStatus& callbac
397 device_status.mouse_position_state.x = touch_value.x.value; 406 device_status.mouse_position_state.x = touch_value.x.value;
398 device_status.mouse_position_state.y = touch_value.y.value; 407 device_status.mouse_position_state.y = touch_value.y.value;
399 408
409 lock.unlock();
400 TriggerOnChange(DeviceTriggerType::Mouse); 410 TriggerOnChange(DeviceTriggerType::Mouse);
401} 411}
402 412
403KeyboardValues EmulatedDevices::GetKeyboardValues() const { 413KeyboardValues EmulatedDevices::GetKeyboardValues() const {
414 std::lock_guard lock{mutex};
404 return device_status.keyboard_values; 415 return device_status.keyboard_values;
405} 416}
406 417
407KeyboardModifierValues EmulatedDevices::GetKeyboardModdifierValues() const { 418KeyboardModifierValues EmulatedDevices::GetKeyboardModdifierValues() const {
419 std::lock_guard lock{mutex};
408 return device_status.keyboard_moddifier_values; 420 return device_status.keyboard_moddifier_values;
409} 421}
410 422
411MouseButtonValues EmulatedDevices::GetMouseButtonsValues() const { 423MouseButtonValues EmulatedDevices::GetMouseButtonsValues() const {
424 std::lock_guard lock{mutex};
412 return device_status.mouse_button_values; 425 return device_status.mouse_button_values;
413} 426}
414 427
415KeyboardKey EmulatedDevices::GetKeyboard() const { 428KeyboardKey EmulatedDevices::GetKeyboard() const {
429 std::lock_guard lock{mutex};
416 return device_status.keyboard_state; 430 return device_status.keyboard_state;
417} 431}
418 432
419KeyboardModifier EmulatedDevices::GetKeyboardModifier() const { 433KeyboardModifier EmulatedDevices::GetKeyboardModifier() const {
434 std::lock_guard lock{mutex};
420 return device_status.keyboard_moddifier_state; 435 return device_status.keyboard_moddifier_state;
421} 436}
422 437
423MouseButton EmulatedDevices::GetMouseButtons() const { 438MouseButton EmulatedDevices::GetMouseButtons() const {
439 std::lock_guard lock{mutex};
424 return device_status.mouse_button_state; 440 return device_status.mouse_button_state;
425} 441}
426 442
427MousePosition EmulatedDevices::GetMousePosition() const { 443MousePosition EmulatedDevices::GetMousePosition() const {
444 std::lock_guard lock{mutex};
428 return device_status.mouse_position_state; 445 return device_status.mouse_position_state;
429} 446}
430 447
431AnalogStickState EmulatedDevices::GetMouseWheel() const { 448AnalogStickState EmulatedDevices::GetMouseWheel() const {
449 std::lock_guard lock{mutex};
432 return device_status.mouse_wheel_state; 450 return device_status.mouse_wheel_state;
433} 451}
434 452
435void EmulatedDevices::TriggerOnChange(DeviceTriggerType type) { 453void EmulatedDevices::TriggerOnChange(DeviceTriggerType type) {
454 std::lock_guard lock{callback_mutex};
436 for (const auto& poller_pair : callback_list) { 455 for (const auto& poller_pair : callback_list) {
437 const InterfaceUpdateCallback& poller = poller_pair.second; 456 const InterfaceUpdateCallback& poller = poller_pair.second;
438 if (poller.on_change) { 457 if (poller.on_change) {
@@ -442,13 +461,13 @@ void EmulatedDevices::TriggerOnChange(DeviceTriggerType type) {
442} 461}
443 462
444int EmulatedDevices::SetCallback(InterfaceUpdateCallback update_callback) { 463int EmulatedDevices::SetCallback(InterfaceUpdateCallback update_callback) {
445 std::lock_guard lock{mutex}; 464 std::lock_guard lock{callback_mutex};
446 callback_list.insert_or_assign(last_callback_key, std::move(update_callback)); 465 callback_list.insert_or_assign(last_callback_key, std::move(update_callback));
447 return last_callback_key++; 466 return last_callback_key++;
448} 467}
449 468
450void EmulatedDevices::DeleteCallback(int key) { 469void EmulatedDevices::DeleteCallback(int key) {
451 std::lock_guard lock{mutex}; 470 std::lock_guard lock{callback_mutex};
452 const auto& iterator = callback_list.find(key); 471 const auto& iterator = callback_list.find(key);
453 if (iterator == callback_list.end()) { 472 if (iterator == callback_list.end()) {
454 LOG_ERROR(Input, "Tried to delete non-existent callback {}", key); 473 LOG_ERROR(Input, "Tried to delete non-existent callback {}", key);