New C++17 backport: folly::enable_shared_from_this
[folly.git] / folly / test / SpinLockTest.cpp
index 7ce90d71e9aee48d9801b591ba0bcb4a17948e89..7c263c377ed55ae2e0ee83723faa9aaa6a1ae9c1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 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/SpinLock.h>
 
-#include <gtest/gtest.h>
+#include <folly/Random.h>
+
 #include <thread>
 
+#include <folly/portability/Asm.h>
+#include <folly/portability/GTest.h>
+
 using folly::SpinLockGuardImpl;
 
 namespace {
@@ -35,17 +39,17 @@ struct LockedVal {
 template <typename LOCK>
 void spinlockTestThread(LockedVal<LOCK>* v) {
   const int max = 1000;
-  unsigned int seed = (uintptr_t)pthread_self();
+  auto rng = folly::ThreadLocalPRNG();
   for (int i = 0; i < max; i++) {
-    asm("pause");
+    folly::asm_pause();
     SpinLockGuardImpl<LOCK> 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 = rand_r(&seed);
+    int byte = folly::Random::rand32(rng);
     memset(v->ar, char(byte), sizeof v->ar);
   }
 }
@@ -62,7 +66,7 @@ struct TryLockState {
 template <typename LOCK>
 void trylockTestThread(TryLockState<LOCK>* state, size_t count) {
   while (true) {
-    asm("pause");
+    folly::asm_pause();
     SpinLockGuardImpl<LOCK> g(state->lock1);
     if (state->obtained >= count) {
       break;
@@ -81,7 +85,7 @@ void trylockTestThread(TryLockState<LOCK>* state, size_t count) {
       auto oldFailed = state->failed;
       while (state->failed == oldFailed && state->obtained < count) {
         state->lock1.unlock();
-        asm("pause");
+        folly::asm_pause();
         state->lock1.lock();
       }