Chromium Code Reviews| 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 of runs remaining | |
|
gab
2016/12/08 20:15:45
rm "of"
fdoray
2016/12/09 16:26:41
Done.
| |
| 21 // BLOCK_SHUTDOWN tasks synchronously. | |
| 22 // | |
| 23 // Example usage: | |
| 24 // | |
| 25 // base::test::ScopedTaskScheduler scoped_task_scheduler; | |
|
robliao
2016/12/09 01:32:54
Nit: It might be clearer to have a leading section
fdoray
2016/12/09 16:26:41
Done.
| |
| 26 // base::PostTask(FROM_HERE, base::Bind(&A)); | |
| 27 // base::RunLoop::RunUntilIdle(); // Returns after running A. | |
| 28 // | |
| 29 // base::RunLoop run_loop; | |
| 30 // base::PostTask(FROM_HERE, base::Bind(&B)); | |
| 31 // base::PostTask(FROM_HERE, base::Bind(&RunLoop::Quit, &run_loop)); | |
| 32 // base::PostTask(FROM_HERE, base::Bind(&C)); | |
| 33 // base::PostTaskWithTraits( | |
| 34 // base::TaskTraits().WithShutdownBehavior( | |
| 35 // base::TaskShutdownBehavior::BLOCK_SHUTDOWN), | |
| 36 // base::Bind(&D)); | |
| 37 // run_loop.Run(); // Returns after running B and RunLoop::Quit. | |
| 38 // | |
| 39 // D runs at the end of this scope because it is BLOCK_SHUTDOWN. | |
| 18 class ScopedTaskScheduler { | 40 class ScopedTaskScheduler { |
| 19 public: | 41 public: |
| 20 // Initializes a TaskScheduler with default arguments. | 42 enum Options { |
| 43 DEFAULT = 0x0, | |
|
robliao
2016/12/09 01:32:54
Is this going to be a future set of flags? If not,
fdoray
2016/12/09 16:26:41
Removed.
| |
| 44 | |
| 45 // Run TaskScheduler on a MessageLoopForUI instead of a MessageLoopForIO. | |
| 46 // TaskScheduler tasks can't use FileDescriptorWatcher with this option. | |
| 47 UI_MAIN_LOOP = 0x1, | |
| 48 }; | |
| 49 | |
| 50 // Registers a TaskScheduler. | |
| 21 ScopedTaskScheduler(); | 51 ScopedTaskScheduler(); |
| 52 ScopedTaskScheduler(Options options); | |
| 22 | 53 |
| 23 // Waits until all TaskScheduler tasks blocking shutdown complete their | 54 // 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(); | 55 ~ScopedTaskScheduler(); |
| 31 | 56 |
| 32 private: | 57 private: |
| 33 const TaskScheduler* task_scheduler_ = nullptr; | 58 const TaskScheduler* task_scheduler_ = nullptr; |
| 59 ThreadChecker thread_checker_; | |
| 34 | 60 |
| 35 DISALLOW_COPY_AND_ASSIGN(ScopedTaskScheduler); | 61 DISALLOW_COPY_AND_ASSIGN(ScopedTaskScheduler); |
| 36 }; | 62 }; |
| 37 | 63 |
| 38 } // namespace test | 64 } // namespace test |
| 39 } // namespace base | 65 } // namespace base |
| 40 | 66 |
| 41 #endif // BASE_TEST_SCOPED_TASK_SCHEDULER_H_ | 67 #endif // BASE_TEST_SCOPED_TASK_SCHEDULER_H_ |
| OLD | NEW |