30 template <
typename LOCK>
36 memset(ar, 0,
sizeof ar);
40 template <
typename LOCK>
41 void spinlockTestThread(LockedVal<LOCK>*
v) {
44 for (
int i = 0;
i <
max;
i++) {
49 for (
size_t j = 1; j <
sizeof v->ar /
sizeof j; ++j) {
54 memset(v->ar,
char(byte),
sizeof v->ar);
58 template <
typename LOCK>
67 template <
typename LOCK>
68 void trylockTestThread(TryLockState<LOCK>*
state,
size_t count) {
71 bool ret = state->lock2.try_lock();
73 if (state->obtained >= count) {
75 state->lock2.unlock();
88 auto oldFailed = state->failed;
89 while (state->failed == oldFailed && state->obtained < count) {
90 state->lock1.unlock();
95 state->locked =
false;
96 state->lock2.unlock();
103 template <
typename LOCK>
104 void correctnessTest() {
105 int nthrs = sysconf(_SC_NPROCESSORS_ONLN) * 2;
106 std::vector<std::thread>
threads;
109 threads.push_back(std::thread(spinlockTestThread<LOCK>, &v));
111 for (
auto&
t : threads) {
116 template <
typename LOCK>
118 int nthrs = sysconf(_SC_NPROCESSORS_ONLN) + 4;
119 std::vector<std::thread>
threads;
120 TryLockState<LOCK>
state;
123 threads.push_back(std::thread(trylockTestThread<LOCK>, &state, count));
125 for (
auto&
t : threads) {
133 EXPECT_GE(state.failed + 1, state.obtained);
139 correctnessTest<folly::SpinLock>();
142 trylockTest<folly::SpinLock>();
#define EXPECT_EQ(val1, val2)
#define EXPECT_GE(val1, val2)
std::vector< std::thread::id > threads
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
#define EXPECT_NE(val1, val2)
TEST(SpinLock, Correctness)
constexpr detail::First first
void asm_volatile_pause()