improve Synchronized LockedPtr class, and add new lock() APIs
[folly.git] / folly / test / SynchronizedTestLib-inl.h
index 9c380bcedb59418871bdbb6bc9e94f7e0bdc7849..3166de8ac4c63d75ad389228cd507a1f624aa5f2 100644 (file)
@@ -60,30 +60,22 @@ void runParallel(size_t numThreads, const Function& function) {
 
   // Variables used to synchronize all threads to try and start them
   // as close to the same time as possible
-  //
-  // TODO: At the moment Synchronized doesn't work with condition variables.
-  // Update this to use Synchronized once the condition_variable support lands.
-  std::mutex threadsReadyMutex;
-  size_t threadsReady = 0;
+  folly::Synchronized<size_t, std::mutex> threadsReady(0);
   std::condition_variable readyCV;
-  std::mutex goMutex;
-  bool go = false;
+  folly::Synchronized<bool, std::mutex> go(false);
   std::condition_variable goCV;
 
   auto worker = [&](size_t threadIndex) {
     // Signal that we are ready
-    {
-      std::lock_guard<std::mutex> lock(threadsReadyMutex);
-      ++threadsReady;
-    }
+    ++(*threadsReady.lock());
     readyCV.notify_one();
 
     // Wait until we are given the signal to start
     // The purpose of this is to try and make sure all threads start
     // as close to the same time as possible.
     {
-      std::unique_lock<std::mutex> lock(goMutex);
-      goCV.wait(lock, [&] { return go; });
+      auto lockedGo = go.lock();
+      goCV.wait(lockedGo.getUniqueLock(), [&] { return *lockedGo; });
     }
 
     function(threadIndex);
@@ -96,14 +88,13 @@ void runParallel(size_t numThreads, const Function& function) {
 
   // Wait for all threads to become ready
   {
-    std::unique_lock<std::mutex> lock(threadsReadyMutex);
-    readyCV.wait(lock, [&] { return threadsReady == numThreads; });
-  }
-  {
-    std::lock_guard<std::mutex> lock(goMutex);
-    go = true;
+    auto readyLocked = threadsReady.lock();
+    readyCV.wait(readyLocked.getUniqueLock(), [&] {
+      return *readyLocked == numThreads;
+    });
   }
   // Now signal the threads that they can go
+  go = true;
   goCV.notify_all();
 
   // Wait for all threads to finish
@@ -112,8 +103,99 @@ void runParallel(size_t numThreads, const Function& function) {
   }
 }
 
+// testBasic() version for shared lock types
+template <class Mutex>
+typename std::enable_if<folly::LockTraits<Mutex>::is_shared>::type
+testBasicImpl() {
+  folly::Synchronized<std::vector<int>, Mutex> obj;
+
+  obj.wlock()->resize(1000);
+
+  folly::Synchronized<std::vector<int>, Mutex> obj2{*obj.wlock()};
+  EXPECT_EQ(1000, obj2.rlock()->size());
+
+  {
+    auto lockedObj = obj.wlock();
+    lockedObj->push_back(10);
+    EXPECT_EQ(1001, lockedObj->size());
+    EXPECT_EQ(10, lockedObj->back());
+    EXPECT_EQ(1000, obj2.wlock()->size());
+    EXPECT_EQ(1000, obj2.rlock()->size());
+
+    {
+      auto unlocker = lockedObj.scopedUnlock();
+      EXPECT_EQ(1001, obj.wlock()->size());
+    }
+  }
+
+  {
+    auto lockedObj = obj.rlock();
+    EXPECT_EQ(1001, lockedObj->size());
+    EXPECT_EQ(1001, obj.rlock()->size());
+    {
+      auto unlocker = lockedObj.scopedUnlock();
+      EXPECT_EQ(1001, obj.wlock()->size());
+    }
+  }
+
+  obj.wlock()->front() = 2;
+
+  {
+    const auto& constObj = obj;
+    // contextualLock() on a const reference should grab a shared lock
+    auto lockedObj = constObj.contextualLock();
+    EXPECT_EQ(2, lockedObj->front());
+    EXPECT_EQ(2, constObj.rlock()->front());
+    EXPECT_EQ(2, obj.rlock()->front());
+  }
+
+  EXPECT_EQ(1001, obj.rlock()->size());
+  EXPECT_EQ(2, obj.rlock()->front());
+  EXPECT_EQ(10, obj.rlock()->back());
+  EXPECT_EQ(1000, obj2.rlock()->size());
+}
+
+// testBasic() version for non-shared lock types
+template <class Mutex>
+typename std::enable_if<!folly::LockTraits<Mutex>::is_shared>::type
+testBasicImpl() {
+  folly::Synchronized<std::vector<int>, Mutex> obj;
+
+  obj.lock()->resize(1000);
+
+  folly::Synchronized<std::vector<int>, Mutex> obj2{*obj.lock()};
+  EXPECT_EQ(1000, obj2.lock()->size());
+
+  {
+    auto lockedObj = obj.lock();
+    lockedObj->push_back(10);
+    EXPECT_EQ(1001, lockedObj->size());
+    EXPECT_EQ(10, lockedObj->back());
+    EXPECT_EQ(1000, obj2.lock()->size());
+
+    {
+      auto unlocker = lockedObj.scopedUnlock();
+      EXPECT_EQ(1001, obj.lock()->size());
+    }
+  }
+
+  obj.lock()->front() = 2;
+
+  EXPECT_EQ(1001, obj.lock()->size());
+  EXPECT_EQ(2, obj.lock()->front());
+  EXPECT_EQ(2, obj.contextualLock()->front());
+  EXPECT_EQ(10, obj.lock()->back());
+  EXPECT_EQ(1000, obj2.lock()->size());
+}
+
 template <class Mutex>
 void testBasic() {
+  testBasicImpl<Mutex>();
+}
+
+// Testing the deprecated SYNCHRONIZED and SYNCHRONIZED_CONST APIs
+template <class Mutex>
+void testDeprecated() {
   folly::Synchronized<std::vector<int>, Mutex> obj;
 
   obj->resize(1000);
@@ -163,7 +245,7 @@ template <class Mutex> void testConcurrency() {
   auto pushNumbers = [&](size_t threadIdx) {
     // Test lock()
     for (size_t n = 0; n < itersPerThread; ++n) {
-      v->push_back((itersPerThread * threadIdx) + n);
+      v.contextualLock()->push_back((itersPerThread * threadIdx) + n);
       sched_yield();
     }
   };
@@ -180,6 +262,71 @@ template <class Mutex> void testConcurrency() {
   }
 }
 
+template <class Mutex>
+void testAcquireLocked() {
+  folly::Synchronized<std::vector<int>, Mutex> v;
+  folly::Synchronized<std::map<int, int>, Mutex> m;
+
+  auto dualLockWorker = [&](size_t threadIdx) {
+    // Note: this will be less awkward with C++ 17's structured
+    // binding functionality, which will make it easier to use the returned
+    // std::tuple.
+    if (threadIdx & 1) {
+      auto ret = acquireLocked(v, m);
+      std::get<0>(ret)->push_back(threadIdx);
+      (*std::get<1>(ret))[threadIdx] = threadIdx + 1;
+    } else {
+      auto ret = acquireLocked(m, v);
+      std::get<1>(ret)->push_back(threadIdx);
+      (*std::get<0>(ret))[threadIdx] = threadIdx + 1;
+    }
+  };
+  static const size_t numThreads = 100;
+  runParallel(numThreads, dualLockWorker);
+
+  std::vector<int> result;
+  v.swap(result);
+
+  EXPECT_EQ(numThreads, result.size());
+  sort(result.begin(), result.end());
+
+  for (size_t i = 0; i < numThreads; ++i) {
+    EXPECT_EQ(i, result[i]);
+  }
+}
+
+template <class Mutex>
+void testAcquireLockedWithConst() {
+  folly::Synchronized<std::vector<int>, Mutex> v;
+  folly::Synchronized<std::map<int, int>, Mutex> m;
+
+  auto dualLockWorker = [&](size_t threadIdx) {
+    const auto& cm = m;
+    if (threadIdx & 1) {
+      auto ret = acquireLocked(v, cm);
+      (void)std::get<1>(ret)->size();
+      std::get<0>(ret)->push_back(threadIdx);
+    } else {
+      auto ret = acquireLocked(cm, v);
+      (void)std::get<0>(ret)->size();
+      std::get<1>(ret)->push_back(threadIdx);
+    }
+  };
+  static const size_t numThreads = 100;
+  runParallel(numThreads, dualLockWorker);
+
+  std::vector<int> result;
+  v.swap(result);
+
+  EXPECT_EQ(numThreads, result.size());
+  sort(result.begin(), result.end());
+
+  for (size_t i = 0; i < numThreads; ++i) {
+    EXPECT_EQ(i, result[i]);
+  }
+}
+
+// Testing the deprecated SYNCHRONIZED_DUAL API
 template <class Mutex> void testDualLocking() {
   folly::Synchronized<std::vector<int>, Mutex> v;
   folly::Synchronized<std::map<int, int>, Mutex> m;
@@ -211,6 +358,7 @@ template <class Mutex> void testDualLocking() {
   }
 }
 
+// Testing the deprecated SYNCHRONIZED_DUAL API
 template <class Mutex> void testDualLockingWithConst() {
   folly::Synchronized<std::vector<int>, Mutex> v;
   folly::Synchronized<std::map<int, int>, Mutex> m;
@@ -243,6 +391,117 @@ template <class Mutex> void testDualLockingWithConst() {
   }
 }
 
+template <class Mutex>
+void testTimed() {
+  folly::Synchronized<std::vector<int>, Mutex> v;
+  folly::Synchronized<uint64_t, Mutex> numTimeouts;
+
+  auto worker = [&](size_t threadIdx) {
+    // Test directly using operator-> on the lock result
+    v.contextualLock()->push_back(2 * threadIdx);
+
+    // Test using lock with a timeout
+    for (;;) {
+      auto lv = v.contextualLock(std::chrono::milliseconds(5));
+      if (!lv) {
+        ++(*numTimeouts.contextualLock());
+        continue;
+      }
+
+      // Sleep for a random time to ensure we trigger timeouts
+      // in other threads
+      randomSleep(std::chrono::milliseconds(5), std::chrono::milliseconds(15));
+      lv->push_back(2 * threadIdx + 1);
+      break;
+    }
+  };
+
+  static const size_t numThreads = 100;
+  runParallel(numThreads, worker);
+
+  std::vector<int> result;
+  v.swap(result);
+
+  EXPECT_EQ(2 * numThreads, result.size());
+  sort(result.begin(), result.end());
+
+  for (size_t i = 0; i < 2 * numThreads; ++i) {
+    EXPECT_EQ(i, result[i]);
+  }
+  // We generally expect a large number of number timeouts here.
+  // I'm not adding a check for it since it's theoretically possible that
+  // we might get 0 timeouts depending on the CPU scheduling if our threads
+  // don't get to run very often.
+  LOG(INFO) << "testTimed: " << *numTimeouts.contextualRLock() << " timeouts";
+
+  // Make sure we can lock with various timeout duration units
+  {
+    auto lv = v.contextualLock(std::chrono::milliseconds(5));
+    EXPECT_TRUE(lv);
+    EXPECT_FALSE(lv.isNull());
+    auto lv2 = v.contextualLock(std::chrono::microseconds(5));
+    // We may or may not acquire lv2 successfully, depending on whether
+    // or not this is a recursive mutex type.
+  }
+  {
+    auto lv = v.contextualLock(std::chrono::seconds(1));
+    EXPECT_TRUE(lv);
+  }
+}
+
+template <class Mutex>
+void testTimedShared() {
+  folly::Synchronized<std::vector<int>, Mutex> v;
+  folly::Synchronized<uint64_t, Mutex> numTimeouts;
+
+  auto worker = [&](size_t threadIdx) {
+    // Test directly using operator-> on the lock result
+    v.wlock()->push_back(threadIdx);
+
+    // Test lock() with a timeout
+    for (;;) {
+      auto lv = v.rlock(std::chrono::milliseconds(10));
+      if (!lv) {
+        ++(*numTimeouts.contextualLock());
+        continue;
+      }
+
+      // Sleep while holding the lock.
+      //
+      // This will block other threads from acquiring the write lock to add
+      // their thread index to v, but it won't block threads that have entered
+      // the for loop and are trying to acquire a read lock.
+      //
+      // For lock types that give preference to readers rather than writers,
+      // this will tend to serialize all threads on the wlock() above.
+      randomSleep(std::chrono::milliseconds(5), std::chrono::milliseconds(15));
+      auto found = std::find(lv->begin(), lv->end(), threadIdx);
+      CHECK(found != lv->end());
+      break;
+    }
+  };
+
+  static const size_t numThreads = 100;
+  runParallel(numThreads, worker);
+
+  std::vector<int> result;
+  v.swap(result);
+
+  EXPECT_EQ(numThreads, result.size());
+  sort(result.begin(), result.end());
+
+  for (size_t i = 0; i < numThreads; ++i) {
+    EXPECT_EQ(i, result[i]);
+  }
+  // We generally expect a small number of timeouts here.
+  // For locks that give readers preference over writers this should usually
+  // be 0.  With locks that give writers preference we do see a small-ish
+  // number of read timeouts.
+  LOG(INFO) << "testTimedShared: " << *numTimeouts.contextualRLock()
+            << " timeouts";
+}
+
+// Testing the deprecated TIMED_SYNCHRONIZED API
 template <class Mutex> void testTimedSynchronized() {
   folly::Synchronized<std::vector<int>, Mutex> v;
   folly::Synchronized<uint64_t, Mutex> numTimeouts;
@@ -263,9 +522,7 @@ template <class Mutex> void testTimedSynchronized() {
           return;
         }
 
-        SYNCHRONIZED(numTimeouts) {
-          ++numTimeouts;
-        }
+        ++(*numTimeouts.contextualLock());
       }
   };
 
@@ -285,13 +542,11 @@ template <class Mutex> void testTimedSynchronized() {
   // I'm not adding a check for it since it's theoretically possible that
   // we might get 0 timeouts depending on the CPU scheduling if our threads
   // don't get to run very often.
-  uint64_t finalNumTimeouts = 0;
-  SYNCHRONIZED(numTimeouts) {
-    finalNumTimeouts = numTimeouts;
-  }
-  LOG(INFO) << "testTimedSynchronized: " << finalNumTimeouts << " timeouts";
+  LOG(INFO) << "testTimedSynchronized: " << *numTimeouts.contextualRLock()
+            << " timeouts";
 }
 
+// Testing the deprecated TIMED_SYNCHRONIZED_CONST API
 template <class Mutex> void testTimedSynchronizedWithConst() {
   folly::Synchronized<std::vector<int>, Mutex> v;
   folly::Synchronized<uint64_t, Mutex> numTimeouts;
@@ -318,9 +573,7 @@ template <class Mutex> void testTimedSynchronizedWithConst() {
           CHECK(found != lv->end());
           return;
         } else {
-          SYNCHRONIZED(numTimeouts) {
-            ++numTimeouts;
-          }
+          ++(*numTimeouts.contextualLock());
         }
       }
     }
@@ -342,12 +595,8 @@ template <class Mutex> void testTimedSynchronizedWithConst() {
   // For locks that give readers preference over writers this should usually
   // be 0.  With locks that give writers preference we do see a small-ish
   // number of read timeouts.
-  uint64_t finalNumTimeouts = 0;
-  SYNCHRONIZED(numTimeouts) {
-    finalNumTimeouts = numTimeouts;
-  }
-  LOG(INFO) << "testTimedSynchronizedWithConst: " << finalNumTimeouts
-            << " timeouts";
+  LOG(INFO) << "testTimedSynchronizedWithConst: "
+            << *numTimeouts.contextualRLock() << " timeouts";
 }
 
 template <class Mutex> void testConstCopy() {