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

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: Fix tests; add a test 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/threading/thread_checker.h"
14
15 namespace base {
16 class TaskRunner;
17 }
18
19 namespace tracked_objects {
20 class Location;
21 }
22
23 namespace extensions {
24
25 // This class represents an event that's expected to happen once. It
26 // allows clients to guarantee that code is run after the event has
27 // happened. If the AsyncEvent is destroyed before it happens, the
28 // delayed closures are destroyed without being run.
29 //
30 // This class is similar to a WaitableEvent combined with several
31 // WaitableEventWatchers, but using it is simpler.
32 //
33 // This class is not thread-safe, and must be used from a single thread.
34 //
35 // OneShotEvent?
36 class AsyncEvent {
Devlin 2013/05/15 00:21:16 Just my opinion, but I think that AsyncEvent is a
Jeffrey Yasskin 2013/05/15 03:07:18 Yeah, this name isn't great. I'd like to use just
37 public:
38 AsyncEvent();
39 ~AsyncEvent();
40
41 // True if MarkHappened has been called.
42 bool has_happened() const {
Jeffrey Yasskin 2013/05/15 03:07:18 Maybe done, released, or notified?
43 DCHECK(thread_checker_.CalledOnValidThread());
44 return happened_;
45 }
46
47 // Causes has_happened() to return true and all queued tasks to be
48 // run in an arbitrary order. This method must only be called once.
49 void MarkHappened();
50
51 // Scheduled |task| to be called on |runner| after has_happened()
52 // becomes true. Inside |task|, if this AsyncEvent is still alive,
53 // CHECK(has_happened()) will never fail (which implies that
54 // AsyncEvent::Reset() doesn't exist).
55 //
56 // |runner|==NULL indicates that |task| should run on
57 // MessageLoopProxy::current().
58 //
59 // Tasks may be run in an arbitrary order, not just FIFO. Tasks
60 // will never be called on the current thread before this function
61 // returns.
62 //
63 // Const because RunAfter doesn't modify the logical state of this
64 // object (which is just the happened_ bit).
65 void RunAfter(const tracked_objects::Location& from_here,
Jeffrey Yasskin 2013/05/15 03:07:18 event.RunAfter(task) isn't great either. Maybe eve
66 const base::Closure& task,
67 const scoped_refptr<base::TaskRunner>& runner = 0) const;
Devlin 2013/05/15 00:21:16 Style forbids default arguments here: http://googl
Jeffrey Yasskin 2013/05/15 03:07:18 Sure, switched to 2 overloads instead and removed
68
69 private:
70 struct TaskInfo;
71
72 base::ThreadChecker thread_checker_;
73
74 bool happened_;
75
76 // The task list is mutable because it's not part of the logical
77 // state of the object. This lets us return const references to the
78 // AsyncEvent to clients that just want to run tasks through it
79 // without worrying that they'll mark the event as having happened.
80 //
81 // Optimization note: We could reduce the size of this class to a
82 // single pointer by storing |happened_| in the low bit of a
83 // pointer, and storing the size and capacity of the array (if any)
84 // on the far end of the pointer.
85 mutable std::vector<TaskInfo> tasks_;
86 };
87
88 };
Devlin 2013/05/15 00:21:16 s/;/ \/\/ namespace extensions
Jeffrey Yasskin 2013/05/15 03:07:18 Done.
89
90 #endif // EXTENSIONS_COMMON_ASYNC_EVENT_H_
Devlin 2013/05/15 00:21:16 nit: remove extra space after //.
Jeffrey Yasskin 2013/05/15 03:07:18 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698