Add InlineExecutor.cpp to Makefile.am
[folly.git] / folly / test / RWSpinLockTest.cpp
index eb6809a02243601beb3fe237d02d0bc23ba7f71c..385f2d2b8212665016ebc4c8e3b22f97d0a61c9a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 // @author xliu (xliux@fb.com)
 //
 
+#include <folly/RWSpinLock.h>
+
 #include <stdlib.h>
-#include <unistd.h>
-#include <vector>
 #include <thread>
+#include <vector>
 
-#include "gtest/gtest.h"
-#include <gflags/gflags.h>
 #include <glog/logging.h>
-#include "folly/RWSpinLock.h"
+
+#include <folly/portability/GFlags.h>
+#include <folly/portability/GTest.h>
+#include <folly/portability/Unistd.h>
 
 DEFINE_int32(num_threads, 8, "num threads");
 
@@ -36,13 +38,12 @@ static const int kMaxReaders = 50;
 static std::atomic<bool> stopThread;
 using namespace folly;
 
-template<typename RWSpinLockT> struct RWSpinLockTest: public testing::Test {
+template <typename RWSpinLockT> struct RWSpinLockTest: public testing::Test {
   typedef RWSpinLockT RWSpinLockType;
 };
 
 typedef testing::Types<RWSpinLock
-#if defined(__GNUC__) && (defined(__i386) || defined(__x86_64__) || \
-    defined(ARCH_K8))
+#ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
         , RWTicketSpinLockT<32, true>,
         RWTicketSpinLockT<32, false>,
         RWTicketSpinLockT<64, true>,
@@ -52,7 +53,7 @@ typedef testing::Types<RWSpinLock
 
 TYPED_TEST_CASE(RWSpinLockTest, Implementations);
 
-template<typename RWSpinLockType>
+template <typename RWSpinLockType>
 static void run(RWSpinLockType* lock) {
   int64_t reads = 0;
   int64_t writes = 0;
@@ -149,7 +150,7 @@ TYPED_TEST(RWSpinLockTest, Write_Holders) {
 TYPED_TEST(RWSpinLockTest, ConcurrentTests) {
   typedef typename TestFixture::RWSpinLockType RWSpinLockType;
   RWSpinLockType l;
-  srand(time(NULL));
+  srand(time(nullptr));
 
   std::vector<std::thread> threads;
   for (int i = 0; i < FLAGS_num_threads; ++i) {
@@ -169,10 +170,11 @@ TYPED_TEST(RWSpinLockTest, ConcurrentTests) {
 TEST(RWSpinLock, lock_unlock_tests) {
   folly::RWSpinLock lock;
   EXPECT_TRUE(lock.try_lock_upgrade());
-  EXPECT_TRUE(lock.try_lock_shared());
+  EXPECT_FALSE(lock.try_lock_shared());
   EXPECT_FALSE(lock.try_lock());
   EXPECT_FALSE(lock.try_lock_upgrade());
   lock.unlock_upgrade();
+  lock.lock_shared();
   EXPECT_FALSE(lock.try_lock());
   EXPECT_TRUE(lock.try_lock_upgrade());
   lock.unlock_upgrade();
@@ -180,15 +182,14 @@ TEST(RWSpinLock, lock_unlock_tests) {
   EXPECT_TRUE(lock.try_lock());
   EXPECT_FALSE(lock.try_lock_upgrade());
   lock.unlock_and_lock_upgrade();
-  EXPECT_TRUE(lock.try_lock_shared());
-  lock.unlock_shared();
+  EXPECT_FALSE(lock.try_lock_shared());
   lock.unlock_upgrade_and_lock_shared();
   lock.unlock_shared();
   EXPECT_EQ(0, lock.bits());
 }
 
 TEST(RWSpinLock, concurrent_holder_test) {
-  srand(time(NULL));
+  srand(time(nullptr));
 
   folly::RWSpinLock lock;
   std::atomic<int64_t> reads(0);
@@ -200,11 +201,10 @@ TEST(RWSpinLock, concurrent_holder_test) {
     while (!stop.load(std::memory_order_acquire)) {
       auto r = (uint32_t)(rand()) % 10;
       if (r < 3) {          // starts from write lock
-        RWSpinLock::ReadHolder rg(
-            RWSpinLock::UpgradedHolder ug(
-              RWSpinLock::WriteHolder(&lock)));
+        RWSpinLock::ReadHolder rg{
+          RWSpinLock::UpgradedHolder{
+            RWSpinLock::WriteHolder{&lock}}};
         writes.fetch_add(1, std::memory_order_acq_rel);;
-
       } else if (r < 6) {   // starts from upgrade lock
         RWSpinLock::UpgradedHolder ug(&lock);
         if (r < 4) {
@@ -214,9 +214,7 @@ TEST(RWSpinLock, concurrent_holder_test) {
         }
         upgrades.fetch_add(1, std::memory_order_acq_rel);;
       } else {
-        RWSpinLock::UpgradedHolder ug(
-            RWSpinLock::WriteHolder(
-              RWSpinLock::ReadHolder(&lock)));
+        RWSpinLock::ReadHolder rg{&lock};
         reads.fetch_add(1, std::memory_order_acq_rel);
       }
     }
@@ -230,7 +228,9 @@ TEST(RWSpinLock, concurrent_holder_test) {
   sleep(5);
   stop.store(true, std::memory_order_release);
 
-  for (auto& t : threads) t.join();
+  for (auto& t : threads) {
+    t.join();
+  }
 
   LOG(INFO) << "reads: " << reads.load(std::memory_order_acquire)
     << "; writes: " << writes.load(std::memory_order_acquire)
@@ -238,9 +238,3 @@ TEST(RWSpinLock, concurrent_holder_test) {
 }
 
 }
-
-int main(int argc, char** argv) {
-  testing::InitGoogleTest(&argc, argv);
-  google::ParseCommandLineFlags(&argc, &argv, true);
-  return RUN_ALL_TESTS();
-}