OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 EXTENSIONS_COMMON_ONE_SHOT_EVENT_H_ | 5 #ifndef EXTENSIONS_COMMON_ONE_SHOT_EVENT_H_ |
6 #define EXTENSIONS_COMMON_ONE_SHOT_EVENT_H_ | 6 #define EXTENSIONS_COMMON_ONE_SHOT_EVENT_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 class TaskRunner; | 17 class SingleThreadTaskRunner; |
18 class TimeDelta; | 18 class TimeDelta; |
19 } | 19 } |
20 | 20 |
21 namespace tracked_objects { | 21 namespace tracked_objects { |
22 class Location; | 22 class Location; |
23 } | 23 } |
24 | 24 |
25 namespace extensions { | 25 namespace extensions { |
26 | 26 |
27 // This class represents an event that's expected to happen once. It | 27 // This class represents an event that's expected to happen once. It |
(...skipping 29 matching lines...) Expand all Loading... | |
57 | 57 |
58 // Scheduled |task| to be called on |runner| after is_signaled() | 58 // Scheduled |task| to be called on |runner| after is_signaled() |
59 // becomes true. If called with |delay|, then the task will happen | 59 // becomes true. If called with |delay|, then the task will happen |
60 // (roughly) |delay| after is_signaled(), *not* |delay| after the | 60 // (roughly) |delay| after is_signaled(), *not* |delay| after the |
61 // post. Inside |task|, if this OneShotEvent is still alive, | 61 // post. Inside |task|, if this OneShotEvent is still alive, |
62 // CHECK(is_signaled()) will never fail (which implies that | 62 // CHECK(is_signaled()) will never fail (which implies that |
63 // OneShotEvent::Reset() doesn't exist). | 63 // OneShotEvent::Reset() doesn't exist). |
64 // | 64 // |
65 // If |*this| is destroyed before being released, none of these | 65 // If |*this| is destroyed before being released, none of these |
66 // tasks will be executed. | 66 // tasks will be executed. |
67 // | 67 // |
Ken Rockot(use gerrit already)
2015/05/04 18:19:58
This should not be deleted. Maybe change to "Omitt
Pranay
2015/05/05 03:07:58
Done.
| |
68 // Omitting the |runner| argument indicates that |task| should run | |
69 // on MessageLoopProxy::current(). | |
70 // | 68 // |
71 // Tasks may be run in an arbitrary order, not just FIFO. Tasks | 69 // Tasks may be run in an arbitrary order, not just FIFO. Tasks |
72 // will never be called on the current thread before this function | 70 // will never be called on the current thread before this function |
73 // returns. Beware that there's no simple way to wait for all tasks | 71 // returns. Beware that there's no simple way to wait for all tasks |
74 // on a OneShotEvent to complete, so it's almost never safe to use | 72 // on a OneShotEvent to complete, so it's almost never safe to use |
75 // base::Unretained() when creating one. | 73 // base::Unretained() when creating one. |
76 // | 74 // |
77 // Const because Post() doesn't modify the logical state of this | 75 // Const because Post() doesn't modify the logical state of this |
78 // object (which is just the is_signaled() bit). | 76 // object (which is just the is_signaled() bit). |
79 void Post(const tracked_objects::Location& from_here, | 77 void Post(const tracked_objects::Location& from_here, |
80 const base::Closure& task) const; | 78 const base::Closure& task) const; |
81 void Post(const tracked_objects::Location& from_here, | 79 void Post(const tracked_objects::Location& from_here, |
82 const base::Closure& task, | 80 const base::Closure& task, |
83 const scoped_refptr<base::TaskRunner>& runner) const; | 81 const scoped_refptr<base::SingleThreadTaskRunner>& runner) const; |
84 void PostDelayed(const tracked_objects::Location& from_here, | 82 void PostDelayed(const tracked_objects::Location& from_here, |
85 const base::Closure& task, | 83 const base::Closure& task, |
86 const base::TimeDelta& delay) const; | 84 const base::TimeDelta& delay) const; |
87 | 85 |
88 private: | 86 private: |
89 struct TaskInfo; | 87 struct TaskInfo; |
90 | 88 |
91 void PostImpl(const tracked_objects::Location& from_here, | 89 void PostImpl(const tracked_objects::Location& from_here, |
92 const base::Closure& task, | 90 const base::Closure& task, |
93 const scoped_refptr<base::TaskRunner>& runner, | 91 const scoped_refptr<base::SingleThreadTaskRunner>& runner, |
94 const base::TimeDelta& delay) const; | 92 const base::TimeDelta& delay) const; |
95 | 93 |
96 base::ThreadChecker thread_checker_; | 94 base::ThreadChecker thread_checker_; |
97 | 95 |
98 bool signaled_; | 96 bool signaled_; |
99 | 97 |
100 // The task list is mutable because it's not part of the logical | 98 // The task list is mutable because it's not part of the logical |
101 // state of the object. This lets us return const references to the | 99 // state of the object. This lets us return const references to the |
102 // OneShotEvent to clients that just want to run tasks through it | 100 // OneShotEvent to clients that just want to run tasks through it |
103 // without worrying that they'll signal the event. | 101 // without worrying that they'll signal the event. |
104 // | 102 // |
105 // Optimization note: We could reduce the size of this class to a | 103 // Optimization note: We could reduce the size of this class to a |
106 // single pointer by storing |signaled_| in the low bit of a | 104 // single pointer by storing |signaled_| in the low bit of a |
107 // pointer, and storing the size and capacity of the array (if any) | 105 // pointer, and storing the size and capacity of the array (if any) |
108 // on the far end of the pointer. | 106 // on the far end of the pointer. |
109 mutable std::vector<TaskInfo> tasks_; | 107 mutable std::vector<TaskInfo> tasks_; |
110 }; | 108 }; |
111 | 109 |
112 } // namespace extensions | 110 } // namespace extensions |
113 | 111 |
114 #endif // EXTENSIONS_COMMON_ONE_SHOT_EVENT_H_ | 112 #endif // EXTENSIONS_COMMON_ONE_SHOT_EVENT_H_ |
OLD | NEW |