Landing Recent QUIC changes until 8/19/2015 17:00 UTC.
[chromium-blink-merge.git] / cc / trees / blocking_task_runner.cc
blobb0a935e25291ee955092fc33d3f5a2d6d66e345a
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"
7 #include <utility>
9 #include "base/callback.h"
10 #include "base/logging.h"
12 namespace cc {
14 // static
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),
24 capture_(0) {
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_);
37 if (!capture_)
38 return task_runner_->PostTask(from_here, task);
39 captured_tasks_.push_back(task);
40 return true;
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);
53 if (capture_)
54 return;
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)
60 tasks[i].Run();
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);
73 } // namespace cc