Skip to content

Commit

Permalink
修复了线程锁的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
telecomadm1145 committed Aug 5, 2024
1 parent f602097 commit 5f3ecd4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
27 changes: 21 additions & 6 deletions CasioEmuMsvc/Containers/ConcurrentObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ template <class T>
class ConcurrentObject {
protected:
mutable std::mutex mtx;
mutable std::thread::id own_thread;
T* storage;

public:
Expand All @@ -22,13 +23,20 @@ class ConcurrentObject {
class ObjectRef {
public:
ConcurrentObject<T>& obj;
bool own_lock = false;
bool own_lock = true;
ObjectRef(ConcurrentObject<T>& 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;
Expand All @@ -40,13 +48,20 @@ class ConcurrentObject {
class ConstObjectRef {
public:
const ConcurrentObject<T>& obj;
bool own_lock = false;
bool own_lock = true;
ConstObjectRef(const ConcurrentObject<T>& 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;
Expand Down
2 changes: 1 addition & 1 deletion CasioEmuMsvc/Gui/CallAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 5f3ecd4

Please sign in to comment.