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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
// Copyright 2013 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 "cc/base/worker_pool.h"
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/stringprintf.h"
#include "base/synchronization/condition_variable.h"
#include "base/threading/simple_thread.h"
#if defined(OS_ANDROID)
// TODO(epenner): Move thread priorities to base. (crbug.com/170549)
#include <sys/resource.h>
#endif
namespace cc {
namespace {
class WorkerPoolTaskImpl : public internal::WorkerPoolTask {
public:
WorkerPoolTaskImpl(const WorkerPool::Callback& task,
const base::Closure& reply)
: internal::WorkerPoolTask(reply),
task_(task) {}
virtual bool IsCheap() OVERRIDE { return false; }
virtual void Run() OVERRIDE {
task_.Run();
}
virtual void RunOnThread(unsigned thread_index) OVERRIDE {
task_.Run();
}
private:
WorkerPool::Callback task_;
};
} // namespace
namespace internal {
WorkerPoolTask::WorkerPoolTask(const base::Closure& reply) : reply_(reply) {
}
WorkerPoolTask::~WorkerPoolTask() {
}
void WorkerPoolTask::DidComplete() {
reply_.Run();
}
} // namespace internal
// Internal to the worker pool. Any data or logic that needs to be
// shared between threads lives in this class. All members are guarded
// by |lock_|.
class WorkerPool::Inner : public base::DelegateSimpleThread::Delegate {
public:
Inner(WorkerPool* worker_pool,
size_t num_threads,
const std::string& thread_name_prefix,
bool need_on_task_completed_callback);
virtual ~Inner();
void Shutdown();
void PostTask(scoped_ptr<internal::WorkerPoolTask> task, bool signal_workers);
// Appends all completed tasks to worker pool's completed tasks queue
// and returns true if idle.
bool CollectCompletedTasks();
// Runs cheap tasks on caller thread until |time_limit| is reached
// and returns true if idle.
bool RunCheapTasksUntilTimeLimit(base::TimeTicks time_limit);
private:
// Appends all completed tasks to |completed_tasks|. Lock must
// already be acquired before calling this function.
bool AppendCompletedTasksWithLockAcquired(
ScopedPtrDeque<internal::WorkerPoolTask>* completed_tasks);
// Schedule a OnTaskCompletedOnOriginThread callback if not already
// pending. Lock must already be acquired before calling this function.
void ScheduleOnTaskCompletedWithLockAcquired();
void OnTaskCompletedOnOriginThread();
// Schedule an OnIdleOnOriginThread callback if not already pending.
// Lock must already be acquired before calling this function.
void ScheduleOnIdleWithLockAcquired();
void OnIdleOnOriginThread();
// Overridden from base::DelegateSimpleThread:
virtual void Run() OVERRIDE;
// Pointer to worker pool. Can only be used on origin thread.
// Not guarded by |lock_|.
WorkerPool* worker_pool_on_origin_thread_;
// This lock protects all members of this class except
// |worker_pool_on_origin_thread_|. Do not read or modify anything
// without holding this lock. Do not block while holding this lock.
mutable base::Lock lock_;
// Condition variable that is waited on by worker threads until new
// tasks are posted or shutdown starts.
base::ConditionVariable has_pending_tasks_cv_;
// Target message loop used for posting callbacks.
scoped_refptr<base::MessageLoopProxy> origin_loop_;
base::WeakPtrFactory<Inner> weak_ptr_factory_;
// Set to true when worker pool requires a callback for each
// completed task.
bool need_on_task_completed_callback_;
const base::Closure on_task_completed_callback_;
// Set when a OnTaskCompletedOnOriginThread() callback is pending.
bool on_task_completed_pending_;
const base::Closure on_idle_callback_;
// Set when a OnIdleOnOriginThread() callback is pending.
bool on_idle_pending_;
// Provides each running thread loop with a unique index. First thread
// loop index is 0.
unsigned next_thread_index_;
// Number of tasks currently running.
unsigned running_task_count_;
// Set during shutdown. Tells workers to exit when no more tasks
// are pending.
bool shutdown_;
typedef ScopedPtrDeque<internal::WorkerPoolTask> TaskDeque;
TaskDeque pending_tasks_;
TaskDeque completed_tasks_;
ScopedPtrDeque<base::DelegateSimpleThread> workers_;
DISALLOW_COPY_AND_ASSIGN(Inner);
};
WorkerPool::Inner::Inner(WorkerPool* worker_pool,
size_t num_threads,
const std::string& thread_name_prefix,
bool need_on_task_completed_callback)
: worker_pool_on_origin_thread_(worker_pool),
lock_(),
has_pending_tasks_cv_(&lock_),
origin_loop_(base::MessageLoopProxy::current()),
weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
need_on_task_completed_callback_(need_on_task_completed_callback),
on_task_completed_callback_(
base::Bind(&WorkerPool::Inner::OnTaskCompletedOnOriginThread,
weak_ptr_factory_.GetWeakPtr())),
on_task_completed_pending_(false),
on_idle_callback_(base::Bind(&WorkerPool::Inner::OnIdleOnOriginThread,
weak_ptr_factory_.GetWeakPtr())),
on_idle_pending_(false),
next_thread_index_(0),
running_task_count_(0),
shutdown_(false) {
base::AutoLock lock(lock_);
while (workers_.size() < num_threads) {
scoped_ptr<base::DelegateSimpleThread> worker = make_scoped_ptr(
new base::DelegateSimpleThread(
this,
thread_name_prefix +
base::StringPrintf("Worker%lu", workers_.size() + 1).c_str()));
worker->Start();
workers_.push_back(worker.Pass());
}
}
WorkerPool::Inner::~Inner() {
base::AutoLock lock(lock_);
DCHECK(shutdown_);
// Cancel all pending callbacks.
weak_ptr_factory_.InvalidateWeakPtrs();
DCHECK_EQ(pending_tasks_.size(), 0);
DCHECK_EQ(completed_tasks_.size(), 0);
DCHECK_EQ(running_task_count_, 0);
}
void WorkerPool::Inner::Shutdown() {
{
base::AutoLock lock(lock_);
DCHECK(!shutdown_);
shutdown_ = true;
// Wake up a worker so it knows it should exit. This will cause all workers
// to exit as each will wake up another worker before exiting.
has_pending_tasks_cv_.Signal();
}
while (workers_.size()) {
scoped_ptr<base::DelegateSimpleThread> worker = workers_.take_front();
worker->Join();
}
}
void WorkerPool::Inner::PostTask(scoped_ptr<internal::WorkerPoolTask> task,
bool signal_workers) {
base::AutoLock lock(lock_);
pending_tasks_.push_back(task.Pass());
// There is more work available, so wake up worker thread.
if (signal_workers)
has_pending_tasks_cv_.Signal();
}
bool WorkerPool::Inner::CollectCompletedTasks() {
base::AutoLock lock(lock_);
return AppendCompletedTasksWithLockAcquired(
&worker_pool_on_origin_thread_->completed_tasks_);
}
bool WorkerPool::Inner::RunCheapTasksUntilTimeLimit(
base::TimeTicks time_limit) {
base::AutoLock lock(lock_);
while (base::TimeTicks::Now() < time_limit) {
scoped_ptr<internal::WorkerPoolTask> task;
// Find next cheap task.
for (TaskDeque::iterator iter = pending_tasks_.begin();
iter != pending_tasks_.end(); ++iter) {
if ((*iter)->IsCheap()) {
task = pending_tasks_.take(iter);
break;
}
}
if (!task) {
// Schedule an idle callback if requested and not pending.
if (!running_task_count_ && pending_tasks_.empty())
ScheduleOnIdleWithLockAcquired();
// Exit when no more cheap tasks are pending.
break;
}
// Increment |running_task_count_| before starting to run task.
running_task_count_++;
{
base::AutoUnlock unlock(lock_);
task->Run();
// Append tasks directly to worker pool's completed tasks queue.
worker_pool_on_origin_thread_->completed_tasks_.push_back(task.Pass());
if (need_on_task_completed_callback_)
worker_pool_on_origin_thread_->OnTaskCompleted();
}
// Decrement |running_task_count_| now that we are done running task.
running_task_count_--;
}
if (!pending_tasks_.empty())
has_pending_tasks_cv_.Signal();
// Append any other completed tasks before releasing lock.
return AppendCompletedTasksWithLockAcquired(
&worker_pool_on_origin_thread_->completed_tasks_);
}
bool WorkerPool::Inner::AppendCompletedTasksWithLockAcquired(
ScopedPtrDeque<internal::WorkerPoolTask>* completed_tasks) {
lock_.AssertAcquired();
while (completed_tasks_.size())
completed_tasks->push_back(completed_tasks_.take_front().Pass());
return !running_task_count_ && pending_tasks_.empty();
}
void WorkerPool::Inner::ScheduleOnTaskCompletedWithLockAcquired() {
lock_.AssertAcquired();
if (on_task_completed_pending_ || !need_on_task_completed_callback_)
return;
origin_loop_->PostTask(FROM_HERE, on_task_completed_callback_);
on_task_completed_pending_ = true;
}
void WorkerPool::Inner::OnTaskCompletedOnOriginThread() {
{
base::AutoLock lock(lock_);
DCHECK(on_task_completed_pending_);
on_task_completed_pending_ = false;
AppendCompletedTasksWithLockAcquired(
&worker_pool_on_origin_thread_->completed_tasks_);
}
worker_pool_on_origin_thread_->OnTaskCompleted();
}
void WorkerPool::Inner::ScheduleOnIdleWithLockAcquired() {
lock_.AssertAcquired();
if (on_idle_pending_)
return;
origin_loop_->PostTask(FROM_HERE, on_idle_callback_);
on_idle_pending_ = true;
}
void WorkerPool::Inner::OnIdleOnOriginThread() {
{
base::AutoLock lock(lock_);
DCHECK(on_idle_pending_);
on_idle_pending_ = false;
// Early out if no longer idle.
if (running_task_count_ || !pending_tasks_.empty())
return;
AppendCompletedTasksWithLockAcquired(
&worker_pool_on_origin_thread_->completed_tasks_);
}
worker_pool_on_origin_thread_->OnIdle();
}
void WorkerPool::Inner::Run() {
#if defined(OS_ANDROID)
// TODO(epenner): Move thread priorities to base. (crbug.com/170549)
int nice_value = 10; // Idle priority.
setpriority(PRIO_PROCESS, base::PlatformThread::CurrentId(), nice_value);
#endif
{
base::AutoLock lock(lock_);
// Get a unique thread index.
int thread_index = next_thread_index_++;
while (true) {
if (pending_tasks_.empty()) {
// Exit when shutdown is set and no more tasks are pending.
if (shutdown_)
break;
// Schedule an idle callback if requested and not pending.
if (!running_task_count_)
ScheduleOnIdleWithLockAcquired();
// Wait for new pending tasks.
has_pending_tasks_cv_.Wait();
continue;
}
// Get next task.
scoped_ptr<internal::WorkerPoolTask> task = pending_tasks_.take_front();
// Increment |running_task_count_| before starting to run task.
running_task_count_++;
// There may be more work available, so wake up another
// worker thread.
has_pending_tasks_cv_.Signal();
{
base::AutoUnlock unlock(lock_);
task->RunOnThread(thread_index);
}
completed_tasks_.push_back(task.Pass());
// Decrement |running_task_count_| now that we are done running task.
running_task_count_--;
// Schedule a task completed callback if requested and not pending.
ScheduleOnTaskCompletedWithLockAcquired();
}
// We noticed we should exit. Wake up the next worker so it knows it should
// exit as well (because the Shutdown() code only signals once).
has_pending_tasks_cv_.Signal();
}
}
WorkerPool::WorkerPool(WorkerPoolClient* client,
size_t num_threads,
base::TimeDelta check_for_completed_tasks_delay,
const std::string& thread_name_prefix)
: client_(client),
origin_loop_(base::MessageLoopProxy::current()),
weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
check_for_completed_tasks_delay_(check_for_completed_tasks_delay),
check_for_completed_tasks_pending_(false),
run_cheap_tasks_callback_(
base::Bind(&WorkerPool::RunCheapTasks,
weak_ptr_factory_.GetWeakPtr())),
run_cheap_tasks_pending_(false),
inner_(make_scoped_ptr(
new Inner(
this,
num_threads,
thread_name_prefix,
// Request OnTaskCompleted() callback when check
// for completed tasks delay is 0.
check_for_completed_tasks_delay == base::TimeDelta()))) {
}
WorkerPool::~WorkerPool() {
Shutdown();
// Cancel all pending callbacks.
weak_ptr_factory_.InvalidateWeakPtrs();
DCHECK_EQ(completed_tasks_.size(), 0);
}
void WorkerPool::Shutdown() {
inner_->Shutdown();
DispatchCompletionCallbacks();
}
void WorkerPool::PostTaskAndReply(
const Callback& task, const base::Closure& reply) {
PostTask(make_scoped_ptr(new WorkerPoolTaskImpl(
task,
reply)).PassAs<internal::WorkerPoolTask>());
}
void WorkerPool::SetRunCheapTasksTimeLimit(
base::TimeTicks run_cheap_tasks_time_limit) {
run_cheap_tasks_time_limit_ = run_cheap_tasks_time_limit;
ScheduleRunCheapTasks();
}
void WorkerPool::OnIdle() {
TRACE_EVENT0("cc", "WorkerPool::OnIdle");
DispatchCompletionCallbacks();
}
void WorkerPool::OnTaskCompleted() {
TRACE_EVENT0("cc", "WorkerPool::OnTaskCompleted");
DispatchCompletionCallbacks();
}
void WorkerPool::ScheduleCheckForCompletedTasks() {
if (check_for_completed_tasks_pending_ ||
check_for_completed_tasks_delay_ == base::TimeDelta())
return;
check_for_completed_tasks_callback_.Reset(
base::Bind(&WorkerPool::CheckForCompletedTasks,
weak_ptr_factory_.GetWeakPtr()));
check_for_completed_tasks_time_ = base::TimeTicks::Now() +
check_for_completed_tasks_delay_;
origin_loop_->PostDelayedTask(
FROM_HERE,
check_for_completed_tasks_callback_.callback(),
check_for_completed_tasks_delay_);
check_for_completed_tasks_pending_ = true;
}
void WorkerPool::CheckForCompletedTasks() {
TRACE_EVENT0("cc", "WorkerPool::CheckForCompletedTasks");
DCHECK(check_for_completed_tasks_pending_);
check_for_completed_tasks_pending_ = false;
// Schedule another check for completed tasks if not idle.
if (!inner_->CollectCompletedTasks())
ScheduleCheckForCompletedTasks();
DispatchCompletionCallbacks();
}
void WorkerPool::CancelCheckForCompletedTasks() {
if (!check_for_completed_tasks_pending_)
return;
check_for_completed_tasks_callback_.Cancel();
check_for_completed_tasks_pending_ = false;
}
void WorkerPool::DispatchCompletionCallbacks() {
TRACE_EVENT0("cc", "WorkerPool::DispatchCompletionCallbacks");
if (completed_tasks_.empty())
return;
while (completed_tasks_.size()) {
scoped_ptr<internal::WorkerPoolTask> task = completed_tasks_.take_front();
task->DidComplete();
}
client_->DidFinishDispatchingWorkerPoolCompletionCallbacks();
}
void WorkerPool::PostTask(scoped_ptr<internal::WorkerPoolTask> task) {
bool signal_workers = true;
if (task->IsCheap()) {
// To make cheap tasks more likely to run on the origin thread, don't wake
// workers when posting them.
signal_workers = false;
ScheduleRunCheapTasks();
}
// Schedule check for completed tasks if not pending.
ScheduleCheckForCompletedTasks();
inner_->PostTask(task.Pass(), signal_workers);
}
void WorkerPool::ScheduleRunCheapTasks() {
if (run_cheap_tasks_pending_)
return;
origin_loop_->PostTask(FROM_HERE, run_cheap_tasks_callback_);
run_cheap_tasks_pending_ = true;
}
void WorkerPool::RunCheapTasks() {
TRACE_EVENT0("cc", "WorkerPool::RunCheapTasks");
DCHECK(run_cheap_tasks_pending_);
run_cheap_tasks_pending_ = false;
while (true) {
base::TimeTicks time_limit = run_cheap_tasks_time_limit_;
if (!check_for_completed_tasks_time_.is_null())
time_limit = std::min(time_limit, check_for_completed_tasks_time_);
bool is_idle = inner_->RunCheapTasksUntilTimeLimit(time_limit);
base::TimeTicks now = base::TimeTicks::Now();
if (now >= run_cheap_tasks_time_limit_) {
TRACE_EVENT_INSTANT0("cc", "WorkerPool::RunCheapTasks out of time");
break;
}
// We must be out of cheap tasks if this happens.
if (check_for_completed_tasks_time_.is_null() ||
now < check_for_completed_tasks_time_)
break;
TRACE_EVENT_INSTANT0("cc", "WorkerPool::RunCheapTasks check time");
CancelCheckForCompletedTasks();
DispatchCompletionCallbacks();
// Schedule another check for completed tasks if not idle.
if (!is_idle)
ScheduleCheckForCompletedTasks();
}
}
} // namespace cc
|