Skip to content

Commit ccc316f

Browse files
committed
Add back manual locking ThreadSafeContext API
It is difficult to remove the use of this in julia ATM.
1 parent e01e3e9 commit ccc316f

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

llvm/include/llvm-c/Orc.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,12 @@ LLVM_C_ABI LLVMOrcThreadSafeContextRef LLVMOrcCreateNewThreadSafeContext(void);
10991099
LLVM_C_ABI LLVMOrcThreadSafeContextRef
11001100
LLVMOrcCreateNewThreadSafeContextFromLLVMContext(LLVMContextRef Ctx);
11011101

1102+
/**
1103+
* Get a reference to the wrapped LLVMContext.
1104+
*/
1105+
LLVMContextRef
1106+
LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx);
1107+
11021108
/**
11031109
* Dispose of a ThreadSafeContext.
11041110
*/

llvm/include/llvm/ExecutionEngine/Orc/ThreadSafeModule.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ class ThreadSafeContext {
3636
};
3737

3838
public:
39+
// RAII based lock for ThreadSafeContext.
40+
class [[nodiscard]] Lock {
41+
public:
42+
Lock(std::shared_ptr<State> S) : S(std::move(S)), L(this->S->Mutex) {}
43+
44+
private:
45+
std::shared_ptr<State> S;
46+
std::unique_lock<std::recursive_mutex> L;
47+
};
48+
3949
/// Construct a null context.
4050
ThreadSafeContext() = default;
4151

@@ -62,6 +72,19 @@ class ThreadSafeContext {
6272
return F((const LLVMContext *)nullptr);
6373
}
6474

75+
/// Returns a pointer to the LLVMContext that was used to construct this
76+
/// instance, or null if the instance was default constructed.
77+
LLVMContext *getContext() { return S ? S->Ctx.get() : nullptr; }
78+
79+
/// Returns a pointer to the LLVMContext that was used to construct this
80+
/// instance, or null if the instance was default constructed.
81+
const LLVMContext *getContext() const { return S ? S->Ctx.get() : nullptr; }
82+
83+
Lock getLock() const {
84+
assert(S && "Can not lock an empty ThreadSafeContext");
85+
return Lock(S);
86+
}
87+
6588
private:
6689
std::shared_ptr<State> S;
6790
};

llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,11 @@ LLVMOrcCreateNewThreadSafeContextFromLLVMContext(LLVMContextRef Ctx) {
734734
return wrap(new ThreadSafeContext(std::unique_ptr<LLVMContext>(unwrap(Ctx))));
735735
}
736736

737+
LLVMContextRef
738+
LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx) {
739+
return wrap(unwrap(TSCtx)->getContext());
740+
}
741+
737742
void LLVMOrcDisposeThreadSafeContext(LLVMOrcThreadSafeContextRef TSCtx) {
738743
delete unwrap(TSCtx);
739744
}

0 commit comments

Comments
 (0)