| 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> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/single_thread_task_runner.h" |
| 18 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
| 19 #include "base/synchronization/waitable_event_watcher.h" | 20 #include "base/synchronization/waitable_event_watcher.h" |
| 20 #include "base/third_party/libevent/event.h" | 21 #include "base/third_party/libevent/event.h" |
| 21 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
| 22 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| 25 namespace base { | 26 namespace base { |
| 26 | 27 |
| 27 class MessagePumpLibeventTest : public testing::Test { | 28 class MessagePumpLibeventTest : public testing::Test { |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 | 171 |
| 171 // Spoof a libevent notification. | 172 // Spoof a libevent notification. |
| 172 OnLibeventNotification(pump.get(), &watcher); | 173 OnLibeventNotification(pump.get(), &watcher); |
| 173 } | 174 } |
| 174 | 175 |
| 175 void QuitMessageLoopAndStart(const Closure& quit_closure) { | 176 void QuitMessageLoopAndStart(const Closure& quit_closure) { |
| 176 quit_closure.Run(); | 177 quit_closure.Run(); |
| 177 | 178 |
| 178 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 179 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 179 RunLoop runloop; | 180 RunLoop runloop; |
| 180 MessageLoop::current()->PostTask(FROM_HERE, runloop.QuitClosure()); | 181 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 182 runloop.QuitClosure()); |
| 181 runloop.Run(); | 183 runloop.Run(); |
| 182 } | 184 } |
| 183 | 185 |
| 184 class NestedPumpWatcher : public MessagePumpLibevent::Watcher { | 186 class NestedPumpWatcher : public MessagePumpLibevent::Watcher { |
| 185 public: | 187 public: |
| 186 NestedPumpWatcher() {} | 188 NestedPumpWatcher() {} |
| 187 ~NestedPumpWatcher() override {} | 189 ~NestedPumpWatcher() override {} |
| 188 | 190 |
| 189 void OnFileCanReadWithoutBlocking(int /* fd */) override { | 191 void OnFileCanReadWithoutBlocking(int /* fd */) override { |
| 190 RunLoop runloop; | 192 RunLoop runloop; |
| 191 MessageLoop::current()->PostTask(FROM_HERE, Bind(&QuitMessageLoopAndStart, | 193 MessageLoop::current()->task_runner()->PostTask( |
| 192 runloop.QuitClosure())); | 194 FROM_HERE, Bind(&QuitMessageLoopAndStart, runloop.QuitClosure())); |
| 193 runloop.Run(); | 195 runloop.Run(); |
| 194 } | 196 } |
| 195 | 197 |
| 196 void OnFileCanWriteWithoutBlocking(int /* fd */) override {} | 198 void OnFileCanWriteWithoutBlocking(int /* fd */) override {} |
| 197 }; | 199 }; |
| 198 | 200 |
| 199 TEST_F(MessagePumpLibeventTest, NestedPumpWatcher) { | 201 TEST_F(MessagePumpLibeventTest, NestedPumpWatcher) { |
| 200 std::unique_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); | 202 std::unique_ptr<MessagePumpLibevent> pump(new MessagePumpLibevent); |
| 201 MessagePumpLibevent::FileDescriptorWatcher watcher; | 203 MessagePumpLibevent::FileDescriptorWatcher watcher; |
| 202 NestedPumpWatcher delegate; | 204 NestedPumpWatcher delegate; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 213 | 215 |
| 214 class QuitWatcher : public BaseWatcher { | 216 class QuitWatcher : public BaseWatcher { |
| 215 public: | 217 public: |
| 216 QuitWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller, | 218 QuitWatcher(MessagePumpLibevent::FileDescriptorWatcher* controller, |
| 217 RunLoop* run_loop) | 219 RunLoop* run_loop) |
| 218 : BaseWatcher(controller), run_loop_(run_loop) {} | 220 : BaseWatcher(controller), run_loop_(run_loop) {} |
| 219 ~QuitWatcher() override {} | 221 ~QuitWatcher() override {} |
| 220 | 222 |
| 221 void OnFileCanReadWithoutBlocking(int /* fd */) override { | 223 void OnFileCanReadWithoutBlocking(int /* fd */) override { |
| 222 // Post a fatal closure to the MessageLoop before we quit it. | 224 // Post a fatal closure to the MessageLoop before we quit it. |
| 223 MessageLoop::current()->PostTask(FROM_HERE, Bind(&FatalClosure)); | 225 MessageLoop::current()->task_runner()->PostTask(FROM_HERE, |
| 226 Bind(&FatalClosure)); |
| 224 | 227 |
| 225 // Now quit the MessageLoop. | 228 // Now quit the MessageLoop. |
| 226 run_loop_->Quit(); | 229 run_loop_->Quit(); |
| 227 } | 230 } |
| 228 | 231 |
| 229 private: | 232 private: |
| 230 RunLoop* run_loop_; // weak | 233 RunLoop* run_loop_; // weak |
| 231 }; | 234 }; |
| 232 | 235 |
| 233 void WriteFDWrapper(const int fd, | 236 void WriteFDWrapper(const int fd, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 253 std::unique_ptr<WaitableEventWatcher> watcher(new WaitableEventWatcher); | 256 std::unique_ptr<WaitableEventWatcher> watcher(new WaitableEventWatcher); |
| 254 | 257 |
| 255 // Tell the pump to watch the pipe. | 258 // Tell the pump to watch the pipe. |
| 256 pump->WatchFileDescriptor(pipefds_[0], false, MessagePumpLibevent::WATCH_READ, | 259 pump->WatchFileDescriptor(pipefds_[0], false, MessagePumpLibevent::WATCH_READ, |
| 257 &controller, &delegate); | 260 &controller, &delegate); |
| 258 | 261 |
| 259 // Make the IO thread wait for |event| before writing to pipefds[1]. | 262 // Make the IO thread wait for |event| before writing to pipefds[1]. |
| 260 const char buf = 0; | 263 const char buf = 0; |
| 261 const WaitableEventWatcher::EventCallback write_fd_task = | 264 const WaitableEventWatcher::EventCallback write_fd_task = |
| 262 Bind(&WriteFDWrapper, pipefds_[1], &buf, 1); | 265 Bind(&WriteFDWrapper, pipefds_[1], &buf, 1); |
| 263 io_loop()->PostTask(FROM_HERE, | 266 io_loop()->task_runner()->PostTask( |
| 264 Bind(IgnoreResult(&WaitableEventWatcher::StartWatching), | 267 FROM_HERE, Bind(IgnoreResult(&WaitableEventWatcher::StartWatching), |
| 265 Unretained(watcher.get()), &event, write_fd_task)); | 268 Unretained(watcher.get()), &event, write_fd_task)); |
| 266 | 269 |
| 267 // Queue |event| to signal on |loop|. | 270 // Queue |event| to signal on |loop|. |
| 268 loop.PostTask(FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&event))); | 271 loop.task_runner()->PostTask( |
| 272 FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&event))); |
| 269 | 273 |
| 270 // Now run the MessageLoop. | 274 // Now run the MessageLoop. |
| 271 run_loop.Run(); | 275 run_loop.Run(); |
| 272 | 276 |
| 273 // StartWatching can move |watcher| to IO thread. Release on IO thread. | 277 // StartWatching can move |watcher| to IO thread. Release on IO thread. |
| 274 io_loop()->PostTask(FROM_HERE, Bind(&WaitableEventWatcher::StopWatching, | 278 io_loop()->task_runner()->PostTask( |
| 275 Owned(watcher.release()))); | 279 FROM_HERE, |
| 280 Bind(&WaitableEventWatcher::StopWatching, Owned(watcher.release()))); |
| 276 } | 281 } |
| 277 | 282 |
| 278 } // namespace | 283 } // namespace |
| 279 | 284 |
| 280 } // namespace base | 285 } // namespace base |
| OLD | NEW |