OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/pending_task.h" | 13 #include "base/pending_task.h" |
14 #include "base/posix/eintr_wrapper.h" | 14 #include "base/posix/eintr_wrapper.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
18 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 | 21 |
22 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
23 #include "base/message_loop/message_pump_win.h" | 23 #include "base/message_pump_win.h" |
24 #include "base/win/scoped_handle.h" | 24 #include "base/win/scoped_handle.h" |
25 #endif | 25 #endif |
26 | 26 |
27 namespace base { | 27 namespace base { |
28 | 28 |
29 class MessageLoopLockTest { | 29 class MessageLoopLockTest { |
30 public: | 30 public: |
31 static void LockWaitUnLock(MessageLoop* loop, | 31 static void LockWaitUnLock(MessageLoop* loop, |
32 WaitableEvent* caller_wait, | 32 base::WaitableEvent* caller_wait, |
33 WaitableEvent* caller_signal) { | 33 base::WaitableEvent* caller_signal) { |
34 | 34 |
35 loop->incoming_queue_lock_.Acquire(); | 35 loop->incoming_queue_lock_.Acquire(); |
36 caller_wait->Signal(); | 36 caller_wait->Signal(); |
37 caller_signal->Wait(); | 37 caller_signal->Wait(); |
38 loop->incoming_queue_lock_.Release(); | 38 loop->incoming_queue_lock_.Release(); |
39 } | 39 } |
40 }; | 40 }; |
41 | 41 |
42 // TODO(darin): Platform-specific MessageLoop tests should be grouped together | 42 // TODO(darin): Platform-specific MessageLoop tests should be grouped together |
43 // to avoid chopping this file up with so many #ifdefs. | 43 // to avoid chopping this file up with so many #ifdefs. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 114 |
115 // TryPost with simulated contention. It must fail. We wait for a helper | 115 // TryPost with simulated contention. It must fail. We wait for a helper |
116 // thread to lock the queue, we TryPost on this thread and finally we | 116 // thread to lock the queue, we TryPost on this thread and finally we |
117 // signal the helper to unlock and exit. | 117 // signal the helper to unlock and exit. |
118 WaitableEvent wait(true, false); | 118 WaitableEvent wait(true, false); |
119 WaitableEvent signal(true, false); | 119 WaitableEvent signal(true, false); |
120 Thread thread("RunTest_PostTask_helper"); | 120 Thread thread("RunTest_PostTask_helper"); |
121 thread.Start(); | 121 thread.Start(); |
122 thread.message_loop()->PostTask( | 122 thread.message_loop()->PostTask( |
123 FROM_HERE, | 123 FROM_HERE, |
124 Bind(&MessageLoopLockTest::LockWaitUnLock, | 124 base::Bind(&MessageLoopLockTest::LockWaitUnLock, |
125 MessageLoop::current(), | 125 MessageLoop::current(), |
126 &wait, | 126 &wait, |
127 &signal)); | 127 &signal)); |
128 | 128 |
129 wait.Wait(); | 129 wait.Wait(); |
130 EXPECT_FALSE(MessageLoop::current()->TryPostTask(FROM_HERE, Bind( | 130 EXPECT_FALSE(MessageLoop::current()->TryPostTask(FROM_HERE, Bind( |
131 &Foo::Test2Mixed, foo.get(), a, &d))); | 131 &Foo::Test2Mixed, foo.get(), a, &d))); |
132 signal.Signal(); | 132 signal.Signal(); |
133 | 133 |
134 // After all tests, post a message that will shut down the message loop | 134 // After all tests, post a message that will shut down the message loop |
(...skipping 1277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1412 | 1412 |
1413 void RunTest_RecursivePosts(MessageLoop::Type message_loop_type, | 1413 void RunTest_RecursivePosts(MessageLoop::Type message_loop_type, |
1414 int num_times) { | 1414 int num_times) { |
1415 MessageLoop loop(message_loop_type); | 1415 MessageLoop loop(message_loop_type); |
1416 loop.PostTask(FROM_HERE, Bind(&PostNTasksThenQuit, num_times)); | 1416 loop.PostTask(FROM_HERE, Bind(&PostNTasksThenQuit, num_times)); |
1417 loop.Run(); | 1417 loop.Run(); |
1418 } | 1418 } |
1419 | 1419 |
1420 #if defined(OS_WIN) | 1420 #if defined(OS_WIN) |
1421 | 1421 |
1422 class DispatcherImpl : public MessageLoopForUI::Dispatcher { | 1422 class DispatcherImpl : public base::MessageLoopForUI::Dispatcher { |
1423 public: | 1423 public: |
1424 DispatcherImpl() : dispatch_count_(0) {} | 1424 DispatcherImpl() : dispatch_count_(0) {} |
1425 | 1425 |
1426 virtual bool Dispatch(const NativeEvent& msg) OVERRIDE { | 1426 virtual bool Dispatch(const NativeEvent& msg) OVERRIDE { |
1427 ::TranslateMessage(&msg); | 1427 ::TranslateMessage(&msg); |
1428 ::DispatchMessage(&msg); | 1428 ::DispatchMessage(&msg); |
1429 // Do not count WM_TIMER since it is not what we post and it will cause | 1429 // Do not count WM_TIMER since it is not what we post and it will cause |
1430 // flakiness. | 1430 // flakiness. |
1431 if (msg.message != WM_TIMER) | 1431 if (msg.message != WM_TIMER) |
1432 ++dispatch_count_; | 1432 ++dispatch_count_; |
(...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2107 // On Linux, the pipe buffer size is 64KiB by default. The bug caused one | 2107 // On Linux, the pipe buffer size is 64KiB by default. The bug caused one |
2108 // byte accumulated in the pipe per two posts, so we should repeat 128K | 2108 // byte accumulated in the pipe per two posts, so we should repeat 128K |
2109 // times to reproduce the bug. | 2109 // times to reproduce the bug. |
2110 const int kNumTimes = 1 << 17; | 2110 const int kNumTimes = 1 << 17; |
2111 RunTest_RecursivePosts(MessageLoop::TYPE_DEFAULT, kNumTimes); | 2111 RunTest_RecursivePosts(MessageLoop::TYPE_DEFAULT, kNumTimes); |
2112 RunTest_RecursivePosts(MessageLoop::TYPE_UI, kNumTimes); | 2112 RunTest_RecursivePosts(MessageLoop::TYPE_UI, kNumTimes); |
2113 RunTest_RecursivePosts(MessageLoop::TYPE_IO, kNumTimes); | 2113 RunTest_RecursivePosts(MessageLoop::TYPE_IO, kNumTimes); |
2114 } | 2114 } |
2115 | 2115 |
2116 } // namespace base | 2116 } // namespace base |
OLD | NEW |