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

Side by Side Diff: base/waitable_event_watcher_posix.cc

Issue 16554: WaitableEvent (Closed)
Patch Set: Addresssing darin's comments (round 2) Created 11 years, 11 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
« no previous file with comments | « base/waitable_event_watcher.h ('k') | base/waitable_event_watcher_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2006-2008 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 "base/waitable_event_watcher.h"
6
7 #include "base/condition_variable.h"
8 #include "base/lock.h"
9 #include "base/message_loop.h"
10 #include "base/waitable_event.h"
11
12 namespace base {
13
14 // -----------------------------------------------------------------------------
15 // WaitableEventWatcher (async waits).
16 //
17 // The basic design is that we add an AsyncWaiter to the wait-list of the event.
18 // That AsyncWaiter has a pointer to MessageLoop, and a Task to be posted to it.
19 // The MessageLoop ends up running the task, which calls the delegate.
20 //
21 // Since the wait can be canceled, we have a thread-safe Flag object which is
22 // set when the wait has been canceled. At each stage in the above, we check the
23 // flag before going onto the next stage. Since the wait may only be canceled in
24 // the MessageLoop which runs the Task, we are assured that the delegate cannot
25 // be called after canceling...
26
27 // -----------------------------------------------------------------------------
28 // A thread-safe, reference-counted, write-once flag.
29 // -----------------------------------------------------------------------------
30 class Flag : public RefCountedThreadSafe<Flag> {
31 public:
32 Flag() { flag_ = false; }
33
34 void Set() {
35 AutoLock locked(lock_);
36 flag_ = true;
37 }
38
39 bool value() const {
40 AutoLock locked(lock_);
41 return flag_;
42 }
43
44 private:
45 mutable Lock lock_;
46 bool flag_;
47 };
48
49 // -----------------------------------------------------------------------------
50 // This is an asynchronous waiter which posts a task to a MessageLoop when
51 // fired. An AsyncWaiter may only be in a single wait-list.
52 // -----------------------------------------------------------------------------
53 class AsyncWaiter : public WaitableEvent::Waiter {
54 public:
55 AsyncWaiter(MessageLoop* message_loop, Task* task, Flag* flag)
56 : message_loop_(message_loop),
57 cb_task_(task),
58 flag_(flag) { }
59
60 bool Fire(WaitableEvent* event) {
61 if (flag_->value()) {
62 // If the callback has been canceled, we don't enqueue the task, we just
63 // delete it instead.
64 delete cb_task_;
65 } else {
66 message_loop_->PostTask(FROM_HERE, cb_task_);
67 }
68
69 // We are removed from the wait-list by the WaitableEvent itself. It only
70 // remains to delete ourselves.
71 delete this;
72
73 // We can always return true because an AsyncWaiter is never in two
74 // different wait-lists at the same time.
75 return true;
76 }
77
78 // See StopWatching for discussion
79 bool Compare(void* tag) {
80 return tag == flag_.get();
81 }
82
83 MessageLoop *const message_loop_;
84 Task *const cb_task_;
85 scoped_refptr<Flag> flag_;
86 };
87
88 // -----------------------------------------------------------------------------
89 // For async waits we need to make a callback in a MessageLoop thread. We do
90 // this by posting this task, which calls the delegate and keeps track of when
91 // the event is canceled.
92 // -----------------------------------------------------------------------------
93 class AsyncCallbackTask : public Task {
94 public:
95 AsyncCallbackTask(Flag* flag, WaitableEventWatcher::Delegate* delegate,
96 WaitableEvent* event)
97 : flag_(flag),
98 delegate_(delegate),
99 event_(event) {
100 }
101
102 void Run() {
103 // Runs in MessageLoop thread.
104 if (!flag_->value())
105 delegate_->OnWaitableEventSignaled(event_);
106
107 // This is to let the WaitableEventWatcher know that the event has occured
108 // because it needs to be able to return NULL from GetWatchedEvent
109 flag_->Set();
110
111 // We are deleted by the MessageLoop
112 }
113
114 private:
115 scoped_refptr<Flag> flag_;
116 WaitableEventWatcher::Delegate *const delegate_;
117 WaitableEvent *const event_;
118 };
119
120 WaitableEventWatcher::WaitableEventWatcher()
121 : event_(NULL),
122 message_loop_(NULL),
123 cancel_flag_(NULL),
124 callback_task_(NULL) {
125 }
126
127 WaitableEventWatcher::~WaitableEventWatcher() {
128 StopWatching();
129 }
130
131 // -----------------------------------------------------------------------------
132 // The Handle is how the user cancels a wait. After deleting the Handle we
133 // insure that the delegate cannot be called.
134 // -----------------------------------------------------------------------------
135 bool WaitableEventWatcher::StartWatching
136 (WaitableEvent* event, WaitableEventWatcher::Delegate* delegate) {
137 MessageLoop *const current_ml = MessageLoop::current();
138 DCHECK(current_ml) << "Cannot create WaitableEventWatcher without a "
139 "current MessageLoop";
140
141 DCHECK(!cancel_flag_.get()) << "StartWatching called while still watching";
142
143 cancel_flag_ = new Flag;
144 callback_task_ = new AsyncCallbackTask(cancel_flag_, delegate, event);
145
146 AutoLock locked(event->lock_);
147
148 if (event->signaled_) {
149 if (!event->manual_reset_)
150 event->signaled_ = false;
151
152 // No hairpinning - we can't call the delegate directly here. We have to
153 // enqueue a task on the MessageLoop as normal.
154 current_ml->PostTask(FROM_HERE, callback_task_);
155 return true;
156 }
157
158 message_loop_ = current_ml;
159 current_ml->AddDestructionObserver(this);
160
161 event_ = event;
162 waiter_ = new AsyncWaiter(current_ml, callback_task_, cancel_flag_);
163 event->Enqueue(waiter_);
164
165 return true;
166 }
167
168 void WaitableEventWatcher::StopWatching() {
169 if (message_loop_) {
170 message_loop_->RemoveDestructionObserver(this);
171 message_loop_ = NULL;
172 }
173
174 if (!cancel_flag_.get()) // if not currently watching...
175 return;
176
177 if (!event_) {
178 // We have no WaitableEvent. This means that we never enqueued a Waiter on
179 // an event because the event was already signaled when StartWatching was
180 // called.
181 //
182 // In this case, a task was enqueued on the MessageLoop and will run.
183 // We set the flag in case the task hasn't yet run. The flag will stop the
184 // delegate getting called. If the task has run then we have the last
185 // reference to the flag and it will be deleted immedately after.
186 cancel_flag_->Set();
187 cancel_flag_ = NULL;
188 return;
189 }
190
191 AutoLock locked(event_->lock_);
192 // We have a lock on the WaitableEvent. No one else can signal the event while
193 // we have it.
194
195 // We have a possible ABA issue here. If Dequeue was to compare only the
196 // pointer values then it's possible that the AsyncWaiter could have been
197 // fired, freed and the memory reused for a different Waiter which was
198 // enqueued in the same wait-list. We would think that that waiter was our
199 // AsyncWaiter and remove it.
200 //
201 // To stop this, Dequeue also takes a tag argument which is passed to the
202 // virtual Compare function before the two are considered a match. So we need
203 // a tag which is good for the lifetime of this handle: the Flag. Since we
204 // have a reference to the Flag, its memory cannot be reused while this object
205 // still exists. So if we find a waiter with the correct pointer value, and
206 // which shares a Flag pointer, we have a real match.
207 if (event_->Dequeue(waiter_, cancel_flag_.get())) {
208 // Case 2: the waiter hasn't been signaled yet; it was still on the wait
209 // list. We've removed it, thus we can delete it and the task (which cannot
210 // have been enqueued with the MessageLoop because the waiter was never
211 // signaled)
212 delete waiter_;
213 delete callback_task_;
214 cancel_flag_ = NULL;
215 return;
216 }
217
218 // Case 3: the waiter isn't on the wait-list, thus it was signaled. It may
219 // not have run yet, so we set the flag to tell it not to bother enqueuing the
220 // task on the MessageLoop, but to delete it instead. The Waiter deletes
221 // itself once run.
222 cancel_flag_->Set();
223 cancel_flag_ = NULL;
224
225 // If the waiter has already run then the task has been enqueued. If the Task
226 // hasn't yet run, the flag will stop the delegate from getting called. (This
227 // is thread safe because one may only delete a Handle from the MessageLoop
228 // thread.)
229 //
230 // If the delegate has already been called then we have nothing to do. The
231 // task has been deleted by the MessageLoop.
232 }
233
234 WaitableEvent* WaitableEventWatcher::GetWatchedEvent() {
235 if (!cancel_flag_.get())
236 return NULL;
237
238 if (cancel_flag_->value())
239 return NULL;
240
241 return event_;
242 }
243
244 // -----------------------------------------------------------------------------
245 // This is called when the MessageLoop which the callback will be run it is
246 // deleted. We need to cancel the callback as if we had been deleted, but we
247 // will still be deleted at some point in the future.
248 // -----------------------------------------------------------------------------
249 void WaitableEventWatcher::WillDestroyCurrentMessageLoop() {
250 StopWatching();
251 }
252
253 } // namespace base
OLDNEW
« no previous file with comments | « base/waitable_event_watcher.h ('k') | base/waitable_event_watcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698