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

Unified Diff: base/test/scoped_task_scheduler.h

Issue 2557083002: Run ScopedTaskScheduler tasks synchronously. (Closed)
Patch Set: self-review 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..6fca60dda86c884377892ac2c5dd2d668eeeb8f8 100644
--- a/base/test/scoped_task_scheduler.h
+++ b/base/test/scoped_task_scheduler.h
@@ -6,6 +6,7 @@
#define BASE_TEST_SCOPED_TASK_SCHEDULER_H_
#include "base/macros.h"
+#include "base/threading/thread_checker.h"
namespace base {
@@ -13,24 +14,45 @@ 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 the following 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.
+//
+// When |scoped_task_scheduler| is destroyed, it runs "D" since it's
+// BLOCK_SHUTDOWN. "C" is skipped.
class ScopedTaskScheduler {
public:
- // Initializes a TaskScheduler with default arguments.
+ // Registers a TaskScheduler that runs on the current thread's MessageLoop. If
+ // the current thread doesn't have a MessageLoop, one is created.
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.
+ // 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