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/threading/sequenced_worker_pool.h" | 5 #include "base/threading/sequenced_worker_pool.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 // during destruction of the SequencedWorkerPool. | 141 // during destruction of the SequencedWorkerPool. |
142 void PostRepostingTask( | 142 void PostRepostingTask( |
143 const scoped_refptr<SequencedWorkerPool>& pool, | 143 const scoped_refptr<SequencedWorkerPool>& pool, |
144 const scoped_refptr<DestructionDeadlockChecker>& checker) { | 144 const scoped_refptr<DestructionDeadlockChecker>& checker) { |
145 Closure reposting_task = | 145 Closure reposting_task = |
146 base::Bind(&TestTracker::PostRepostingTask, this, pool, checker); | 146 base::Bind(&TestTracker::PostRepostingTask, this, pool, checker); |
147 pool->PostWorkerTaskWithShutdownBehavior( | 147 pool->PostWorkerTaskWithShutdownBehavior( |
148 FROM_HERE, reposting_task, SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 148 FROM_HERE, reposting_task, SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
149 } | 149 } |
150 | 150 |
| 151 // This task reposts itself back onto the SequencedWorkerPool before it |
| 152 // finishes running. |
| 153 void PostRepostingBlockingTask( |
| 154 const scoped_refptr<SequencedWorkerPool>& pool, |
| 155 const SequencedWorkerPool::SequenceToken& token) { |
| 156 Closure reposting_task = |
| 157 base::Bind(&TestTracker::PostRepostingBlockingTask, this, pool, token); |
| 158 pool->PostSequencedWorkerTaskWithShutdownBehavior(token, |
| 159 FROM_HERE, reposting_task, SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 160 } |
| 161 |
151 // Waits until the given number of tasks have started executing. | 162 // Waits until the given number of tasks have started executing. |
152 void WaitUntilTasksBlocked(size_t count) { | 163 void WaitUntilTasksBlocked(size_t count) { |
153 { | 164 { |
154 base::AutoLock lock(lock_); | 165 base::AutoLock lock(lock_); |
155 while (started_events_ < count) | 166 while (started_events_ < count) |
156 cond_var_.Wait(); | 167 cond_var_.Wait(); |
157 } | 168 } |
158 cond_var_.Signal(); | 169 cond_var_.Signal(); |
159 } | 170 } |
160 | 171 |
(...skipping 627 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
788 scoped_refptr<DestructionDeadlockChecker> checker( | 799 scoped_refptr<DestructionDeadlockChecker> checker( |
789 new DestructionDeadlockChecker(pool())); | 800 new DestructionDeadlockChecker(pool())); |
790 tracker()->PostRepostingTask(pool(), checker); | 801 tracker()->PostRepostingTask(pool(), checker); |
791 } | 802 } |
792 | 803 |
793 // Shutting down the pool should destroy the DestructionDeadlockCheckers, | 804 // Shutting down the pool should destroy the DestructionDeadlockCheckers, |
794 // which in turn should not deadlock in their destructors. | 805 // which in turn should not deadlock in their destructors. |
795 pool()->Shutdown(); | 806 pool()->Shutdown(); |
796 } | 807 } |
797 | 808 |
| 809 // Similar to the test AvoidsDeadlockOnShutdown, but there are now also |
| 810 // sequenced, blocking tasks in the queue during shutdown. |
| 811 TEST_F(SequencedWorkerPoolTest, |
| 812 AvoidsDeadlockOnShutdownWithSequencedBlockingTasks) { |
| 813 const std::string sequence_token_name("name"); |
| 814 for (int i = 0; i < 4; ++i) { |
| 815 scoped_refptr<DestructionDeadlockChecker> checker( |
| 816 new DestructionDeadlockChecker(pool())); |
| 817 tracker()->PostRepostingTask(pool(), checker); |
| 818 |
| 819 SequencedWorkerPool::SequenceToken token1 = |
| 820 pool()->GetNamedSequenceToken(sequence_token_name); |
| 821 tracker()->PostRepostingBlockingTask(pool(), token1); |
| 822 } |
| 823 |
| 824 // Shutting down the pool should destroy the DestructionDeadlockCheckers, |
| 825 // which in turn should not deadlock in their destructors. |
| 826 pool()->Shutdown(); |
| 827 } |
| 828 |
798 // Verify that FlushForTesting works as intended. | 829 // Verify that FlushForTesting works as intended. |
799 TEST_F(SequencedWorkerPoolTest, FlushForTesting) { | 830 TEST_F(SequencedWorkerPoolTest, FlushForTesting) { |
800 // Should be fine to call on a new instance. | 831 // Should be fine to call on a new instance. |
801 pool()->FlushForTesting(); | 832 pool()->FlushForTesting(); |
802 | 833 |
803 // Queue up a bunch of work, including a long delayed task and | 834 // Queue up a bunch of work, including a long delayed task and |
804 // a task that produces additional tasks as an artifact. | 835 // a task that produces additional tasks as an artifact. |
805 pool()->PostDelayedWorkerTask( | 836 pool()->PostDelayedWorkerTask( |
806 FROM_HERE, | 837 FROM_HERE, |
807 base::Bind(&TestTracker::FastTask, tracker(), 0), | 838 base::Bind(&TestTracker::FastTask, tracker(), 0), |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
955 SequencedWorkerPoolSequencedTaskRunner, TaskRunnerTest, | 986 SequencedWorkerPoolSequencedTaskRunner, TaskRunnerTest, |
956 SequencedWorkerPoolSequencedTaskRunnerTestDelegate); | 987 SequencedWorkerPoolSequencedTaskRunnerTestDelegate); |
957 | 988 |
958 INSTANTIATE_TYPED_TEST_CASE_P( | 989 INSTANTIATE_TYPED_TEST_CASE_P( |
959 SequencedWorkerPoolSequencedTaskRunner, SequencedTaskRunnerTest, | 990 SequencedWorkerPoolSequencedTaskRunner, SequencedTaskRunnerTest, |
960 SequencedWorkerPoolSequencedTaskRunnerTestDelegate); | 991 SequencedWorkerPoolSequencedTaskRunnerTestDelegate); |
961 | 992 |
962 } // namespace | 993 } // namespace |
963 | 994 |
964 } // namespace base | 995 } // namespace base |
OLD | NEW |