| 6 | #define BASE_ONE_SHOT_EVENT_H_ |
| 7 |
| 8 | #include <vector> |
| 9 |
| 10 | #include "base/callback_forward.h" |
| 11 | #include "base/logging.h" |
| 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "base/memory/weak_ptr.h" |
| 14 | #include "base/threading/thread_checker.h" |
| 15 |
| 16 | namespace base { |
| 17 |
| 18 | class Location; |
| 19 | class SingleThreadTaskRunner; |
| 20 | class TimeDelta; |
| 21 |
| 22 | // This class represents an event that's expected to happen once. It |
| 23 | // allows clients to guarantee that code is run after the OneShotEvent |
| 24 | // is signaled. If the OneShotEvent is destroyed before it's |
| 25 | // signaled, the closures are destroyed without being run. |
| 26 | // |
| 27 | // This class is similar to a WaitableEvent combined with several |
| 28 | // WaitableEventWatchers, but using it is simpler. |
| 29 | // |
| 30 | // This class is not thread-safe, and must be used from a single thread. |
| 31 | class BASE_EXPORT OneShotEvent { |
| 32 | public: |
| 33 | OneShotEvent(); |
| 34 | // Use the following constructor to create an already signaled event. This is |
| 35 | // useful if you construct the event on a different thread from where it is |
| 36 | // used, in which case it is not possible to call Signal() just after |
| 37 | // construction. |
| 38 | explicit OneShotEvent(bool signaled); |
| 39 | ~OneShotEvent(); |
| 40 |
| 41 | // True if Signal has been called. This function is mostly for |
| 42 | // migrating old code; usually calling Post() unconditionally will |
| 43 | // result in more readable code. |
| 44 | bool is_signaled() const { |
| 45 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 46 | return signaled_; |
| 47 | } |
| 48 |
| 49 | // Causes is_signaled() to return true and all queued tasks to be |
| 50 | // run in an arbitrary order. This method must only be called once. |
| 51 | void Signal(); |
| 52 |
| 53 | // Scheduled |task| to be called on |runner| after is_signaled() |
| 54 | // becomes true. If called with |delay|, then the task will happen |
| 55 | // (roughly) |delay| after is_signaled(), *not* |delay| after the |
| 56 | // post. Inside |task|, if this OneShotEvent is still alive, |
| 57 | // CHECK(is_signaled()) will never fail (which implies that |
| 58 | // OneShotEvent::Reset() doesn't exist). |
| 59 | // |
| 60 | // If |*this| is destroyed before being released, none of these |
| 61 | // tasks will be executed. |
| 62 | // |
| 63 | // Omitting the |runner| argument indicates that |task| should run |
| 64 | // on current thread's TaskRunner. |
| 65 | // |
| 66 | // Tasks may be run in an arbitrary order, not just FIFO. Tasks |
| 67 | // will never be called on the current thread before this function |
| 68 | // returns. Beware that there's no simple way to wait for all tasks |
| 69 | // on a OneShotEvent to complete, so it's almost never safe to use |
| 70 | // base::Unretained() when creating one. |
| 71 | // |
| 72 | // Const because Post() doesn't modify the logical state of this |
| 73 | // object (which is just the is_signaled() bit). |
| 74 | void Post(const Location& from_here, OnceClosure task) const; |
| 75 | void Post(const Location& from_here, |
| 76 | OnceClosure task, |
| 77 | const scoped_refptr<SingleThreadTaskRunner>& runner) const; |
| 78 | void PostDelayed(const Location& from_here, |
| 79 | OnceClosure task, |
| 80 | const TimeDelta& delay) const; |
| 81 |
| 82 | private: |
| 83 | struct TaskInfo; |
| 84 |
| 85 | void PostImpl(const Location& from_here, |
| 86 | OnceClosure task, |
| 87 | const scoped_refptr<SingleThreadTaskRunner>& runner, |
| 88 | const TimeDelta& delay) const; |
| 89 |
| 90 | ThreadChecker thread_checker_; |
| 91 |
| 92 | bool signaled_; |
| 93 |
| 94 | // The task list is mutable because it's not part of the logical |
| 95 | // state of the object. This lets us return const references to the |
| 96 | // OneShotEvent to clients that just want to run tasks through it |
| 97 | // without worrying that they'll signal the event. |
| 98 | // |
| 99 | // Optimization note: We could reduce the size of this class to a |
| 100 | // single pointer by storing |signaled_| in the low bit of a |
| 101 | // pointer, and storing the size and capacity of the array (if any) |
| 102 | // on the far end of the pointer. |
| 103 | mutable std::vector<TaskInfo> tasks_; |
| 104 | }; |
| 105 |
| 106 | } // namespace base |
| 107 |
| 108 | #endif // BASE_ONE_SHOT_EVENT_H_ |
| 109 |