From 5f3ecd4c3967cb99d144769ea40a50fda4309291 Mon Sep 17 00:00:00 2001 From: telecomadm1145 Date: Mon, 5 Aug 2024 16:08:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E9=94=81=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CasioEmuMsvc/Containers/ConcurrentObject.h | 27 +++++++++++++++++----- CasioEmuMsvc/Gui/CallAnalysis.cpp | 2 +- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/CasioEmuMsvc/Containers/ConcurrentObject.h b/CasioEmuMsvc/Containers/ConcurrentObject.h index bb07bad..a02d12b 100644 --- a/CasioEmuMsvc/Containers/ConcurrentObject.h +++ b/CasioEmuMsvc/Containers/ConcurrentObject.h @@ -4,6 +4,7 @@ template class ConcurrentObject { protected: mutable std::mutex mtx; + mutable std::thread::id own_thread; T* storage; public: @@ -22,13 +23,20 @@ class ConcurrentObject { class ObjectRef { public: ConcurrentObject& obj; - bool own_lock = false; + bool own_lock = true; ObjectRef(ConcurrentObject& obj) : obj(obj) { - own_lock = obj.mtx.try_lock(); + if (obj.own_thread == std::this_thread::get_id()) { + own_lock = false; + return; + } + obj.own_thread = std::this_thread::get_id(); + obj.mtx.lock(); } ~ObjectRef() { - if (own_lock) + if (own_lock) { obj.mtx.unlock(); + obj.own_thread = std::thread::id::id(); + } } T* operator->() { return obj.storage; @@ -40,13 +48,20 @@ class ConcurrentObject { class ConstObjectRef { public: const ConcurrentObject& obj; - bool own_lock = false; + bool own_lock = true; ConstObjectRef(const ConcurrentObject& obj) : obj(obj) { - own_lock = obj.mtx.try_lock(); + if (obj.own_thread == std::this_thread::get_id()) { + own_lock = false; + return; + } + obj.own_thread = std::this_thread::get_id(); + obj.mtx.lock(); } ~ConstObjectRef() { - if (own_lock) + if (own_lock) { obj.mtx.unlock(); + obj.own_thread = std::thread::id::id(); + } } const T* operator->() { return obj.storage; diff --git a/CasioEmuMsvc/Gui/CallAnalysis.cpp b/CasioEmuMsvc/Gui/CallAnalysis.cpp index cc69c0c..0184af9 100644 --- a/CasioEmuMsvc/Gui/CallAnalysis.cpp +++ b/CasioEmuMsvc/Gui/CallAnalysis.cpp @@ -40,7 +40,7 @@ struct CallAnalysis : public UIWindow { fc.xr0 = (sender.reg_r[3] << 24) | (sender.reg_r[2] << 16) | (sender.reg_r[1] << 8) | (sender.reg_r[0]); fc.pc = pc; fc.lr = lr; - fc.stack = sender.GetBacktrace(); + fc.stack = sender.GetBacktrace(); // 已经上锁了,草( funcs[pc].push_back(fc); } }