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 #include "extensions/common/one_shot_event.h" | 5 #include "extensions/common/one_shot_event.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/lazy_instance.h" | |
9 #include "base/location.h" | 8 #include "base/location.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
11 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
12 #include "base/time/time.h" | |
13 | 11 |
14 using base::TaskRunner; | 12 using base::TaskRunner; |
15 | 13 |
16 namespace extensions { | 14 namespace extensions { |
17 | 15 |
18 struct OneShotEvent::TaskInfo { | 16 struct OneShotEvent::TaskInfo { |
19 TaskInfo() {} | 17 TaskInfo() {} |
20 TaskInfo(const tracked_objects::Location& from_here, | 18 TaskInfo(const tracked_objects::Location& from_here, |
21 const scoped_refptr<TaskRunner>& runner, | 19 const scoped_refptr<TaskRunner>& runner, |
22 const base::Closure& task, | 20 const base::Closure& task) |
23 const base::TimeDelta& delay) | 21 : from_here(from_here), runner(runner), task(task) { |
24 : from_here(from_here), runner(runner), task(task), delay(delay) { | |
25 CHECK(runner.get()); // Detect mistakes with a decent stack frame. | 22 CHECK(runner.get()); // Detect mistakes with a decent stack frame. |
26 } | 23 } |
27 tracked_objects::Location from_here; | 24 tracked_objects::Location from_here; |
28 scoped_refptr<TaskRunner> runner; | 25 scoped_refptr<TaskRunner> runner; |
29 base::Closure task; | 26 base::Closure task; |
30 base::TimeDelta delay; | |
31 }; | 27 }; |
32 | 28 |
33 OneShotEvent::OneShotEvent() : signaled_(false) { | 29 OneShotEvent::OneShotEvent() : signaled_(false) { |
34 // It's acceptable to construct the OneShotEvent on one thread, but | 30 // It's acceptable to construct the OneShotEvent on one thread, but |
35 // immediately move it to another thread. | 31 // immediately move it to another thread. |
36 thread_checker_.DetachFromThread(); | 32 thread_checker_.DetachFromThread(); |
37 } | 33 } |
38 OneShotEvent::OneShotEvent(bool signaled) : signaled_(signaled) { | 34 OneShotEvent::OneShotEvent(bool signaled) : signaled_(signaled) { |
39 thread_checker_.DetachFromThread(); | 35 thread_checker_.DetachFromThread(); |
40 } | 36 } |
41 OneShotEvent::~OneShotEvent() {} | 37 OneShotEvent::~OneShotEvent() {} |
42 | 38 |
43 void OneShotEvent::Post(const tracked_objects::Location& from_here, | 39 void OneShotEvent::Post(const tracked_objects::Location& from_here, |
44 const base::Closure& task) const { | 40 const base::Closure& task) const { |
45 PostImpl( | 41 Post(from_here, task, base::MessageLoopProxy::current()); |
46 from_here, task, base::MessageLoopProxy::current(), base::TimeDelta()); | |
47 } | 42 } |
48 | 43 |
49 void OneShotEvent::Post(const tracked_objects::Location& from_here, | 44 void OneShotEvent::Post(const tracked_objects::Location& from_here, |
50 const base::Closure& task, | 45 const base::Closure& task, |
51 const scoped_refptr<TaskRunner>& runner) const { | 46 const scoped_refptr<TaskRunner>& runner) const { |
52 PostImpl(from_here, task, runner, base::TimeDelta()); | 47 DCHECK(thread_checker_.CalledOnValidThread()); |
53 } | |
54 | 48 |
55 void OneShotEvent::PostDelayed(const tracked_objects::Location& from_here, | 49 if (is_signaled()) { |
56 const base::Closure& task, | 50 runner->PostTask(from_here, task); |
57 const base::TimeDelta& delay) const { | 51 } else { |
58 PostImpl(from_here, task, base::MessageLoopProxy::current(), delay); | 52 tasks_.push_back(TaskInfo(from_here, runner, task)); |
| 53 } |
59 } | 54 } |
60 | 55 |
61 void OneShotEvent::Signal() { | 56 void OneShotEvent::Signal() { |
62 DCHECK(thread_checker_.CalledOnValidThread()); | 57 DCHECK(thread_checker_.CalledOnValidThread()); |
63 | 58 |
64 CHECK(!signaled_) << "Only call Signal once."; | 59 CHECK(!signaled_) << "Only call Signal once."; |
65 | 60 |
66 signaled_ = true; | 61 signaled_ = true; |
67 // After this point, a call to Post() from one of the queued tasks | 62 // After this point, a call to Post() from one of the queued tasks |
68 // could proceed immediately, but the fact that this object is | 63 // could proceed immediately, but the fact that this object is |
69 // single-threaded prevents that from being relevant. | 64 // single-threaded prevents that from being relevant. |
70 | 65 |
71 // We could randomize tasks_ in debug mode in order to check that | 66 // We could randomize tasks_ in debug mode in order to check that |
72 // the order doesn't matter... | 67 // the order doesn't matter... |
73 for (size_t i = 0; i < tasks_.size(); ++i) { | 68 for (size_t i = 0; i < tasks_.size(); ++i) { |
74 const TaskInfo& task = tasks_[i]; | 69 tasks_[i].runner->PostTask(tasks_[i].from_here, tasks_[i].task); |
75 if (task.delay != base::TimeDelta()) | |
76 task.runner->PostDelayedTask(task.from_here, task.task, task.delay); | |
77 else | |
78 task.runner->PostTask(task.from_here, task.task); | |
79 } | 70 } |
80 } | 71 } |
81 | 72 |
82 void OneShotEvent::PostImpl(const tracked_objects::Location& from_here, | |
83 const base::Closure& task, | |
84 const scoped_refptr<TaskRunner>& runner, | |
85 const base::TimeDelta& delay) const { | |
86 DCHECK(thread_checker_.CalledOnValidThread()); | |
87 | |
88 if (is_signaled()) { | |
89 if (delay != base::TimeDelta()) | |
90 runner->PostDelayedTask(from_here, task, delay); | |
91 else | |
92 runner->PostTask(from_here, task); | |
93 } else { | |
94 tasks_.push_back(TaskInfo(from_here, runner, task, delay)); | |
95 } | |
96 } | |
97 | |
98 } // namespace extensions | 73 } // namespace extensions |
OLD | NEW |