diff --git a/android/app/src/cpp/android_host_interface.cpp b/android/app/src/cpp/android_host_interface.cpp index 7f410c163..754dfc7f8 100644 --- a/android/app/src/cpp/android_host_interface.cpp +++ b/android/app/src/cpp/android_host_interface.cpp @@ -240,6 +240,7 @@ void AndroidHostInterface::RunOnEmulationThread(std::function 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 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 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()) diff --git a/android/app/src/cpp/android_host_interface.h b/android/app/src/cpp/android_host_interface.h index ef445ecfb..224f36fe0 100644 --- a/android/app/src/cpp/android_host_interface.h +++ b/android/app/src/cpp/android_host_interface.h @@ -86,6 +86,7 @@ private: std::mutex m_mutex; std::condition_variable m_sleep_cv; std::deque> m_callback_queue; + std::atomic_bool m_callbacks_outstanding{false}; std::thread m_emulation_thread; std::atomic_bool m_emulation_thread_stop_request{false};