Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / disk_cache / blockfile / in_flight_io.cc
blobd5f172b6eddcd6cd66f6da76702e3c8c78d86bb2
1 // Copyright (c) 2012 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 "net/disk_cache/blockfile/in_flight_io.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/profiler/scoped_tracker.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/threading/thread_restrictions.h"
16 namespace disk_cache {
18 BackgroundIO::BackgroundIO(InFlightIO* controller)
19 : result_(-1), io_completed_(true, false), controller_(controller) {
22 // Runs on the primary thread.
23 void BackgroundIO::OnIOSignalled() {
24 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed.
25 tracked_objects::ScopedTracker tracking_profile(
26 FROM_HERE_WITH_EXPLICIT_FUNCTION("477117 BackgroundIO::OnIOSignalled"));
27 if (controller_)
28 controller_->InvokeCallback(this, false);
31 void BackgroundIO::Cancel() {
32 // controller_ may be in use from the background thread at this time.
33 base::AutoLock lock(controller_lock_);
34 DCHECK(controller_);
35 controller_ = NULL;
38 BackgroundIO::~BackgroundIO() {
41 // ---------------------------------------------------------------------------
43 InFlightIO::InFlightIO()
44 : callback_task_runner_(base::ThreadTaskRunnerHandle::Get()),
45 running_(false),
46 single_thread_(false) {
49 InFlightIO::~InFlightIO() {
52 // Runs on the background thread.
53 void BackgroundIO::NotifyController() {
54 base::AutoLock lock(controller_lock_);
55 if (controller_)
56 controller_->OnIOComplete(this);
59 void InFlightIO::WaitForPendingIO() {
60 while (!io_list_.empty()) {
61 // Block the current thread until all pending IO completes.
62 IOList::iterator it = io_list_.begin();
63 InvokeCallback(it->get(), true);
67 void InFlightIO::DropPendingIO() {
68 while (!io_list_.empty()) {
69 IOList::iterator it = io_list_.begin();
70 BackgroundIO* operation = it->get();
71 operation->Cancel();
72 DCHECK(io_list_.find(operation) != io_list_.end());
73 io_list_.erase(make_scoped_refptr(operation));
77 // Runs on a background thread.
78 void InFlightIO::OnIOComplete(BackgroundIO* operation) {
79 #ifndef NDEBUG
80 if (callback_task_runner_->RunsTasksOnCurrentThread()) {
81 DCHECK(single_thread_ || !running_);
82 single_thread_ = true;
84 #endif
86 callback_task_runner_->PostTask(
87 FROM_HERE, base::Bind(&BackgroundIO::OnIOSignalled, operation));
88 operation->io_completed()->Signal();
91 // Runs on the primary thread.
92 void InFlightIO::InvokeCallback(BackgroundIO* operation, bool cancel_task) {
94 // http://crbug.com/74623
95 base::ThreadRestrictions::ScopedAllowWait allow_wait;
96 operation->io_completed()->Wait();
98 running_ = true;
100 if (cancel_task)
101 operation->Cancel();
103 // Make sure that we remove the operation from the list before invoking the
104 // callback (so that a subsequent cancel does not invoke the callback again).
105 DCHECK(io_list_.find(operation) != io_list_.end());
106 DCHECK(!operation->HasOneRef());
107 io_list_.erase(make_scoped_refptr(operation));
108 OnOperationComplete(operation, cancel_task);
111 // Runs on the primary thread.
112 void InFlightIO::OnOperationPosted(BackgroundIO* operation) {
113 DCHECK(callback_task_runner_->RunsTasksOnCurrentThread());
114 io_list_.insert(make_scoped_refptr(operation));
117 } // namespace disk_cache