Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 1f502bd

Browse files
author
Zachary Turner
committed
Replace Execution Engine's mutex with std::recursive_mutex.
This change has a bit of a trickle down effect due to the fact that there are a number of derived implementations of ExecutionEngine, and that the mutex is not tightly encapsulated so is used by other classes directly. Reviewed by: rnk Differential Revision: http://reviews.llvm.org/D4196 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211214 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent b279154 commit 1f502bd

File tree

7 files changed

+69
-71
lines changed

7 files changed

+69
-71
lines changed

include/llvm/ExecutionEngine/ExecutionEngine.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
#include "llvm/IR/ValueMap.h"
2323
#include "llvm/MC/MCCodeGenInfo.h"
2424
#include "llvm/Support/ErrorHandling.h"
25-
#include "llvm/Support/Mutex.h"
2625
#include "llvm/Target/TargetMachine.h"
2726
#include "llvm/Target/TargetOptions.h"
2827
#include <map>
28+
#include <mutex>
2929
#include <string>
3030
#include <vector>
3131

@@ -42,7 +42,6 @@ class JITEventListener;
4242
class JITMemoryManager;
4343
class MachineCodeInfo;
4444
class Module;
45-
class MutexGuard;
4645
class ObjectCache;
4746
class RTDyldMemoryManager;
4847
class Triple;
@@ -59,7 +58,7 @@ class ExecutionEngineState {
5958
public:
6059
struct AddressMapConfig : public ValueMapConfig<const GlobalValue*> {
6160
typedef ExecutionEngineState *ExtraData;
62-
static sys::Mutex *getMutex(ExecutionEngineState *EES);
61+
static std::recursive_mutex *getMutex(ExecutionEngineState *EES);
6362
static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old);
6463
static void onRAUW(ExecutionEngineState *, const GlobalValue *,
6564
const GlobalValue *);
@@ -164,7 +163,7 @@ class ExecutionEngine {
164163
/// lock - This lock protects the ExecutionEngine, MCJIT, JIT, JITResolver and
165164
/// JITEmitter classes. It must be held while changing the internal state of
166165
/// any of those classes.
167-
sys::Mutex lock;
166+
std::recursive_mutex lock;
168167

169168
//===--------------------------------------------------------------------===//
170169
// ExecutionEngine Startup

include/llvm/IR/ValueMap.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828

2929
#include "llvm/ADT/DenseMap.h"
3030
#include "llvm/IR/ValueHandle.h"
31-
#include "llvm/Support/Mutex.h"
3231
#include "llvm/Support/type_traits.h"
3332
#include <iterator>
33+
#include <mutex>
3434

3535
namespace llvm {
3636

@@ -45,7 +45,7 @@ class ValueMapConstIterator;
4545
/// This class defines the default behavior for configurable aspects of
4646
/// ValueMap<>. User Configs should inherit from this class to be as compatible
4747
/// as possible with future versions of ValueMap.
48-
template<typename KeyT, typename MutexT = sys::Mutex>
48+
template<typename KeyT, typename MutexT = std::recursive_mutex>
4949
struct ValueMapConfig {
5050
typedef MutexT mutex_type;
5151

@@ -216,11 +216,11 @@ class ValueMapCallbackVH : public CallbackVH {
216216
ValueMapCallbackVH Copy(*this);
217217
typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
218218
if (M)
219-
M->acquire();
219+
M->lock();
220220
Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this.
221221
Copy.Map->Map.erase(Copy); // Definitely destroys *this.
222222
if (M)
223-
M->release();
223+
M->unlock();
224224
}
225225
void allUsesReplacedWith(Value *new_key) override {
226226
assert(isa<KeySansPointerT>(new_key) &&
@@ -229,7 +229,7 @@ class ValueMapCallbackVH : public CallbackVH {
229229
ValueMapCallbackVH Copy(*this);
230230
typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
231231
if (M)
232-
M->acquire();
232+
M->lock();
233233

234234
KeyT typed_new_key = cast<KeySansPointerT>(new_key);
235235
// Can destroy *this:
@@ -245,7 +245,7 @@ class ValueMapCallbackVH : public CallbackVH {
245245
}
246246
}
247247
if (M)
248-
M->release();
248+
M->unlock();
249249
}
250250
};
251251

lib/ExecutionEngine/ExecutionEngine.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void *ExecutionEngineState::RemoveMapping(const GlobalValue *ToUnmap) {
166166
}
167167

168168
void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
169-
MutexGuard locked(lock);
169+
std::lock_guard<std::recursive_mutex> locked(lock);
170170

171171
DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
172172
<< "\' to [" << Addr << "]\n";);
@@ -184,14 +184,14 @@ void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
184184
}
185185

186186
void ExecutionEngine::clearAllGlobalMappings() {
187-
MutexGuard locked(lock);
187+
std::lock_guard<std::recursive_mutex> locked(lock);
188188

189189
EEState.getGlobalAddressMap().clear();
190190
EEState.getGlobalAddressReverseMap().clear();
191191
}
192192

193193
void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
194-
MutexGuard locked(lock);
194+
std::lock_guard<std::recursive_mutex> locked(lock);
195195

196196
for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
197197
EEState.RemoveMapping(FI);
@@ -201,7 +201,7 @@ void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
201201
}
202202

203203
void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
204-
MutexGuard locked(lock);
204+
std::lock_guard<std::recursive_mutex> locked(lock);
205205

206206
ExecutionEngineState::GlobalAddressMapTy &Map =
207207
EEState.getGlobalAddressMap();
@@ -228,15 +228,15 @@ void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
228228
}
229229

