Consistently have the namespace closing comment
[folly.git] / folly / test / SmallLocksTest.cpp
index d7e27578adca527c95806406b6cf09083916664a..32a20d77fa3cc54f31ac2ba6b4655e0c5f4381e9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 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.
 
 #include <folly/SmallLocks.h>
 
-#include <folly/Random.h>
-
 #include <cassert>
+#include <condition_variable>
 #include <cstdio>
 #include <mutex>
-#include <condition_variable>
 #include <string>
+#include <thread>
 #include <vector>
-#include <pthread.h>
-#include <unistd.h>
 
-#include <thread>
+#include <glog/logging.h>
 
-#include <gtest/gtest.h>
+#include <folly/Random.h>
+#include <folly/portability/Asm.h>
+#include <folly/portability/GTest.h>
+#include <folly/portability/PThread.h>
+#include <folly/portability/Unistd.h>
 
 using folly::MSLGuard;
 using folly::MicroLock;
 using folly::MicroSpinLock;
-using folly::PicoSpinLock;
 using std::string;
 
+#ifdef FOLLY_PICO_SPIN_LOCK_H_
+using folly::PicoSpinLock;
+#endif
+
 namespace {
 
 struct LockedVal {
@@ -53,10 +57,12 @@ struct LockedVal {
 // these classes are POD).
 FOLLY_PACK_PUSH
 struct ignore1 { MicroSpinLock msl; int16_t foo; } FOLLY_PACK_ATTR;
-struct ignore2 { PicoSpinLock<uint32_t> psl; int16_t foo; } FOLLY_PACK_ATTR;
 static_assert(sizeof(ignore1) == 3, "Size check failed");
-static_assert(sizeof(ignore2) == 6, "Size check failed");
 static_assert(sizeof(MicroSpinLock) == 1, "Size check failed");
+#ifdef FOLLY_PICO_SPIN_LOCK_H_
+struct ignore2 { PicoSpinLock<uint32_t> psl; int16_t foo; } FOLLY_PACK_ATTR;
+static_assert(sizeof(ignore2) == 6, "Size check failed");
+#endif
 FOLLY_PACK_POP
 
 LockedVal v;
@@ -65,12 +71,12 @@ void splock_test() {
   const int max = 1000;
   auto rng = folly::ThreadLocalPRNG();
   for (int i = 0; i < max; i++) {
-    folly::asm_pause();
+    folly::asm_volatile_pause();
     MSLGuard g(v.lock);
 
     int first = v.ar[0];
-    for (size_t i = 1; i < sizeof v.ar / sizeof i; ++i) {
-      EXPECT_EQ(first, v.ar[i]);
+    for (size_t j = 1; j < sizeof v.ar / sizeof j; ++j) {
+      EXPECT_EQ(first, v.ar[j]);
     }
 
     int byte = folly::Random::rand32(rng);
@@ -78,14 +84,16 @@ void splock_test() {
   }
 }
 
-template<class T> struct PslTest {
+#ifdef FOLLY_PICO_SPIN_LOCK_H_
+template <class T> struct PslTest {
   PicoSpinLock<T> lock;
 
   PslTest() { lock.init(); }
 
   void doTest() {
-    T ourVal = rand() % (T(1) << (sizeof(T) * 8 - 1));
-    for (int i = 0; i < 10000; ++i) {
+    using UT = typename std::make_unsigned<T>::type;
+    T ourVal = rand() % T(UT(1) << (sizeof(UT) * 8 - 1));
+    for (int i = 0; i < 100; ++i) {
       std::lock_guard<PicoSpinLock<T>> guard(lock);
       lock.setData(ourVal);
       for (int n = 0; n < 10; ++n) {
@@ -96,7 +104,7 @@ template<class T> struct PslTest {
   }
 };
 
-template<class T>
+template <class T>
 void doPslTest() {
   PslTest<T> testObj;
 
@@ -109,6 +117,7 @@ void doPslTest() {
     t.join();
   }
 }
+#endif
 
 struct TestClobber {
   TestClobber() {
@@ -126,7 +135,7 @@ struct TestClobber {
   MicroSpinLock lock_;
 };
 
-}
+} // namespace
 
 TEST(SmallLocks, SpinLockCorrectness) {
   EXPECT_EQ(sizeof(MicroSpinLock), 1);
@@ -141,6 +150,7 @@ TEST(SmallLocks, SpinLockCorrectness) {
   }
 }
 
+#ifdef FOLLY_PICO_SPIN_LOCK_H_
 TEST(SmallLocks, PicoSpinCorrectness) {
   doPslTest<int16_t>();
   doPslTest<uint16_t>();
@@ -164,13 +174,19 @@ TEST(SmallLocks, PicoSpinSigned) {
   }
   EXPECT_EQ(val.getData(), -8);
 }
+#endif
 
 TEST(SmallLocks, RegClobber) {
   TestClobber().go();
 }
 
 FOLLY_PACK_PUSH
+#if defined(__SANITIZE_ADDRESS__) && !defined(__clang__) && \
+    (defined(__GNUC__) || defined(__GNUG__))
+static_assert(sizeof(MicroLock) == 4, "Size check failed");
+#else
 static_assert(sizeof(MicroLock) == 1, "Size check failed");
+#endif
 FOLLY_PACK_POP
 
 namespace {
@@ -200,9 +216,9 @@ struct SimpleBarrier {
   std::condition_variable cv_;
   bool ready_;
 };
-}
+} // namespace
 
-static void runMicroLockTest() {
+TEST(SmallLocks, MicroLock) {
   volatile uint64_t counters[4] = {0, 0, 0, 0};
   std::vector<std::thread> threads;
   static const unsigned nrThreads = 20;
@@ -215,12 +231,9 @@ static void runMicroLockTest() {
   // affect bits outside the ones MicroLock is defined to affect.
   struct {
     uint8_t a;
-    volatile uint8_t b;
-    union {
-      MicroLock alock;
-      uint8_t c;
-    };
-    volatile uint8_t d;
+    std::atomic<uint8_t> b;
+    MicroLock alock;
+    std::atomic<uint8_t> d;
   } x;
 
   uint8_t origB = 'b';
@@ -228,7 +241,7 @@ static void runMicroLockTest() {
 
   x.a = 'a';
   x.b = origB;
-  x.c = 0;
+  x.alock.init();
   x.d = origD;
 
   // This thread touches other parts of the host word to show that
@@ -273,17 +286,16 @@ static void runMicroLockTest() {
 
   EXPECT_EQ(x.a, 'a');
   EXPECT_EQ(x.b, (uint8_t)(origB + iterPerThread / 2));
-  EXPECT_EQ(x.c, 0);
   EXPECT_EQ(x.d, (uint8_t)(origD + iterPerThread / 2));
   for (unsigned i = 0; i < 4; ++i) {
     EXPECT_EQ(counters[i], ((uint64_t)nrThreads * iterPerThread) / 4);
   }
 }
 
-TEST(SmallLocks, MicroLock) { runMicroLockTest(); }
 TEST(SmallLocks, MicroLockTryLock) {
   MicroLock lock;
   lock.init();
   EXPECT_TRUE(lock.try_lock());
   EXPECT_FALSE(lock.try_lock());
+  lock.unlock();
 }