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

Side by Side Diff: base/synchronization/waitable_event_watcher_win.cc

Issue 12094106: Refactor: Simplify WaitableEventWatcher. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 10 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/synchronization/waitable_event_watcher.h" 5 #include "base/synchronization/waitable_event_watcher.h"
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/synchronization/waitable_event.h" 8 #include "base/synchronization/waitable_event.h"
9 #include "base/win/object_watcher.h" 9 #include "base/win/object_watcher.h"
10 10
11 namespace base { 11 namespace base {
12 12
13 WaitableEventWatcher::ObjectWatcherHelper::ObjectWatcherHelper(
14 WaitableEventWatcher* watcher)
15 : watcher_(watcher) {
16 };
17
18 void WaitableEventWatcher::ObjectWatcherHelper::OnObjectSignaled(HANDLE h) {
19 watcher_->OnObjectSignaled();
20 }
21
22
23 WaitableEventWatcher::WaitableEventWatcher() 13 WaitableEventWatcher::WaitableEventWatcher()
24 : ALLOW_THIS_IN_INITIALIZER_LIST(helper_(this)), 14 : event_(NULL) {
25 event_(NULL),
26 delegate_(NULL) {
27 } 15 }
28 16
29 WaitableEventWatcher::~WaitableEventWatcher() { 17 WaitableEventWatcher::~WaitableEventWatcher() {
30 } 18 }
31 19
32 bool WaitableEventWatcher::StartWatching(WaitableEvent* event, 20 bool WaitableEventWatcher::StartWatching(
33 Delegate* delegate) { 21 WaitableEvent* event,
34 delegate_ = delegate; 22 const EventCallback& callback) {
23 callback_ = callback;
35 event_ = event; 24 event_ = event;
36 25 return watcher_.StartWatching(event->handle(), this);
37 return watcher_.StartWatching(event->handle(), &helper_);
38 } 26 }
39 27
40 void WaitableEventWatcher::StopWatching() { 28 void WaitableEventWatcher::StopWatching() {
41 delegate_ = NULL; 29 callback_.Reset();
42 event_ = NULL; 30 event_ = NULL;
43 watcher_.StopWatching(); 31 watcher_.StopWatching();
44 } 32 }
45 33
46 WaitableEvent* WaitableEventWatcher::GetWatchedEvent() { 34 WaitableEvent* WaitableEventWatcher::GetWatchedEvent() {
47 return event_; 35 return event_;
48 } 36 }
49 37
50 void WaitableEventWatcher::OnObjectSignaled() { 38 void WaitableEventWatcher::OnObjectSignaled(HANDLE h) {
51 WaitableEvent* event = event_; 39 WaitableEvent* event = event_;
52 Delegate* delegate = delegate_; 40 EventCallback callback = callback_;
53 event_ = NULL; 41 event_ = NULL;
54 delegate_ = NULL; 42 callback_.Reset();
55 DCHECK(event); 43 DCHECK(event);
56 44
57 delegate->OnWaitableEventSignaled(event); 45 callback.Run(event);
58 } 46 }
59 47
60 } // namespace base 48 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698