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

Side by Side Diff: extensions/common/async_event.h

Issue 14757022: Add a non-blocking "OneShotEvent" class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: dcronin's comments; ExtensionSystem::ready; and WeakPtr support 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 #ifndef EXTENSIONS_COMMON_ASYNC_EVENT_H_
6 #define EXTENSIONS_COMMON_ASYNC_EVENT_H_
7
8 #include <vector>
9
10 #include "base/callback_forward.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/thread_checker.h"
15
16 namespace base {
17 class TaskRunner;
18 }
19
20 namespace tracked_objects {
21 class Location;
22 }
23
24 namespace extensions {
25
26 // This class represents an event that's expected to happen once. It
27 // allows clients to guarantee that code is run after the event has
28 // happened. If the AsyncEvent is destroyed before it happens, the
29 // delayed closures are destroyed without being run.
30 //
31 // This class is similar to a WaitableEvent combined with several
32 // WaitableEventWatchers, but using it is simpler.
Matt Perry 2013/05/15 21:08:54 Maybe there's a way to unify these concepts someho
Jeffrey Yasskin 2013/05/15 22:46:50 Signal is easy enough. With "Latch" as the class,
Matt Perry 2013/05/15 22:55:40 As a consumer of the class, I think I'd find it ha
Jeffrey Yasskin 2013/05/15 23:17:53 Renamed to is_signaled(), and renamed the class a
33 //
34 // This class is not thread-safe, and must be used from a single thread.
35 class AsyncEvent : public base::SupportsWeakPtr<AsyncEvent> {
Matt Perry 2013/05/15 21:08:54 Can you change the class name? And I don't like wh
Jeffrey Yasskin 2013/05/15 22:46:50 Changed. We'll see if you want something else. ;)
36 public:
37 AsyncEvent();
38 ~AsyncEvent();
39
40 // True if MarkHappened has been called.
41 bool has_happened() const {
42 DCHECK(thread_checker_.CalledOnValidThread());
43 return happened_;
44 }
45
46 // Causes has_happened() to return true and all queued tasks to be
47 // run in an arbitrary order. This method must only be called once.
48 void MarkHappened();
49
50 // Scheduled |task| to be called on |runner| after has_happened()
51 // becomes true. Inside |task|, if this AsyncEvent is still alive,
52 // CHECK(has_happened()) will never fail (which implies that
53 // AsyncEvent::Reset() doesn't exist).
54 //
55 // If |*this| is destroyed before happening, none of these tasks
56 // will be executed.
57 //
58 // Omitting the |runner| argument indicates that |task| should run
59 // on MessageLoopProxy::current().
60 //
61 // Tasks may be run in an arbitrary order, not just FIFO. Tasks
62 // will never be called on the current thread before this function
63 // returns.
64 //
65 // Const because RunAfter doesn't modify the logical state of this
66 // object (which is just the happened_ bit).
67 void RunAfter(const tracked_objects::Location& from_here,
Matt Perry 2013/05/15 21:08:54 Is from_here really useful? Seems that most caller
Jeffrey Yasskin 2013/05/15 22:46:50 I think you're saying that any function passed to
Matt Perry 2013/05/15 22:55:40 Yes, I think it's less likely to be useful for thi
Jeffrey Yasskin 2013/05/15 23:17:53 I managed to convince Matt that the from_here was
68 const base::Closure& task) const;
69 void RunAfter(const tracked_objects::Location& from_here,
70 const base::Closure& task,
71 const scoped_refptr<base::TaskRunner>& runner) const;
72
73 private:
74 struct TaskInfo;
75
76 base::ThreadChecker thread_checker_;
77
78 bool happened_;
79
80 // The task list is mutable because it's not part of the logical
81 // state of the object. This lets us return const references to the
82 // AsyncEvent to clients that just want to run tasks through it
83 // without worrying that they'll mark the event as having happened.
84 //
85 // Optimization note: We could reduce the size of this class to a
Matt Perry 2013/05/15 21:08:54 There won't be enough of these objects for that to
Jeffrey Yasskin 2013/05/15 22:46:50 There might be, but there aren't yet, which is why
86 // single pointer by storing |happened_| in the low bit of a
87 // pointer, and storing the size and capacity of the array (if any)
88 // on the far end of the pointer.
89 mutable std::vector<TaskInfo> tasks_;
90 };
91
92 } // namespace extensions
93
94 #endif // EXTENSIONS_COMMON_ASYNC_EVENT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698