230230
void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
231-
MutexGuard locked(lock);
231+
std::lock_guard<std::recursive_mutex> locked(lock);
232232

233233
ExecutionEngineState::GlobalAddressMapTy::iterator I =
234234
EEState.getGlobalAddressMap().find(GV);
235235
return I != EEState.getGlobalAddressMap().end() ? I->second : nullptr;
236236
}
237237

238238
const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
239-
MutexGuard locked(lock);
239+
std::lock_guard<std::recursive_mutex> locked(lock);
240240

241241
// If we haven't computed the reverse mapping yet, do so first.
242242
if (EEState.getGlobalAddressReverseMap().empty()) {
@@ -555,7 +555,7 @@ void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
555555
if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
556556
return getPointerToFunction(F);
557557

558-
MutexGuard locked(lock);
558+
std::lock_guard<std::recursive_mutex> locked(lock);
559559
if (void *P = EEState.getGlobalAddressMap()[GV])
560560
return P;
561561

@@ -1346,7 +1346,7 @@ ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
13461346
: EE(EE), GlobalAddressMap(this) {
13471347
}
13481348

1349-
sys::Mutex *
1349+
std::recursive_mutex *
13501350
ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
13511351
return &EES->EE.lock;
13521352
}

lib/ExecutionEngine/JIT/JIT.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,18 @@ namespace {
9696
/// bugpoint or gdb users to search for a function by name without any context.
9797
class JitPool {
9898
SmallPtrSet<JIT*, 1> JITs; // Optimize for process containing just 1 JIT.
99-
mutable sys::Mutex Lock;
99+
mutable std::recursive_mutex Lock;
100100
public:
101101
void Add(JIT *jit) {
102-
MutexGuard guard(Lock);
102+
std::lock_guard<std::recursive_mutex> guard(Lock);
103103
JITs.insert(jit);
104104
}
105105
void Remove(JIT *jit) {
106-
MutexGuard guard(Lock);
106+
std::lock_guard<std::recursive_mutex> guard(Lock);
107107
JITs.erase(jit);
108108
}
109109
void *getPointerToNamedFunction(const char *Name) const {
110-
MutexGuard guard(Lock);
110+
std::lock_guard<std::recursive_mutex> guard(Lock);
111111
assert(JITs.size() != 0 && "No Jit registered");
112112
//search function in every instance of JIT
113113
for (SmallPtrSet<JIT*, 1>::const_iterator Jit = JITs.begin(),
@@ -150,7 +150,7 @@ JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
150150
AllJits->Add(this);
151151

152152
// Add target data
153-
MutexGuard locked(lock);
153+
std::lock_guard<std::recursive_mutex> locked(lock);
154154
FunctionPassManager &PM = jitstate->getPM();
155155
M->setDataLayout(TM.getDataLayout());
156156
PM.add(new DataLayoutPass(M));
@@ -177,7 +177,7 @@ JIT::~JIT() {
177177
/// addModule - Add a new Module to the JIT. If we previously removed the last
178178
/// Module, we need re-initialize jitstate with a valid Module.
179179
void JIT::addModule(Module *M) {
180-
MutexGuard locked(lock);
180+
std::lock_guard<std::recursive_mutex> locked(lock);
181181

182182
if (Modules.empty()) {
183183
assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
@@ -206,7 +206,7 @@ void JIT::addModule(Module *M) {
206206
bool JIT::removeModule(Module *M) {
207207
bool result = ExecutionEngine::removeModule(M);
208208

209-
MutexGuard locked(lock);
209+
std::lock_guard<std::recursive_mutex> locked(lock);
210210

211211
if (jitstate && jitstate->getModule() == M) {
212212
delete jitstate;
@@ -408,13 +408,13 @@ GenericValue JIT::runFunction(Function *F,
408408
void JIT::RegisterJITEventListener(JITEventListener *L) {
409409
if (!L)
410410
return;
411-
MutexGuard locked(lock);
411+
std::lock_guard<std::recursive_mutex> locked(lock);
412412
EventListeners.push_back(L);
413413
}
414414
void JIT::UnregisterJITEventListener(JITEventListener *L) {
415415
if (!L)
416416
return;
417-
MutexGuard locked(lock);
417+
std::lock_guard<std::recursive_mutex> locked(lock);
418418
std::vector<JITEventListener*>::reverse_iterator I=
419419
std::find(EventListeners.rbegin(), EventListeners.rend(), L);
420420
if (I != EventListeners.rend()) {
@@ -426,14 +426,14 @@ void JIT::NotifyFunctionEmitted(
426426
const Function &F,
427427
void *Code, size_t Size,
428428
const JITEvent_EmittedFunctionDetails &Details) {
429-
MutexGuard locked(lock);
429+
std::lock_guard<std::recursive_mutex> locked(lock);
430430
for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
431431
EventListeners[I]->NotifyFunctionEmitted(F, Code, Size, Details);
432432
}
433433
}
434434

435435
void JIT::NotifyFreeingMachineCode(void *OldPtr) {
436-
MutexGuard locked(lock);
436+
std::lock_guard<std::recursive_mutex> locked(lock);
437437
for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
438438
EventListeners[I]->NotifyFreeingMachineCode(OldPtr);
439439
}
@@ -444,7 +444,7 @@ void JIT::NotifyFreeingMachineCode(void *OldPtr) {
444444
/// GlobalAddress[F] with the address of F's machine code.
445445
///
446446
void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) {
447-
MutexGuard locked(lock);
447+
std::lock_guard<std::recursive_mutex> locked(lock);
448448

449449
class MCIListener : public JITEventListener {
450450
MachineCodeInfo *const MCI;
@@ -505,7 +505,7 @@ void *JIT::getPointerToFunction(Function *F) {
505505
if (void *Addr = getPointerToGlobalIfAvailable(F))
506506
return Addr; // Check if function already code gen'd
507507

508-
MutexGuard locked(lock);
508+
std::lock_guard<std::recursive_mutex> locked(lock);
509509

510510
// Now that this thread owns the lock, make sure we read in the function if it
511511
// exists in this Module.
@@ -534,7 +534,7 @@ void *JIT::getPointerToFunction(Function *F) {
534534
}
535535

536536
void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) {
537-
MutexGuard locked(lock);
537+
std::lock_guard<std::recursive_mutex> locked(lock);
538538

539539
BasicBlockAddressMapTy::iterator I =
540540
getBasicBlockAddressMap().find(BB);
@@ -546,7 +546,7 @@ void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) {
546546
}
547547

548548
void JIT::clearPointerToBasicBlock(const BasicBlock *BB) {
549-
MutexGuard locked(lock);
549+
std::lock_guard<std::recursive_mutex> locked(lock);
550550
getBasicBlockAddressMap().erase(BB);
551551
}
552552

@@ -555,7 +555,7 @@ void *JIT::getPointerToBasicBlock(BasicBlock *BB) {
555555
(void)getPointerToFunction(BB->getParent());
556556

557557
// resolve basic block address
558-
MutexGuard locked(lock);
558+
std::lock_guard<std::recursive_mutex> locked(lock);
559559

560560
BasicBlockAddressMapTy::iterator I =
561561
getBasicBlockAddressMap().find(BB);
@@ -592,7 +592,7 @@ void *JIT::getPointerToNamedFunction(const std::string &Name,
592592
/// variable, possibly emitting it to memory if needed. This is used by the
593593
/// Emitter.
594594
void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
595-
MutexGuard locked(lock);
595+
std::lock_guard<std::recursive_mutex> locked(lock);
596596

597597
void *Ptr = getPointerToGlobalIfAvailable(GV);
598598
if (Ptr) return Ptr;
@@ -666,7 +666,7 @@ char* JIT::getMemoryForGV(const GlobalVariable* GV) {
666666
size_t S = getDataLayout()->getTypeAllocSize(GlobalType);
667667
size_t A = getDataLayout()->getPreferredAlignment(GV);
668668
if (GV->isThreadLocal()) {
669-
MutexGuard locked(lock);
669+
std::lock_guard<std::recursive_mutex> locked(lock);
670670
Ptr = TJI.allocateThreadLocalMemory(S);
671671
} else if (TJI.allocateSeparateGVMemory()) {
672672
if (A <= 8) {
@@ -687,7 +687,7 @@ char* JIT::getMemoryForGV(const GlobalVariable* GV) {
687687
}
688688

689689
void JIT::addPendingFunction(Function *F) {
690-
MutexGuard locked(lock);
690+
std::lock_guard<std::recursive_mutex> locked(lock);
691691
jitstate->getPendingFunctions().push_back(F);
692692
}
693693

0 commit comments

Comments
 (0)