OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ | 5 #ifndef BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ |
6 #define BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ | 6 #define BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/sequence_checker.h" | 10 #include "base/sequence_checker.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 // Each instance of this object can be waiting on a single WaitableEvent. When | 28 // Each instance of this object can be waiting on a single WaitableEvent. When |
29 // the waitable event is signaled, a callback is invoked on the sequence that | 29 // the waitable event is signaled, a callback is invoked on the sequence that |
30 // called StartWatching(). This callback can be deleted by deleting the waiter. | 30 // called StartWatching(). This callback can be deleted by deleting the waiter. |
31 // | 31 // |
32 // Typical usage: | 32 // Typical usage: |
33 // | 33 // |
34 // class MyClass { | 34 // class MyClass { |
35 // public: | 35 // public: |
36 // void DoStuffWhenSignaled(WaitableEvent *waitable_event) { | 36 // void DoStuffWhenSignaled(WaitableEvent *waitable_event) { |
37 // watcher_.StartWatching(waitable_event, | 37 // watcher_.StartWatching(waitable_event, |
38 // base::Bind(&MyClass::OnWaitableEventSignaled, this); | 38 // base::BindOnce(&MyClass::OnWaitableEventSignaled, this); |
39 // } | 39 // } |
40 // private: | 40 // private: |
41 // void OnWaitableEventSignaled(WaitableEvent* waitable_event) { | 41 // void OnWaitableEventSignaled(WaitableEvent* waitable_event) { |
42 // // OK, time to do stuff! | 42 // // OK, time to do stuff! |
43 // } | 43 // } |
44 // base::WaitableEventWatcher watcher_; | 44 // base::WaitableEventWatcher watcher_; |
45 // }; | 45 // }; |
46 // | 46 // |
47 // In the above example, MyClass wants to "do stuff" when waitable_event | 47 // In the above example, MyClass wants to "do stuff" when waitable_event |
48 // becomes signaled. WaitableEventWatcher makes this task easy. When MyClass | 48 // becomes signaled. WaitableEventWatcher makes this task easy. When MyClass |
49 // goes out of scope, the watcher_ will be destroyed, and there is no need to | 49 // goes out of scope, the watcher_ will be destroyed, and there is no need to |
50 // worry about OnWaitableEventSignaled being called on a deleted MyClass | 50 // worry about OnWaitableEventSignaled being called on a deleted MyClass |
51 // pointer. | 51 // pointer. |
52 // | 52 // |
53 // BEWARE: With automatically reset WaitableEvents, a signal may be lost if it | 53 // BEWARE: With automatically reset WaitableEvents, a signal may be lost if it |
54 // occurs just before a WaitableEventWatcher is deleted. There is currently no | 54 // occurs just before a WaitableEventWatcher is deleted. There is currently no |
55 // safe way to stop watching an automatic reset WaitableEvent without possibly | 55 // safe way to stop watching an automatic reset WaitableEvent without possibly |
56 // missing a signal. | 56 // missing a signal. |
57 // | 57 // |
58 // NOTE: you /are/ allowed to delete the WaitableEvent while still waiting on | 58 // NOTE: you /are/ allowed to delete the WaitableEvent while still waiting on |
59 // it with a Watcher. It will act as if the event was never signaled. | 59 // it with a Watcher. It will act as if the event was never signaled. |
60 | 60 |
61 class BASE_EXPORT WaitableEventWatcher | 61 class BASE_EXPORT WaitableEventWatcher |
62 #if defined(OS_WIN) | 62 #if defined(OS_WIN) |
63 : public win::ObjectWatcher::Delegate | 63 : public win::ObjectWatcher::Delegate |
64 #endif | 64 #endif |
65 { | 65 { |
66 public: | 66 public: |
67 typedef Callback<void(WaitableEvent*)> EventCallback; | 67 using EventCallback = OnceCallback<void(WaitableEvent*)>; |
68 WaitableEventWatcher(); | 68 WaitableEventWatcher(); |
69 | 69 |
70 #if defined(OS_WIN) | 70 #if defined(OS_WIN) |
71 ~WaitableEventWatcher() override; | 71 ~WaitableEventWatcher() override; |
72 #else | 72 #else |
73 ~WaitableEventWatcher(); | 73 ~WaitableEventWatcher(); |
74 #endif | 74 #endif |
75 | 75 |
76 // When |event| is signaled, |callback| is called on the sequence that called | 76 // When |event| is signaled, |callback| is called on the sequence that called |
77 // StartWatching(). | 77 // StartWatching(). |
78 bool StartWatching(WaitableEvent* event, const EventCallback& callback); | 78 bool StartWatching(WaitableEvent* event, EventCallback callback); |
79 | 79 |
80 // Cancel the current watch. Must be called from the same sequence which | 80 // Cancel the current watch. Must be called from the same sequence which |
81 // started the watch. | 81 // started the watch. |
82 // | 82 // |
83 // Does nothing if no event is being watched, nor if the watch has completed. | 83 // Does nothing if no event is being watched, nor if the watch has completed. |
84 // The callback will *not* be called for the current watch after this | 84 // The callback will *not* be called for the current watch after this |
85 // function returns. Since the callback runs on the same sequence as this | 85 // function returns. Since the callback runs on the same sequence as this |
86 // function, it cannot be called during this function either. | 86 // function, it cannot be called during this function either. |
87 void StopWatching(); | 87 void StopWatching(); |
88 | 88 |
(...skipping 19 matching lines...) Expand all Loading... |
108 // sequence. | 108 // sequence. |
109 SequenceChecker sequence_checker_; | 109 SequenceChecker sequence_checker_; |
110 #endif | 110 #endif |
111 | 111 |
112 DISALLOW_COPY_AND_ASSIGN(WaitableEventWatcher); | 112 DISALLOW_COPY_AND_ASSIGN(WaitableEventWatcher); |
113 }; | 113 }; |
114 | 114 |
115 } // namespace base | 115 } // namespace base |
116 | 116 |
117 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ | 117 #endif // BASE_SYNCHRONIZATION_WAITABLE_EVENT_WATCHER_H_ |
OLD | NEW |