| OLD | NEW |
| 1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef NET_DISK_CACHE_IN_FLIGHT_IO_H_ | 5 #ifndef NET_DISK_CACHE_IN_FLIGHT_IO_H_ |
| 6 #define NET_DISK_CACHE_IN_FLIGHT_IO_H_ | 6 #define NET_DISK_CACHE_IN_FLIGHT_IO_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
| 12 #include "base/synchronization/lock.h" |
| 12 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
| 13 | 14 |
| 14 namespace disk_cache { | 15 namespace disk_cache { |
| 15 | 16 |
| 16 class InFlightIO; | 17 class InFlightIO; |
| 17 | 18 |
| 18 // This class represents a single asynchronous IO operation while it is being | 19 // This class represents a single asynchronous IO operation while it is being |
| 19 // bounced between threads. | 20 // bounced between threads. |
| 20 class BackgroundIO : public base::RefCountedThreadSafe<BackgroundIO> { | 21 class BackgroundIO : public base::RefCountedThreadSafe<BackgroundIO> { |
| 21 public: | 22 public: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 41 | 42 |
| 42 int result() { return result_; } | 43 int result() { return result_; } |
| 43 | 44 |
| 44 base::WaitableEvent* io_completed() { | 45 base::WaitableEvent* io_completed() { |
| 45 return &io_completed_; | 46 return &io_completed_; |
| 46 } | 47 } |
| 47 | 48 |
| 48 protected: | 49 protected: |
| 49 virtual ~BackgroundIO(); | 50 virtual ~BackgroundIO(); |
| 50 | 51 |
| 51 InFlightIO* controller_; // The controller that tracks all operations. | 52 // Notifies the controller about the end of the operation, from the background |
| 53 // thread. |
| 54 void NotifyController(); |
| 55 |
| 52 int result_; // Final operation result. | 56 int result_; // Final operation result. |
| 53 | 57 |
| 54 private: | 58 private: |
| 55 friend class base::RefCountedThreadSafe<BackgroundIO>; | 59 friend class base::RefCountedThreadSafe<BackgroundIO>; |
| 56 | 60 |
| 57 // Notifies the controller about the end of the operation, from the background | |
| 58 // thread. | |
| 59 void NotifyController(); | |
| 60 | |
| 61 // An event to signal when the operation completes. | 61 // An event to signal when the operation completes. |
| 62 base::WaitableEvent io_completed_; | 62 base::WaitableEvent io_completed_; |
| 63 InFlightIO* controller_; // The controller that tracks all operations. |
| 64 base::Lock controller_lock_; // A lock protecting clearing of controller_. |
| 63 | 65 |
| 64 DISALLOW_COPY_AND_ASSIGN(BackgroundIO); | 66 DISALLOW_COPY_AND_ASSIGN(BackgroundIO); |
| 65 }; | 67 }; |
| 66 | 68 |
| 67 // This class keeps track of asynchronous IO operations. A single instance | 69 // This class keeps track of asynchronous IO operations. A single instance |
| 68 // of this class is meant to be used to start an asynchronous operation (using | 70 // of this class is meant to be used to start an asynchronous operation (using |
| 69 // PostXX, exposed by a derived class). This class will post the operation to a | 71 // PostXX, exposed by a derived class). This class will post the operation to a |
| 70 // worker thread, hanlde the notification when the operation finishes and | 72 // worker thread, hanlde the notification when the operation finishes and |
| 71 // perform the callback on the same thread that was used to start the operation. | 73 // perform the callback on the same thread that was used to start the operation. |
| 72 // | 74 // |
| (...skipping 15 matching lines...) Expand all Loading... |
| 88 // of just waiting for step 7. | 90 // of just waiting for step 7. |
| 89 class InFlightIO { | 91 class InFlightIO { |
| 90 public: | 92 public: |
| 91 InFlightIO(); | 93 InFlightIO(); |
| 92 virtual ~InFlightIO(); | 94 virtual ~InFlightIO(); |
| 93 | 95 |
| 94 // Blocks the current thread until all IO operations tracked by this object | 96 // Blocks the current thread until all IO operations tracked by this object |
| 95 // complete. | 97 // complete. |
| 96 void WaitForPendingIO(); | 98 void WaitForPendingIO(); |
| 97 | 99 |
| 100 // Drops current pending operation without waiting for them to complete. |
| 101 void DropPendingIO(); |
| 102 |
| 98 // Called on a background thread when |operation| completes. | 103 // Called on a background thread when |operation| completes. |
| 99 void OnIOComplete(BackgroundIO* operation); | 104 void OnIOComplete(BackgroundIO* operation); |
| 100 | 105 |
| 101 // Invokes the users' completion callback at the end of the IO operation. | 106 // Invokes the users' completion callback at the end of the IO operation. |
| 102 // |cancel_task| is true if the actual task posted to the thread is still | 107 // |cancel_task| is true if the actual task posted to the thread is still |
| 103 // queued (because we are inside WaitForPendingIO), and false if said task is | 108 // queued (because we are inside WaitForPendingIO), and false if said task is |
| 104 // the one performing the call. | 109 // the one performing the call. |
| 105 void InvokeCallback(BackgroundIO* operation, bool cancel_task); | 110 void InvokeCallback(BackgroundIO* operation, bool cancel_task); |
| 106 | 111 |
| 107 protected: | 112 protected: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 123 | 128 |
| 124 bool running_; // True after the first posted operation completes. | 129 bool running_; // True after the first posted operation completes. |
| 125 bool single_thread_; // True if we only have one thread. | 130 bool single_thread_; // True if we only have one thread. |
| 126 | 131 |
| 127 DISALLOW_COPY_AND_ASSIGN(InFlightIO); | 132 DISALLOW_COPY_AND_ASSIGN(InFlightIO); |
| 128 }; | 133 }; |
| 129 | 134 |
| 130 } // namespace disk_cache | 135 } // namespace disk_cache |
| 131 | 136 |
| 132 #endif // NET_DISK_CACHE_IN_FLIGHT_IO_H_ | 137 #endif // NET_DISK_CACHE_IN_FLIGHT_IO_H_ |
| OLD | NEW |