proxygen
ParkingLotTest.cpp File Reference

Go to the source code of this file.

Classes

class  WaitableMutex
 

Functions

 TEST (ParkingLot, multilot)
 
 TEST (ParkingLot, LargeWord)
 
 TEST (ParkingLot, WaitableMutexTest)
 

Function Documentation

TEST ( ParkingLot  ,
multilot   
)

Definition at line 26 of file ParkingLotTest.cpp.

References count, folly::data(), EXPECT_EQ, large, folly::Baton< MayBlock, Atom >::post(), folly::RemoveContinue, small, and folly::Baton< MayBlock, Atom >::wait().

26  {
27  using SmallLot = ParkingLot<bool>;
28  using LargeLot = ParkingLot<uint64_t>;
29  SmallLot smalllot;
30  LargeLot largelot;
31  folly::Baton<> sb;
32  folly::Baton<> lb;
33 
34  std::thread small([&]() {
35  smalllot.park(0, false, [] { return true; }, [&]() { sb.post(); });
36  });
37  std::thread large([&]() {
38  largelot.park(0, true, [] { return true; }, [&]() { lb.post(); });
39  });
40  sb.wait();
41  lb.wait();
42 
43  int count = 0;
44  smalllot.unpark(0, [&](bool data) {
45  count++;
46  EXPECT_EQ(data, false);
47  return UnparkControl::RemoveContinue;
48  });
49  EXPECT_EQ(count, 1);
50  count = 0;
51  largelot.unpark(0, [&](bool data) {
52  count++;
53  EXPECT_EQ(data, true);
54  return UnparkControl::RemoveContinue;
55  });
56  EXPECT_EQ(count, 1);
57 
58  small.join();
59  large.join();
60 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
auto large
auto small
FOLLY_ALWAYS_INLINE void wait(const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:170
void post() noexcept
Definition: Baton.h:123
int * count
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
TEST ( ParkingLot  ,
LargeWord   
)

Definition at line 64 of file ParkingLotTest.cpp.

References lot, and folly::ParkingLot< Data >::park().

64  {
66  std::atomic<uint64_t> w{0};
67 
68  lot.park(0, false, [&]() { return w == 1; }, []() {});
69 
70  // Validate should return false, will hang otherwise.
71 }
ParkingLot lot
ParkResult park(const Key key, D &&data, ToPark &&toPark, PreWait &&preWait)
Definition: ParkingLot.h:189
TEST ( ParkingLot  ,
WaitableMutexTest   
)

Definition at line 106 of file ParkingLotTest.cpp.

References g(), folly::kIsSanitizeThread, folly::futures::sleep(), folly::pushmi::detail::t, and WaitableMutex::wait().

106  {
107  if (kIsSanitizeThread) {
108  return;
109  }
110  std::atomic<bool> go{false};
111  WaitableMutex mu;
112  std::thread t([&]() {
113  std::lock_guard<WaitableMutex> g(mu);
114  mu.wait([&]() { return go == true; });
115  });
116  sleep(1);
117 
118  {
119  std::lock_guard<WaitableMutex> g(mu);
120  go = true;
121  }
122  t.join();
123 }
void wait(Wait wfunc)
constexpr bool kIsSanitizeThread
Definition: Portability.h:124
Future< Unit > sleep(Duration dur, Timekeeper *tk)
Definition: Future.cpp:42
g_t g(f_t)