1424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// found in the LICENSE file. 4424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 5424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "cc/trees/blocking_task_runner.h" 6424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 7424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include <utility> 8424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/callback.h" 10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/logging.h" 11424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/message_loop/message_loop_proxy.h" 12424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 13424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace cc { 14424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 15424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// static 161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciscoped_ptr<BlockingTaskRunner> BlockingTaskRunner::Create( 171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci scoped_refptr<base::SingleThreadTaskRunner> task_runner) { 181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci return make_scoped_ptr(new BlockingTaskRunner(task_runner)); 19424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 20424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)BlockingTaskRunner::BlockingTaskRunner( 22424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) scoped_refptr<base::SingleThreadTaskRunner> task_runner) 23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) : thread_id_(base::PlatformThread::CurrentId()), 24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) task_runner_(task_runner), 25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) capture_(0) { 26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 27424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)BlockingTaskRunner::~BlockingTaskRunner() {} 29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool BlockingTaskRunner::BelongsToCurrentThread() { 31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return base::PlatformThread::CurrentId() == thread_id_; 32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)bool BlockingTaskRunner::PostTask(const tracked_objects::Location& from_here, 35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const base::Closure& task) { 36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::AutoLock lock(lock_); 37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) DCHECK(task_runner_.get() || capture_); 38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) if (!capture_) 39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return task_runner_->PostTask(from_here, task); 40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) captured_tasks_.push_back(task); 41424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return true; 42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void BlockingTaskRunner::SetCapture(bool capture) { 45424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) DCHECK(BelongsToCurrentThread()); 46424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 47424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) std::vector<base::Closure> tasks; 48424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 49424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) { 50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) base::AutoLock lock(lock_); 51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) capture_ += capture ? 1 : -1; 52424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) DCHECK_GE(capture_, 0); 53424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 54424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) if (capture_) 55424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return; 56424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 57424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // We're done capturing, so grab all the captured tasks and run them. 58424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) tasks.swap(captured_tasks_); 59424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 60424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) for (size_t i = 0; i < tasks.size(); ++i) 61424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) tasks[i].Run(); 62424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 63424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciBlockingTaskRunner::CapturePostTasks::CapturePostTasks( 651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci BlockingTaskRunner* blocking_runner) 661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci : blocking_runner_(blocking_runner) { 67424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) blocking_runner_->SetCapture(true); 68424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 69424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 70424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)BlockingTaskRunner::CapturePostTasks::~CapturePostTasks() { 71424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) blocking_runner_->SetCapture(false); 72424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 73424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 74424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} // namespace cc 75