Merge pull request #14079 from Dentomologist/use_asyncworkthread_pushblocking_instead_of_sync_event

CubebStream: Use WorkQueueThread::PushBlocking instead of sync_event
This commit is contained in:
Jordan Woyak 2025-11-07 16:31:46 -06:00 committed by GitHub
commit 51cd0a676a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 45 deletions

View File

@ -40,14 +40,11 @@ CubebStream::CubebStream()
#ifdef _WIN32 #ifdef _WIN32
: m_work_queue("Cubeb Worker") : m_work_queue("Cubeb Worker")
{ {
Common::Event sync_event; m_work_queue.PushBlocking([this] {
m_work_queue.Push([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
auto const result = CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE); auto const result = CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
m_coinit_success = result == S_OK; m_coinit_success = result == S_OK;
m_should_couninit = result == S_OK || result == S_FALSE; m_should_couninit = result == S_OK || result == S_FALSE;
}); });
sync_event.Wait();
} }
#else #else
= default; = default;
@ -60,11 +57,8 @@ bool CubebStream::Init()
#ifdef _WIN32 #ifdef _WIN32
if (!m_coinit_success) if (!m_coinit_success)
return false; return false;
Common::Event sync_event; m_work_queue.PushBlocking([this, &return_value] {
m_work_queue.Push([this, &return_value, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
#endif #endif
m_ctx = CubebUtils::GetContext(); m_ctx = CubebUtils::GetContext();
if (m_ctx) if (m_ctx)
{ {
@ -98,7 +92,6 @@ bool CubebStream::Init()
#ifdef _WIN32 #ifdef _WIN32
}); });
sync_event.Wait();
#endif #endif
return return_value; return return_value;
@ -111,9 +104,7 @@ bool CubebStream::SetRunning(bool running)
#ifdef _WIN32 #ifdef _WIN32
if (!m_coinit_success) if (!m_coinit_success)
return false; return false;
Common::Event sync_event; m_work_queue.PushBlocking([this, running, &return_value] {
m_work_queue.Push([this, running, &return_value, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
#endif #endif
if (running) if (running)
return_value = cubeb_stream_start(m_stream) == CUBEB_OK; return_value = cubeb_stream_start(m_stream) == CUBEB_OK;
@ -121,7 +112,6 @@ bool CubebStream::SetRunning(bool running)
return_value = cubeb_stream_stop(m_stream) == CUBEB_OK; return_value = cubeb_stream_stop(m_stream) == CUBEB_OK;
#ifdef _WIN32 #ifdef _WIN32
}); });
sync_event.Wait();
#endif #endif
return return_value; return return_value;
@ -130,9 +120,7 @@ bool CubebStream::SetRunning(bool running)
CubebStream::~CubebStream() CubebStream::~CubebStream()
{ {
#ifdef _WIN32 #ifdef _WIN32
Common::Event sync_event; m_work_queue.PushBlocking([this] {
m_work_queue.Push([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
#endif #endif
cubeb_stream_stop(m_stream); cubeb_stream_stop(m_stream);
cubeb_stream_destroy(m_stream); cubeb_stream_destroy(m_stream);
@ -144,7 +132,6 @@ CubebStream::~CubebStream()
} }
m_coinit_success = false; m_coinit_success = false;
}); });
sync_event.Wait();
#endif #endif
m_ctx.reset(); m_ctx.reset();
} }
@ -154,13 +141,10 @@ void CubebStream::SetVolume(int volume)
#ifdef _WIN32 #ifdef _WIN32
if (!m_coinit_success) if (!m_coinit_success)
return; return;
Common::Event sync_event; m_work_queue.PushBlocking([this, volume] {
m_work_queue.Push([this, volume, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
#endif #endif
cubeb_stream_set_volume(m_stream, volume / 100.0f); cubeb_stream_set_volume(m_stream, volume / 100.0f);
#ifdef _WIN32 #ifdef _WIN32
}); });
sync_event.Wait();
#endif #endif
} }

View File

