Android: Fix wait for emulation thread returning early

This commit is contained in:
Connor McLaughlin 2020-11-07 22:02:48 +10:00
parent ac89379ca6
commit f68ad7d0c3
2 changed files with 14 additions and 7 deletions

View File

@ -240,6 +240,7 @@ void AndroidHostInterface::RunOnEmulationThread(std::function<void()> function,
m_mutex.lock();
m_callback_queue.push_back(std::move(function));
m_callbacks_outstanding.store(true);
m_sleep_cv.notify_one();
if (blocking)
@ -247,7 +248,7 @@ void AndroidHostInterface::RunOnEmulationThread(std::function<void()> function,
// TODO: Don't spin
for (;;)
{
if (m_callback_queue.empty())
if (!m_callbacks_outstanding.load())
break;
m_mutex.unlock();
@ -319,13 +320,18 @@ void AndroidHostInterface::EmulationThreadLoop()
std::unique_lock<std::mutex> lock(m_mutex);
for (;;)
{
while (!m_callback_queue.empty())
if (!m_callback_queue.empty())
{
auto callback = std::move(m_callback_queue.front());
m_callback_queue.pop_front();
lock.unlock();
callback();
lock.lock();
do
{
auto callback = std::move(m_callback_queue.front());
m_callback_queue.pop_front();
lock.unlock();
callback();
lock.lock();
}
while (!m_callback_queue.empty());
m_callbacks_outstanding.store(false);
}
if (m_emulation_thread_stop_request.load())

View File

@ -86,6 +86,7 @@ private:
std::mutex m_mutex;
std::condition_variable m_sleep_cv;
std::deque<std::function<void()>> m_callback_queue;
std::atomic_bool m_callbacks_outstanding{false};
std::thread m_emulation_thread;
std::atomic_bool m_emulation_thread_stop_request{false};