| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/disk_cache/file.h" | 5 #include "net/disk_cache/file.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 186 |
| 187 // --------------------------------------------------------------------------- | 187 // --------------------------------------------------------------------------- |
| 188 | 188 |
| 189 void InFlightIO::PostRead(disk_cache::File *file, void* buf, size_t buf_len, | 189 void InFlightIO::PostRead(disk_cache::File *file, void* buf, size_t buf_len, |
| 190 size_t offset, disk_cache::FileIOCallback *callback) { | 190 size_t offset, disk_cache::FileIOCallback *callback) { |
| 191 scoped_refptr<BackgroundIO> operation = | 191 scoped_refptr<BackgroundIO> operation = |
| 192 new BackgroundIO(file, buf, buf_len, offset, callback, this); | 192 new BackgroundIO(file, buf, buf_len, offset, callback, this); |
| 193 io_list_.insert(operation.get()); | 193 io_list_.insert(operation.get()); |
| 194 file->AddRef(); // Balanced on InvokeCallback() | 194 file->AddRef(); // Balanced on InvokeCallback() |
| 195 | 195 |
| 196 if (!callback_thread_) |
| 197 callback_thread_ = MessageLoop::current(); |
| 198 |
| 196 WorkerPool::PostTask(FROM_HERE, | 199 WorkerPool::PostTask(FROM_HERE, |
| 197 NewRunnableMethod(operation.get(), &BackgroundIO::Read), | 200 NewRunnableMethod(operation.get(), &BackgroundIO::Read), |
| 198 true); | 201 true); |
| 199 } | 202 } |
| 200 | 203 |
| 201 void InFlightIO::PostWrite(disk_cache::File* file, const void* buf, | 204 void InFlightIO::PostWrite(disk_cache::File* file, const void* buf, |
| 202 size_t buf_len, size_t offset, | 205 size_t buf_len, size_t offset, |
| 203 disk_cache::FileIOCallback* callback, | 206 disk_cache::FileIOCallback* callback, |
| 204 bool delete_buffer) { | 207 bool delete_buffer) { |
| 205 scoped_refptr<BackgroundIO> operation = | 208 scoped_refptr<BackgroundIO> operation = |
| 206 new BackgroundIO(file, buf, buf_len, offset, callback, this); | 209 new BackgroundIO(file, buf, buf_len, offset, callback, this); |
| 207 io_list_.insert(operation.get()); | 210 io_list_.insert(operation.get()); |
| 208 file->AddRef(); // Balanced on InvokeCallback() | 211 file->AddRef(); // Balanced on InvokeCallback() |
| 209 | 212 |
| 213 if (!callback_thread_) |
| 214 callback_thread_ = MessageLoop::current(); |
| 215 |
| 210 WorkerPool::PostTask(FROM_HERE, | 216 WorkerPool::PostTask(FROM_HERE, |
| 211 NewRunnableMethod(operation.get(), &BackgroundIO::Write, | 217 NewRunnableMethod(operation.get(), &BackgroundIO::Write, |
| 212 delete_buffer), | 218 delete_buffer), |
| 213 true); | 219 true); |
| 214 } | 220 } |
| 215 | 221 |
| 216 void InFlightIO::WaitForPendingIO() { | 222 void InFlightIO::WaitForPendingIO() { |
| 217 while (!io_list_.empty()) { | 223 while (!io_list_.empty()) { |
| 218 // Block the current thread until all pending IO completes. | 224 // Block the current thread until all pending IO completes. |
| 219 IOList::iterator it = io_list_.begin(); | 225 IOList::iterator it = io_list_.begin(); |
| 220 InvokeCallback(*it, true); | 226 InvokeCallback(*it, true); |
| 221 } | 227 } |
| 228 // Unit tests can use different threads. |
| 229 callback_thread_ = NULL; |
| 222 } | 230 } |
| 223 | 231 |
| 224 // Runs on a worker thread. | 232 // Runs on a worker thread. |
| 225 void InFlightIO::OnIOComplete(BackgroundIO* operation) { | 233 void InFlightIO::OnIOComplete(BackgroundIO* operation) { |
| 226 callback_thread_->PostTask(FROM_HERE, | 234 callback_thread_->PostTask(FROM_HERE, |
| 227 NewRunnableMethod(operation, | 235 NewRunnableMethod(operation, |
| 228 &BackgroundIO::OnIOSignalled)); | 236 &BackgroundIO::OnIOSignalled)); |
| 229 operation->io_completed()->Signal(); | 237 operation->io_completed()->Signal(); |
| 230 } | 238 } |
| 231 | 239 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 } | 373 } |
| 366 | 374 |
| 367 size_t File::GetLength() { | 375 size_t File::GetLength() { |
| 368 DCHECK(init_); | 376 DCHECK(init_); |
| 369 size_t ret = lseek(platform_file_, 0, SEEK_END); | 377 size_t ret = lseek(platform_file_, 0, SEEK_END); |
| 370 return ret; | 378 return ret; |
| 371 } | 379 } |
| 372 | 380 |
| 373 // Static. | 381 // Static. |
| 374 void File::WaitForPendingIO(int* num_pending_io) { | 382 void File::WaitForPendingIO(int* num_pending_io) { |
| 375 if (*num_pending_io) | 383 // We may be running unit tests so we should allow InFlightIO to reset the |
| 376 Singleton<InFlightIO>::get()->WaitForPendingIO(); | 384 // message loop. |
| 385 Singleton<InFlightIO>::get()->WaitForPendingIO(); |
| 377 } | 386 } |
| 378 | 387 |
| 379 } // namespace disk_cache | 388 } // namespace disk_cache |
| OLD | NEW |