Add Baton variants with multiple posters and with a non-blocking waiter
[folly.git] / folly / test / RWSpinLockTest.cpp
index 6b58c9a1f92a94b96bcc71f79f317772f04e71d7..1f9c9eef91cc5f98395e8c7c4fe2f0975d661191 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 "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");
 
@@ -41,8 +43,7 @@ template<typename RWSpinLockT> struct RWSpinLockTest: public testing::Test {
 };
 
 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>,
@@ -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) {
@@ -188,7 +189,7 @@ TEST(RWSpinLock, lock_unlock_tests) {
 }
 
 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);
       }
     }
@@ -238,9 +236,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();
-}