dolphin/Source/Core/AudioCommon
Dentomologist 3b97a7bded CubebStream: Use WorkQueueThread::PushBlocking instead of sync_event
Push and wait on WorkQueueThread items using PushBlocking. Previously we
created a Common::Event sync_event on the caller's stack, called Wait on
it, then had the WorkQueueThread call Set on the sync_event once the
thread was done.

In addition to being simpler the new way avoids a use-after-free that
could happen in convoluted and unlikely yet possible thread scheduling
sequences.

One such case can be triggered as follows:

* Set your audio backend to Cubeb
* In CubebStream::SetVolume set a breakpoint at the call to Wait and at
  the call to cubeb_stream_set_volume.
* Start a game.
* Continue until the Cubeb Worker thread hits the
  cubeb_stream_set_volume breakpoint and Emuthread hits the Wait
  breakpoint, freezing each thread when it hits its breakpoint.
* Unfreeze Cubeb Worker.
* In Event::Set set a breakpoint at the end of the scope containing the
  lock_guard such that the guard has been constructed but not destructed
  when the breakpoint is hit.
* Continue until that breakpoint is hit by Cubeb Worker. If other
  threads hit it first keep going.
* Freeze Cubeb Worker.
* For convenience remove the breakpoint in Event::Set so other threads
  don't trigger it.
* In CubebStream::SetRunning set a breakpoint at the call to Wait.
* Unfreeze Emuthread and continue until the breakpoint is hit.
* In Cubeb Worker go to Event::Set and examine the values of m_mutex's
  member variables. In Visual Studio Debug these are locking_thread_id
  == 0xcccccc01 and ownership_levels == 0xcccccccc. This is the result
  of Visual Studio overwriting the memory used on the stack by
  sync_event in CubebStream::SetVolume with cc bytes to represent
  uninitialized memory on the stack (since that function already
  returned), and then allocating enough memory on the stack when calling
  AudioCommon::SetSoundStreamRunning and then CubebStream::SetRunning
  that it overwrote one byte of the memory formerly occupied by
  locking_thread_id.
* If you unfreeze Cubeb Worker at this point it will trigger the lock
  guard's destructor which will then try to unlock m_mutex. Since
  m_mutex is no longer in scope this is a use-after-free, and in VS
  debug triggers a debug assert due to locking_thread_id not matching
  the current thread id.
2025-11-07 13:19:18 -08:00
..
AlsaSoundStream.cpp AudioCommon: unlock mutex explicitly to avoid -Wunused-result warning 2024-06-20 02:25:15 +01:00
AlsaSoundStream.h AudioCommon: Re-add missing includes 2025-06-08 11:35:02 +02:00
AudioCommon.cpp Merge pull request #13727 from JoshuaVandaele/fmt-11.2.0-localtime-deprec 2025-06-08 04:04:37 +01:00
AudioCommon.h AudioCommon: Remove unused includes 2025-05-25 10:30:41 +02:00
CMakeLists.txt AudioCommon: Added Granular Synthesis 2025-03-14 01:22:35 -05:00
CubebStream.cpp CubebStream: Use WorkQueueThread::PushBlocking instead of sync_event 2025-11-07 13:19:18 -08:00
CubebStream.h AudioCommon: Remove unused includes 2025-05-25 10:30:41 +02:00
CubebUtils.cpp CubebUtils: Add COM helper class 2025-05-07 20:33:22 +04:00
CubebUtils.h CubebUtils: Add COM helper class 2025-05-07 20:33:22 +04:00
Enums.h AudioPanel: Refactor to use Config system. Some options were changed to a different format, for easier compatibility. 2025-03-26 12:26:01 -07:00
Mixer.cpp Merge pull request #13725 from Sam-Belliveau/more-consistent-looping 2025-08-05 20:02:58 -04:00
Mixer.h Merge pull request #13725 from Sam-Belliveau/more-consistent-looping 2025-08-05 20:02:58 -04:00
NullSoundStream.cpp AudioCommon/Mixer: Skip sample processing when NullSoundStream is being used. 2025-07-23 17:53:21 -05:00
NullSoundStream.h AudioCommon: rename isValid() to IsValid() 2021-08-08 03:05:03 +01:00
OpenALStream.cpp AudioCommon: Remove unused qualifiers and make variables constant 2025-05-30 21:48:38 +02:00
OpenALStream.h AudioCommon: Remove unused includes 2025-05-25 10:30:41 +02:00
OpenSLESStream.cpp Android: Ask system for optimal audio buffer size and sample rate 2025-05-25 11:59:33 +02:00
OpenSLESStream.h Android: Ask system for optimal audio buffer size and sample rate 2025-05-25 11:59:33 +02:00
PulseAudioStream.cpp Port Main.DSP to MainSettings 2021-10-15 23:24:46 -04:00
PulseAudioStream.h AudioCommon: Remove unused includes 2025-05-25 10:30:41 +02:00
SoundStream.h AudioCommon: rename isValid() to IsValid() 2021-08-08 03:05:03 +01:00
SurroundDecoder.cpp AudioCommon: Remove unused qualifiers and make variables constant 2025-05-30 21:48:38 +02:00
SurroundDecoder.h Common: Move FixedSizeQueue into Common namespace 2023-03-22 01:23:15 -04:00
WASAPIStream.cpp Merge pull request #13697 from tygyh/AudioCommon/Remove-unused-includes 2025-06-07 17:45:50 -05:00
WASAPIStream.h Make overriding explicit and remove redundant virtual specifiers on overriding destructors - Core & UnitTests 2025-05-01 15:00:37 +02:00
WaveFile.cpp WaveFile: Add m_ prefix to member variables 2025-06-01 11:41:09 -07:00
WaveFile.h WaveFile: Add m_ prefix to member variables 2025-06-01 11:41:09 -07:00