SpinLockTest.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2014-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/SpinLock.h>
  17. #include <folly/Random.h>
  18. #include <thread>
  19. #include <folly/portability/Asm.h>
  20. #include <folly/portability/GTest.h>
  21. using folly::SpinLockGuardImpl;
  22. namespace {
  23. template <typename LOCK>
  24. struct LockedVal {
  25. int ar[1024];
  26. LOCK lock;
  27. LockedVal() {
  28. memset(ar, 0, sizeof ar);
  29. }
  30. };
  31. template <typename LOCK>
  32. void spinlockTestThread(LockedVal<LOCK>* v) {
  33. const int max = 1000;
  34. auto rng = folly::ThreadLocalPRNG();
  35. for (int i = 0; i < max; i++) {
  36. folly::asm_volatile_pause();
  37. SpinLockGuardImpl<LOCK> g(v->lock);
  38. int first = v->ar[0];
  39. for (size_t j = 1; j < sizeof v->ar / sizeof j; ++j) {
  40. EXPECT_EQ(first, v->ar[j]);
  41. }
  42. int byte = folly::Random::rand32(rng);
  43. memset(v->ar, char(byte), sizeof v->ar);
  44. }
  45. }
  46. template <typename LOCK>
  47. struct TryLockState {
  48. LOCK lock1;
  49. LOCK lock2;
  50. bool locked{false};
  51. uint64_t obtained{0};
  52. uint64_t failed{0};
  53. };
  54. template <typename LOCK>
  55. void trylockTestThread(TryLockState<LOCK>* state, size_t count) {
  56. while (true) {
  57. folly::asm_volatile_pause();
  58. bool ret = state->lock2.try_lock();
  59. SpinLockGuardImpl<LOCK> g(state->lock1);
  60. if (state->obtained >= count) {
  61. if (ret) {
  62. state->lock2.unlock();
  63. }
  64. break;
  65. }
  66. if (ret) {
  67. // We got lock2.
  68. EXPECT_NE(state->locked, ret);
  69. ++state->obtained;
  70. state->locked = true;
  71. // Release lock1 and wait until at least one other thread fails to
  72. // obtain the lock2 before continuing.
  73. auto oldFailed = state->failed;
  74. while (state->failed == oldFailed && state->obtained < count) {
  75. state->lock1.unlock();
  76. folly::asm_volatile_pause();
  77. state->lock1.lock();
  78. }
  79. state->locked = false;
  80. state->lock2.unlock();
  81. } else {
  82. ++state->failed;
  83. }
  84. }
  85. }
  86. template <typename LOCK>
  87. void correctnessTest() {
  88. int nthrs = sysconf(_SC_NPROCESSORS_ONLN) * 2;
  89. std::vector<std::thread> threads;
  90. LockedVal<LOCK> v;
  91. for (int i = 0; i < nthrs; ++i) {
  92. threads.push_back(std::thread(spinlockTestThread<LOCK>, &v));
  93. }
  94. for (auto& t : threads) {
  95. t.join();
  96. }
  97. }
  98. template <typename LOCK>
  99. void trylockTest() {
  100. int nthrs = sysconf(_SC_NPROCESSORS_ONLN) + 4;
  101. std::vector<std::thread> threads;
  102. TryLockState<LOCK> state;
  103. size_t count = 100;
  104. for (int i = 0; i < nthrs; ++i) {
  105. threads.push_back(std::thread(trylockTestThread<LOCK>, &state, count));
  106. }
  107. for (auto& t : threads) {
  108. t.join();
  109. }
  110. EXPECT_EQ(count, state.obtained);
  111. // Each time the code obtains lock2 it waits for another thread to fail
  112. // to acquire it. The only time this might not happen is on the very last
  113. // loop when no other threads are left.
  114. EXPECT_GE(state.failed + 1, state.obtained);
  115. }
  116. } // namespace
  117. TEST(SpinLock, Correctness) {
  118. correctnessTest<folly::SpinLock>();
  119. }
  120. TEST(SpinLock, TryLock) {
  121. trylockTest<folly::SpinLock>();
  122. }