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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#include "lix/libutil/thread-pool.hh"
#include "lix/libutil/async.hh"
#include "lix/libutil/error.hh"
#include <atomic>
#include <exception>
#include <gtest/gtest.h>
#include <memory>
static auto onThreadExit(auto fn)
{
auto deferred = kj::defer(std::move(fn));
return std::make_shared<decltype(deferred)>(std::move(deferred));
}
namespace nix {
TEST(ThreadPool, creates_threads)
{
ThreadPool t{"test", 2};
std::atomic_bool unblockA{false}, unblockB{false};
std::atomic_bool started{false};
t.enqueue([&] {
started = true;
started.notify_all();
unblockA.wait(false);
unblockB = true;
unblockB.notify_all();
});
started.wait(false);
// now no work is pending. the next enqueue should start a
// new thread; if it does not we'll deadlock and time out.
started = false;
t.enqueue([&] {
started = true;
started.notify_all();
unblockB.wait(false);
});
started.wait(false);
unblockA = true;
unblockA.notify_all();
t.process();
}
TEST(ThreadPool, early_quit)
{
ThreadPool t{"test", 2};
bool ran_anyway = false;
struct Dead : BaseException {};
std::atomic_bool unblockA{false}, unblockB{false};
std::atomic_bool started{false};
t.enqueue([&] {
started = true;
started.notify_all();
unblockA.wait(false);
thread_local auto _ = onThreadExit([&] {
unblockB = true;
unblockB.notify_all();
});
throw Dead{};
});
started.wait(false);
started = false;
t.enqueue([&] {
started = true;
started.notify_all();
unblockB.wait(false);
});
started.wait(false);
// this one should never run. the first thread saw an exception,
// and the second thread should have exited early because of it.
t.enqueue([&] { ran_anyway = true; });
unblockA = true;
unblockA.notify_all();
ASSERT_THROW(t.process(), Dead);
ASSERT_FALSE(ran_anyway);
}
TEST(ThreadPool, early_quit_async)
{
ThreadPool t{"test", 2};
bool ran_anyway = false;
struct Dead : BaseException {};
std::atomic_bool unblockA{false}, unblockB{false};
std::atomic_bool started{false};
t.enqueue([&] {
started = true;
started.notify_all();
unblockA.wait(false);
thread_local auto _ = onThreadExit([&] {
unblockB = true;
unblockB.notify_all();
});
throw Dead{};
});
started.wait(false);
started = false;
t.enqueue([&] {
started = true;
started.notify_all();
unblockB.wait(false);
});
started.wait(false);
// this one should never run. the first thread saw an exception,
// and the second thread should have exited early because of it.
t.enqueue([&] { ran_anyway = true; });
unblockA = true;
unblockA.notify_all();
AsyncIoRoot aio;
ASSERT_THROW(aio.blockOn(t.processAsync()), Dead);
ASSERT_FALSE(ran_anyway);
}
TEST(ThreadPool, always_rethrows)
{
ThreadPool t{"test"};
struct Dead : BaseException {};
std::atomic_bool flag{false};
t.enqueue([&] {
thread_local auto _ = onThreadExit([&] {
flag = true;
flag.notify_all();
});
throw Dead{};
});
flag.wait(false);
ASSERT_THROW(t.process(), Dead);
}
TEST(ThreadPool, always_rethrows_async)
{
ThreadPool t{"test"};
struct Dead : BaseException {};
std::atomic_bool flag{false};
t.enqueue([&] {
thread_local auto _ = onThreadExit([&] {
flag = true;
flag.notify_all();
});
throw Dead{};
});
flag.wait(false);
AsyncIoRoot aio;
ASSERT_THROW(aio.blockOn(t.processAsync()), Dead);
}
}
|