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