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

Unified Diff: base/synchronization/waitable_event_watcher.h

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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/synchronization/waitable_event_watcher_posix.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/synchronization/waitable_event_watcher.h
diff --git a/base/synchronization/waitable_event_watcher.h b/base/synchronization/waitable_event_watcher.h
index f5c1510b8f7ddc8ebcf2e7cbc33e7ca534552145..f2f5f0458d669bdd587c6889e25ee77e91036058 100644
--- a/base/synchronization/waitable_event_watcher.h
+++ b/base/synchronization/waitable_event_watcher.h
@@ -23,7 +23,6 @@ class AsyncWaiter;
class AsyncCallbackTask;
class WaitableEvent;
-// -----------------------------------------------------------------------------
// This class provides a way to wait on a WaitableEvent asynchronously.
//
// Each instance of this object can be waiting on a single WaitableEvent. When
@@ -32,15 +31,16 @@ class WaitableEvent;
//
// Typical usage:
//
-// class MyClass : public base::WaitableEventWatcher::Delegate {
+// class MyClass {
// public:
// void DoStuffWhenSignaled(WaitableEvent *waitable_event) {
-// watcher_.StartWatching(waitable_event, this);
+// watcher_.StartWatching(waitable_event,
+// base::Bind(&MyClass::OnWaitableEventSignaled, this);
// }
-// virtual void OnWaitableEventSignaled(WaitableEvent* waitable_event) {
+// private:
+// void OnWaitableEventSignaled(WaitableEvent* waitable_event) {
// // OK, time to do stuff!
// }
-// private:
// base::WaitableEventWatcher watcher_;
// };
//
@@ -57,106 +57,56 @@ class WaitableEvent;
//
// NOTE: you /are/ allowed to delete the WaitableEvent while still waiting on
// it with a Watcher. It will act as if the event was never signaled.
-// -----------------------------------------------------------------------------
class BASE_EXPORT WaitableEventWatcher
-#if !defined(OS_WIN)
- : public MessageLoop::DestructionObserver
+#if defined(OS_WIN)
+ : public win::ObjectWatcher::Delegate {
+#else
+ : public MessageLoop::DestructionObserver {
#endif
-{
public:
-
+ typedef Callback<void(WaitableEvent*)> EventCallback;
WaitableEventWatcher();
virtual ~WaitableEventWatcher();
- class BASE_EXPORT Delegate {
- public:
- // -------------------------------------------------------------------------
- // This is called on the MessageLoop thread when WaitableEvent has been
- // signaled.
- //
- // Note: the event may not be signaled by the time that this function is
- // called. This indicates only that it has been signaled at some point in
- // the past.
- // -------------------------------------------------------------------------
- virtual void OnWaitableEventSignaled(WaitableEvent* waitable_event) = 0;
-
- protected:
- virtual ~Delegate() { }
- };
-
- // ---------------------------------------------------------------------------
- // When @event is signaled, the given delegate is called on the thread of the
- // current message loop when StartWatching is called. The delegate is not
- // deleted.
- // ---------------------------------------------------------------------------
- bool StartWatching(WaitableEvent* event, Delegate* delegate);
-
- // ---------------------------------------------------------------------------
+ // When @event is signaled, the given callback is called on the thread of the
+ // current message loop when StartWatching is called.
+ bool StartWatching(WaitableEvent* event, const EventCallback& callback);
+
// Cancel the current watch. Must be called from the same thread which
// started the watch.
//
// Does nothing if no event is being watched, nor if the watch has completed.
- // The delegate will *not* be called for the current watch after this
- // function returns. Since the delegate runs on the same thread as this
+ // The callback will *not* be called for the current watch after this
+ // function returns. Since the callback runs on the same thread as this
// function, it cannot be called during this function either.
- // ---------------------------------------------------------------------------
void StopWatching();
- // ---------------------------------------------------------------------------
// Return the currently watched event, or NULL if no object is currently being
// watched.
- // ---------------------------------------------------------------------------
WaitableEvent* GetWatchedEvent();
- // ---------------------------------------------------------------------------
- // Return the delegate, or NULL if there is no delegate.
- // ---------------------------------------------------------------------------
- Delegate* delegate() {
- return delegate_;
- }
+ // Return the callback that will be invoked when the event is
+ // signaled.
+ const EventCallback& callback() const { return callback_; }
private:
#if defined(OS_WIN)
- // ---------------------------------------------------------------------------
- // The helper class exists because, if WaitableEventWatcher were to inherit
- // from ObjectWatcher::Delegate, then it couldn't also have an inner class
- // called Delegate (at least on Windows). Thus this object exists to proxy
- // the callback function
- // ---------------------------------------------------------------------------
- class ObjectWatcherHelper : public win::ObjectWatcher::Delegate {
- public:
- ObjectWatcherHelper(WaitableEventWatcher* watcher);
-
- // -------------------------------------------------------------------------
- // Implementation of ObjectWatcher::Delegate
- // -------------------------------------------------------------------------
- void OnObjectSignaled(HANDLE h);
-
- private:
- WaitableEventWatcher *const watcher_;
- };
-
- void OnObjectSignaled();
-
- ObjectWatcherHelper helper_;
+ virtual void OnObjectSignaled(HANDLE h) OVERRIDE;
win::ObjectWatcher watcher_;
#else
- // ---------------------------------------------------------------------------
// Implementation of MessageLoop::DestructionObserver
- // ---------------------------------------------------------------------------
virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
MessageLoop* message_loop_;
scoped_refptr<Flag> cancel_flag_;
AsyncWaiter* waiter_;
- base::Closure callback_;
+ base::Closure internal_callback_;
scoped_refptr<WaitableEvent::WaitableEventKernel> kernel_;
#endif
WaitableEvent* event_;
-
- Delegate* delegate_;
+ EventCallback callback_;
};
} // namespace base
« no previous file with comments | « no previous file | base/synchronization/waitable_event_watcher_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698