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 primary 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 #ifndef NDEBUG | |
40 if (callback_thread_ == MessageLoop::current()) { | |
41 DCHECK(single_thread_ || !running_); | |
42 single_thread_ = true; | |
43 } | |
44 running_ = true; | |
45 #endif | |
46 | |
47 callback_thread_->PostTask(FROM_HERE, | |
48 NewRunnableMethod(operation, | |
49 &BackgroundIO::OnIOSignalled)); | |
50 operation->io_completed()->Signal(); | |
51 } | |
52 | |
53 // Runs on the primary thread. | |
54 void InFlightIO::InvokeCallback(BackgroundIO* operation, bool cancel_task) { | |
55 operation->io_completed()->Wait(); | |
56 | |
57 if (cancel_task) | |
58 operation->Cancel(); | |
59 | |
60 // Make sure that we remove the operation from the list before invoking the | |
61 // callback (so that a subsequent cancel does not invoke the callback again). | |
62 DCHECK(io_list_.find(operation) != io_list_.end()); | |
63 io_list_.erase(operation); | |
64 OnOperationComplete(operation, cancel_task); | |
65 } | |
66 | |
67 // Runs on the primary thread. | |
68 void InFlightIO::OnOperationPosted(BackgroundIO* operation) { | |
69 DCHECK(callback_thread_ == MessageLoop::current()); | |
70 io_list_.insert(operation); | |
71 } | |
72 | |
73 } // namespace disk_cache | |
OLD | NEW |