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"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/threading/thread_restrictions.h"
15 namespace disk_cache
{
17 BackgroundIO::BackgroundIO(InFlightIO
* controller
)
18 : result_(-1), io_completed_(true, false), controller_(controller
) {
21 // Runs on the primary thread.
22 void BackgroundIO::OnIOSignalled() {
24 controller_
->InvokeCallback(this, false);
27 void BackgroundIO::Cancel() {
28 // controller_ may be in use from the background thread at this time.
29 base::AutoLock
lock(controller_lock_
);
34 BackgroundIO::~BackgroundIO() {
37 // ---------------------------------------------------------------------------
39 InFlightIO::InFlightIO()
40 : callback_task_runner_(base::ThreadTaskRunnerHandle::Get()),
42 single_thread_(false) {
45 InFlightIO::~InFlightIO() {
48 // Runs on the background thread.
49 void BackgroundIO::NotifyController() {
50 base::AutoLock
lock(controller_lock_
);
52 controller_
->OnIOComplete(this);
55 void InFlightIO::WaitForPendingIO() {
56 while (!io_list_
.empty()) {
57 // Block the current thread until all pending IO completes.
58 IOList::iterator it
= io_list_
.begin();
59 InvokeCallback(it
->get(), true);
63 void InFlightIO::DropPendingIO() {
64 while (!io_list_
.empty()) {
65 IOList::iterator it
= io_list_
.begin();
66 BackgroundIO
* operation
= it
->get();
68 DCHECK(io_list_
.find(operation
) != io_list_
.end());
69 io_list_
.erase(make_scoped_refptr(operation
));
73 // Runs on a background thread.
74 void InFlightIO::OnIOComplete(BackgroundIO
* operation
) {
76 if (callback_task_runner_
->RunsTasksOnCurrentThread()) {
77 DCHECK(single_thread_
|| !running_
);
78 single_thread_
= true;
82 callback_task_runner_
->PostTask(
83 FROM_HERE
, base::Bind(&BackgroundIO::OnIOSignalled
, operation
));
84 operation
->io_completed()->Signal();
87 // Runs on the primary thread.
88 void InFlightIO::InvokeCallback(BackgroundIO
* operation
, bool cancel_task
) {
90 // http://crbug.com/74623
91 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
92 operation
->io_completed()->Wait();
99 // Make sure that we remove the operation from the list before invoking the
100 // callback (so that a subsequent cancel does not invoke the callback again).
101 DCHECK(io_list_
.find(operation
) != io_list_
.end());
102 DCHECK(!operation
->HasOneRef());
103 io_list_
.erase(make_scoped_refptr(operation
));
104 OnOperationComplete(operation
, cancel_task
);
107 // Runs on the primary thread.
108 void InFlightIO::OnOperationPosted(BackgroundIO
* operation
) {
109 DCHECK(callback_task_runner_
->RunsTasksOnCurrentThread());
110 io_list_
.insert(make_scoped_refptr(operation
));
113 } // namespace disk_cache