| 6 |
| 7 | #include <stddef.h> |
| 8 |
| 9 | #include "base/callback.h" |
| 10 | #include "base/location.h" |
| 11 | #include "base/single_thread_task_runner.h" |
| 12 | #include "base/task_runner.h" |
| 13 | #include "base/threading/thread_task_runner_handle.h" |
| 14 | #include "base/time/time.h" |
| 15 |
| 16 | namespace base { |
| 17 |
| 18 | struct OneShotEvent::TaskInfo { |
| 19 | TaskInfo() {} |
| 20 | TaskInfo(const Location& from_here, |
| 21 | const scoped_refptr<SingleThreadTaskRunner>& runner, |
| 22 | OnceClosure task, |
| 23 | const TimeDelta& delay) |
| 24 | : from_here(from_here), |
| 25 | runner(runner), |
| 26 | task(std::move(task)), |
| 27 | delay(delay) { |
| 28 | CHECK(runner.get()); // Detect mistakes with a decent stack frame. |
| 29 | } |
| 30 | TaskInfo(TaskInfo&&) = default; |
| 31 |
| 32 | Location from_here; |
| 33 | scoped_refptr<SingleThreadTaskRunner> runner; |
| 34 | OnceClosure task; |
| 35 | TimeDelta delay; |
| 36 |
| 37 | private: |
| 38 | DISALLOW_COPY_AND_ASSIGN(TaskInfo); |
| 39 | }; |
| 40 |
| 41 | OneShotEvent::OneShotEvent() : signaled_(false) { |
| 42 | // It's acceptable to construct the OneShotEvent on one thread, but |
| 43 | // immediately move it to another thread. |
| 44 | thread_checker_.DetachFromThread(); |
| 45 | } |
| 46 | OneShotEvent::OneShotEvent(bool signaled) : signaled_(signaled) { |
| 47 | thread_checker_.DetachFromThread(); |
| 48 | } |
| 49 | OneShotEvent::~OneShotEvent() {} |
| 50 |
| 51 | void OneShotEvent::Post(const Location& from_here, OnceClosure task) const { |
| 52 | PostImpl(from_here, std::move(task), ThreadTaskRunnerHandle::Get(), |
| 53 | TimeDelta()); |
| 54 | } |
| 55 |
| 56 | void OneShotEvent::Post( |
| 57 | const Location& from_here, |
| 58 | OnceClosure task, |
| 59 | const scoped_refptr<SingleThreadTaskRunner>& runner) const { |
| 60 | PostImpl(from_here, std::move(task), runner, TimeDelta()); |
| 61 | } |
| 62 |
| 63 | void OneShotEvent::PostDelayed(const Location& from_here, |
| 64 | OnceClosure task, |
| 65 | const TimeDelta& delay) const { |
| 66 | PostImpl(from_here, std::move(task), ThreadTaskRunnerHandle::Get(), delay); |
| 67 | } |
| 68 |
| 69 | void OneShotEvent::Signal() { |
| 70 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 71 |
| 72 | CHECK(!signaled_) << "Only call Signal once."; |
| 73 |
| 74 | signaled_ = true; |
| 75 | // After this point, a call to Post() from one of the queued tasks |
| 76 | // could proceed immediately, but the fact that this object is |
| 77 | // single-threaded prevents that from being relevant. |
| 78 |
| 79 | // Move tasks to a temporary to ensure no new ones are added. |
| 80 | std::vector<TaskInfo> moved_tasks; |
| 81 | std::swap(moved_tasks, tasks_); |
| 82 |
| 83 | // We could randomize tasks in debug mode in order to check that |
| 84 | // the order doesn't matter... |
| 85 | for (TaskInfo& task : moved_tasks) { |
| 86 | if (task.delay.is_zero()) |
| 87 | task.runner->PostTask(task.from_here, std::move(task.task)); |
| 88 | else |
| 89 | task.runner->PostDelayedTask(task.from_here, std::move(task.task), |
| 90 | task.delay); |
| 91 | } |
| 92 | DCHECK(tasks_.empty()) << "No new tasks should be added during task running!"; |
| 93 | } |
| 94 |
| 95 | void OneShotEvent::PostImpl(const Location& from_here, |
| 96 | OnceClosure task, |
| 97 | const scoped_refptr<SingleThreadTaskRunner>& runner, |
| 98 | const TimeDelta& delay) const { |
| 99 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 100 |
| 101 | if (is_signaled()) { |
| 102 | if (delay.is_zero()) |
| 103 | runner->PostTask(from_here, std::move(task)); |
| 104 | else |
| 105 | runner->PostDelayedTask(from_here, std::move(task), delay); |
| 106 | } else { |
| 107 | tasks_.emplace_back(from_here, runner, std::move(task), delay); |
| 108 | } |
| 109 | } |
| 110 |
| 111 | } // namespace base |
| 112 |