/*
- * 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 {
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);
}
}
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;
auto oldFailed = state->failed;
while (state->failed == oldFailed && state->obtained < count) {
state->lock1.unlock();
- asm("pause");
+ folly::asm_pause();
state->lock1.lock();
}