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

Side by Side Diff: base/synchronization/waitable_event_watcher_unittest.cc

Issue 2839213002: [Synchronization] Fix a crash in WaitableEventWatcher (Closed)
Patch Set: [Synchronization] Fix a crash in WaitableEventWatcher. Created 3 years, 7 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 unified diff | Download patch
OLDNEW
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/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 20 matching lines...) Expand all
31 31
32 void QuitWhenSignaled(WaitableEvent* event) { 32 void QuitWhenSignaled(WaitableEvent* event) {
33 MessageLoop::current()->QuitWhenIdle(); 33 MessageLoop::current()->QuitWhenIdle();
34 } 34 }
35 35
36 class DecrementCountContainer { 36 class DecrementCountContainer {
37 public: 37 public:
38 explicit DecrementCountContainer(int* counter) : counter_(counter) { 38 explicit DecrementCountContainer(int* counter) : counter_(counter) {
39 } 39 }
40 void OnWaitableEventSignaled(WaitableEvent* object) { 40 void OnWaitableEventSignaled(WaitableEvent* object) {
41 // NOTE: |object| may be already deleted.
41 --(*counter_); 42 --(*counter_);
42 } 43 }
43 private: 44 private:
44 int* counter_; 45 int* counter_;
45 }; 46 };
46 47
47 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { 48 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) {
48 MessageLoop message_loop(message_loop_type); 49 MessageLoop message_loop(message_loop_type);
49 50
50 // A manual-reset event that is not yet signaled. 51 // A manual-reset event that is not yet signaled.
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 { 111 {
111 WaitableEventWatcher watcher; 112 WaitableEventWatcher watcher;
112 { 113 {
113 MessageLoop message_loop(message_loop_type); 114 MessageLoop message_loop(message_loop_type);
114 115
115 watcher.StartWatching(&event, BindOnce(&QuitWhenSignaled)); 116 watcher.StartWatching(&event, BindOnce(&QuitWhenSignaled));
116 } 117 }
117 } 118 }
118 } 119 }
119 120
120 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) { 121 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type, bool with_delay) {
121 // Delete the WaitableEvent out from under the Watcher. This is explictly 122 // Delete the WaitableEvent out from under the Watcher. This is explictly
122 // allowed by the interface. 123 // allowed by the interface.
123 124
124 MessageLoop message_loop(message_loop_type); 125 MessageLoop message_loop(message_loop_type);
125 126
126 { 127 {
127 WaitableEventWatcher watcher; 128 WaitableEventWatcher watcher;
128 129
129 WaitableEvent* event = 130 auto event = base::MakeUnique<WaitableEvent>(
danakj 2017/04/27 20:01:08 needs ptr_util.h
atuchin 2017/04/28 06:44:29 Done.
130 new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, 131 WaitableEvent::ResetPolicy::AUTOMATIC,
131 WaitableEvent::InitialState::NOT_SIGNALED); 132 WaitableEvent::InitialState::NOT_SIGNALED);
132 133
133 watcher.StartWatching(event, BindOnce(&QuitWhenSignaled)); 134 watcher.StartWatching(event.get(), BindOnce(&QuitWhenSignaled));
134 delete event; 135 event.reset();
136 if (with_delay)
137 PlatformThread::Sleep(TimeDelta::FromMilliseconds(10));
danakj 2017/04/27 20:01:08 What is this sleep meant to do?
atuchin 2017/04/28 06:44:29 Unfortunately, without that sleep the test fails t
danakj 2017/04/28 17:54:49 IIUC we don't have the ability to run code on this
atuchin 2017/05/02 07:41:19 Yes, we don't have a such ability. But I have foun
135 } 138 }
136 } 139 }
137 140
141 void RunTest_SignalAndDelete(MessageLoop::Type message_loop_type,
142 bool with_delay) {
143 // Signal and immediately delete the WaitableEvent out from under the Watcher.
144
145 MessageLoop message_loop(message_loop_type);
146
147 {
148 WaitableEventWatcher watcher;
149
150 auto event = base::MakeUnique<WaitableEvent>(
151 WaitableEvent::ResetPolicy::AUTOMATIC,
152 WaitableEvent::InitialState::NOT_SIGNALED);
153
154 watcher.StartWatching(event.get(), BindOnce(&QuitWhenSignaled));
155 event->Signal();
156 event.reset();
157 if (with_delay)
158 PlatformThread::Sleep(TimeDelta::FromMilliseconds(10));
danakj 2017/04/27 20:01:08 Also this sleep?
atuchin 2017/04/28 06:44:29 See the answer in another one.
159 }
160 }
161
138 } // namespace 162 } // namespace
139 163
140 //----------------------------------------------------------------------------- 164 //-----------------------------------------------------------------------------
141 165
142 TEST(WaitableEventWatcherTest, BasicSignal) { 166 TEST(WaitableEventWatcherTest, BasicSignal) {
143 for (int i = 0; i < kNumTestingMessageLoops; i++) { 167 for (int i = 0; i < kNumTestingMessageLoops; i++) {
144 RunTest_BasicSignal(testing_message_loops[i]); 168 RunTest_BasicSignal(testing_message_loops[i]);
145 } 169 }
146 } 170 }
147 171
(...skipping 10 matching lines...) Expand all
158 } 182 }
159 183
160 TEST(WaitableEventWatcherTest, OutlivesMessageLoop) { 184 TEST(WaitableEventWatcherTest, OutlivesMessageLoop) {
161 for (int i = 0; i < kNumTestingMessageLoops; i++) { 185 for (int i = 0; i < kNumTestingMessageLoops; i++) {
162 RunTest_OutlivesMessageLoop(testing_message_loops[i]); 186 RunTest_OutlivesMessageLoop(testing_message_loops[i]);
163 } 187 }
164 } 188 }
165 189
166 TEST(WaitableEventWatcherTest, DeleteUnder) { 190 TEST(WaitableEventWatcherTest, DeleteUnder) {
167 for (int i = 0; i < kNumTestingMessageLoops; i++) { 191 for (int i = 0; i < kNumTestingMessageLoops; i++) {
168 RunTest_DeleteUnder(testing_message_loops[i]); 192 RunTest_DeleteUnder(testing_message_loops[i], false);
193 RunTest_DeleteUnder(testing_message_loops[i], true);
169 } 194 }
170 } 195 }
171 196
197 TEST(WaitableEventWatcherTest, SignalAndDelete) {
198 for (int i = 0; i < kNumTestingMessageLoops; i++) {
199 RunTest_SignalAndDelete(testing_message_loops[i], false);
200 RunTest_SignalAndDelete(testing_message_loops[i], true);
201 }
202 }
203
172 } // namespace base 204 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698