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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/worker_pool_linux.h"
#include <set>
#include "base/condition_variable.h"
#include "base/lock.h"
#include "base/platform_thread.h"
#include "base/task.h"
#include "base/waitable_event.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
// Peer class to provide passthrough access to LinuxDynamicThreadPool internals.
class LinuxDynamicThreadPool::LinuxDynamicThreadPoolPeer {
public:
explicit LinuxDynamicThreadPoolPeer(LinuxDynamicThreadPool* pool)
: pool_(pool) {}
Lock* lock() { return &pool_->lock_; }
ConditionVariable* tasks_available_cv() {
return &pool_->tasks_available_cv_;
}
const std::queue<Task*>& tasks() const { return pool_->tasks_; }
int num_idle_threads() const { return pool_->num_idle_threads_; }
ConditionVariable* num_idle_threads_cv() {
return pool_->num_idle_threads_cv_.get();
}
void set_num_idle_threads_cv(ConditionVariable* cv) {
pool_->num_idle_threads_cv_.reset(cv);
}
private:
LinuxDynamicThreadPool* pool_;
DISALLOW_COPY_AND_ASSIGN(LinuxDynamicThreadPoolPeer);
};
} // namespace base
namespace {
// IncrementingTask's main purpose is to increment a counter. It also updates a
// set of unique thread ids, and signals a ConditionVariable on completion.
// Note that since it does not block, there is no way to control the number of
// threads used if more than one IncrementingTask is consecutively posted to the
// thread pool, since the first one might finish executing before the subsequent
// PostTask() calls get invoked.
class IncrementingTask : public Task {
public:
IncrementingTask(Lock* counter_lock,
int* counter,
Lock* unique_threads_lock,
std::set<PlatformThreadId>* unique_threads)
: counter_lock_(counter_lock),
unique_threads_lock_(unique_threads_lock),
unique_threads_(unique_threads),
counter_(counter) {}
virtual void Run() {
AddSelfToUniqueThreadSet();
AutoLock locked(*counter_lock_);
(*counter_)++;
}
void AddSelfToUniqueThreadSet() {
AutoLock locked(*unique_threads_lock_);
unique_threads_->insert(PlatformThread::CurrentId());
}
private:
Lock* counter_lock_;
Lock* unique_threads_lock_;
std::set<PlatformThreadId>* unique_threads_;
int* counter_;
DISALLOW_COPY_AND_ASSIGN(IncrementingTask);
};
// BlockingIncrementingTask is a simple wrapper around IncrementingTask that
// allows for waiting at the start of Run() for a WaitableEvent to be signalled.
class BlockingIncrementingTask : public Task {
public:
BlockingIncrementingTask(Lock* counter_lock,
int* counter,
Lock* unique_threads_lock,
std::set<PlatformThreadId>* unique_threads,
Lock* num_waiting_to_start_lock,
int* num_waiting_to_start,
ConditionVariable* num_waiting_to_start_cv,
base::WaitableEvent* start)
: incrementer_(
counter_lock, counter, unique_threads_lock, unique_threads),
num_waiting_to_start_lock_(num_waiting_to_start_lock),
num_waiting_to_start_(num_waiting_to_start),
num_waiting_to_start_cv_(num_waiting_to_start_cv),
start_(start) {}
virtual void Run() {
{
AutoLock num_waiting_to_start_locked(*num_waiting_to_start_lock_);
(*num_waiting_to_start_)++;
}
num_waiting_to_start_cv_->Signal();
CHECK(start_->Wait());
incrementer_.Run();
}
private:
IncrementingTask incrementer_;
Lock* num_waiting_to_start_lock_;
int* num_waiting_to_start_;
ConditionVariable* num_waiting_to_start_cv_;
base::WaitableEvent* start_;
DISALLOW_COPY_AND_ASSIGN(BlockingIncrementingTask);
};
class LinuxDynamicThreadPoolTest : public testing::Test {
protected:
LinuxDynamicThreadPoolTest()
: pool_(new base::LinuxDynamicThreadPool("dynamic_pool", 60*60)),
peer_(pool_.get()),
counter_(0),
num_waiting_to_start_(0),
num_waiting_to_start_cv_(&num_waiting_to_start_lock_),
start_(true, false) {}
virtual void SetUp() {
peer_.set_num_idle_threads_cv(new ConditionVariable(peer_.lock()));
}
virtual void TearDown() {
// Wake up the idle threads so they can terminate.
if (pool_.get()) pool_->Terminate();
}
void WaitForTasksToStart(int num_tasks) {
AutoLock num_waiting_to_start_locked(num_waiting_to_start_lock_);
while (num_waiting_to_start_ < num_tasks) {
num_waiting_to_start_cv_.Wait();
}
}
void WaitForIdleThreads(int num_idle_threads) {
AutoLock pool_locked(*peer_.lock());
while (peer_.num_idle_threads() < num_idle_threads) {
peer_.num_idle_threads_cv()->Wait();
}
}
Task* CreateNewIncrementingTask() {
return new IncrementingTask(&counter_lock_, &counter_,
&unique_threads_lock_, &unique_threads_);
}
Task* CreateNewBlockingIncrementingTask() {
return new BlockingIncrementingTask(
&counter_lock_, &counter_, &unique_threads_lock_, &unique_threads_,
&num_waiting_to_start_lock_, &num_waiting_to_start_,
&num_waiting_to_start_cv_, &start_);
}
scoped_refptr<base::LinuxDynamicThreadPool> pool_;
base::LinuxDynamicThreadPool::LinuxDynamicThreadPoolPeer peer_;
Lock counter_lock_;
int counter_;
Lock unique_threads_lock_;
std::set<PlatformThreadId> unique_threads_;
Lock num_waiting_to_start_lock_;
int num_waiting_to_start_;
ConditionVariable num_waiting_to_start_cv_;
base::WaitableEvent start_;
};
TEST_F(LinuxDynamicThreadPoolTest, Basic) {
EXPECT_EQ(0, peer_.num_idle_threads());
EXPECT_EQ(0U, unique_threads_.size());
EXPECT_EQ(0U, peer_.tasks().size());
// Add one task and wait for it to be completed.
pool_->PostTask(CreateNewIncrementingTask());
WaitForIdleThreads(1);
EXPECT_EQ(1U, unique_threads_.size()) <<
"There should be only one thread allocated for one task.";
EXPECT_EQ(1, peer_.num_idle_threads());
EXPECT_EQ(1, counter_);
}
TEST_F(LinuxDynamicThreadPoolTest, ReuseIdle) {
// Add one task and wait for it to be completed.
pool_->PostTask(CreateNewIncrementingTask());
WaitForIdleThreads(1);
// Add another 2 tasks. One should reuse the existing worker thread.
pool_->PostTask(CreateNewBlockingIncrementingTask());
pool_->PostTask(CreateNewBlockingIncrementingTask());
WaitForTasksToStart(2);
start_.Signal();
WaitForIdleThreads(2);
EXPECT_EQ(2U, unique_threads_.size());
EXPECT_EQ(2, peer_.num_idle_threads());
EXPECT_EQ(3, counter_);
}
TEST_F(LinuxDynamicThreadPoolTest, TwoActiveTasks) {
// Add two blocking tasks.
pool_->PostTask(CreateNewBlockingIncrementingTask());
pool_->PostTask(CreateNewBlockingIncrementingTask());
EXPECT_EQ(0, counter_) << "Blocking tasks should not have started yet.";
WaitForTasksToStart(2);
start_.Signal();
WaitForIdleThreads(2);
EXPECT_EQ(2U, unique_threads_.size());
EXPECT_EQ(2, peer_.num_idle_threads()) << "Existing threads are now idle.";
EXPECT_EQ(2, counter_);
}
TEST_F(LinuxDynamicThreadPoolTest, Complex) {
// Add two non blocking tasks and wait for them to finish.
pool_->PostTask(CreateNewIncrementingTask());
WaitForIdleThreads(1);
// Add two blocking tasks, start them simultaneously, and wait for them to
// finish.
pool_->PostTask(CreateNewBlockingIncrementingTask());
pool_->PostTask(CreateNewBlockingIncrementingTask());
WaitForTasksToStart(2);
start_.Signal();
WaitForIdleThreads(2);
EXPECT_EQ(3, counter_);
EXPECT_EQ(2, peer_.num_idle_threads());
EXPECT_EQ(2U, unique_threads_.size());
// Wake up all idle threads so they can exit.
{
AutoLock locked(*peer_.lock());
while (peer_.num_idle_threads() > 0) {
peer_.tasks_available_cv()->Signal();
peer_.num_idle_threads_cv()->Wait();
}
}
// Add another non blocking task. There are no threads to reuse.
pool_->PostTask(CreateNewIncrementingTask());
WaitForIdleThreads(1);
EXPECT_EQ(3U, unique_threads_.size());
EXPECT_EQ(1, peer_.num_idle_threads());
EXPECT_EQ(4, counter_);
}
} // namespace
|