OLD | NEW |
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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/waitable_event.h" | 12 #include "base/waitable_event.h" |
13 | 13 |
14 namespace disk_cache { | 14 namespace disk_cache { |
15 | 15 |
16 class InFlightIO; | 16 class InFlightIO; |
17 | 17 |
18 // This class represents a single asynchronous IO operation while it is being | 18 // This class represents a single asynchronous IO operation while it is being |
19 // bounced between threads. | 19 // bounced between threads. |
20 class BackgroundIO : public base::RefCountedThreadSafe<BackgroundIO> { | 20 class BackgroundIO : public base::RefCountedThreadSafe<BackgroundIO> { |
21 public: | 21 public: |
22 // Other than the actual parameters for the IO operation (including the | 22 // Other than the actual parameters for the IO operation (including the |
23 // |callback| that must be notified at the end), we need the controller that | 23 // |callback| that must be notified at the end), we need the controller that |
24 // is keeping track of all operations. When done, we notify the controller | 24 // is keeping track of all operations. When done, we notify the controller |
25 // (we do NOT invoke the callback), in the worker thead that completed the | 25 // (we do NOT invoke the callback), in the worker thead that completed the |
26 // operation. | 26 // operation. |
27 explicit BackgroundIO(InFlightIO* controller) | 27 explicit BackgroundIO(InFlightIO* controller); |
28 : controller_(controller), result_(-1), io_completed_(true, false) {} | |
29 | 28 |
30 // This method signals the controller that this operation is finished, in the | 29 // This method signals the controller that this operation is finished, in the |
31 // original thread. In practice, this is a RunableMethod that allows | 30 // original thread. In practice, this is a RunableMethod that allows |
32 // cancellation. | 31 // cancellation. |
33 void OnIOSignalled(); | 32 void OnIOSignalled(); |
34 | 33 |
35 // Allows the cancellation of the task to notify the controller (step number 8 | 34 // Allows the cancellation of the task to notify the controller (step number 8 |
36 // in the diagram below). In practice, if the controller waits for the | 35 // in the diagram below). In practice, if the controller waits for the |
37 // operation to finish it doesn't have to wait for the final task to be | 36 // operation to finish it doesn't have to wait for the final task to be |
38 // processed by the message loop so calling this method prevents its delivery. | 37 // processed by the message loop so calling this method prevents its delivery. |
39 // Note that this method is not intended to cancel the actual IO operation or | 38 // Note that this method is not intended to cancel the actual IO operation or |
40 // to prevent the first notification to take place (OnIOComplete). | 39 // to prevent the first notification to take place (OnIOComplete). |
41 void Cancel(); | 40 void Cancel(); |
42 | 41 |
43 int result() { return result_; } | 42 int result() { return result_; } |
44 | 43 |
45 base::WaitableEvent* io_completed() { | 44 base::WaitableEvent* io_completed() { |
46 return &io_completed_; | 45 return &io_completed_; |
47 } | 46 } |
48 | 47 |
49 protected: | 48 protected: |
50 virtual ~BackgroundIO() {} | 49 virtual ~BackgroundIO(); |
51 | 50 |
52 InFlightIO* controller_; // The controller that tracks all operations. | 51 InFlightIO* controller_; // The controller that tracks all operations. |
53 int result_; // Final operation result. | 52 int result_; // Final operation result. |
54 | 53 |
55 private: | 54 private: |
56 friend class base::RefCountedThreadSafe<BackgroundIO>; | 55 friend class base::RefCountedThreadSafe<BackgroundIO>; |
57 | 56 |
58 // Notifies the controller about the end of the operation, from the background | 57 // Notifies the controller about the end of the operation, from the background |
59 // thread. | 58 // thread. |
60 void NotifyController(); | 59 void NotifyController(); |
(...skipping 21 matching lines...) Expand all Loading... |
82 // 7. <- PostTask <- | 81 // 7. <- PostTask <- |
83 // 8. BackgroundIO::OnIOSignalled() | 82 // 8. BackgroundIO::OnIOSignalled() |
84 // 9. InFlightIO::InvokeCallback() | 83 // 9. InFlightIO::InvokeCallback() |
85 // 10. DerivedInFlightIO::OnOperationComplete() | 84 // 10. DerivedInFlightIO::OnOperationComplete() |
86 // 11. invoke callback | 85 // 11. invoke callback |
87 // | 86 // |
88 // Shutdown is a special case that is handled though WaitForPendingIO() instead | 87 // Shutdown is a special case that is handled though WaitForPendingIO() instead |
89 // of just waiting for step 7. | 88 // of just waiting for step 7. |
90 class InFlightIO { | 89 class InFlightIO { |
91 public: | 90 public: |
92 InFlightIO() | 91 InFlightIO(); |
93 : callback_thread_(base::MessageLoopProxy::CreateForCurrentThread()), | 92 virtual ~InFlightIO(); |
94 running_(false), single_thread_(false) {} | |
95 virtual ~InFlightIO() {} | |
96 | 93 |
97 // Blocks the current thread until all IO operations tracked by this object | 94 // Blocks the current thread until all IO operations tracked by this object |
98 // complete. | 95 // complete. |
99 void WaitForPendingIO(); | 96 void WaitForPendingIO(); |
100 | 97 |
101 // Called on a background thread when |operation| completes. | 98 // Called on a background thread when |operation| completes. |
102 void OnIOComplete(BackgroundIO* operation); | 99 void OnIOComplete(BackgroundIO* operation); |
103 | 100 |
104 // Invokes the users' completion callback at the end of the IO operation. | 101 // Invokes the users' completion callback at the end of the IO operation. |
105 // |cancel_task| is true if the actual task posted to the thread is still | 102 // |cancel_task| is true if the actual task posted to the thread is still |
(...skipping 20 matching lines...) Expand all Loading... |
126 | 123 |
127 bool running_; // True after the first posted operation completes. | 124 bool running_; // True after the first posted operation completes. |
128 bool single_thread_; // True if we only have one thread. | 125 bool single_thread_; // True if we only have one thread. |
129 | 126 |
130 DISALLOW_COPY_AND_ASSIGN(InFlightIO); | 127 DISALLOW_COPY_AND_ASSIGN(InFlightIO); |
131 }; | 128 }; |
132 | 129 |
133 } // namespace disk_cache | 130 } // namespace disk_cache |
134 | 131 |
135 #endif // NET_DISK_CACHE_IN_FLIGHT_IO_H_ | 132 #endif // NET_DISK_CACHE_IN_FLIGHT_IO_H_ |
OLD | NEW |