diff options
author | Amirsaman Memaripour <amirsaman.memaripour@10gen.com> | 2020-01-14 23:23:37 +0000 |
---|---|---|
committer | A. Jesse Jiryu Davis <jesse@mongodb.com> | 2020-01-27 15:40:33 -0500 |
commit | 30d38ea4e047f9d801ea316d002631301dcee2dd (patch) | |
tree | b95081aa0d2512cd6c78c40e94ab5ad96dae4069 | |
parent | 93586b0b07cbcda28e3e71504581cba273f07ac8 (diff) | |
download | mongo-30d38ea4e047f9d801ea316d002631301dcee2dd.tar.gz |
Revert "SERVER-45212 Add try_lock() to SpinLock"
-rw-r--r-- | src/mongo/db/client.h | 3 | ||||
-rw-r--r-- | src/mongo/util/concurrency/spin_lock.h | 30 | ||||
-rw-r--r-- | src/mongo/util/concurrency/spin_lock_test.cpp | 79 |
3 files changed, 31 insertions, 81 deletions
diff --git a/src/mongo/db/client.h b/src/mongo/db/client.h index 64c0e7407c7..6aea69b5c2a 100644 --- a/src/mongo/db/client.h +++ b/src/mongo/db/client.h @@ -152,9 +152,6 @@ public: void unlock() { _lock.unlock(); } - bool try_lock() { - return _lock.try_lock(); - } /** * Makes a new operation context representing an operation on this client. At most diff --git a/src/mongo/util/concurrency/spin_lock.h b/src/mongo/util/concurrency/spin_lock.h index 380ad9f7c4e..5c5a17b4b74 100644 --- a/src/mongo/util/concurrency/spin_lock.h +++ b/src/mongo/util/concurrency/spin_lock.h @@ -42,7 +42,7 @@ namespace mongo { #if defined(_WIN32) -class SpinLock : public Latch { +class SpinLock { SpinLock(const SpinLock&) = delete; SpinLock& operator=(const SpinLock&) = delete; @@ -55,18 +55,14 @@ public: DeleteCriticalSection(&_cs); } - void lock() override { + void lock() { EnterCriticalSection(&_cs); } - void unlock() override { + void unlock() { LeaveCriticalSection(&_cs); } - bool try_lock() override { - return TryEnterCriticalSection(&_cs); - } - private: CRITICAL_SECTION _cs; }; @@ -74,52 +70,44 @@ private: #else #if MONGO_CONFIG_DEBUG_BUILD -class SpinLock : public Latch { +class SpinLock { SpinLock(const SpinLock&) = delete; SpinLock& operator=(const SpinLock&) = delete; public: SpinLock() = default; - void lock() override { + void lock() { _mutex.lock(); } - void unlock() override { + void unlock() { _mutex.unlock(); } - bool try_lock() override { - return _mutex.try_lock(); - } - private: stdx::mutex _mutex; // NOLINT }; #else -class SpinLock : public Latch { +class SpinLock { SpinLock(const SpinLock&) = delete; SpinLock& operator=(const SpinLock&) = delete; public: SpinLock() = default; - void unlock() override { + void unlock() { _locked.clear(std::memory_order_release); } - void lock() override { + void lock() { if (MONGO_likely(_tryLock())) return; _lockSlowPath(); } - bool try_lock() override { - return _tryLock(); - } - private: bool _tryLock() { bool wasLocked = _locked.test_and_set(std::memory_order_acquire); diff --git a/src/mongo/util/concurrency/spin_lock_test.cpp b/src/mongo/util/concurrency/spin_lock_test.cpp index 005091c499b..62266221b2a 100644 --- a/src/mongo/util/concurrency/spin_lock_test.cpp +++ b/src/mongo/util/concurrency/spin_lock_test.cpp @@ -47,7 +47,7 @@ class LockTester { public: LockTester(SpinLock* spin, int* counter) : _spin(spin), _counter(counter), _requests(0) {} - virtual ~LockTester() { + ~LockTester() { delete _t; } @@ -72,7 +72,7 @@ private: void test(int increments) { while (increments-- > 0) { - acquireLock(); + _spin->lock(); ++(*_counter); ++_requests; _spin->unlock(); @@ -81,70 +81,35 @@ private: LockTester(LockTester&); LockTester& operator=(LockTester&); - -protected: - SpinLock* spin() const { - return _spin; - } - - virtual void acquireLock() { - spin()->lock(); - } }; -class TryLockTester final : public LockTester { -public: - TryLockTester(SpinLock* spin, int* counter) : LockTester(spin, counter) {} -protected: - void acquireLock() override { - while (!spin()->try_lock()) { - } - } -}; +TEST(Concurrency, ConcurrentIncs) { + SpinLock spin; + int counter = 0; -class SpinLockTester { -public: - template <class T> - void run(std::string testName) { - SpinLock spin; - int counter = 0; + const int threads = 64; + const int incs = 50000; + LockTester* testers[threads]; - T* testers[_threads]; - Timer timer; + Timer timer; - for (int i = 0; i < _threads; i++) { - testers[i] = new T(&spin, &counter); - } - for (int i = 0; i < _threads; i++) { - testers[i]->start(_incs); - } - for (int i = 0; i < _threads; i++) { - testers[i]->join(); - ASSERT_EQUALS(testers[i]->requests(), _incs); - delete testers[i]; - } - - int ms = timer.millis(); - mongo::unittest::log() << "spinlock " << testName << " time: " << ms << std::endl; - - ASSERT_EQUALS(counter, _threads * _incs); + for (int i = 0; i < threads; i++) { + testers[i] = new LockTester(&spin, &counter); + } + for (int i = 0; i < threads; i++) { + testers[i]->start(incs); + } + for (int i = 0; i < threads; i++) { + testers[i]->join(); + ASSERT_EQUALS(testers[i]->requests(), incs); + delete testers[i]; } -private: - const int _threads = 64; - const int _incs = 50000; -}; - - -TEST(Concurrency, ConcurrentIncs) { - SpinLockTester tester; - tester.run<LockTester>("ConcurrentIncs"); -} + int ms = timer.millis(); + mongo::unittest::log() << "spinlock ConcurrentIncs time: " << ms << std::endl; -TEST(Concurrency, ConcurrentIncsWithTryLock) { - SpinLockTester tester; - tester.run<TryLockTester>("ConcurrentIncsWithTryLock"); + ASSERT_EQUALS(counter, threads * incs); } } // namespace |