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

Unified Diff: base/task_scheduler/task_tracker.h

Issue 1705943002: TaskScheduler [5/9] Task Tracker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_3_pq
Patch Set: expand tests Created 4 years, 10 months 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/task_scheduler/task_tracker.h
diff --git a/base/task_scheduler/task_tracker.h b/base/task_scheduler/task_tracker.h
new file mode 100644
index 0000000000000000000000000000000000000000..8c291991fc4e5361f8459ba47e03eed37859d058
--- /dev/null
+++ b/base/task_scheduler/task_tracker.h
@@ -0,0 +1,95 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_TASK_SCHEDULER_TASK_TRACKER_H_
+#define BASE_TASK_SCHEDULER_TASK_TRACKER_H_
+
+#include "base/base_export.h"
+#include "base/callback_forward.h"
+#include "base/macros.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/synchronization/condition_variable.h"
+#include "base/task_scheduler/scheduler_lock.h"
+#include "base/task_scheduler/task.h"
+#include "base/task_scheduler/task_traits.h"
+
+namespace base {
+namespace internal {
+
+// All tasks of the scheduler go through a TaskTracker when they are posted and
+// when they are executed. The TaskTracker enforces shutdown semantics and takes
+// care of tracing and profiling. This class is thread-safe.
+class BASE_EXPORT TaskTracker {
+ public:
+ TaskTracker();
+ ~TaskTracker();
+
+ // Synchronously shuts down the task tracker. Once this is called, only tasks
+ // posted with the BLOCK_SHUTDOWN behavior will be run. Returns when:
+ // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their
+ // execution.
+ // - All posted BLOCK_SHUTDOWN tasks have completed their execution.
+ // This must only be called once.
+ void Shutdown();
+
+ // Posts |task| by calling |post_task_callback| unless the current shutdown
+ // state prevents that. A task forwarded to |post_task_callback| must be
+ // handed off to this class' RunTask() when it is to be executed.
gab 2016/03/09 21:53:25 s/handed off/handed back/
fdoray 2016/03/15 17:28:09 Done.
+ void PostTask(const Callback<void(scoped_ptr<Task>)>& post_task_callback,
+ scoped_ptr<Task> task);
+
+ // Runs |task| unless the current shutdown state prevents that. |task| must
+ // have been successfully posted via PostTask() first.
+ void RunTask(const Task* task);
+
+ bool shutdown_completed() const {
+ AutoSchedulerLock auto_lock(lock_);
+ return shutdown_completed_;
+ }
+
+ bool is_shutting_down_for_testing() const {
+ AutoSchedulerLock auto_lock(lock_);
+ return is_shutting_down_;
+ }
+
+ private:
+ // Called before a task with |shutdown_behavior| is handed off to
+ // |post_task_callback| by PostTask(). Updates |num_tasks_blocking_shutdown_|
+ // and returns true if the current shutdown state allows the task to be
+ // posted.
+ bool BeforePostTask(TaskShutdownBehavior shutdown_behavior);
+
+ // Called before a task with |shutdown_behavior| is run by RunTask(). Updates
+ // |num_tasks_blocking_shutdown_| and returns true if the current shutdown
+ // state allows the task to be run.
+ bool BeforeRunTask(TaskShutdownBehavior shutdown_behavior);
+
+ // Called after a task with |shutdown_behavior| has been run by RunTask().
+ // Updates |num_tasks_blocking_shutdown_| and signals |cv_| when appropriate.
+ void AfterRunTask(TaskShutdownBehavior shutdown_behavior);
+
+ // Synchronizes access to all members.
+ mutable SchedulerLock lock_;
+
+ // Condition variable signaled when |num_tasks_blocking_shutdown_| reaches
+ // zero while |is_shutting_down_|.
+ scoped_ptr<ConditionVariable> cv_;
+
+ // Number of tasks blocking shutdown.
+ size_t num_tasks_blocking_shutdown_;
+
+ // True when Shutdown() is waiting for tasks blocking shutdown to complete
+ // their execution.
+ bool is_shutting_down_;
+
+ // True once Shutdown() has returned. No new task can be scheduled after this.
+ bool shutdown_completed_;
+
+ DISALLOW_COPY_AND_ASSIGN(TaskTracker);
+};
+
+} // namespace internal
+} // namespace base
+
+#endif // BASE_TASK_SCHEDULER_TASK_TRACKER_H_

Powered by Google App Engine
This is Rietveld 408576698