/*
- * 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
#include <folly/SpinLock.h>
#include <folly/Random.h>
const int max = 1000;
auto rng = folly::ThreadLocalPRNG();
for (int i = 0; i < max; i++) {
- folly::asm_pause();
+ folly::asm_volatile_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 = folly::Random::rand32(rng);
template <typename LOCK>
void trylockTestThread(TryLockState<LOCK>* state, size_t count) {
while (true) {
- folly::asm_pause();
+ folly::asm_volatile_pause();
+ bool ret = state->lock2.try_lock();
SpinLockGuardImpl<LOCK> g(state->lock1);
if (state->obtained >= count) {
+ if (ret) {
+ state->lock2.unlock();
+ }
break;
}
- bool ret = state->lock2.trylock();
- EXPECT_NE(state->locked, ret);
if (ret) {
// We got lock2.
+ EXPECT_NE(state->locked, ret);
++state->obtained;
state->locked = true;
auto oldFailed = state->failed;
while (state->failed == oldFailed && state->obtained < count) {
state->lock1.unlock();
- folly::asm_pause();
+ folly::asm_volatile_pause();
state->lock1.lock();
}
EXPECT_GE(state.failed + 1, state.obtained);
}
-} // unnamed namespace
-
-#if __x86_64__
-TEST(SpinLock, MslCorrectness) {
- correctnessTest<folly::SpinLockMslImpl>();
-}
-TEST(SpinLock, MslTryLock) {
- trylockTest<folly::SpinLockMslImpl>();
-}
-#endif
-
-#if __APPLE__
-TEST(SpinLock, AppleCorrectness) {
- correctnessTest<folly::SpinLockAppleImpl>();
-}
-TEST(SpinLock, AppleTryLock) {
- trylockTest<folly::SpinLockAppleImpl>();
-}
-#endif
-
-#if FOLLY_HAVE_PTHREAD_SPINLOCK_T
-TEST(SpinLock, PthreadCorrectness) {
- correctnessTest<folly::SpinLockPthreadImpl>();
-}
-TEST(SpinLock, PthreadTryLock) {
- trylockTest<folly::SpinLockPthreadImpl>();
-}
-#endif
+} // namespace
-TEST(SpinLock, MutexCorrectness) {
- correctnessTest<folly::SpinLockPthreadMutexImpl>();
+TEST(SpinLock, Correctness) {
+ correctnessTest<folly::SpinLock>();
}
-TEST(SpinLock, MutexTryLock) {
- trylockTest<folly::SpinLockPthreadMutexImpl>();
+TEST(SpinLock, TryLock) {
+ trylockTest<folly::SpinLock>();
}