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