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

Side by Side Diff: base/win/object_watcher.h

Issue 2598753002: Allow ObjectWatcher to be used from sequenced tasks. (Closed)
Patch Set: self-review Created 4 years 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 | « no previous file | base/win/object_watcher.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #ifndef BASE_WIN_OBJECT_WATCHER_H_ 5 #ifndef BASE_WIN_OBJECT_WATCHER_H_
6 #define BASE_WIN_OBJECT_WATCHER_H_ 6 #define BASE_WIN_OBJECT_WATCHER_H_
7 7
8 #include <windows.h> 8 #include <windows.h>
9 9
10 #include "base/base_export.h" 10 #include "base/base_export.h"
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "base/single_thread_task_runner.h" 15 #include "base/sequenced_task_runner.h"
16 16
17 namespace base { 17 namespace base {
18 namespace win { 18 namespace win {
19 19
20 // A class that provides a means to asynchronously wait for a Windows object to 20 // A class that provides a means to asynchronously wait for a Windows object to
21 // become signaled. It is an abstraction around RegisterWaitForSingleObject 21 // become signaled. It is an abstraction around RegisterWaitForSingleObject
22 // that provides a notification callback, OnObjectSignaled, that runs back on 22 // that provides a notification callback, OnObjectSignaled, that runs back on
23 // the origin thread (i.e., the thread that called StartWatching). 23 // the origin sequence (i.e., the sequence that called StartWatching).
24 // 24 //
25 // This class acts like a smart pointer such that when it goes out-of-scope, 25 // This class acts like a smart pointer such that when it goes out-of-scope,
26 // UnregisterWaitEx is automatically called, and any in-flight notification is 26 // UnregisterWaitEx is automatically called, and any in-flight notification is
27 // suppressed. 27 // suppressed.
28 // 28 //
29 // Typical usage: 29 // Typical usage:
30 // 30 //
31 // class MyClass : public base::win::ObjectWatcher::Delegate { 31 // class MyClass : public base::win::ObjectWatcher::Delegate {
32 // public: 32 // public:
33 // void DoStuffWhenSignaled(HANDLE object) { 33 // void DoStuffWhenSignaled(HANDLE object) {
34 // watcher_.StartWatchingOnce(object, this); 34 // watcher_.StartWatchingOnce(object, this);
35 // } 35 // }
36 // void OnObjectSignaled(HANDLE object) override { 36 // void OnObjectSignaled(HANDLE object) override {
37 // // OK, time to do stuff! 37 // // OK, time to do stuff!
38 // } 38 // }
39 // private: 39 // private:
40 // base::win::ObjectWatcher watcher_; 40 // base::win::ObjectWatcher watcher_;
41 // }; 41 // };
42 // 42 //
43 // In the above example, MyClass wants to "do stuff" when object becomes 43 // In the above example, MyClass wants to "do stuff" when object becomes
44 // signaled. ObjectWatcher makes this task easy. When MyClass goes out of 44 // signaled. ObjectWatcher makes this task easy. When MyClass goes out of
45 // scope, the watcher_ will be destroyed, and there is no need to worry about 45 // scope, the watcher_ will be destroyed, and there is no need to worry about
46 // OnObjectSignaled being called on a deleted MyClass pointer. Easy! 46 // OnObjectSignaled being called on a deleted MyClass pointer. Easy!
47 // If the object is already signaled before being watched, OnObjectSignaled is 47 // If the object is already signaled before being watched, OnObjectSignaled is
48 // still called after (but not necessarily immediately after) watch is started. 48 // still called after (but not necessarily immediately after) watch is started.
49 // 49 //
50 // NOTE: Except for the constructor, all public methods of this class must be 50 // NOTE: Except for the constructor, all public methods of this class must be
51 // called on the same thread. A ThreadTaskRunnerHandle must be set on that 51 // called in sequence, in a scope where SequencedTaskRunnerHandle::IsSet().
52 // thread.
53 class BASE_EXPORT ObjectWatcher { 52 class BASE_EXPORT ObjectWatcher {
54 public: 53 public:
55 class BASE_EXPORT Delegate { 54 class BASE_EXPORT Delegate {
56 public: 55 public:
57 virtual ~Delegate() {} 56 virtual ~Delegate() {}
58 // Called from the thread that started the watch when a signaled object is 57 // Called from the sequence that started the watch when a signaled object is
59 // detected. To continue watching the object, StartWatching must be called 58 // detected. To continue watching the object, StartWatching must be called
60 // again. 59 // again.
61 virtual void OnObjectSignaled(HANDLE object) = 0; 60 virtual void OnObjectSignaled(HANDLE object) = 0;
62 }; 61 };
63 62
64 ObjectWatcher(); 63 ObjectWatcher();
65 ~ObjectWatcher(); 64 ~ObjectWatcher();
66 65
67 // When the object is signaled, the given delegate is notified on the thread 66 // When the object is signaled, the given delegate is notified on the sequence
68 // where StartWatchingOnce is called. The ObjectWatcher is not responsible for 67 // where StartWatchingOnce is called. The ObjectWatcher is not responsible for
69 // deleting the delegate. 68 // deleting the delegate.
70 // Returns whether watching was successfully initiated. 69 // Returns whether watching was successfully initiated.
71 bool StartWatchingOnce(HANDLE object, Delegate* delegate); 70 bool StartWatchingOnce(HANDLE object, Delegate* delegate);
72 71
73 // Notifies the delegate, on the thread where this method is called, each time 72 // Notifies the delegate, on the sequence where this method is called, each
74 // the object is set. By definition, the handle must be an auto-reset object. 73 // time the object is set. By definition, the handle must be an auto-reset
75 // The caller must ensure that it (or any Windows system code) doesn't reset 74 // object. The caller must ensure that it (or any Windows system code) doesn't
76 // the event or else the delegate won't be called. 75 // reset the event or else the delegate won't be called.
77 // Returns whether watching was successfully initiated. 76 // Returns whether watching was successfully initiated.
78 bool StartWatchingMultipleTimes(HANDLE object, Delegate* delegate); 77 bool StartWatchingMultipleTimes(HANDLE object, Delegate* delegate);
79 78
80 // Stops watching. Does nothing if the watch has already completed. If the 79 // Stops watching. Does nothing if the watch has already completed. If the
81 // watch is still active, then it is canceled, and the associated delegate is 80 // watch is still active, then it is canceled, and the associated delegate is
82 // not notified. 81 // not notified.
83 // 82 //
84 // Returns true if the watch was canceled. Otherwise, false is returned. 83 // Returns true if the watch was canceled. Otherwise, false is returned.
85 bool StopWatching(); 84 bool StopWatching();
86 85
(...skipping 18 matching lines...) Expand all
105 // A callback pre-bound to Signal() that is posted to the caller's task runner 104 // A callback pre-bound to Signal() that is posted to the caller's task runner
106 // when the wait completes. 105 // when the wait completes.
107 Closure callback_; 106 Closure callback_;
108 107
109 // The object being watched. 108 // The object being watched.
110 HANDLE object_ = nullptr; 109 HANDLE object_ = nullptr;
111 110
112 // The wait handle returned by RegisterWaitForSingleObject. 111 // The wait handle returned by RegisterWaitForSingleObject.
113 HANDLE wait_object_ = nullptr; 112 HANDLE wait_object_ = nullptr;
114 113
115 // The task runner of the thread on which the watch was started. 114 // The task runner of the sequence on which the watch was started.
116 scoped_refptr<SingleThreadTaskRunner> task_runner_; 115 scoped_refptr<SequencedTaskRunner> task_runner_;
117 116
118 bool run_once_ = true; 117 bool run_once_ = true;
119 118
120 WeakPtrFactory<ObjectWatcher> weak_factory_; 119 WeakPtrFactory<ObjectWatcher> weak_factory_;
121 120
122 DISALLOW_COPY_AND_ASSIGN(ObjectWatcher); 121 DISALLOW_COPY_AND_ASSIGN(ObjectWatcher);
123 }; 122 };
124 123
125 } // namespace win 124 } // namespace win
126 } // namespace base 125 } // namespace base
127 126
128 #endif // BASE_WIN_OBJECT_WATCHER_H_ 127 #endif // BASE_WIN_OBJECT_WATCHER_H_
OLDNEW
« no previous file with comments | « no previous file | base/win/object_watcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698