summaryrefslogtreecommitdiff
path: root/src/mongo/util/concurrency/spin_lock_test.cpp
diff options
context:
space:
mode:
authorAmirsaman Memaripour <amirsaman.memaripour@10gen.com>2020-01-16 20:44:59 +0000
committerevergreen <evergreen@mongodb.com>2020-01-16 20:44:59 +0000
commit7ba6fede8aab0ab8a77515d7c11a017b213d8f93 (patch)
tree5b28127f9feefb4db1202a02c61e61371c647f37 /src/mongo/util/concurrency/spin_lock_test.cpp
parentbb6e5391a9591139804038b3524a1b92ad1c327b (diff)
downloadmongo-7ba6fede8aab0ab8a77515d7c11a017b213d8f93.tar.gz
SERVER-45212 Add try_lock() to SpinLock
Diffstat (limited to 'src/mongo/util/concurrency/spin_lock_test.cpp')
-rw-r--r--src/mongo/util/concurrency/spin_lock_test.cpp79
1 files changed, 57 insertions, 22 deletions
diff --git a/src/mongo/util/concurrency/spin_lock_test.cpp b/src/mongo/util/concurrency/spin_lock_test.cpp
index 62266221b2a..f9f760604ff 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) {}
- ~LockTester() {
+ virtual ~LockTester() {
delete _t;
}
@@ -72,7 +72,7 @@ private:
void test(int increments) {
while (increments-- > 0) {
- _spin->lock();
+ acquireLock();
++(*_counter);
++_requests;
_spin->unlock();
@@ -81,35 +81,70 @@ 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) {}
-TEST(Concurrency, ConcurrentIncs) {
- SpinLock spin;
- int counter = 0;
+protected:
+ void acquireLock() override {
+ while (!spin()->try_lock()) {
+ }
+ }
+};
- const int threads = 64;
- const int incs = 50000;
- LockTester* testers[threads];
+class SpinLockTester {
+public:
+ template <class T>
+ void run(std::string testName) {
+ SpinLock spin;
+ int counter = 0;
- Timer timer;
+ std::vector<T*> testers(_threads);
+ Timer timer;
- 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];
+ 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);
}
- int ms = timer.millis();
- mongo::unittest::log() << "spinlock ConcurrentIncs time: " << ms << std::endl;
+private:
+ const int _threads = 64;
+ const int _incs = 50000;
+};
+
+
+TEST(Concurrency, ConcurrentIncs) {
+ SpinLockTester tester;
+ tester.run<LockTester>("ConcurrentIncs");
+}
- ASSERT_EQUALS(counter, threads * incs);
+TEST(Concurrency, ConcurrentIncsWithTryLock) {
+ SpinLockTester tester;
+ tester.run<TryLockTester>("ConcurrentIncsWithTryLock");
}
} // namespace