Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(747)

Side by Side Diff: extensions/common/one_shot_event.cc

Issue 14757022: Add a non-blocking "OneShotEvent" class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rename Latch to OneShotEvent Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 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 != NULL); // Detect mistakes with a decent stack frame.
not at google - send to devlin 2013/05/16 15:51:08 just CHECK(runner)?
Jeffrey Yasskin 2013/05/16 19:05:36 I have no idea. Sure.
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::ThenRun(const tracked_objects::Location& from_here,
33 const base::Closure& task) const {
not at google - send to devlin 2013/05/16 15:51:08 indentation +1 looks like there are a couple of ot
Jeffrey Yasskin 2013/05/16 19:05:36 Oops, done.
34 ThenRun(from_here, task, base::MessageLoopProxy::current());
35 }
36
37 void OneShotEvent::ThenRun(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()) {
not at google - send to devlin 2013/05/16 15:51:08 I actually wonder if this may at some point cause
Jeffrey Yasskin 2013/05/16 19:05:36 We talked about this offline, and agreed on the fo
Matt Perry 2013/05/16 19:12:04 RunWhenSignaled?
Jeffrey Yasskin 2013/05/16 19:14:42 I dislike that one because it implies the task wil
Jeffrey Yasskin 2013/05/16 21:36:16 A quick vote in the extensions room concluded that
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 ThenRun() from one of the queued
56 // tasks 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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698