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