Use float literals when initializing float values
[folly.git] / folly / test / SmallLocksTest.cpp
index d7e27578adca527c95806406b6cf09083916664a..5aef9ea175debcba09864226d995041e8e5a0079 100644 (file)
 
 #include <gtest/gtest.h>
 
+#include <folly/portability/Asm.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 +58,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;
@@ -78,6 +85,7 @@ void splock_test() {
   }
 }
 
+#ifdef FOLLY_PICO_SPIN_LOCK_H_
 template<class T> struct PslTest {
   PicoSpinLock<T> lock;
 
@@ -109,6 +117,7 @@ void doPslTest() {
     t.join();
   }
 }
+#endif
 
 struct TestClobber {
   TestClobber() {
@@ -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 {
@@ -202,7 +218,7 @@ struct SimpleBarrier {
 };
 }
 
-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;
@@ -216,10 +232,7 @@ static void runMicroLockTest() {
   struct {
     uint8_t a;
     volatile uint8_t b;
-    union {
-      MicroLock alock;
-      uint8_t c;
-    };
+    MicroLock alock;
     volatile uint8_t d;
   } x;
 
@@ -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();
 }