@ -36,14 +36,11 @@ void CEXIMic::StreamInit()
#ifdef _WIN32 #ifdef _WIN32
if (!m_coinit_success) if (!m_coinit_success)
return; return;
Common::Event sync_event; m_work_queue.PushBlocking([this] {
m_work_queue.Push([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
#endif #endif
m_cubeb_ctx = CubebUtils::GetContext(); m_cubeb_ctx = CubebUtils::GetContext();
#ifdef _WIN32 #ifdef _WIN32
}); });
sync_event.Wait();
#endif #endif
} }
@ -56,14 +53,11 @@ void CEXIMic::StreamTerminate()
#ifdef _WIN32 #ifdef _WIN32
if (!m_coinit_success) if (!m_coinit_success)
return; return;
Common::Event sync_event; m_work_queue.PushBlocking([this] {
m_work_queue.Push([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
#endif #endif
m_cubeb_ctx.reset(); m_cubeb_ctx.reset();
#ifdef _WIN32 #ifdef _WIN32
}); });
sync_event.Wait();
#endif #endif
} }
} }
@ -104,9 +98,7 @@ void CEXIMic::StreamStart()
#ifdef _WIN32 #ifdef _WIN32
if (!m_coinit_success) if (!m_coinit_success)
return; return;
Common::Event sync_event; m_work_queue.PushBlocking([this] {
m_work_queue.Push([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
#endif #endif
// Open stream with current parameters // Open stream with current parameters
stream_size = buff_size_samples * 500; stream_size = buff_size_samples * 500;
@ -142,7 +134,6 @@ void CEXIMic::StreamStart()
INFO_LOG_FMT(EXPANSIONINTERFACE, "started cubeb stream"); INFO_LOG_FMT(EXPANSIONINTERFACE, "started cubeb stream");
#ifdef _WIN32 #ifdef _WIN32
}); });
sync_event.Wait();
#endif #endif
} }
@ -151,9 +142,7 @@ void CEXIMic::StreamStop()
if (m_cubeb_stream) if (m_cubeb_stream)
{ {
#ifdef _WIN32 #ifdef _WIN32
Common::Event sync_event; m_work_queue.PushBlocking([this] {
m_work_queue.Push([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
#endif #endif
if (cubeb_stream_stop(m_cubeb_stream) != CUBEB_OK) if (cubeb_stream_stop(m_cubeb_stream) != CUBEB_OK)
ERROR_LOG_FMT(EXPANSIONINTERFACE, "Error stopping cubeb stream"); ERROR_LOG_FMT(EXPANSIONINTERFACE, "Error stopping cubeb stream");
@ -161,7 +150,6 @@ void CEXIMic::StreamStop()
m_cubeb_stream = nullptr; m_cubeb_stream = nullptr;
#ifdef _WIN32 #ifdef _WIN32
}); });
sync_event.Wait();
#endif #endif
} }
@ -217,14 +205,11 @@ CEXIMic::CEXIMic(Core::System& system, int index)
next_int_ticks = 0; next_int_ticks = 0;
#ifdef _WIN32 #ifdef _WIN32
Common::Event sync_event; m_work_queue.PushBlocking([this] {
m_work_queue.Push([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
auto result = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE); auto result = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
m_coinit_success = result == S_OK; m_coinit_success = result == S_OK;
m_should_couninit = result == S_OK || result == S_FALSE; m_should_couninit = result == S_OK || result == S_FALSE;
}); });
sync_event.Wait();
#endif #endif
StreamInit(); StreamInit();
@ -237,13 +222,10 @@ CEXIMic::~CEXIMic()
#ifdef _WIN32 #ifdef _WIN32
if (m_should_couninit) if (m_should_couninit)
{ {
Common::Event sync_event; m_work_queue.PushBlocking([this] {
m_work_queue.Push([this, &sync_event] {
Common::ScopeGuard sync_event_guard([&sync_event] { sync_event.Set(); });
m_should_couninit = false; m_should_couninit = false;
CoUninitialize(); CoUninitialize();
}); });
sync_event.Wait();
} }
m_coinit_success = false; m_coinit_success = false;
#endif #endif