SaturatingSemaphoreTest.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2017-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/synchronization/SaturatingSemaphore.h>
  17. #include <folly/portability/GTest.h>
  18. #include <folly/test/DeterministicSchedule.h>
  19. /// Test helper functions
  20. using folly::SaturatingSemaphore;
  21. using DSched = folly::test::DeterministicSchedule;
  22. template <bool MayBlock, template <typename> class Atom = std::atomic>
  23. void run_basic_test() {
  24. SaturatingSemaphore<MayBlock, Atom> f;
  25. ASSERT_FALSE(f.ready());
  26. ASSERT_FALSE(f.try_wait());
  27. ASSERT_FALSE(f.try_wait_until(
  28. std::chrono::steady_clock::now() + std::chrono::microseconds(1)));
  29. ASSERT_FALSE(f.try_wait_until(
  30. std::chrono::steady_clock::now() + std::chrono::microseconds(1),
  31. f.wait_options().spin_max(std::chrono::microseconds(1))));
  32. f.post();
  33. f.post();
  34. f.wait();
  35. f.wait(f.wait_options().spin_max(std::chrono::nanoseconds(100)));
  36. ASSERT_TRUE(f.ready());
  37. ASSERT_TRUE(f.try_wait());
  38. ASSERT_TRUE(f.try_wait_until(
  39. std::chrono::steady_clock::now() + std::chrono::microseconds(1)));
  40. f.wait();
  41. f.reset();
  42. ASSERT_FALSE(f.try_wait());
  43. }
  44. template <bool MayBlock, template <typename> class Atom = std::atomic>
  45. void run_pingpong_test(int numRounds) {
  46. using WF = SaturatingSemaphore<MayBlock, Atom>;
  47. std::array<WF, 17> flags;
  48. WF& a = flags[0];
  49. WF& b = flags[16]; // different cache line
  50. auto thr = DSched::thread([&] {
  51. for (int i = 0; i < numRounds; ++i) {
  52. a.try_wait();
  53. a.wait();
  54. a.reset();
  55. b.post();
  56. }
  57. });
  58. for (int i = 0; i < numRounds; ++i) {
  59. a.post();
  60. b.try_wait();
  61. b.wait();
  62. b.reset();
  63. }
  64. DSched::join(thr);
  65. }
  66. template <bool MayBlock, template <typename> class Atom = std::atomic>
  67. void run_multi_poster_multi_waiter_test(int np, int nw) {
  68. SaturatingSemaphore<MayBlock, Atom> f;
  69. std::atomic<int> posted{0};
  70. std::atomic<int> waited{0};
  71. std::atomic<bool> go_post{false};
  72. std::atomic<bool> go_wait{false};
  73. std::vector<std::thread> prod(np);
  74. std::vector<std::thread> cons(nw);
  75. for (int i = 0; i < np; ++i) {
  76. prod[i] = DSched::thread([&] {
  77. while (!go_post.load()) {
  78. /* spin */;
  79. }
  80. f.post();
  81. posted.fetch_add(1);
  82. });
  83. }
  84. for (int i = 0; i < nw; ++i) {
  85. cons[i] = DSched::thread([&] {
  86. ASSERT_FALSE(f.ready());
  87. ASSERT_FALSE(f.try_wait());
  88. ASSERT_FALSE(f.try_wait_for(std::chrono::microseconds(1)));
  89. ASSERT_FALSE(f.try_wait_until(
  90. std::chrono::steady_clock::now() + std::chrono::microseconds(1)));
  91. ASSERT_FALSE(f.try_wait_until(
  92. std::chrono::steady_clock::now() + std::chrono::microseconds(1),
  93. f.wait_options().spin_max(std::chrono::microseconds(0))));
  94. waited.fetch_add(1);
  95. while (!go_wait.load()) {
  96. /* spin */;
  97. }
  98. ASSERT_TRUE(f.ready());
  99. ASSERT_TRUE(f.try_wait());
  100. ASSERT_TRUE(f.try_wait_for(std::chrono::microseconds(1)));
  101. ASSERT_TRUE(f.try_wait_until(
  102. std::chrono::steady_clock::now() + std::chrono::microseconds(1)));
  103. ASSERT_TRUE(f.try_wait_until(
  104. std::chrono::steady_clock::now() + std::chrono::microseconds(1),
  105. f.wait_options().spin_max(std::chrono::microseconds(0))));
  106. f.wait();
  107. });
  108. }
  109. while (waited.load() < nw) {
  110. /* spin */;
  111. }
  112. go_post.store(true);
  113. while (posted.load() < np) {
  114. /* spin */;
  115. }
  116. go_wait.store(true);
  117. for (auto& t : prod) {
  118. DSched::join(t);
  119. }
  120. for (auto& t : cons) {
  121. DSched::join(t);
  122. }
  123. }
  124. /// Tests
  125. TEST(SaturatingSemaphore, basic_spin_only) {
  126. run_basic_test<false>();
  127. }
  128. TEST(SaturatingSemaphore, basic_may_block) {
  129. run_basic_test<true>();
  130. }
  131. TEST(SaturatingSemaphore, pingpong_spin_only) {
  132. run_pingpong_test<false>(1000);
  133. }
  134. TEST(SaturatingSemaphore, pingpong_may_block) {
  135. run_pingpong_test<true>(1000);
  136. }
  137. TEST(SaturatingSemaphore, multi_poster_multi_waiter_spin_only) {
  138. run_multi_poster_multi_waiter_test<false>(1, 1);
  139. run_multi_poster_multi_waiter_test<false>(1, 10);
  140. run_multi_poster_multi_waiter_test<false>(10, 1);
  141. run_multi_poster_multi_waiter_test<false>(10, 10);
  142. }
  143. TEST(SaturatingSemaphore, multi_poster_multi_waiter_may_block) {
  144. run_multi_poster_multi_waiter_test<true>(1, 1);
  145. run_multi_poster_multi_waiter_test<true>(1, 10);
  146. run_multi_poster_multi_waiter_test<true>(10, 1);
  147. run_multi_poster_multi_waiter_test<true>(10, 10);
  148. }