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 | |
199 WorkerPool::PostTask(FROM_HERE, | 196 WorkerPool::PostTask(FROM_HERE, |
200 NewRunnableMethod(operation.get(), &BackgroundIO::Read), | 197 NewRunnableMethod(operation.get(), &BackgroundIO::Read), |
201 true); | 198 true); |
202 } | 199 } |
203 | 200 |
204 void InFlightIO::PostWrite(disk_cache::File* file, const void* buf, | 201 void InFlightIO::PostWrite(disk_cache::File* file, const void* buf, |
205 size_t buf_len, size_t offset, | 202 size_t buf_len, size_t offset, |
206 disk_cache::FileIOCallback* callback, | 203 disk_cache::FileIOCallback* callback, |
207 bool delete_buffer) { | 204 bool delete_buffer) { |
208 scoped_refptr<BackgroundIO> operation = | 205 scoped_refptr<BackgroundIO> operation = |
209 new BackgroundIO(file, buf, buf_len, offset, callback, this); | 206 new BackgroundIO(file, buf, buf_len, offset, callback, this); |
210 io_list_.insert(operation.get()); | 207 io_list_.insert(operation.get()); |
211 file->AddRef(); // Balanced on InvokeCallback() | 208 file->AddRef(); // Balanced on InvokeCallback() |
212 | 209 |
213 if (!callback_thread_) | |
214 callback_thread_ = MessageLoop::current(); | |
215 | |
216 WorkerPool::PostTask(FROM_HERE, | 210 WorkerPool::PostTask(FROM_HERE, |
217 NewRunnableMethod(operation.get(), &BackgroundIO::Write, | 211 NewRunnableMethod(operation.get(), &BackgroundIO::Write, |
218 delete_buffer), | 212 delete_buffer), |
219 true); | 213 true); |
220 } | 214 } |
221 | 215 |
222 void InFlightIO::WaitForPendingIO() { | 216 void InFlightIO::WaitForPendingIO() { |
223 while (!io_list_.empty()) { | 217 while (!io_list_.empty()) { |
224 // Block the current thread until all pending IO completes. | 218 // Block the current thread until all pending IO completes. |
225 IOList::iterator it = io_list_.begin(); | 219 IOList::iterator it = io_list_.begin(); |
226 InvokeCallback(*it, true); | 220 InvokeCallback(*it, true); |
227 } | 221 } |
228 // Unit tests can use different threads. | |
229 callback_thread_ = NULL; | |
230 } | 222 } |
231 | 223 |
232 // Runs on a worker thread. | 224 // Runs on a worker thread. |
233 void InFlightIO::OnIOComplete(BackgroundIO* operation) { | 225 void InFlightIO::OnIOComplete(BackgroundIO* operation) { |
234 callback_thread_->PostTask(FROM_HERE, | 226 callback_thread_->PostTask(FROM_HERE, |
235 NewRunnableMethod(operation, | 227 NewRunnableMethod(operation, |
236 &BackgroundIO::OnIOSignalled)); | 228 &BackgroundIO::OnIOSignalled)); |
237 operation->io_completed()->Signal(); | 229 operation->io_completed()->Signal(); |
238 } | 230 } |
239 | 231 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 } | 365 } |
374 | 366 |
375 size_t File::GetLength() { | 367 size_t File::GetLength() { |
376 DCHECK(init_); | 368 DCHECK(init_); |
377 size_t ret = lseek(platform_file_, 0, SEEK_END); | 369 size_t ret = lseek(platform_file_, 0, SEEK_END); |
378 return ret; | 370 return ret; |
379 } | 371 } |
380 | 372 |
381 // Static. | 373 // Static. |
382 void File::WaitForPendingIO(int* num_pending_io) { | 374 void File::WaitForPendingIO(int* num_pending_io) { |
383 // We may be running unit tests so we should allow InFlightIO to reset the | 375 if (*num_pending_io) |
384 // message loop. | 376 Singleton<InFlightIO>::get()->WaitForPendingIO(); |
385 Singleton<InFlightIO>::get()->WaitForPendingIO(); | |
386 } | 377 } |
387 | 378 |
388 } // namespace disk_cache | 379 } // namespace disk_cache |
OLD | NEW |