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 #ifndef BASE_TEST_SCOPED_TASK_SCHEDULER_H_ | 5 #ifndef BASE_TEST_SCOPED_TASK_SCHEDULER_H_ |
6 #define BASE_TEST_SCOPED_TASK_SCHEDULER_H_ | 6 #define BASE_TEST_SCOPED_TASK_SCHEDULER_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/threading/thread_checker.h" |
9 | 10 |
10 namespace base { | 11 namespace base { |
11 | 12 |
| 13 class MessageLoop; |
12 class TaskScheduler; | 14 class TaskScheduler; |
13 | 15 |
14 namespace test { | 16 namespace test { |
15 | 17 |
16 // Initializes a TaskScheduler and allows usage of the | 18 // Allows usage of the base/task_scheduler/post_task.h API within its scope. |
17 // base/task_scheduler/post_task.h API within its scope. | 19 // |
| 20 // To run pending tasks synchronously, call RunLoop::Run/RunUntilIdle() on the |
| 21 // thread where the ScopedTaskScheduler lives. The destructor runs remaining |
| 22 // BLOCK_SHUTDOWN tasks synchronously. |
| 23 // |
| 24 // Example usage: |
| 25 // |
| 26 // In this snippet, RunUntilIdle() returns after "A" is run. |
| 27 // base::test::ScopedTaskScheduler scoped_task_scheduler; |
| 28 // base::PostTask(FROM_HERE, base::Bind(&A)); |
| 29 // base::RunLoop::RunUntilIdle(); // Returns after running A. |
| 30 // |
| 31 // In this snippet, run_loop.Run() returns after running "B" and |
| 32 // "RunLoop::Quit". |
| 33 // base::RunLoop run_loop; |
| 34 // base::PostTask(FROM_HERE, base::Bind(&B)); |
| 35 // base::PostTask(FROM_HERE, base::Bind(&RunLoop::Quit, &run_loop)); |
| 36 // base::PostTask(FROM_HERE, base::Bind(&C)); |
| 37 // base::PostTaskWithTraits( |
| 38 // base::TaskTraits().WithShutdownBehavior( |
| 39 // base::TaskShutdownBehavior::BLOCK_SHUTDOWN), |
| 40 // base::Bind(&D)); |
| 41 // run_loop.Run(); // Returns after running B and RunLoop::Quit. |
| 42 // |
| 43 // At this point, |scoped_task_scheduler| will be destroyed. The destructor |
| 44 // runs "D" because it's BLOCK_SHUTDOWN. "C" is skipped. |
18 class ScopedTaskScheduler { | 45 class ScopedTaskScheduler { |
19 public: | 46 public: |
20 // Initializes a TaskScheduler with default arguments. | 47 // Registers a TaskScheduler that instantiates a MessageLoop on the current |
| 48 // thread and runs its tasks on it. |
21 ScopedTaskScheduler(); | 49 ScopedTaskScheduler(); |
22 | 50 |
23 // Waits until all TaskScheduler tasks blocking shutdown complete their | 51 // Registers a TaskScheduler that runs its tasks on |external_message_loop|. |
24 // execution (see TaskShutdownBehavior). Then, joins all TaskScheduler threads | 52 // |external_message_loop| must be bound to the current thread. |
25 // and deletes the TaskScheduler. | 53 explicit ScopedTaskScheduler(MessageLoop* external_message_loop); |
26 // | 54 |
27 // Note that joining TaskScheduler threads may involve waiting for | 55 // Runs all pending BLOCK_SHUTDOWN tasks and unregisters the TaskScheduler. |
28 // CONTINUE_ON_SHUTDOWN tasks to complete their execution. Normally, in | |
29 // production, the process exits without joining TaskScheduler threads. | |
30 ~ScopedTaskScheduler(); | 56 ~ScopedTaskScheduler(); |
31 | 57 |
32 private: | 58 private: |
33 const TaskScheduler* task_scheduler_ = nullptr; | 59 const TaskScheduler* task_scheduler_ = nullptr; |
| 60 ThreadChecker thread_checker_; |
34 | 61 |
35 DISALLOW_COPY_AND_ASSIGN(ScopedTaskScheduler); | 62 DISALLOW_COPY_AND_ASSIGN(ScopedTaskScheduler); |
36 }; | 63 }; |
37 | 64 |
38 } // namespace test | 65 } // namespace test |
39 } // namespace base | 66 } // namespace base |
40 | 67 |
41 #endif // BASE_TEST_SCOPED_TASK_SCHEDULER_H_ | 68 #endif // BASE_TEST_SCOPED_TASK_SCHEDULER_H_ |
OLD | NEW |