ipc/chromium/src/base/waitable_event_watcher.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #ifndef BASE_WAITABLE_EVENT_WATCHER_H_
michael@0 6 #define BASE_WAITABLE_EVENT_WATCHER_H_
michael@0 7
michael@0 8 #include "build/build_config.h"
michael@0 9
michael@0 10 #if defined(OS_WIN)
michael@0 11 #include "base/object_watcher.h"
michael@0 12 #else
michael@0 13 #include "base/message_loop.h"
michael@0 14 #include "base/waitable_event.h"
michael@0 15 #endif
michael@0 16
michael@0 17 namespace base {
michael@0 18
michael@0 19 class Flag;
michael@0 20 class AsyncWaiter;
michael@0 21 class AsyncCallbackTask;
michael@0 22 class WaitableEvent;
michael@0 23
michael@0 24 // -----------------------------------------------------------------------------
michael@0 25 // This class provides a way to wait on a WaitableEvent asynchronously.
michael@0 26 //
michael@0 27 // Each instance of this object can be waiting on a single WaitableEvent. When
michael@0 28 // the waitable event is signaled, a callback is made in the thread of a given
michael@0 29 // MessageLoop. This callback can be deleted by deleting the waiter.
michael@0 30 //
michael@0 31 // Typical usage:
michael@0 32 //
michael@0 33 // class MyClass : public base::WaitableEventWatcher::Delegate {
michael@0 34 // public:
michael@0 35 // void DoStuffWhenSignaled(WaitableEvent *waitable_event) {
michael@0 36 // watcher_.StartWatching(waitable_event, this);
michael@0 37 // }
michael@0 38 // virtual void OnWaitableEventSignaled(WaitableEvent* waitable_event) {
michael@0 39 // // OK, time to do stuff!
michael@0 40 // }
michael@0 41 // private:
michael@0 42 // base::WaitableEventWatcher watcher_;
michael@0 43 // };
michael@0 44 //
michael@0 45 // In the above example, MyClass wants to "do stuff" when waitable_event
michael@0 46 // becomes signaled. WaitableEventWatcher makes this task easy. When MyClass
michael@0 47 // goes out of scope, the watcher_ will be destroyed, and there is no need to
michael@0 48 // worry about OnWaitableEventSignaled being called on a deleted MyClass
michael@0 49 // pointer.
michael@0 50 //
michael@0 51 // BEWARE: With automatically reset WaitableEvents, a signal may be lost if it
michael@0 52 // occurs just before a WaitableEventWatcher is deleted. There is currently no
michael@0 53 // safe way to stop watching an automatic reset WaitableEvent without possibly
michael@0 54 // missing a signal.
michael@0 55 //
michael@0 56 // NOTE: you /are/ allowed to delete the WaitableEvent while still waiting on
michael@0 57 // it with a Watcher. It will act as if the event was never signaled.
michael@0 58 // -----------------------------------------------------------------------------
michael@0 59
michael@0 60 class WaitableEventWatcher
michael@0 61 #if defined(OS_POSIX)
michael@0 62 : public MessageLoop::DestructionObserver
michael@0 63 #endif
michael@0 64 {
michael@0 65 public:
michael@0 66
michael@0 67 WaitableEventWatcher();
michael@0 68 ~WaitableEventWatcher();
michael@0 69
michael@0 70 class Delegate {
michael@0 71 public:
michael@0 72 virtual ~Delegate() { }
michael@0 73
michael@0 74 // -------------------------------------------------------------------------
michael@0 75 // This is called on the MessageLoop thread when WaitableEvent has been
michael@0 76 // signaled.
michael@0 77 //
michael@0 78 // Note: the event may not be signaled by the time that this function is
michael@0 79 // called. This indicates only that it has been signaled at some point in
michael@0 80 // the past.
michael@0 81 // -------------------------------------------------------------------------
michael@0 82 virtual void OnWaitableEventSignaled(WaitableEvent* waitable_event) = 0;
michael@0 83 };
michael@0 84
michael@0 85 // ---------------------------------------------------------------------------
michael@0 86 // When @event is signaled, the given delegate is called on the thread of the
michael@0 87 // current message loop when StartWatching is called. The delegate is not
michael@0 88 // deleted.
michael@0 89 // ---------------------------------------------------------------------------
michael@0 90 bool StartWatching(WaitableEvent* event, Delegate* delegate);
michael@0 91
michael@0 92 // ---------------------------------------------------------------------------
michael@0 93 // Cancel the current watch. Must be called from the same thread which
michael@0 94 // started the watch.
michael@0 95 //
michael@0 96 // Does nothing if no event is being watched, nor if the watch has completed.
michael@0 97 // The delegate will *not* be called for the current watch after this
michael@0 98 // function returns. Since the delegate runs on the same thread as this
michael@0 99 // function, it cannot be called during this function either.
michael@0 100 // ---------------------------------------------------------------------------
michael@0 101 void StopWatching();
michael@0 102
michael@0 103 // ---------------------------------------------------------------------------
michael@0 104 // Return the currently watched event, or NULL if no object is currently being
michael@0 105 // watched.
michael@0 106 // ---------------------------------------------------------------------------
michael@0 107 WaitableEvent* GetWatchedEvent();
michael@0 108
michael@0 109 private:
michael@0 110 WaitableEvent* event_;
michael@0 111
michael@0 112 #if defined(OS_WIN)
michael@0 113 // ---------------------------------------------------------------------------
michael@0 114 // The helper class exists because, if WaitableEventWatcher were to inherit
michael@0 115 // from ObjectWatcher::Delegate, then it couldn't also have an inner class
michael@0 116 // called Delegate (at least on Windows). Thus this object exists to proxy
michael@0 117 // the callback function
michael@0 118 // ---------------------------------------------------------------------------
michael@0 119 class ObjectWatcherHelper : public ObjectWatcher::Delegate {
michael@0 120 public:
michael@0 121 ObjectWatcherHelper(WaitableEventWatcher* watcher);
michael@0 122
michael@0 123 // -------------------------------------------------------------------------
michael@0 124 // Implementation of ObjectWatcher::Delegate
michael@0 125 // -------------------------------------------------------------------------
michael@0 126 void OnObjectSignaled(HANDLE h);
michael@0 127
michael@0 128 private:
michael@0 129 WaitableEventWatcher *const watcher_;
michael@0 130 };
michael@0 131
michael@0 132 void OnObjectSignaled();
michael@0 133
michael@0 134 Delegate* delegate_;
michael@0 135 ObjectWatcherHelper helper_;
michael@0 136 ObjectWatcher watcher_;
michael@0 137 #else
michael@0 138 // ---------------------------------------------------------------------------
michael@0 139 // Implementation of MessageLoop::DestructionObserver
michael@0 140 // ---------------------------------------------------------------------------
michael@0 141 void WillDestroyCurrentMessageLoop();
michael@0 142
michael@0 143 MessageLoop* message_loop_;
michael@0 144 scoped_refptr<Flag> cancel_flag_;
michael@0 145 AsyncWaiter* waiter_;
michael@0 146 AsyncCallbackTask* callback_task_;
michael@0 147 scoped_refptr<WaitableEvent::WaitableEventKernel> kernel_;
michael@0 148 #endif
michael@0 149 };
michael@0 150
michael@0 151 } // namespace base
michael@0 152
michael@0 153 #endif // BASE_WAITABLE_EVENT_WATCHER_H_

mercurial