OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this |
2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
3 // LICENSE file. | 3 // LICENSE file. |
4 | 4 |
5 // For 64-bit file access (off_t = off64_t, lseek64, etc). | 5 // For 64-bit file access (off_t = off64_t, lseek64, etc). |
6 #define _FILE_OFFSET_BITS 64 | 6 #define _FILE_OFFSET_BITS 64 |
7 | 7 |
8 #include "net/base/file_stream.h" | 8 #include "net/base/file_stream.h" |
9 | 9 |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 | 202 |
203 // This is used to synchronize between the AsyncContext destructor (which runs | 203 // This is used to synchronize between the AsyncContext destructor (which runs |
204 // on the IO thread and OnBackgroundIOCompleted() which runs on the WorkerPool | 204 // on the IO thread and OnBackgroundIOCompleted() which runs on the WorkerPool |
205 // thread. | 205 // thread. |
206 base::WaitableEvent background_io_completed_; | 206 base::WaitableEvent background_io_completed_; |
207 | 207 |
208 // These variables are only valid when background_io_completed is signaled. | 208 // These variables are only valid when background_io_completed is signaled. |
209 int result_; | 209 int result_; |
210 CancelableCallbackTask* message_loop_task_; | 210 CancelableCallbackTask* message_loop_task_; |
211 | 211 |
| 212 bool is_closing_; |
| 213 |
212 DISALLOW_COPY_AND_ASSIGN(AsyncContext); | 214 DISALLOW_COPY_AND_ASSIGN(AsyncContext); |
213 }; | 215 }; |
214 | 216 |
215 FileStream::AsyncContext::AsyncContext() | 217 FileStream::AsyncContext::AsyncContext() |
216 : message_loop_(MessageLoopForIO::current()), | 218 : message_loop_(MessageLoopForIO::current()), |
217 callback_(NULL), | 219 callback_(NULL), |
218 background_io_completed_callback_( | 220 background_io_completed_callback_( |
219 this, &AsyncContext::OnBackgroundIOCompleted), | 221 this, &AsyncContext::OnBackgroundIOCompleted), |
220 background_io_completed_(true, false), | 222 background_io_completed_(true, false), |
221 message_loop_task_(NULL) {} | 223 message_loop_task_(NULL), |
| 224 is_closing_(false) {} |
222 | 225 |
223 FileStream::AsyncContext::~AsyncContext() { | 226 FileStream::AsyncContext::~AsyncContext() { |
| 227 is_closing_ = true; |
224 if (callback_) { | 228 if (callback_) { |
225 // If |callback_| is non-NULL, that implies either the worker thread is | 229 // If |callback_| is non-NULL, that implies either the worker thread is |
226 // still running the IO task, or the completion callback is queued up on the | 230 // still running the IO task, or the completion callback is queued up on the |
227 // MessageLoopForIO, but AsyncContext() got deleted before then. | 231 // MessageLoopForIO, but AsyncContext() got deleted before then. |
228 const bool need_to_wait = !background_io_completed_.IsSignaled(); | 232 const bool need_to_wait = !background_io_completed_.IsSignaled(); |
229 base::Time start = base::Time::Now(); | 233 base::Time start = base::Time::Now(); |
230 RunAsynchronousCallback(); | 234 RunAsynchronousCallback(); |
231 if (need_to_wait) { | 235 if (need_to_wait) { |
232 // We want to see if we block the message loop for too long. | 236 // We want to see if we block the message loop for too long. |
233 UMA_HISTOGRAM_TIMES("AsyncIO.FileStreamClose", base::Time::Now() - start); | 237 UMA_HISTOGRAM_TIMES("AsyncIO.FileStreamClose", base::Time::Now() - start); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 // Wait() here ensures that all modifications from the WorkerPool thread are | 277 // Wait() here ensures that all modifications from the WorkerPool thread are |
274 // now visible. | 278 // now visible. |
275 background_io_completed_.Wait(); | 279 background_io_completed_.Wait(); |
276 | 280 |
277 // Either we're in the MessageLoop's task, in which case Cancel() doesn't do | 281 // Either we're in the MessageLoop's task, in which case Cancel() doesn't do |
278 // anything, or we're in ~AsyncContext(), in which case this prevents the call | 282 // anything, or we're in ~AsyncContext(), in which case this prevents the call |
279 // from happening again. Must do it here after calling Wait(). | 283 // from happening again. Must do it here after calling Wait(). |
280 message_loop_task_->Cancel(); | 284 message_loop_task_->Cancel(); |
281 message_loop_task_ = NULL; | 285 message_loop_task_ = NULL; |
282 | 286 |
| 287 if (is_closing_) { |
| 288 callback_ = NULL; |
| 289 return; |
| 290 } |
| 291 |
283 DCHECK(callback_); | 292 DCHECK(callback_); |
284 CompletionCallback* temp = NULL; | 293 CompletionCallback* temp = NULL; |
285 std::swap(temp, callback_); | 294 std::swap(temp, callback_); |
286 background_io_completed_.Reset(); | 295 background_io_completed_.Reset(); |
287 temp->Run(result_); | 296 temp->Run(result_); |
288 } | 297 } |
289 | 298 |
290 // FileStream ------------------------------------------------------------ | 299 // FileStream ------------------------------------------------------------ |
291 | 300 |
292 FileStream::FileStream() : file_(base::kInvalidPlatformFileValue) { | 301 FileStream::FileStream() : file_(base::kInvalidPlatformFileValue) { |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 int64 seek_position = Seek(FROM_BEGIN, bytes); | 455 int64 seek_position = Seek(FROM_BEGIN, bytes); |
447 if (seek_position != bytes) | 456 if (seek_position != bytes) |
448 return ERR_UNEXPECTED; | 457 return ERR_UNEXPECTED; |
449 | 458 |
450 // And truncate the file. | 459 // And truncate the file. |
451 int result = ftruncate(file_, bytes); | 460 int result = ftruncate(file_, bytes); |
452 return result == 0 ? seek_position : MapErrorCode(errno); | 461 return result == 0 ? seek_position : MapErrorCode(errno); |
453 } | 462 } |
454 | 463 |
455 } // namespace net | 464 } // namespace net |
OLD | NEW |