OLD | NEW |
(Empty) | |
| 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 "extensions/common/one_shot_event.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/location.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/task_runner.h" |
| 11 |
| 12 using base::TaskRunner; |
| 13 |
| 14 namespace extensions { |
| 15 |
| 16 struct OneShotEvent::TaskInfo { |
| 17 TaskInfo() {} |
| 18 TaskInfo(const tracked_objects::Location& from_here, |
| 19 const scoped_refptr<TaskRunner>& runner, |
| 20 const base::Closure& task) |
| 21 : from_here(from_here), runner(runner), task(task) { |
| 22 CHECK(runner); // Detect mistakes with a decent stack frame. |
| 23 } |
| 24 tracked_objects::Location from_here; |
| 25 scoped_refptr<TaskRunner> runner; |
| 26 base::Closure task; |
| 27 }; |
| 28 |
| 29 OneShotEvent::OneShotEvent() : signaled_(false) {} |
| 30 OneShotEvent::~OneShotEvent() {} |
| 31 |
| 32 void OneShotEvent::Post(const tracked_objects::Location& from_here, |
| 33 const base::Closure& task) const { |
| 34 Post(from_here, task, base::MessageLoopProxy::current()); |
| 35 } |
| 36 |
| 37 void OneShotEvent::Post(const tracked_objects::Location& from_here, |
| 38 const base::Closure& task, |
| 39 const scoped_refptr<TaskRunner>& runner) const { |
| 40 DCHECK(thread_checker_.CalledOnValidThread()); |
| 41 |
| 42 if (is_signaled()) { |
| 43 runner->PostTask(from_here, task); |
| 44 } else { |
| 45 tasks_.push_back(TaskInfo(from_here, runner, task)); |
| 46 } |
| 47 } |
| 48 |
| 49 void OneShotEvent::Signal() { |
| 50 DCHECK(thread_checker_.CalledOnValidThread()); |
| 51 |
| 52 CHECK(!signaled_) << "Only call Signal once."; |
| 53 |
| 54 signaled_ = true; |
| 55 // After this point, a call to Post() from one of the queued tasks |
| 56 // could proceed immediately, but the fact that this object is |
| 57 // single-threaded prevents that from being relevant. |
| 58 |
| 59 // We could randomize tasks_ in debug mode in order to check that |
| 60 // the order doesn't matter... |
| 61 for (size_t i = 0; i < tasks_.size(); ++i) { |
| 62 tasks_[i].runner->PostTask(tasks_[i].from_here, tasks_[i].task); |
| 63 } |
| 64 } |
| 65 |
| 66 } // namespace extensions |
OLD | NEW |