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