OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2010 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. | |
4 | |
5 #include "net/disk_cache/in_flight_io.h" | |
6 | |
7 #include "base/logging.h" | |
8 | |
9 namespace disk_cache { | |
10 | |
11 // Runs on the IO thread. | |
12 void BackgroundIO::OnIOSignalled() { | |
13 if (controller_) | |
14 controller_->InvokeCallback(this, false); | |
15 } | |
16 | |
17 void BackgroundIO::Cancel() { | |
18 DCHECK(controller_); | |
19 controller_ = NULL; | |
20 } | |
21 | |
22 // Runs on the background thread. | |
23 void BackgroundIO::NotifyController() { | |
24 controller_->OnIOComplete(this); | |
25 } | |
26 | |
27 // --------------------------------------------------------------------------- | |
28 | |
29 void InFlightIO::WaitForPendingIO() { | |
30 while (!io_list_.empty()) { | |
31 // Block the current thread until all pending IO completes. | |
32 IOList::iterator it = io_list_.begin(); | |
33 InvokeCallback(*it, true); | |
34 } | |
35 } | |
36 | |
37 // Runs on a background thread. | |
38 void InFlightIO::OnIOComplete(BackgroundIO* operation) { | |
39 callback_thread_->PostTask(FROM_HERE, | |
40 NewRunnableMethod(operation, | |
41 &BackgroundIO::OnIOSignalled)); | |
42 operation->io_completed()->Signal(); | |
43 } | |
44 | |
45 // Runs on the IO thread. | |
46 void InFlightIO::InvokeCallback(BackgroundIO* operation, bool cancel_task) { | |
47 operation->io_completed()->Wait(); | |
48 | |
49 if (cancel_task) | |
50 operation->Cancel(); | |
51 | |
52 // Make sure that we remove the operation from the list before invoking the | |
53 // callback (so that a subsequent cancel does not invoke the callback again). | |
54 DCHECK(io_list_.find(operation) != io_list_.end()); | |
55 io_list_.erase(operation); | |
56 OnOperationComplete(operation, cancel_task); | |
57 } | |
58 | |
59 // Runs on the IO thread. | |
60 void InFlightIO::OnOperationPosted(BackgroundIO* operation) { | |
61 io_list_.insert(operation); | |
62 } | |
63 | |
64 } // namespace disk_cache | |
OLD | NEW |