[email protected] | 4739f70 | 2012-03-20 23:15:10 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | c2c5cfc | 2014-03-03 16:35:28 | [diff] [blame] | 5 | #include "net/disk_cache/blockfile/in_flight_io.h" |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 6 | |
[email protected] | 940d11f | 2011-10-20 00:27:15 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 8 | #include "base/location.h" |
[email protected] | 81e549a | 2014-08-21 04:19:55 | [diff] [blame] | 9 | #include "base/single_thread_task_runner.h" |
| 10 | #include "base/task_runner.h" |
[email protected] | 3a7b66d | 2012-04-26 16:34:16 | [diff] [blame] | 11 | #include "base/threading/thread_restrictions.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 12 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 13 | |
| 14 | namespace disk_cache { |
| 15 | |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 16 | BackgroundIO::BackgroundIO(InFlightIO* controller) |
gab | 07efd10 | 2016-06-02 13:33:17 | [diff] [blame] | 17 | : result_(-1), |
| 18 | io_completed_(base::WaitableEvent::ResetPolicy::MANUAL, |
| 19 | base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 20 | controller_(controller) {} |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 21 | |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 22 | // Runs on the primary thread. |
| 23 | void BackgroundIO::OnIOSignalled() { |
| 24 | if (controller_) |
| 25 | controller_->InvokeCallback(this, false); |
| 26 | } |
| 27 | |
| 28 | void BackgroundIO::Cancel() { |
[email protected] | 4739f70 | 2012-03-20 23:15:10 | [diff] [blame] | 29 | // controller_ may be in use from the background thread at this time. |
| 30 | base::AutoLock lock(controller_lock_); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 31 | DCHECK(controller_); |
| 32 | controller_ = NULL; |
| 33 | } |
| 34 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 35 | BackgroundIO::~BackgroundIO() = default; |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 36 | |
| 37 | // --------------------------------------------------------------------------- |
| 38 | |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 39 | InFlightIO::InFlightIO() |
[email protected] | 81e549a | 2014-08-21 04:19:55 | [diff] [blame] | 40 | : callback_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
pkasting | 83ae339b | 2016-05-23 22:37:36 | [diff] [blame] | 41 | running_(false) {} |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 42 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 43 | InFlightIO::~InFlightIO() = default; |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 44 | |
[email protected] | 4739f70 | 2012-03-20 23:15:10 | [diff] [blame] | 45 | // Runs on the background thread. |
| 46 | void BackgroundIO::NotifyController() { |
| 47 | base::AutoLock lock(controller_lock_); |
| 48 | if (controller_) |
| 49 | controller_->OnIOComplete(this); |
| 50 | } |
| 51 | |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 52 | void InFlightIO::WaitForPendingIO() { |
| 53 | while (!io_list_.empty()) { |
| 54 | // Block the current thread until all pending IO completes. |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 55 | auto it = io_list_.begin(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 56 | InvokeCallback(it->get(), true); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
[email protected] | 4739f70 | 2012-03-20 23:15:10 | [diff] [blame] | 60 | void InFlightIO::DropPendingIO() { |
| 61 | while (!io_list_.empty()) { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 62 | auto it = io_list_.begin(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 63 | BackgroundIO* operation = it->get(); |
[email protected] | 4739f70 | 2012-03-20 23:15:10 | [diff] [blame] | 64 | operation->Cancel(); |
| 65 | DCHECK(io_list_.find(operation) != io_list_.end()); |
kylechar | da69d88 | 2017-10-04 05:49:52 | [diff] [blame] | 66 | io_list_.erase(base::WrapRefCounted(operation)); |
[email protected] | 4739f70 | 2012-03-20 23:15:10 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
peary2 | aeac7780 | 2017-06-01 04:11:16 | [diff] [blame] | 70 | // Runs in a background sequence. |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 71 | void InFlightIO::OnIOComplete(BackgroundIO* operation) { |
pkasting | 83ae339b | 2016-05-23 22:37:36 | [diff] [blame] | 72 | #if DCHECK_IS_ON() |
peary2 | aeac7780 | 2017-06-01 04:11:16 | [diff] [blame] | 73 | if (callback_task_runner_->RunsTasksInCurrentSequence()) { |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 74 | DCHECK(single_thread_ || !running_); |
| 75 | single_thread_ = true; |
| 76 | } |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 77 | #endif |
| 78 | |
[email protected] | 81e549a | 2014-08-21 04:19:55 | [diff] [blame] | 79 | callback_task_runner_->PostTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame^] | 80 | FROM_HERE, base::BindOnce(&BackgroundIO::OnIOSignalled, operation)); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 81 | operation->io_completed()->Signal(); |
| 82 | } |
| 83 | |
| 84 | // Runs on the primary thread. |
| 85 | void InFlightIO::InvokeCallback(BackgroundIO* operation, bool cancel_task) { |
[email protected] | 3a7b66d | 2012-04-26 16:34:16 | [diff] [blame] | 86 | { |
| 87 | // http://crbug.com/74623 |
Etienne Pierre-doray | 22c3f92 | 2018-11-12 23:54:10 | [diff] [blame] | 88 | base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope allow_wait; |
[email protected] | 3a7b66d | 2012-04-26 16:34:16 | [diff] [blame] | 89 | operation->io_completed()->Wait(); |
| 90 | } |
[email protected] | ec14a18 | 2011-06-21 00:27:54 | [diff] [blame] | 91 | running_ = true; |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 92 | |
| 93 | if (cancel_task) |
| 94 | operation->Cancel(); |
| 95 | |
| 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()); |
[email protected] | 15f5db6 | 2012-03-23 22:14:24 | [diff] [blame] | 99 | DCHECK(!operation->HasOneRef()); |
kylechar | da69d88 | 2017-10-04 05:49:52 | [diff] [blame] | 100 | io_list_.erase(base::WrapRefCounted(operation)); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 101 | OnOperationComplete(operation, cancel_task); |
| 102 | } |
| 103 | |
| 104 | // Runs on the primary thread. |
| 105 | void InFlightIO::OnOperationPosted(BackgroundIO* operation) { |
peary2 | aeac7780 | 2017-06-01 04:11:16 | [diff] [blame] | 106 | DCHECK(callback_task_runner_->RunsTasksInCurrentSequence()); |
kylechar | da69d88 | 2017-10-04 05:49:52 | [diff] [blame] | 107 | io_list_.insert(base::WrapRefCounted(operation)); |
[email protected] | fb2622f | 2010-07-13 18:00:56 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | } // namespace disk_cache |