| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/task_scheduler/scheduler_worker.h" | 5 #include "base/task_scheduler/scheduler_worker.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/synchronization/condition_variable.h" | 17 #include "base/synchronization/condition_variable.h" |
| 18 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
| 19 #include "base/task_scheduler/scheduler_lock.h" | 19 #include "base/task_scheduler/scheduler_lock.h" |
| 20 #include "base/task_scheduler/sequence.h" | 20 #include "base/task_scheduler/sequence.h" |
| 21 #include "base/task_scheduler/task.h" | 21 #include "base/task_scheduler/task.h" |
| 22 #include "base/task_scheduler/task_tracker.h" | 22 #include "base/task_scheduler/task_tracker.h" |
| 23 #include "base/task_scheduler/test_utils.h" |
| 23 #include "base/test/test_timeouts.h" | 24 #include "base/test/test_timeouts.h" |
| 24 #include "base/threading/platform_thread.h" | 25 #include "base/threading/platform_thread.h" |
| 25 #include "base/threading/simple_thread.h" | 26 #include "base/threading/simple_thread.h" |
| 26 #include "base/time/time.h" | 27 #include "base/time/time.h" |
| 27 #include "build/build_config.h" | 28 #include "build/build_config.h" |
| 28 #include "testing/gmock/include/gmock/gmock.h" | 29 #include "testing/gmock/include/gmock/gmock.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 30 | 31 |
| 31 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
| 32 #include <objbase.h> | 33 #include <objbase.h> |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 | 167 |
| 167 // Check if a Sequence should be returned. | 168 // Check if a Sequence should be returned. |
| 168 if (outer_->num_sequences_to_create_ == 0) | 169 if (outer_->num_sequences_to_create_ == 0) |
| 169 return nullptr; | 170 return nullptr; |
| 170 --outer_->num_sequences_to_create_; | 171 --outer_->num_sequences_to_create_; |
| 171 } | 172 } |
| 172 | 173 |
| 173 // Create a Sequence with TasksPerSequence() Tasks. | 174 // Create a Sequence with TasksPerSequence() Tasks. |
| 174 scoped_refptr<Sequence> sequence(new Sequence); | 175 scoped_refptr<Sequence> sequence(new Sequence); |
| 175 for (size_t i = 0; i < outer_->TasksPerSequence(); ++i) { | 176 for (size_t i = 0; i < outer_->TasksPerSequence(); ++i) { |
| 176 std::unique_ptr<Task> task( | 177 auto task = |
| 177 new Task(FROM_HERE, | 178 MakeUnique<Task>(FROM_HERE, |
| 178 BindOnce(&TaskSchedulerWorkerTest::RunTaskCallback, | 179 BindOnce(&TaskSchedulerWorkerTest::RunTaskCallback, |
| 179 Unretained(outer_)), | 180 Unretained(outer_)), |
| 180 TaskTraits(), TimeDelta())); | 181 test::CreateTaskTraits(), TimeDelta()); |
| 181 EXPECT_TRUE(outer_->task_tracker_.WillPostTask(task.get())); | 182 EXPECT_TRUE(outer_->task_tracker_.WillPostTask(task.get())); |
| 182 sequence->PushTask(std::move(task)); | 183 sequence->PushTask(std::move(task)); |
| 183 } | 184 } |
| 184 | 185 |
| 185 ExpectCallToDidRunTask(); | 186 ExpectCallToDidRunTask(); |
| 186 | 187 |
| 187 { | 188 { |
| 188 // Add the Sequence to the vector of created Sequences. | 189 // Add the Sequence to the vector of created Sequences. |
| 189 AutoSchedulerLock auto_lock(outer_->lock_); | 190 AutoSchedulerLock auto_lock(outer_->lock_); |
| 190 outer_->created_sequences_.push_back(sequence); | 191 outer_->created_sequences_.push_back(sequence); |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 override { | 435 override { |
| 435 EXPECT_TRUE(controls_->expect_get_work_); | 436 EXPECT_TRUE(controls_->expect_get_work_); |
| 436 | 437 |
| 437 // Sends one item of work to signal |work_processed_|. On subsequent calls, | 438 // Sends one item of work to signal |work_processed_|. On subsequent calls, |
| 438 // sends nullptr to indicate there's no more work to be done. | 439 // sends nullptr to indicate there's no more work to be done. |
| 439 if (controls_->work_requested_) | 440 if (controls_->work_requested_) |
| 440 return nullptr; | 441 return nullptr; |
| 441 | 442 |
| 442 controls_->work_requested_ = true; | 443 controls_->work_requested_ = true; |
| 443 scoped_refptr<Sequence> sequence(new Sequence); | 444 scoped_refptr<Sequence> sequence(new Sequence); |
| 444 std::unique_ptr<Task> task(new Task( | 445 auto task = MakeUnique<Task>( |
| 445 FROM_HERE, | 446 FROM_HERE, |
| 446 BindOnce( | 447 BindOnce( |
| 447 [](WaitableEvent* work_processed, WaitableEvent* work_running) { | 448 [](WaitableEvent* work_processed, WaitableEvent* work_running) { |
| 448 work_processed->Signal(); | 449 work_processed->Signal(); |
| 449 work_running->Wait(); | 450 work_running->Wait(); |
| 450 }, | 451 }, |
| 451 Unretained(&controls_->work_processed_), | 452 Unretained(&controls_->work_processed_), |
| 452 Unretained(&controls_->work_running_)), | 453 Unretained(&controls_->work_running_)), |
| 453 TaskTraits().WithBaseSyncPrimitives().WithShutdownBehavior( | 454 test::CreateTaskTraits().WithBaseSyncPrimitives().WithShutdownBehavior( |
| 454 TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), | 455 TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), |
| 455 TimeDelta())); | 456 TimeDelta()); |
| 456 EXPECT_TRUE(task_tracker_->WillPostTask(task.get())); | 457 EXPECT_TRUE(task_tracker_->WillPostTask(task.get())); |
| 457 sequence->PushTask(std::move(task)); | 458 sequence->PushTask(std::move(task)); |
| 458 return sequence; | 459 return sequence; |
| 459 } | 460 } |
| 460 | 461 |
| 461 void DidRunTask() override {} | 462 void DidRunTask() override {} |
| 462 | 463 |
| 463 bool CanDetach(SchedulerWorker* worker) override { | 464 bool CanDetach(SchedulerWorker* worker) override { |
| 464 // Saving |can_detach_| now so that callers waiting on |detach_requested_| | 465 // Saving |can_detach_| now so that callers waiting on |detach_requested_| |
| 465 // have the thread go to sleep and then allow detachment. | 466 // have the thread go to sleep and then allow detachment. |
| (...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 955 // COM library wasn't already initialized on the thread. | 956 // COM library wasn't already initialized on the thread. |
| 956 EXPECT_EQ(S_OK, delegate_raw->coinitialize_hresult()); | 957 EXPECT_EQ(S_OK, delegate_raw->coinitialize_hresult()); |
| 957 | 958 |
| 958 worker->JoinForTesting(); | 959 worker->JoinForTesting(); |
| 959 } | 960 } |
| 960 | 961 |
| 961 #endif // defined(OS_WIN) | 962 #endif // defined(OS_WIN) |
| 962 | 963 |
| 963 } // namespace internal | 964 } // namespace internal |
| 964 } // namespace base | 965 } // namespace base |
| OLD | NEW |