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 TaskRunner; |
| 18 class TimeDelta; |
18 } | 19 } |
19 | 20 |
20 namespace tracked_objects { | 21 namespace tracked_objects { |
21 class Location; | 22 class Location; |
22 } | 23 } |
23 | 24 |
24 namespace extensions { | 25 namespace extensions { |
25 | 26 |
26 // 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 |
27 // allows clients to guarantee that code is run after the OneShotEvent | 28 // allows clients to guarantee that code is run after the OneShotEvent |
(...skipping 20 matching lines...) Expand all Loading... |
48 bool is_signaled() const { | 49 bool is_signaled() const { |
49 DCHECK(thread_checker_.CalledOnValidThread()); | 50 DCHECK(thread_checker_.CalledOnValidThread()); |
50 return signaled_; | 51 return signaled_; |
51 } | 52 } |
52 | 53 |
53 // Causes is_signaled() to return true and all queued tasks to be | 54 // Causes is_signaled() to return true and all queued tasks to be |
54 // run in an arbitrary order. This method must only be called once. | 55 // run in an arbitrary order. This method must only be called once. |
55 void Signal(); | 56 void Signal(); |
56 | 57 |
57 // Scheduled |task| to be called on |runner| after is_signaled() | 58 // Scheduled |task| to be called on |runner| after is_signaled() |
58 // becomes true. Inside |task|, if this OneShotEvent is still | 59 // becomes true. If called with |delay|, then the task will happen |
59 // alive, CHECK(is_signaled()) will never fail (which implies that | 60 // (roughly) |delay| after is_signaled(), *not* |delay| after the |
| 61 // post. Inside |task|, if this OneShotEvent is still alive, |
| 62 // CHECK(is_signaled()) will never fail (which implies that |
60 // OneShotEvent::Reset() doesn't exist). | 63 // OneShotEvent::Reset() doesn't exist). |
61 // | 64 // |
62 // If |*this| is destroyed before being released, none of these | 65 // If |*this| is destroyed before being released, none of these |
63 // tasks will be executed. | 66 // tasks will be executed. |
64 // | 67 // |
65 // Omitting the |runner| argument indicates that |task| should run | 68 // Omitting the |runner| argument indicates that |task| should run |
66 // on MessageLoopProxy::current(). | 69 // on MessageLoopProxy::current(). |
67 // | 70 // |
68 // Tasks may be run in an arbitrary order, not just FIFO. Tasks | 71 // Tasks may be run in an arbitrary order, not just FIFO. Tasks |
69 // will never be called on the current thread before this function | 72 // will never be called on the current thread before this function |
70 // returns. Beware that there's no simple way to wait for all tasks | 73 // returns. Beware that there's no simple way to wait for all tasks |
71 // on a OneShotEvent to complete, so it's almost never safe to use | 74 // on a OneShotEvent to complete, so it's almost never safe to use |
72 // base::Unretained() when creating one. | 75 // base::Unretained() when creating one. |
73 // | 76 // |
74 // Const because Post() doesn't modify the logical state of this | 77 // Const because Post() doesn't modify the logical state of this |
75 // object (which is just the is_signaled() bit). | 78 // object (which is just the is_signaled() bit). |
76 void Post(const tracked_objects::Location& from_here, | 79 void Post(const tracked_objects::Location& from_here, |
77 const base::Closure& task) const; | 80 const base::Closure& task) const; |
78 void Post(const tracked_objects::Location& from_here, | 81 void Post(const tracked_objects::Location& from_here, |
79 const base::Closure& task, | 82 const base::Closure& task, |
80 const scoped_refptr<base::TaskRunner>& runner) const; | 83 const scoped_refptr<base::TaskRunner>& runner) const; |
| 84 void PostDelayed(const tracked_objects::Location& from_here, |
| 85 const base::Closure& task, |
| 86 const base::TimeDelta& delay) const; |
81 | 87 |
82 private: | 88 private: |
83 struct TaskInfo; | 89 struct TaskInfo; |
84 | 90 |
| 91 void PostImpl(const tracked_objects::Location& from_here, |
| 92 const base::Closure& task, |
| 93 const scoped_refptr<base::TaskRunner>& runner, |
| 94 const base::TimeDelta& delay) const; |
| 95 |
85 base::ThreadChecker thread_checker_; | 96 base::ThreadChecker thread_checker_; |
86 | 97 |
87 bool signaled_; | 98 bool signaled_; |
88 | 99 |
89 // The task list is mutable because it's not part of the logical | 100 // The task list is mutable because it's not part of the logical |
90 // state of the object. This lets us return const references to the | 101 // state of the object. This lets us return const references to the |
91 // OneShotEvent to clients that just want to run tasks through it | 102 // OneShotEvent to clients that just want to run tasks through it |
92 // without worrying that they'll signal the event. | 103 // without worrying that they'll signal the event. |
93 // | 104 // |
94 // Optimization note: We could reduce the size of this class to a | 105 // Optimization note: We could reduce the size of this class to a |
95 // single pointer by storing |signaled_| in the low bit of a | 106 // single pointer by storing |signaled_| in the low bit of a |
96 // pointer, and storing the size and capacity of the array (if any) | 107 // pointer, and storing the size and capacity of the array (if any) |
97 // on the far end of the pointer. | 108 // on the far end of the pointer. |
98 mutable std::vector<TaskInfo> tasks_; | 109 mutable std::vector<TaskInfo> tasks_; |
99 }; | 110 }; |
100 | 111 |
101 } // namespace extensions | 112 } // namespace extensions |
102 | 113 |
103 #endif // EXTENSIONS_COMMON_ONE_SHOT_EVENT_H_ | 114 #endif // EXTENSIONS_COMMON_ONE_SHOT_EVENT_H_ |
OLD | NEW |