Chromium Code Reviews| 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..e698b491da9f0e0df6f23639a6e667a8852df71d |
| --- /dev/null |
| +++ b/base/task_scheduler/task_tracker.h |
| @@ -0,0 +1,96 @@ |
| +// 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(); |
| + |
| + // Shuts down the task tracker. Once this is called, only tasks posted with |
|
robliao
2016/03/01 22:29:41
The blocking behavior is important enough to reemp
fdoray
2016/03/02 00:38:41
Done.
|
| + // 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| unless the current shutdown state prevents that. Posting a |
|
robliao
2016/03/01 22:29:41
Calls |post_task_callback| unless the current shut
fdoray
2016/03/02 00:38:41
Done.
|
| + // task involves updating the internal state of the TaskTracker, generating a |
| + // tracing event and invoking |post_task_callback|. |post_task_callback| must |
|
gab
2016/03/01 22:18:47
',' before "and"
fdoray
2016/03/02 00:38:41
Done. (Conflicts with robliao@'s comment)
|
| + // add the task it receives to a pool from which tasks are run until |
| + // shutdown_completed() returns true. |
|
gab
2016/03/01 22:18:47
s/are run until shutdown_completed() returns true/
fdoray
2016/03/02 00:38:41
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(). |
|
gab
2016/03/01 22:18:47
s/./ first./
fdoray
2016/03/02 00:38:41
Done.
|
| + 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: |
| + // Informs TaskTracker that a task with |shutdown_behavior| is about to be |
| + // posted. Updates |num_tasks_blocking_shutdown_| and returns true if this |
| + // is allowed by the current shutdown state. |
| + bool RequestPostTask(TaskShutdownBehavior shutdown_behavior); |
| + |
| + // Informs TaskTracker that a task with |shutdown_behavior| is about to be |
| + // scheduled. Updates |num_tasks_blocking_shutdown_| and returns true if this |
| + // is allowed by the current shutdown state. |
| + bool RequestScheduleTask(TaskShutdownBehavior shutdown_behavior); |
| + |
| + // Informs TaskTracker that a task with |shutdown_behavior| has been executed. |
| + // Updates |num_tasks_blocking_shutdown_| and signals |cv_| when appropriate. |
| + void DidExecuteTask(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_ |