-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathlatch_test.cpp
More file actions
109 lines (83 loc) · 2.87 KB
/
latch_test.cpp
File metadata and controls
109 lines (83 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <deque>
#include <thread>
#include <dispenso/latch.h>
#include <gtest/gtest.h>
using namespace std::chrono_literals;
TEST(Latch, ArriveAndWait) {
size_t publishData = 0;
dispenso::Latch latch(3);
std::deque<std::thread> threads;
for (size_t i = 0; i < 2; ++i) {
threads.emplace_back([&latch, &publishData]() {
latch.arrive_and_wait();
EXPECT_EQ(publishData, 3);
});
}
// Give plenty of time for hijinx if there were any bug.
std::this_thread::sleep_for(10ms);
publishData = 3;
// Wait cannot succeed until we also throw our hat in the ring, since we have 3 threads in the
// group, but only two threads waiting to check for a new value of publishData. We do this
// after setting the value of publishData, from only one thread (main thread). After
// arrive_and_wait, wait succeeds, and waiting threads are woken, and they should see the correct
// value of publishData.
latch.arrive_and_wait();
for (auto& t : threads) {
t.join();
}
}
TEST(Latch, CountDown) {
size_t publishData = 0;
dispenso::Latch latch(3);
std::deque<std::thread> threads;
for (size_t i = 0; i < 2; ++i) {
threads.emplace_back([&latch, &publishData]() {
latch.count_down();
if (latch.try_wait()) {
EXPECT_EQ(publishData, 3);
} else {
latch.wait();
EXPECT_EQ(publishData, 3);
}
});
}
publishData = 3;
// Wait cannot succeed until we also throw our hat in the ring, since we have 3 threads in the
// group, but only two threads waiting to check for a new value of publishData. We do this
// after setting the value of publishData, from only one thread (main thread). After count_down,
// wait succeeds, and waiting threads are woken, and they should see the correct value of
// publishData.
latch.count_down();
// Wait isn't required here.
for (auto& t : threads) {
t.join();
}
}
TEST(Latch, ArriveAndWaitWithCountDown) {
size_t publishData = 0;
dispenso::Latch latch(3);
std::deque<std::thread> threads;
for (size_t i = 0; i < 2; ++i) {
threads.emplace_back([&latch, &publishData]() {
latch.arrive_and_wait();
EXPECT_EQ(publishData, 3);
});
}
publishData = 3;
// Wait cannot succeed until we also throw our hat in the ring, since we have 3 threads in the
// group, but only two threads waiting to check for a new value of publishData. We do this
// after setting the value of publishData, from only one thread (main thread). After count_down,
// wait succeeds, and waiting threads are woken, and they should see the correct value of
// publishData.
latch.count_down();
// Wait isn't required here.
for (auto& t : threads) {
t.join();
}
}