Skip to content

Commit 8e0c18f

Browse files
use std::recursive_mutex instead of recreating it
1 parent c3e3b15 commit 8e0c18f

File tree

1 file changed

+3
-64
lines changed

1 file changed

+3
-64
lines changed

lib/CppInterOp/CppInterOp.cpp

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -91,74 +91,13 @@ using namespace llvm;
9191
using namespace std;
9292

9393
#define LOCK(InterpInfo) \
94-
LockRAII interop_lock = \
95-
LockRAII((InterpInfo).InterpreterLock, (InterpInfo).ThreadIdLock, \
96-
(InterpInfo).CurrentThreadId)
97-
98-
namespace {
99-
struct LockRAII {
100-
LockRAII(std::mutex& resource_lock, std::mutex& thread_id_lock,
101-
std::optional<std::thread::id>& current_thread_id)
102-
: ResourceLock(resource_lock), ThreadIdLock(thread_id_lock),
103-
CurrentThreadId(current_thread_id) {
104-
ThreadIdLock.lock();
105-
if (!ResourceLock.try_lock()) {
106-
if (CurrentThreadId) {
107-
if ((*CurrentThreadId) != std::this_thread::get_id()) {
108-
ThreadIdLock.unlock();
109-
ResourceLock.lock(); // blocking
110-
bool res = ThreadIdLock.try_lock(); // should be free
111-
assert(res && "Internal Error: Interpreter lock not held, but "
112-
"ThreadId lock held.\nPlease report this as a bug.\n");
113-
ThreadIdLock.unlock();
114-
return;
115-
}
116-
owned = false;
117-
ThreadIdLock.unlock();
118-
return;
119-
}
120-
assert(false &&
121-
"Internal Error: A thread holds lock for the current "
122-
"interpreter, but no information available on which thread "
123-
"holds the lock.\nPlease report this as a bug.\n");
124-
}
125-
assert(!CurrentThreadId &&
126-
"Internal Error: Lock released but thread id not cleared.\nPlease "
127-
"report this as a bug.\n");
128-
CurrentThreadId = std::this_thread::get_id();
129-
ThreadIdLock.unlock();
130-
}
131-
132-
~LockRAII() {
133-
assert(!ResourceLock.try_lock() &&
134-
"Internal Error: Resource lock not held");
135-
if (!owned)
136-
return;
137-
ThreadIdLock.lock();
138-
CurrentThreadId = std::nullopt;
139-
ThreadIdLock.unlock();
140-
ResourceLock.unlock();
141-
}
142-
143-
LockRAII(LockRAII&&) = delete;
144-
LockRAII(const LockRAII&) = delete;
145-
LockRAII& operator=(const LockRAII&) = delete;
146-
LockRAII& operator=(LockRAII&&) = delete;
147-
148-
private:
149-
bool owned = true;
150-
std::mutex& ResourceLock;
151-
std::mutex& ThreadIdLock;
152-
std::optional<std::thread::id>& CurrentThreadId;
153-
};
154-
} // namespace
94+
std::lock_guard<std::recursive_mutex> interop_lock( \
95+
(InterpInfo).InterpreterLock)
15596

15697
struct InterpreterInfo {
15798
compat::Interpreter* Interpreter = nullptr;
15899
bool isOwned = true;
159-
std::mutex InterpreterLock;
160-
std::mutex ThreadIdLock;
161-
std::optional<std::thread::id> CurrentThreadId = std::nullopt;
100+
std::recursive_mutex InterpreterLock;
162101

163102
InterpreterInfo(compat::Interpreter* I, bool Owned)
164103
: Interpreter(I), isOwned(Owned) {}

0 commit comments

Comments
 (0)