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/in_flight_io.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/threading/thread_restrictions.h"
12 namespace disk_cache
{
14 BackgroundIO::BackgroundIO(InFlightIO
* controller
)
15 : result_(-1), io_completed_(true, false), controller_(controller
) {
18 // Runs on the primary thread.
19 void BackgroundIO::OnIOSignalled() {
21 controller_
->InvokeCallback(this, false);
24 void BackgroundIO::Cancel() {
25 // controller_ may be in use from the background thread at this time.
26 base::AutoLock
lock(controller_lock_
);
31 BackgroundIO::~BackgroundIO() {
34 // ---------------------------------------------------------------------------
36 InFlightIO::InFlightIO()
37 : callback_thread_(base::MessageLoopProxy::current()),
38 running_(false), single_thread_(false) {
41 InFlightIO::~InFlightIO() {
44 // Runs on the background thread.
45 void BackgroundIO::NotifyController() {
46 base::AutoLock
lock(controller_lock_
);
48 controller_
->OnIOComplete(this);
51 void InFlightIO::WaitForPendingIO() {
52 while (!io_list_
.empty()) {
53 // Block the current thread until all pending IO completes.
54 IOList::iterator it
= io_list_
.begin();
55 InvokeCallback(it
->get(), true);
59 void InFlightIO::DropPendingIO() {
60 while (!io_list_
.empty()) {
61 IOList::iterator it
= io_list_
.begin();
62 BackgroundIO
* operation
= it
->get();
64 DCHECK(io_list_
.find(operation
) != io_list_
.end());
65 io_list_
.erase(make_scoped_refptr(operation
));
69 // Runs on a background thread.
70 void InFlightIO::OnIOComplete(BackgroundIO
* operation
) {
72 if (callback_thread_
->BelongsToCurrentThread()) {
73 DCHECK(single_thread_
|| !running_
);
74 single_thread_
= true;
78 callback_thread_
->PostTask(FROM_HERE
,
79 base::Bind(&BackgroundIO::OnIOSignalled
,
81 operation
->io_completed()->Signal();
84 // Runs on the primary thread.
85 void InFlightIO::InvokeCallback(BackgroundIO
* operation
, bool cancel_task
) {
87 // http://crbug.com/74623
88 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
89 operation
->io_completed()->Wait();
96 // Make sure that we remove the operation from the list before invoking the
97 // callback (so that a subsequent cancel does not invoke the callback again).
98 DCHECK(io_list_
.find(operation
) != io_list_
.end());
99 DCHECK(!operation
->HasOneRef());
100 io_list_
.erase(make_scoped_refptr(operation
));
101 OnOperationComplete(operation
, cancel_task
);
104 // Runs on the primary thread.
105 void InFlightIO::OnOperationPosted(BackgroundIO
* operation
) {
106 DCHECK(callback_thread_
->BelongsToCurrentThread());
107 io_list_
.insert(make_scoped_refptr(operation
));
110 } // namespace disk_cache