summaryrefslogtreecommitdiff
path: root/Source/Android
diff options
context:
space:
mode:
authorJosJuice <josjuice@gmail.com>2023-06-11 12:18:16 +0200
committerJosJuice <josjuice@gmail.com>2023-06-12 23:39:42 +0200
commit3519a7070d5dc476cb1911bb1d2b05befbe0804b (patch)
tree2726f8e77ee86370128285dff68872b0b292b18f /Source/Android
parent552404292238bd1758346cc526827580aec4f532 (diff)
Android: And Lock and Unlock wrappers to HostThreadLock
This way we can ensure DeclareAsHostThread and UndeclareAsHostThread are called when locking and unlocking.
Diffstat (limited to 'Source/Android')
-rw-r--r--Source/Android/jni/Host.h28
-rw-r--r--Source/Android/jni/MainAndroid.cpp10
2 files changed, 29 insertions, 9 deletions
diff --git a/Source/Android/jni/Host.h b/Source/Android/jni/Host.h
index 6025c62644..78269ec90e 100644
--- a/Source/Android/jni/Host.h
+++ b/Source/Android/jni/Host.h
@@ -12,13 +12,33 @@
// sequentially for access.
struct HostThreadLock
{
- static std::mutex s_host_identity_mutex;
- std::unique_lock<std::mutex> m_lock;
-
+public:
explicit HostThreadLock() : m_lock(s_host_identity_mutex) { Core::DeclareAsHostThread(); }
+
+ ~HostThreadLock()
+ {
+ if (m_lock.owns_lock())
+ Core::UndeclareAsHostThread();
+ }
+
HostThreadLock(const HostThreadLock& other) = delete;
HostThreadLock(HostThreadLock&& other) = delete;
HostThreadLock& operator=(const HostThreadLock& other) = delete;
HostThreadLock& operator=(HostThreadLock&& other) = delete;
- ~HostThreadLock() { Core::UndeclareAsHostThread(); }
+
+ void Lock()
+ {
+ m_lock.lock();
+ Core::DeclareAsHostThread();
+ }
+
+ void Unlock()
+ {
+ m_lock.unlock();
+ Core::UndeclareAsHostThread();
+ }
+
+private:
+ static std::mutex s_host_identity_mutex;
+ std::unique_lock<std::mutex> m_lock;
};
diff --git a/Source/Android/jni/MainAndroid.cpp b/Source/Android/jni/MainAndroid.cpp
index d99326aaae..0b8a0760ab 100644
--- a/Source/Android/jni/MainAndroid.cpp
+++ b/Source/Android/jni/MainAndroid.cpp
@@ -447,9 +447,9 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SurfaceDestr
while (s_is_booting.IsSet())
{
// Need to wait for boot to finish before we can pause
- host_identity_guard.m_lock.unlock();
+ host_identity_guard.Unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
- host_identity_guard.m_lock.lock();
+ host_identity_guard.Lock();
}
if (Core::GetState() == Core::State::Running)
@@ -572,15 +572,15 @@ static void Run(JNIEnv* env, std::unique_ptr<BootParameters>&& boot, bool riivol
while (Core::IsRunning())
{
- host_identity_guard.m_lock.unlock();
+ host_identity_guard.Unlock();
s_update_main_frame_event.Wait();
- host_identity_guard.m_lock.lock();
+ host_identity_guard.Lock();
Core::HostDispatchJobs();
}
s_game_metadata_is_valid = false;
Core::Shutdown();
- host_identity_guard.m_lock.unlock();
+ host_identity_guard.Unlock();
env->CallStaticVoidMethod(IDCache::GetNativeLibraryClass(),
IDCache::GetFinishEmulationActivity());