1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/trees/blocking_task_runner.h"
9 #include "base/callback.h"
10 #include "base/logging.h"
15 scoped_ptr
<BlockingTaskRunner
> BlockingTaskRunner::Create(
16 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
) {
17 return make_scoped_ptr(new BlockingTaskRunner(task_runner
));
20 BlockingTaskRunner::BlockingTaskRunner(
21 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
)
22 : thread_id_(base::PlatformThread::CurrentId()),
23 task_runner_(task_runner
),
27 BlockingTaskRunner::~BlockingTaskRunner() {}
29 bool BlockingTaskRunner::BelongsToCurrentThread() {
30 return base::PlatformThread::CurrentId() == thread_id_
;
33 bool BlockingTaskRunner::PostTask(const tracked_objects::Location
& from_here
,
34 const base::Closure
& task
) {
35 base::AutoLock
lock(lock_
);
36 DCHECK(task_runner_
.get() || capture_
);
38 return task_runner_
->PostTask(from_here
, task
);
39 captured_tasks_
.push_back(task
);
43 void BlockingTaskRunner::SetCapture(bool capture
) {
44 DCHECK(BelongsToCurrentThread());
46 std::vector
<base::Closure
> tasks
;
49 base::AutoLock
lock(lock_
);
50 capture_
+= capture
? 1 : -1;
51 DCHECK_GE(capture_
, 0);
56 // We're done capturing, so grab all the captured tasks and run them.
57 tasks
.swap(captured_tasks_
);
59 for (size_t i
= 0; i
< tasks
.size(); ++i
)
63 BlockingTaskRunner::CapturePostTasks::CapturePostTasks(
64 BlockingTaskRunner
* blocking_runner
)
65 : blocking_runner_(blocking_runner
) {
66 blocking_runner_
->SetCapture(true);
69 BlockingTaskRunner::CapturePostTasks::~CapturePostTasks() {
70 blocking_runner_
->SetCapture(false);