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/task_tracker.h" | 5 #include "base/task_scheduler/task_tracker.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.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/logging.h" | 13 #include "base/logging.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/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 19 #include "base/synchronization/atomic_flag.h" |
19 #include "base/synchronization/waitable_event.h" | 20 #include "base/synchronization/waitable_event.h" |
20 #include "base/task_scheduler/scheduler_lock.h" | 21 #include "base/task_scheduler/scheduler_lock.h" |
21 #include "base/task_scheduler/task.h" | 22 #include "base/task_scheduler/task.h" |
22 #include "base/task_scheduler/task_traits.h" | 23 #include "base/task_scheduler/task_traits.h" |
23 #include "base/task_scheduler/test_utils.h" | 24 #include "base/task_scheduler/test_utils.h" |
24 #include "base/test/test_simple_task_runner.h" | 25 #include "base/test/test_simple_task_runner.h" |
25 #include "base/threading/platform_thread.h" | 26 #include "base/threading/platform_thread.h" |
26 #include "base/threading/sequenced_task_runner_handle.h" | 27 #include "base/threading/sequenced_task_runner_handle.h" |
27 #include "base/threading/simple_thread.h" | 28 #include "base/threading/simple_thread.h" |
28 #include "base/threading/thread_restrictions.h" | 29 #include "base/threading/thread_restrictions.h" |
29 #include "base/threading/thread_task_runner_handle.h" | 30 #include "base/threading/thread_task_runner_handle.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
31 | 32 |
32 namespace base { | 33 namespace base { |
33 namespace internal { | 34 namespace internal { |
34 | 35 |
35 namespace { | 36 namespace { |
36 | 37 |
37 constexpr size_t kLoadTestNumIterations = 100; | 38 constexpr size_t kLoadTestNumIterations = 100; |
38 | 39 |
39 // Calls TaskTracker::Shutdown() asynchronously. | 40 // Calls TaskTracker::Shutdown() asynchronously. |
40 class ThreadCallingShutdown : public SimpleThread { | 41 class ThreadCallingShutdown : public SimpleThread { |
41 public: | 42 public: |
42 explicit ThreadCallingShutdown(TaskTracker* tracker) | 43 explicit ThreadCallingShutdown(TaskTracker* tracker) |
43 : SimpleThread("ThreadCallingShutdown"), | 44 : SimpleThread("ThreadCallingShutdown"), tracker_(tracker) {} |
44 tracker_(tracker), | |
45 has_returned_(WaitableEvent::ResetPolicy::MANUAL, | |
46 WaitableEvent::InitialState::NOT_SIGNALED) {} | |
47 | 45 |
48 // Returns true once the async call to Shutdown() has returned. | 46 // Returns true once the async call to Shutdown() has returned. |
49 bool has_returned() { return has_returned_.IsSignaled(); } | 47 bool has_returned() { return has_returned_.IsSet(); } |
50 | 48 |
51 private: | 49 private: |
52 void Run() override { | 50 void Run() override { |
53 tracker_->Shutdown(); | 51 tracker_->Shutdown(); |
54 has_returned_.Signal(); | 52 has_returned_.Set(); |
55 } | 53 } |
56 | 54 |
57 TaskTracker* const tracker_; | 55 TaskTracker* const tracker_; |
58 WaitableEvent has_returned_; | 56 AtomicFlag has_returned_; |
59 | 57 |
60 DISALLOW_COPY_AND_ASSIGN(ThreadCallingShutdown); | 58 DISALLOW_COPY_AND_ASSIGN(ThreadCallingShutdown); |
61 }; | 59 }; |
62 | 60 |
63 class ThreadPostingAndRunningTask : public SimpleThread { | 61 class ThreadPostingAndRunningTask : public SimpleThread { |
64 public: | 62 public: |
65 enum class Action { | 63 enum class Action { |
66 WILL_POST, | 64 WILL_POST, |
67 RUN, | 65 RUN, |
68 WILL_POST_AND_RUN, | 66 WILL_POST_AND_RUN, |
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
596 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 594 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
597 | 595 |
598 // Unblock shutdown by running |block_shutdown_task|. | 596 // Unblock shutdown by running |block_shutdown_task|. |
599 tracker_.RunTask(block_shutdown_task.get()); | 597 tracker_.RunTask(block_shutdown_task.get()); |
600 EXPECT_EQ(kLoadTestNumIterations + 1, NumTasksExecuted()); | 598 EXPECT_EQ(kLoadTestNumIterations + 1, NumTasksExecuted()); |
601 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 599 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
602 } | 600 } |
603 | 601 |
604 } // namespace internal | 602 } // namespace internal |
605 } // namespace base | 603 } // namespace base |
OLD | NEW |