Implement MoveFileLocal (with creating a snapshot).
[chromium-blink-merge.git] / net / disk_cache / blockfile / in_flight_io.cc
blobcbfcd1729fce5dbecbc0b5dbc744b60147fafd83
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 if (controller_)
25 controller_->InvokeCallback(this, false);
28 void BackgroundIO::Cancel() {
29 // controller_ may be in use from the background thread at this time.
30 base::AutoLock lock(controller_lock_);
31 DCHECK(controller_);
32 controller_ = NULL;
35 BackgroundIO::~BackgroundIO() {
38 // ---------------------------------------------------------------------------
40 InFlightIO::InFlightIO()
41 : callback_task_runner_(base::ThreadTaskRunnerHandle::Get()),
42 running_(false),
43 single_thread_(false) {
46 InFlightIO::~InFlightIO() {
49 // Runs on the background thread.
50 void BackgroundIO::NotifyController() {
51 base::AutoLock lock(controller_lock_);
52 if (controller_)
53 controller_->OnIOComplete(this);
56 void InFlightIO::WaitForPendingIO() {
57 while (!io_list_.empty()) {
58 // Block the current thread until all pending IO completes.
59 IOList::iterator it = io_list_.begin();
60 InvokeCallback(it->get(), true);
64 void InFlightIO::DropPendingIO() {
65 while (!io_list_.empty()) {
66 IOList::iterator it = io_list_.begin();
67 BackgroundIO* operation = it->get();
68 operation->Cancel();
69 DCHECK(io_list_.find(operation) != io_list_.end());
70 io_list_.erase(make_scoped_refptr(operation));
74 // Runs on a background thread.
75 void InFlightIO::OnIOComplete(BackgroundIO* operation) {
76 #ifndef NDEBUG
77 if (callback_task_runner_->RunsTasksOnCurrentThread()) {
78 DCHECK(single_thread_ || !running_);
79 single_thread_ = true;
81 #endif
83 callback_task_runner_->PostTask(
84 FROM_HERE, base::Bind(&BackgroundIO::OnIOSignalled, operation));
85 operation->io_completed()->Signal();
88 // Runs on the primary thread.
89 void InFlightIO::InvokeCallback(BackgroundIO* operation, bool cancel_task) {
91 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
92 tracked_objects::ScopedTracker tracking_profile(
93 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 InFlightIO::InvokeCallback"));
95 // http://crbug.com/74623
96 base::ThreadRestrictions::ScopedAllowWait allow_wait;
97 operation->io_completed()->Wait();
99 running_ = true;
101 if (cancel_task)
102 operation->Cancel();
104 // Make sure that we remove the operation from the list before invoking the
105 // callback (so that a subsequent cancel does not invoke the callback again).
106 DCHECK(io_list_.find(operation) != io_list_.end());
107 DCHECK(!operation->HasOneRef());
108 io_list_.erase(make_scoped_refptr(operation));
109 OnOperationComplete(operation, cancel_task);
112 // Runs on the primary thread.
113 void InFlightIO::OnOperationPosted(BackgroundIO* operation) {
114 DCHECK(callback_task_runner_->RunsTasksOnCurrentThread());
115 io_list_.insert(make_scoped_refptr(operation));
118 } // namespace disk_cache