mirror of
https://github.com/greg7mdp/parallel-hashmap.git
synced 2026-08-29 08:34:39 +08:00
Take care of the case where another thread inserts the same key between unlock()/lock()
This commit is contained in:
Vendored
+10
-1
@@ -3805,10 +3805,19 @@ protected:
|
|||||||
mutexlock = std::move(typename Lockable::ReadWriteLock(inner));
|
mutexlock = std::move(typename Lockable::ReadWriteLock(inner));
|
||||||
size_t offset = set._find_key(key, hashval);
|
size_t offset = set._find_key(key, hashval);
|
||||||
if (offset == (size_t)-1) {
|
if (offset == (size_t)-1) {
|
||||||
mutexlock.switch_to_unique();
|
if (mutexlock.switch_to_unique()) {
|
||||||
|
// we did an unlock/lock, and another thread could have inserted the same key, so we need to
|
||||||
|
// do a find() again.
|
||||||
|
offset = set._find_key(key, hashval);
|
||||||
|
if (offset == (size_t)-1) {
|
||||||
offset = set.prepare_insert(hashval);
|
offset = set.prepare_insert(hashval);
|
||||||
return std::make_tuple(&inner, offset, true);
|
return std::make_tuple(&inner, offset, true);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
offset = set.prepare_insert(hashval);
|
||||||
|
return std::make_tuple(&inner, offset, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
return std::make_tuple(&inner, offset, false);
|
return std::make_tuple(&inner, offset, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Vendored
+5
-4
@@ -4635,7 +4635,7 @@ public:
|
|||||||
void unlock() {}
|
void unlock() {}
|
||||||
void lock_shared() {}
|
void lock_shared() {}
|
||||||
void unlock_shared() {}
|
void unlock_shared() {}
|
||||||
void switch_to_unique() {}
|
bool switch_to_unique() { return false; }
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----------------------------------------------------
|
// ----------------------------------------------------
|
||||||
@@ -4711,7 +4711,7 @@ public:
|
|||||||
|
|
||||||
mutex_type *mutex() const noexcept { return m_; }
|
mutex_type *mutex() const noexcept { return m_; }
|
||||||
|
|
||||||
void switch_to_unique() {}
|
bool switch_to_unique() { return false; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutex_type *m_;
|
mutex_type *m_;
|
||||||
@@ -4791,7 +4791,7 @@ public:
|
|||||||
|
|
||||||
mutex_type *mutex() const noexcept { return m_; }
|
mutex_type *mutex() const noexcept { return m_; }
|
||||||
|
|
||||||
void switch_to_unique() {}
|
bool switch_to_unique() { return false; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutex_type *m_;
|
mutex_type *m_;
|
||||||
@@ -4873,10 +4873,11 @@ public:
|
|||||||
|
|
||||||
mutex_type *mutex() const noexcept { return m_; }
|
mutex_type *mutex() const noexcept { return m_; }
|
||||||
|
|
||||||
void switch_to_unique() {
|
bool switch_to_unique() {
|
||||||
assert(locked_shared_);
|
assert(locked_shared_);
|
||||||
unlock_shared();
|
unlock_shared();
|
||||||
lock();
|
lock();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user