From 4c93cf00f2f9e25e2b1cc41d6dff49f2f211620e Mon Sep 17 00:00:00 2001 From: Gregory Popovitch Date: Wed, 29 Nov 2023 13:16:09 -0500 Subject: [PATCH] Remove the `Upgrade` lock stuff. (#221) * Remove the `Upgrade` lock stuff. * Need to repeat the `find()` if we released the mutex to get a write lock. * Add a couple `assert`s. --- parallel_hashmap/phmap.h | 8 +++++--- parallel_hashmap/phmap_base.h | 31 ++++++++++--------------------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/parallel_hashmap/phmap.h b/parallel_hashmap/phmap.h index 4b1e95b..b10a0e2 100644 --- a/parallel_hashmap/phmap.h +++ b/parallel_hashmap/phmap.h @@ -3465,12 +3465,14 @@ public: auto hashval = this->hash(key); Inner& inner = sets_[subidx(hashval)]; auto& set = inner.set_; - typename Lockable::UpgradeLock m(inner); + typename Lockable::ReadWriteLock m(inner); auto it = set.find(key, hashval); if (it == set.end()) return 0; - - typename Lockable::UpgradeToUnique unique(m); + + if (m.switch_to_unique()) { + it = set.find(key, hashval); + } set._erase(it); return 1; } diff --git a/parallel_hashmap/phmap_base.h b/parallel_hashmap/phmap_base.h index 7f0e26a..d90d236 100644 --- a/parallel_hashmap/phmap_base.h +++ b/parallel_hashmap/phmap_base.h @@ -4834,7 +4834,8 @@ public: m_->unlock(); } - void lock_shared() { + void lock_shared() { + assert(!locked_); if (!locked_shared_) { m_->lock_shared(); locked_shared_ = true; @@ -4848,7 +4849,8 @@ public: } } - void lock() { + void lock() { + assert(!locked_shared_); if (!locked_) { m_->lock(); locked_ = true; @@ -4955,12 +4957,11 @@ public: // using Lockable = phmap::LockableImpl; // Lockable m; // -// Lockable::UpgradeLock read_lock(m); // take a upgradable lock -// -// { -// Lockable::UpgradeToUnique unique_lock(read_lock); -// // now locked for write -// } +// Lockable::ReadWriteLock read_lock(m); // take a lock (read if supported, otherwise write) +// ... do something +// +// m.switch_to_unique(); // returns true if we had a read lock and switched to write +// // now locked for write // // --------------------------------------------------------------------------- // Generic mutex support (always write locks) @@ -4972,12 +4973,10 @@ public: using mutex_type = Mtx_; using Base = LockableBaseImpl; using SharedLock = typename Base::WriteLock; - using UpgradeLock = typename Base::WriteLock; using UniqueLock = typename Base::WriteLock; using ReadWriteLock = typename Base::WriteLock; using SharedLocks = typename Base::WriteLocks; using UniqueLocks = typename Base::WriteLocks; - using UpgradeToUnique = typename Base::DoNothing; // we already have unique ownership }; // --------------------------------------------------------------------------- @@ -4990,10 +4989,8 @@ public: using mutex_type = phmap::NullMutex; using Base = LockableBaseImpl; using SharedLock = typename Base::DoNothing; - using UpgradeLock = typename Base::DoNothing; using ReadWriteLock = typename Base::DoNothing; using UniqueLock = typename Base::DoNothing; - using UpgradeToUnique = typename Base::DoNothing; using SharedLocks = typename Base::DoNothing; using UniqueLocks = typename Base::DoNothing; }; @@ -5022,11 +5019,9 @@ public: using Base = LockableBaseImpl; using SharedLock = typename Base::ReadLock; using ReadWriteLock = typename Base::ReadWriteLock; - using UpgradeLock = typename Base::WriteLock; using UniqueLock = typename Base::WriteLock; using SharedLocks = typename Base::ReadLocks; using UniqueLocks = typename Base::WriteLocks; - using UpgradeToUnique = typename Base::DoNothing; // we already have unique ownership }; #endif @@ -5035,7 +5030,7 @@ public: // Microsoft SRWLOCK support (read and write lock support) // use: `phmap::srwlock` instead of `std::mutex` // -------------------------------------------------------------------------- -#if defined(_MSVC_LANG) && defined(SRWLOCK_INIT) +#if defined(_MSC_VER) && defined(SRWLOCK_INIT) class srwlock { SRWLOCK _lock; @@ -5058,11 +5053,9 @@ public: using Base = LockableBaseImpl; using SharedLock = typename Base::ReadLock; using ReadWriteLock = typename Base::ReadWriteLock; - using UpgradeLock = typename Base::WriteLock; using UniqueLock = typename Base::WriteLock; using SharedLocks = typename Base::ReadLocks; using UniqueLocks = typename Base::WriteLocks; - using UpgradeToUnique = typename Base::DoNothing; // we already have unique ownership }; #endif @@ -5081,11 +5074,9 @@ public: using Base = LockableBaseImpl; using SharedLock = boost::shared_lock; using ReadWriteLock = typename Base::ReadWriteLock; - using UpgradeLock = boost::unique_lock; // assume can't upgrade using UniqueLock = boost::unique_lock; using SharedLocks = typename Base::ReadLocks; using UniqueLocks = typename Base::WriteLocks; - using UpgradeToUnique = typename Base::DoNothing; // we already have unique ownership }; #endif // BOOST_THREAD_SHARED_MUTEX_HPP @@ -5104,11 +5095,9 @@ public: using Base = LockableBaseImpl; using SharedLock = std::shared_lock; using ReadWriteLock = typename Base::ReadWriteLock; - using UpgradeLock = std::unique_lock; // assume can't upgrade using UniqueLock = std::unique_lock; using SharedLocks = typename Base::ReadLocks; using UniqueLocks = typename Base::WriteLocks; - using UpgradeToUnique = typename Base::DoNothing; // we already have unique ownership }; #endif // PHMAP_HAVE_SHARED_MUTEX