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 #include "base/synchronization/waitable_event_watcher.h" | 5 #include "base/synchronization/waitable_event_watcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ptr_util.h" | |
10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
12 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
13 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
15 #include "base/threading/sequenced_task_runner_handle.h" | |
14 #include "build/build_config.h" | 16 #include "build/build_config.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 namespace base { | 19 namespace base { |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 // The message loops on which each waitable event timer should be tested. | 23 // The message loops on which each waitable event timer should be tested. |
22 const MessageLoop::Type testing_message_loops[] = { | 24 const MessageLoop::Type testing_message_loops[] = { |
23 MessageLoop::TYPE_DEFAULT, | 25 MessageLoop::TYPE_DEFAULT, |
24 MessageLoop::TYPE_IO, | 26 MessageLoop::TYPE_IO, |
25 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. | 27 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. |
26 MessageLoop::TYPE_UI, | 28 MessageLoop::TYPE_UI, |
27 #endif | 29 #endif |
28 }; | 30 }; |
29 | 31 |
30 const int kNumTestingMessageLoops = arraysize(testing_message_loops); | 32 const int kNumTestingMessageLoops = arraysize(testing_message_loops); |
31 | 33 |
32 void QuitWhenSignaled(WaitableEvent* event) { | 34 void QuitWhenSignaled(WaitableEvent* event) { |
33 MessageLoop::current()->QuitWhenIdle(); | 35 MessageLoop::current()->QuitWhenIdle(); |
34 } | 36 } |
35 | 37 |
36 class DecrementCountContainer { | 38 class DecrementCountContainer { |
37 public: | 39 public: |
38 explicit DecrementCountContainer(int* counter) : counter_(counter) { | 40 explicit DecrementCountContainer(int* counter) : counter_(counter) { |
39 } | 41 } |
40 void OnWaitableEventSignaled(WaitableEvent* object) { | 42 void OnWaitableEventSignaled(WaitableEvent* object) { |
43 // NOTE: |object| may be already deleted. | |
41 --(*counter_); | 44 --(*counter_); |
42 } | 45 } |
43 private: | 46 private: |
44 int* counter_; | 47 int* counter_; |
45 }; | 48 }; |
46 | 49 |
47 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { | 50 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { |
48 MessageLoop message_loop(message_loop_type); | 51 MessageLoop message_loop(message_loop_type); |
49 | 52 |
50 // A manual-reset event that is not yet signaled. | 53 // A manual-reset event that is not yet signaled. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 | 122 |
120 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) { | 123 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) { |
121 // Delete the WaitableEvent out from under the Watcher. This is explictly | 124 // Delete the WaitableEvent out from under the Watcher. This is explictly |
122 // allowed by the interface. | 125 // allowed by the interface. |
123 | 126 |
124 MessageLoop message_loop(message_loop_type); | 127 MessageLoop message_loop(message_loop_type); |
125 | 128 |
126 { | 129 { |
127 WaitableEventWatcher watcher; | 130 WaitableEventWatcher watcher; |
128 | 131 |
129 WaitableEvent* event = | 132 auto event = new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, |
danakj
2017/05/04 16:09:37
auto*
| |
130 new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, | 133 WaitableEvent::InitialState::NOT_SIGNALED); |
131 WaitableEvent::InitialState::NOT_SIGNALED); | |
132 | 134 |
133 watcher.StartWatching(event, BindOnce(&QuitWhenSignaled)); | 135 watcher.StartWatching(event, BindOnce(&QuitWhenSignaled)); |
134 delete event; | 136 delete event; |
135 } | 137 } |
136 } | 138 } |
137 | 139 |
140 void RunTest_SignalAndDelete(MessageLoop::Type message_loop_type) { | |
141 // Signal and immediately delete the WaitableEvent out from under the Watcher. | |
142 | |
143 MessageLoop message_loop(message_loop_type); | |
144 | |
145 { | |
146 WaitableEventWatcher watcher; | |
147 | |
148 auto event = base::MakeUnique<WaitableEvent>( | |
149 WaitableEvent::ResetPolicy::AUTOMATIC, | |
150 WaitableEvent::InitialState::NOT_SIGNALED); | |
151 | |
152 watcher.StartWatching(event.get(), BindOnce(&QuitWhenSignaled)); | |
153 event->Signal(); | |
154 event.reset(); | |
155 | |
156 // Wait for the watcher callback: | |
danakj
2017/05/04 16:09:38
nit: s/:/./
| |
157 RunLoop().Run(); | |
158 } | |
159 } | |
160 | |
138 } // namespace | 161 } // namespace |
139 | 162 |
140 //----------------------------------------------------------------------------- | 163 //----------------------------------------------------------------------------- |
141 | 164 |
142 TEST(WaitableEventWatcherTest, BasicSignal) { | 165 TEST(WaitableEventWatcherTest, BasicSignal) { |
143 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 166 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
144 RunTest_BasicSignal(testing_message_loops[i]); | 167 RunTest_BasicSignal(testing_message_loops[i]); |
145 } | 168 } |
146 } | 169 } |
147 | 170 |
(...skipping 14 matching lines...) Expand all Loading... | |
162 RunTest_OutlivesMessageLoop(testing_message_loops[i]); | 185 RunTest_OutlivesMessageLoop(testing_message_loops[i]); |
163 } | 186 } |
164 } | 187 } |
165 | 188 |
166 TEST(WaitableEventWatcherTest, DeleteUnder) { | 189 TEST(WaitableEventWatcherTest, DeleteUnder) { |
167 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 190 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
168 RunTest_DeleteUnder(testing_message_loops[i]); | 191 RunTest_DeleteUnder(testing_message_loops[i]); |
169 } | 192 } |
170 } | 193 } |
171 | 194 |
195 TEST(WaitableEventWatcherTest, SignalAndDelete) { | |
196 for (int i = 0; i < kNumTestingMessageLoops; i++) { | |
197 RunTest_SignalAndDelete(testing_message_loops[i]); | |
198 } | |
199 } | |
200 | |
172 } // namespace base | 201 } // namespace base |
OLD | NEW |