Skip to content

Commit

Permalink
Expose ValueMap's mutex type as a typedef instead of a sys::Mutex.
Browse files Browse the repository at this point in the history
This enables static polymorphism of the mutex type, which is
necessary in order to replace the standard mutex implementation
with a different type.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211080 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Zachary Turner committed Jun 17, 2014
1 parent 408691f commit 84fea77
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
10 changes: 6 additions & 4 deletions include/llvm/IR/ValueMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class ValueMapConstIterator;
/// This class defines the default behavior for configurable aspects of
/// ValueMap<>. User Configs should inherit from this class to be as compatible
/// as possible with future versions of ValueMap.
template<typename KeyT>
template<typename KeyT, typename MutexT = sys::Mutex>
struct ValueMapConfig {
typedef MutexT mutex_type;

/// If FollowRAUW is true, the ValueMap will update mappings on RAUW. If it's
/// false, the ValueMap will leave the original mapping in place.
enum { FollowRAUW = true };
Expand All @@ -67,7 +69,7 @@ struct ValueMapConfig {
/// and onDelete) and not inside other ValueMap methods. NULL means that no
/// mutex is necessary.
template<typename ExtraDataT>
static sys::Mutex *getMutex(const ExtraDataT &/*Data*/) { return nullptr; }
static mutex_type *getMutex(const ExtraDataT &/*Data*/) { return nullptr; }
};

/// See the file comment.
Expand Down Expand Up @@ -212,7 +214,7 @@ class ValueMapCallbackVH : public CallbackVH {
void deleted() override {
// Make a copy that won't get changed even when *this is destroyed.
ValueMapCallbackVH Copy(*this);
sys::Mutex *M = Config::getMutex(Copy.Map->Data);
typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
if (M)
M->acquire();
Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this.
Expand All @@ -225,7 +227,7 @@ class ValueMapCallbackVH : public CallbackVH {
"Invalid RAUW on key of ValueMap<>");
// Make a copy that won't get changed even when *this is destroyed.
ValueMapCallbackVH Copy(*this);
sys::Mutex *M = Config::getMutex(Copy.Map->Data);
typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
if (M)
M->acquire();

Expand Down
14 changes: 7 additions & 7 deletions unittests/IR/ValueMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ TYPED_TEST(ValueMapTest, ConfiguredCollisionBehavior) {
// TODO: Implement this when someone needs it.
}

template<typename KeyT>
struct LockMutex : ValueMapConfig<KeyT> {
template<typename KeyT, typename MutexT>
struct LockMutex : ValueMapConfig<KeyT, MutexT> {
struct ExtraData {
sys::Mutex *M;
mutex_type *M;
bool *CalledRAUW;
bool *CalledDeleted;
};
Expand All @@ -192,15 +192,15 @@ struct LockMutex : ValueMapConfig<KeyT> {
*Data.CalledDeleted = true;
EXPECT_FALSE(Data.M->tryacquire()) << "Mutex should already be locked.";
}
static sys::Mutex *getMutex(const ExtraData &Data) { return Data.M; }
static mutex_type *getMutex(const ExtraData &Data) { return Data.M; }
};
#if LLVM_ENABLE_THREADS
TYPED_TEST(ValueMapTest, LocksMutex) {
sys::Mutex M(false); // Not recursive.
bool CalledRAUW = false, CalledDeleted = false;
typename LockMutex<TypeParam*>::ExtraData Data =
{&M, &CalledRAUW, &CalledDeleted};
ValueMap<TypeParam*, int, LockMutex<TypeParam*> > VM(Data);
typedef LockMutex<TypeParam*, sys::Mutex> ConfigType;
typename ConfigType::ExtraData Data = {&M, &CalledRAUW, &CalledDeleted};
ValueMap<TypeParam*, int, ConfigType> VM(Data);
VM[this->BitcastV.get()] = 7;
this->BitcastV->replaceAllUsesWith(this->AddV.get());
this->AddV.reset();
Expand Down

0 comments on commit 84fea77

Please sign in to comment.