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/message_loop/message_pump_libevent.h" | 5 #include "base/message_loop/message_pump_libevent.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
| 9 #include <memory> |
| 10 |
9 #include "base/bind.h" | 11 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
11 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
12 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/ptr_util.h" |
13 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
14 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
15 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
16 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
17 #include "base/synchronization/waitable_event_watcher.h" | 19 #include "base/synchronization/waitable_event_watcher.h" |
18 #include "base/third_party/libevent/event.h" | 20 #include "base/third_party/libevent/event.h" |
19 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
20 #include "build/build_config.h" | 22 #include "build/build_config.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
22 | 24 |
(...skipping 25 matching lines...) Expand all Loading... |
48 return static_cast<MessageLoopForIO*>(io_thread_.message_loop()); | 50 return static_cast<MessageLoopForIO*>(io_thread_.message_loop()); |
49 } | 51 } |
50 | 52 |
51 void OnLibeventNotification( | 53 void OnLibeventNotification( |
52 MessagePumpLibevent* pump, | 54 MessagePumpLibevent* pump, |
53 MessagePumpLibevent::FileDescriptorWatcher* controller) { | 55 MessagePumpLibevent::FileDescriptorWatcher* controller) { |
54 pump->OnLibeventNotification(0, EV_WRITE | EV_READ, controller); | 56 pump->OnLibeventNotification(0, EV_WRITE | EV_READ, controller); |
55 } | 57 } |
56 | 58 |
57 int pipefds_[2]; | 59 int pipefds_[2]; |
58 scoped_ptr<MessageLoop> ui_loop_; | 60 std::unique_ptr<MessageLoop> ui_loop_; |
59 | 61 |
60 private: | 62 private: |
61 Thread io_thread_; | 63 Thread io_thread_; |
62 }; | 64 }; |
63 | 65 |
64 namespace { | 66 namespace { |
65 | 67 |
66 // Concrete implementation of MessagePumpLibevent::Watcher that does | 68 // Concrete implementation of MessagePumpLibevent::Watcher that does |
67 // nothing useful. | 69 // nothing useful. |
68 class StupidWatcher : public MessagePumpLibevent::Watcher { | 70 class StupidWatcher : public MessagePumpLibevent::Watcher { |
(...skipping 19 matching lines...) Expand all Loading... |
88 MessagePumpLibevent::FileDescriptorWatcher watcher; | 90 MessagePumpLibevent::FileDescriptorWatcher watcher; |
89 StupidWatcher delegate; | 91 StupidWatcher delegate; |
90 | 92 |
91 ASSERT_DEATH(io_loop()->WatchFileDescriptor( | 93 ASSERT_DEATH(io_loop()->WatchFileDescriptor( |
92 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate), | 94 STDOUT_FILENO, false, MessageLoopForIO::WATCH_READ, &watcher, &delegate), |
93 "Check failed: " | 95 "Check failed: " |
94 "watch_file_descriptor_caller_checker_.CalledOnValidThread\\(\\)"); | 96 "watch_file_descriptor_caller_checker_.CalledOnValidThread\\(\\)"); |
95 } | 97 } |
96 | 98 |
97 TEST_F(MessagePumpLibeventTest, QuitOutsideOfRun) { | 99 TEST_F(MessagePumpLibeventTest, QuitOutsideOfRun) { |
98 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 100 std::unique_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
99 ASSERT_DEATH(pump->Quit(), "Check failed: in_run_. " | 101 ASSERT_DEATH(pump->Quit(), "Check failed: in_run_. " |
100 "Quit was called outside of Run!"); | 102 "Quit was called outside of Run!"); |
101 } | 103 } |
102 | 104 |
103 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 105 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
104 | 106 |
105 class BaseWatcher : public MessagePumpLibevent::Watcher { | 107 class BaseWatcher : public MessagePumpLibevent::Watcher { |
106 public: | 108 public: |
107 explicit BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller) | 109 explicit BaseWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller) |
108 : controller_(controller) { | 110 : controller_(controller) { |
(...skipping 19 matching lines...) Expand all Loading... |
128 ~DeleteWatcher() override { DCHECK(!controller_); } | 130 ~DeleteWatcher() override { DCHECK(!controller_); } |
129 | 131 |
130 void OnFileCanWriteWithoutBlocking(int /* fd */) override { | 132 void OnFileCanWriteWithoutBlocking(int /* fd */) override { |
131 DCHECK(controller_); | 133 DCHECK(controller_); |
132 delete controller_; | 134 delete controller_; |
133 controller_ = NULL; | 135 controller_ = NULL; |
134 } | 136 } |
135 }; | 137 }; |
136 | 138 |
137 TEST_F(MessagePumpLibeventTest, DeleteWatcher) { | 139 TEST_F(MessagePumpLibeventTest, DeleteWatcher) { |
138 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 140 std::unique_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
139 MessagePumpLibevent::FileDescriptorWatcher* watcher = | 141 MessagePumpLibevent::FileDescriptorWatcher* watcher = |
140 new MessagePumpLibevent::FileDescriptorWatcher; | 142 new MessagePumpLibevent::FileDescriptorWatcher; |
141 DeleteWatcher delegate(watcher); | 143 DeleteWatcher delegate(watcher); |
142 pump->WatchFileDescriptor(pipefds_[1], | 144 pump->WatchFileDescriptor(pipefds_[1], |
143 false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate); | 145 false, MessagePumpLibevent::WATCH_READ_WRITE, watcher, &delegate); |
144 | 146 |
145 // Spoof a libevent notification. | 147 // Spoof a libevent notification. |
146 OnLibeventNotification(pump.get(), watcher); | 148 OnLibeventNotification(pump.get(), watcher); |
147 } | 149 } |
148 | 150 |
149 class StopWatcher : public BaseWatcher { | 151 class StopWatcher : public BaseWatcher { |
150 public: | 152 public: |
151 explicit StopWatcher( | 153 explicit StopWatcher( |
152 MessagePumpLibevent::FileDescriptorWatcher* controller) | 154 MessagePumpLibevent::FileDescriptorWatcher* controller) |
153 : BaseWatcher(controller) {} | 155 : BaseWatcher(controller) {} |
154 | 156 |
155 ~StopWatcher() override {} | 157 ~StopWatcher() override {} |
156 | 158 |
157 void OnFileCanWriteWithoutBlocking(int /* fd */) override { | 159 void OnFileCanWriteWithoutBlocking(int /* fd */) override { |
158 controller_->StopWatchingFileDescriptor(); | 160 controller_->StopWatchingFileDescriptor(); |
159 } | 161 } |
160 }; | 162 }; |
161 | 163 |
162 TEST_F(MessagePumpLibeventTest, StopWatcher) { | 164 TEST_F(MessagePumpLibeventTest, StopWatcher) { |
163 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 165 std::unique_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
164 MessagePumpLibevent::FileDescriptorWatcher watcher; | 166 MessagePumpLibevent::FileDescriptorWatcher watcher; |
165 StopWatcher delegate(&watcher); | 167 StopWatcher delegate(&watcher); |
166 pump->WatchFileDescriptor(pipefds_[1], | 168 pump->WatchFileDescriptor(pipefds_[1], |
167 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); | 169 false, MessagePumpLibevent::WATCH_READ_WRITE, &watcher, &delegate); |
168 | 170 |
169 // Spoof a libevent notification. | 171 // Spoof a libevent notification. |
170 OnLibeventNotification(pump.get(), &watcher); | 172 OnLibeventNotification(pump.get(), &watcher); |
171 } | 173 } |
172 | 174 |
173 void QuitMessageLoopAndStart(const Closure& quit_closure) { | 175 void QuitMessageLoopAndStart(const Closure& quit_closure) { |
(...skipping 14 matching lines...) Expand all Loading... |
188 RunLoop runloop; | 190 RunLoop runloop; |
189 MessageLoop::current()->PostTask(FROM_HERE, Bind(&QuitMessageLoopAndStart, | 191 MessageLoop::current()->PostTask(FROM_HERE, Bind(&QuitMessageLoopAndStart, |
190 runloop.QuitClosure())); | 192 runloop.QuitClosure())); |
191 runloop.Run(); | 193 runloop.Run(); |
192 } | 194 } |
193 | 195 |
194 void OnFileCanWriteWithoutBlocking(int /* fd */) override {} | 196 void OnFileCanWriteWithoutBlocking(int /* fd */) override {} |
195 }; | 197 }; |
196 | 198 |
197 TEST_F(MessagePumpLibeventTest, NestedPumpWatcher) { | 199 TEST_F(MessagePumpLibeventTest, NestedPumpWatcher) { |
198 scoped_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 200 std::unique_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
199 MessagePumpLibevent::FileDescriptorWatcher watcher; | 201 MessagePumpLibevent::FileDescriptorWatcher watcher; |
200 NestedPumpWatcher delegate; | 202 NestedPumpWatcher delegate; |
201 pump->WatchFileDescriptor(pipefds_[1], | 203 pump->WatchFileDescriptor(pipefds_[1], |
202 false, MessagePumpLibevent::WATCH_READ, &watcher, &delegate); | 204 false, MessagePumpLibevent::WATCH_READ, &watcher, &delegate); |
203 | 205 |
204 // Spoof a libevent notification. | 206 // Spoof a libevent notification. |
205 OnLibeventNotification(pump.get(), &watcher); | 207 OnLibeventNotification(pump.get(), &watcher); |
206 } | 208 } |
207 | 209 |
208 void FatalClosure() { | 210 void FatalClosure() { |
(...skipping 26 matching lines...) Expand all Loading... |
235 ASSERT_TRUE(WriteFileDescriptor(fd, buf, size)); | 237 ASSERT_TRUE(WriteFileDescriptor(fd, buf, size)); |
236 } | 238 } |
237 | 239 |
238 // Tests that MessagePumpLibevent quits immediately when it is quit from | 240 // Tests that MessagePumpLibevent quits immediately when it is quit from |
239 // libevent's event_base_loop(). | 241 // libevent's event_base_loop(). |
240 TEST_F(MessagePumpLibeventTest, QuitWatcher) { | 242 TEST_F(MessagePumpLibeventTest, QuitWatcher) { |
241 // Delete the old MessageLoop so that we can manage our own one here. | 243 // Delete the old MessageLoop so that we can manage our own one here. |
242 ui_loop_.reset(); | 244 ui_loop_.reset(); |
243 | 245 |
244 MessagePumpLibevent* pump = new MessagePumpLibevent; // owned by |loop|. | 246 MessagePumpLibevent* pump = new MessagePumpLibevent; // owned by |loop|. |
245 MessageLoop loop(make_scoped_ptr(pump)); | 247 MessageLoop loop(WrapUnique(pump)); |
246 RunLoop run_loop; | 248 RunLoop run_loop; |
247 MessagePumpLibevent::FileDescriptorWatcher controller; | 249 MessagePumpLibevent::FileDescriptorWatcher controller; |
248 QuitWatcher delegate(&controller, &run_loop); | 250 QuitWatcher delegate(&controller, &run_loop); |
249 WaitableEvent event(false /* manual_reset */, false /* initially_signaled */); | 251 WaitableEvent event(false /* manual_reset */, false /* initially_signaled */); |
250 scoped_ptr<WaitableEventWatcher> watcher(new WaitableEventWatcher); | 252 std::unique_ptr<WaitableEventWatcher> watcher(new WaitableEventWatcher); |
251 | 253 |
252 // Tell the pump to watch the pipe. | 254 // Tell the pump to watch the pipe. |
253 pump->WatchFileDescriptor(pipefds_[0], false, MessagePumpLibevent::WATCH_READ, | 255 pump->WatchFileDescriptor(pipefds_[0], false, MessagePumpLibevent::WATCH_READ, |
254 &controller, &delegate); | 256 &controller, &delegate); |
255 | 257 |
256 // Make the IO thread wait for |event| before writing to pipefds[1]. | 258 // Make the IO thread wait for |event| before writing to pipefds[1]. |
257 const char buf = 0; | 259 const char buf = 0; |
258 const WaitableEventWatcher::EventCallback write_fd_task = | 260 const WaitableEventWatcher::EventCallback write_fd_task = |
259 Bind(&WriteFDWrapper, pipefds_[1], &buf, 1); | 261 Bind(&WriteFDWrapper, pipefds_[1], &buf, 1); |
260 io_loop()->PostTask(FROM_HERE, | 262 io_loop()->PostTask(FROM_HERE, |
261 Bind(IgnoreResult(&WaitableEventWatcher::StartWatching), | 263 Bind(IgnoreResult(&WaitableEventWatcher::StartWatching), |
262 Unretained(watcher.get()), &event, write_fd_task)); | 264 Unretained(watcher.get()), &event, write_fd_task)); |
263 | 265 |
264 // Queue |event| to signal on |loop|. | 266 // Queue |event| to signal on |loop|. |
265 loop.PostTask(FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&event))); | 267 loop.PostTask(FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&event))); |
266 | 268 |
267 // Now run the MessageLoop. | 269 // Now run the MessageLoop. |
268 run_loop.Run(); | 270 run_loop.Run(); |
269 | 271 |
270 // StartWatching can move |watcher| to IO thread. Release on IO thread. | 272 // StartWatching can move |watcher| to IO thread. Release on IO thread. |
271 io_loop()->PostTask(FROM_HERE, Bind(&WaitableEventWatcher::StopWatching, | 273 io_loop()->PostTask(FROM_HERE, Bind(&WaitableEventWatcher::StopWatching, |
272 Owned(watcher.release()))); | 274 Owned(watcher.release()))); |
273 } | 275 } |
274 | 276 |
275 } // namespace | 277 } // namespace |
276 | 278 |
277 } // namespace base | 279 } // namespace base |
OLD | NEW |