Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: base/test/scoped_task_scheduler.h

Issue 2557083002: Run ScopedTaskScheduler tasks synchronously. (Closed)
Patch Set: CR gab #20 Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 the following 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 // When |scoped_task_scheduler| is destroyed, it runs "D" since it's
robliao 2016/12/13 03:27:42 It's not immediately clear that the entire code sa
fdoray 2016/12/13 19:14:14 Done.
fdoray 2016/12/13 20:38:31 The 2 first blocks of code don't have to be taken
44 // 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 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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698