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