Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/include/llvm-c/Orc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,12 @@ LLVM_C_ABI LLVMOrcThreadSafeContextRef LLVMOrcCreateNewThreadSafeContext(void);
LLVM_C_ABI LLVMOrcThreadSafeContextRef
LLVMOrcCreateNewThreadSafeContextFromLLVMContext(LLVMContextRef Ctx);

/**
* Get a reference to the wrapped LLVMContext.
*/
LLVMContextRef
LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx);

/**
* Dispose of a ThreadSafeContext.
*/
Expand Down
23 changes: 23 additions & 0 deletions llvm/include/llvm/ExecutionEngine/Orc/ThreadSafeModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ class ThreadSafeContext {
};

public:
// RAII based lock for ThreadSafeContext.
class [[nodiscard]] Lock {
public:
Lock(std::shared_ptr<State> S) : S(std::move(S)), L(this->S->Mutex) {}

private:
std::shared_ptr<State> S;
std::unique_lock<std::recursive_mutex> L;
};

/// Construct a null context.
ThreadSafeContext() = default;

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

/// Returns a pointer to the LLVMContext that was used to construct this
/// instance, or null if the instance was default constructed.
LLVMContext *getContext() { return S ? S->Ctx.get() : nullptr; }

/// Returns a pointer to the LLVMContext that was used to construct this
/// instance, or null if the instance was default constructed.
const LLVMContext *getContext() const { return S ? S->Ctx.get() : nullptr; }

Lock getLock() const {
assert(S && "Can not lock an empty ThreadSafeContext");
return Lock(S);
}

private:
std::shared_ptr<State> S;
};
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,11 @@ LLVMOrcCreateNewThreadSafeContextFromLLVMContext(LLVMContextRef Ctx) {
return wrap(new ThreadSafeContext(std::unique_ptr<LLVMContext>(unwrap(Ctx))));
}

LLVMContextRef
LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx) {
return wrap(unwrap(TSCtx)->getContext());
}

void LLVMOrcDisposeThreadSafeContext(LLVMOrcThreadSafeContextRef TSCtx) {
delete unwrap(TSCtx);
}
Expand Down