OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "net/disk_cache/blockfile/in_flight_io.h" | |
6 | |
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" | |
15 | |
16 namespace disk_cache { | |
17 | |
18 BackgroundIO::BackgroundIO(InFlightIO* controller) | |
19 : result_(-1), io_completed_(true, false), controller_(controller) { | |
20 } | |
21 | |
22 // Runs on the primary thread. | |
23 void BackgroundIO::OnIOSignalled() { | |
24 if (controller_) | |
25 controller_->InvokeCallback(this, false); | |
26 } | |
27 | |
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; | |
33 } | |
34 | |
35 BackgroundIO::~BackgroundIO() { | |
36 } | |
37 | |
38 // --------------------------------------------------------------------------- | |
39 | |
40 InFlightIO::InFlightIO() | |
41 : callback_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
42 running_(false), | |
43 single_thread_(false) { | |
44 } | |
45 | |
46 InFlightIO::~InFlightIO() { | |
47 } | |
48 | |
49 // Runs on the background thread. | |
50 void BackgroundIO::NotifyController() { | |
51 base::AutoLock lock(controller_lock_); | |
52 if (controller_) | |
53 controller_->OnIOComplete(this); | |
54 } | |
55 | |
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); | |
61 } | |
62 } | |
63 | |
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)); | |
71 } | |
72 } | |
73 | |
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; | |
80 } | |
81 #endif | |
82 | |
83 callback_task_runner_->PostTask( | |
84 FROM_HERE, base::Bind(&BackgroundIO::OnIOSignalled, operation)); | |
85 operation->io_completed()->Signal(); | |
86 } | |
87 | |
88 // Runs on the primary thread. | |
89 void InFlightIO::InvokeCallback(BackgroundIO* operation, bool cancel_task) { | |
90 { | |
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")); | |
94 | |
95 // http://crbug.com/74623 | |
96 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
97 operation->io_completed()->Wait(); | |
98 } | |
99 running_ = true; | |
100 | |
101 if (cancel_task) | |
102 operation->Cancel(); | |
103 | |
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); | |
110 } | |
111 | |
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)); | |
116 } | |
117 | |
118 } // namespace disk_cache | |
OLD | NEW |