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

Unified Diff: base/test/scoped_task_scheduler.h

Issue 2557083002: Run ScopedTaskScheduler tasks synchronously. (Closed)
Patch Set: add owners 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 side-by-side diff with in-line comments
Download patch
Index: base/test/scoped_task_scheduler.h
diff --git a/base/test/scoped_task_scheduler.h b/base/test/scoped_task_scheduler.h
index f2b252b601f81d83df4baab85d62375b908ffa80..2b278b44a8472dc25819e1313a4d6e409bcb2a9b 100644
--- a/base/test/scoped_task_scheduler.h
+++ b/base/test/scoped_task_scheduler.h
@@ -6,31 +6,58 @@
#define BASE_TEST_SCOPED_TASK_SCHEDULER_H_
#include "base/macros.h"
+#include "base/threading/thread_checker.h"
namespace base {
+class MessageLoop;
class TaskScheduler;
namespace test {
-// Initializes a TaskScheduler and allows usage of the
-// base/task_scheduler/post_task.h API within its scope.
+// Allows usage of the base/task_scheduler/post_task.h API within its scope.
+//
+// To run pending tasks synchronously, call RunLoop::Run/RunUntilIdle() on the
+// thread where the ScopedTaskScheduler lives. The destructor runs remaining
+// BLOCK_SHUTDOWN tasks synchronously.
+//
+// Example usage:
+//
+// In this snippet, RunUntilIdle() returns after "A" is run.
+// base::test::ScopedTaskScheduler scoped_task_scheduler;
+// base::PostTask(FROM_HERE, base::Bind(&A));
+// base::RunLoop::RunUntilIdle(); // Returns after running A.
+//
+// In this snippet, run_loop.Run() returns after running "B" and
+// "RunLoop::Quit".
+// base::RunLoop run_loop;
+// base::PostTask(FROM_HERE, base::Bind(&B));
+// base::PostTask(FROM_HERE, base::Bind(&RunLoop::Quit, &run_loop));
+// base::PostTask(FROM_HERE, base::Bind(&C));
+// base::PostTaskWithTraits(
+// base::TaskTraits().WithShutdownBehavior(
+// base::TaskShutdownBehavior::BLOCK_SHUTDOWN),
+// base::Bind(&D));
+// run_loop.Run(); // Returns after running B and RunLoop::Quit.
+//
+// At this point, |scoped_task_scheduler| will be destroyed. The destructor
+// runs "D" because it's BLOCK_SHUTDOWN. "C" is skipped.
class ScopedTaskScheduler {
public:
- // Initializes a TaskScheduler with default arguments.
+ // Registers a TaskScheduler that instantiates a MessageLoop on the current
+ // thread and runs its tasks on it.
ScopedTaskScheduler();
- // Waits until all TaskScheduler tasks blocking shutdown complete their
- // execution (see TaskShutdownBehavior). Then, joins all TaskScheduler threads
- // and deletes the TaskScheduler.
- //
- // Note that joining TaskScheduler threads may involve waiting for
- // CONTINUE_ON_SHUTDOWN tasks to complete their execution. Normally, in
- // production, the process exits without joining TaskScheduler threads.
+ // Registers a TaskScheduler that runs its tasks on |external_message_loop|.
+ // |external_message_loop| must be bound to the current thread.
+ ScopedTaskScheduler(MessageLoop* external_message_loop);
dcheng 2016/12/15 06:47:36 Nit: explicit
fdoray 2016/12/15 16:39:32 Done.
+
+ // Runs all pending BLOCK_SHUTDOWN tasks and unregisters the TaskScheduler.
~ScopedTaskScheduler();
private:
const TaskScheduler* task_scheduler_ = nullptr;
+ ThreadChecker thread_checker_;
DISALLOW_COPY_AND_ASSIGN(ScopedTaskScheduler);
};

Powered by Google App Engine
This is Rietveld 408576698