summaryrefslogtreecommitdiffstats
path: root/net/disk_cache/in_flight_io.cc
diff options
context:
space:
mode:
authorrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-13 18:00:56 +0000
committerrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-13 18:00:56 +0000
commitfb2622f6816ed20ffd8a35994f7372b67613ba92 (patch)
tree2aa33016e72361032264904916c4374e4784fd11 /net/disk_cache/in_flight_io.cc
parentea9a4ee67732b90e834def1cf98be1d047a93063 (diff)
downloadchromium_src-fb2622f6816ed20ffd8a35994f7372b67613ba92.zip
chromium_src-fb2622f6816ed20ffd8a35994f7372b67613ba92.tar.gz
chromium_src-fb2622f6816ed20ffd8a35994f7372b67613ba92.tar.bz2
Disk cache: Switch the disk cache to use the cache_thread.
Add an InFlightBackendIO class that handles posting of cacheoperations back and forth between the IO thread and the cachethread. BUG=26730 TEST=unit tests Review URL: http://codereview.chromium.org/2945002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52185 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/disk_cache/in_flight_io.cc')
-rw-r--r--net/disk_cache/in_flight_io.cc73
1 files changed, 73 insertions, 0 deletions
diff --git a/net/disk_cache/in_flight_io.cc b/net/disk_cache/in_flight_io.cc
new file mode 100644
index 0000000..24b0e9c
--- /dev/null
+++ b/net/disk_cache/in_flight_io.cc
@@ -0,0 +1,73 @@
+// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/disk_cache/in_flight_io.h"
+
+#include "base/logging.h"
+
+namespace disk_cache {
+
+// Runs on the primary thread.
+void BackgroundIO::OnIOSignalled() {
+ if (controller_)
+ controller_->InvokeCallback(this, false);
+}
+
+void BackgroundIO::Cancel() {
+ DCHECK(controller_);
+ controller_ = NULL;
+}
+
+// Runs on the background thread.
+void BackgroundIO::NotifyController() {
+ controller_->OnIOComplete(this);
+}
+
+// ---------------------------------------------------------------------------
+
+void InFlightIO::WaitForPendingIO() {
+ while (!io_list_.empty()) {
+ // Block the current thread until all pending IO completes.
+ IOList::iterator it = io_list_.begin();
+ InvokeCallback(*it, true);
+ }
+}
+
+// Runs on a background thread.
+void InFlightIO::OnIOComplete(BackgroundIO* operation) {
+#ifndef NDEBUG
+ if (callback_thread_ == MessageLoop::current()) {
+ DCHECK(single_thread_ || !running_);
+ single_thread_ = true;
+ }
+ running_ = true;
+#endif
+
+ callback_thread_->PostTask(FROM_HERE,
+ NewRunnableMethod(operation,
+ &BackgroundIO::OnIOSignalled));
+ operation->io_completed()->Signal();
+}
+
+// Runs on the primary thread.
+void InFlightIO::InvokeCallback(BackgroundIO* operation, bool cancel_task) {
+ operation->io_completed()->Wait();
+
+ if (cancel_task)
+ operation->Cancel();
+
+ // Make sure that we remove the operation from the list before invoking the
+ // callback (so that a subsequent cancel does not invoke the callback again).
+ DCHECK(io_list_.find(operation) != io_list_.end());
+ io_list_.erase(operation);
+ OnOperationComplete(operation, cancel_task);
+}
+
+// Runs on the primary thread.
+void InFlightIO::OnOperationPosted(BackgroundIO* operation) {
+ DCHECK(callback_thread_ == MessageLoop::current());
+ io_list_.insert(operation);
+}
+
+} // namespace disk_cache