Skip to content

Commit 7659fcb

Browse files
use std::recursive_mutex instead of recreating it
1 parent 821829c commit 7659fcb

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
@@ -89,74 +89,13 @@ using namespace llvm;
8989
using namespace std;
9090

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

15495
struct InterpreterInfo {
15596
compat::Interpreter* Interpreter = nullptr;
15697
bool isOwned = true;
157-
std::mutex InterpreterLock;
158-
std::mutex ThreadIdLock;
159-
std::optional<std::thread::id> CurrentThreadId = std::nullopt;
98+
std::recursive_mutex InterpreterLock;
16099

161100
InterpreterInfo(compat::Interpreter* I, bool Owned)
162101
: Interpreter(I), isOwned(Owned) {}

0 commit comments

Comments
 (0)