Skip to content

Commit

Permalink
Allow move construction of the resource pool
Browse files Browse the repository at this point in the history
  • Loading branch information
pchintalapudi committed Apr 7, 2022
1 parent 7102da2 commit c173550
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ JuliaOJIT::JuliaOJIT()
#endif
GlobalJD(ES.createBareJITDylib("JuliaGlobals")),
JD(ES.createBareJITDylib("JuliaOJIT")),
ContextPool([](){ return orc::ThreadSafeContext(std::make_unique<LLVMContext>()); }),
#ifdef JL_USE_JITLINK
// TODO: Port our memory management optimisations to JITLink instead of using the
// default InProcessMemoryManager.
Expand Down
20 changes: 12 additions & 8 deletions src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class JuliaOJIT {
template<typename ResourceT, size_t max = 0>
struct ResourcePool {
public:
ResourcePool(function_ref<ResourceT()> creator) : creator(std::move(creator)) {}
ResourcePool(function_ref<ResourceT()> creator) : creator(std::move(creator)), mutex(std::make_unique<WNMutex>()) {}
class OwningResource {
public:
OwningResource(ResourcePool &pool, ResourceT resource) : pool(pool), resource(std::move(resource)) {}
Expand Down Expand Up @@ -247,29 +247,33 @@ class JuliaOJIT {
}

ResourceT acquire_() {
std::unique_lock<std::mutex> lock(mutex);
std::unique_lock<std::mutex> lock(mutex->mutex);
if (!pool.empty()) {
return pool.pop_back_val();
}
if (!max || created < max) {
created++;
return creator();
}
empty.wait(lock, [&](){ return !pool.empty(); });
mutex->empty.wait(lock, [&](){ return !pool.empty(); });
assert(!pool.empty() && "Expected resource pool to have a value!");
return pool.pop_back_val();
}
void release_(ResourceT &&resource) {
std::lock_guard<std::mutex> lock(mutex);
std::lock_guard<std::mutex> lock(mutex->mutex);
pool.push_back(std::move(resource));
empty.notify_one();
mutex->empty.notify_one();
}
private:
llvm::function_ref<ResourceT()> creator;
size_t created = 0;
llvm::SmallVector<ResourceT, max == 0 ? 8 : max> pool;
std::mutex mutex;
std::condition_variable empty;
struct WNMutex {
std::mutex mutex;
std::condition_variable empty;
};

std::unique_ptr<WNMutex> mutex;
};
struct OptimizerT {
OptimizerT(legacy::PassManager &PM, std::mutex &mutex, int optlevel) : optlevel(optlevel), PM(PM), mutex(mutex) {}
Expand Down Expand Up @@ -349,7 +353,7 @@ class JuliaOJIT {
orc::JITDylib &GlobalJD;
orc::JITDylib &JD;

ResourcePool<orc::ThreadSafeContext> ContextPool{[](){ return orc::ThreadSafeContext(std::make_unique<LLVMContext>()); }};
ResourcePool<orc::ThreadSafeContext> ContextPool;

#ifndef JL_USE_JITLINK
std::shared_ptr<RTDyldMemoryManager> MemMgr;
Expand Down

0 comments on commit c173550

Please sign in to comment.