OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_TASK_SCHEDULER_TASK_TRACKER_H_ |
| 6 #define BASE_TASK_SCHEDULER_TASK_TRACKER_H_ |
| 7 |
| 8 #include "base/base_export.h" |
| 9 #include "base/callback_forward.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/metrics/histogram_base.h" |
| 13 #include "base/synchronization/condition_variable.h" |
| 14 #include "base/task_scheduler/scheduler_lock.h" |
| 15 #include "base/task_scheduler/task.h" |
| 16 #include "base/task_scheduler/task_traits.h" |
| 17 |
| 18 namespace base { |
| 19 namespace internal { |
| 20 |
| 21 // All tasks go through the scheduler's TaskTracker when they are posted and |
| 22 // when they are executed. The TaskTracker enforces shutdown semantics and takes |
| 23 // care of tracing and profiling. This class is thread-safe. |
| 24 class BASE_EXPORT TaskTracker { |
| 25 public: |
| 26 TaskTracker(); |
| 27 ~TaskTracker(); |
| 28 |
| 29 // Synchronously shuts down the scheduler. Once this is called, only tasks |
| 30 // posted with the BLOCK_SHUTDOWN behavior will be run. Returns when: |
| 31 // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their |
| 32 // execution. |
| 33 // - All posted BLOCK_SHUTDOWN tasks have completed their execution. |
| 34 // This must only be called once. |
| 35 void Shutdown(); |
| 36 |
| 37 // Posts |task| by calling |post_task_callback| unless the current shutdown |
| 38 // state prevents that. A task forwarded to |post_task_callback| must be |
| 39 // handed back to this instance's RunTask() when it is to be executed. |
| 40 void PostTask(const Callback<void(scoped_ptr<Task>)>& post_task_callback, |
| 41 scoped_ptr<Task> task); |
| 42 |
| 43 // Runs |task| unless the current shutdown state prevents that. |task| must |
| 44 // have been successfully posted via PostTask() first. |
| 45 void RunTask(const Task* task); |
| 46 |
| 47 // Returns true while shutdown is in progress (i.e. Shutdown() has been called |
| 48 // but hasn't returned). |
| 49 bool IsShuttingDownForTesting() const; |
| 50 |
| 51 bool shutdown_completed() const { |
| 52 AutoSchedulerLock auto_lock(lock_); |
| 53 return shutdown_completed_; |
| 54 } |
| 55 |
| 56 private: |
| 57 // Called before a task with |shutdown_behavior| is handed off to |
| 58 // |post_task_callback| by PostTask(). Updates |num_tasks_blocking_shutdown_| |
| 59 // if necessary and returns true if the current shutdown state allows the task |
| 60 // to be posted. |
| 61 bool BeforePostTask(TaskShutdownBehavior shutdown_behavior); |
| 62 |
| 63 // Called before a task with |shutdown_behavior| is run by RunTask(). Updates |
| 64 // |num_tasks_blocking_shutdown_| if necessary and returns true if the current |
| 65 // shutdown state allows the task to be run. |
| 66 bool BeforeRunTask(TaskShutdownBehavior shutdown_behavior); |
| 67 |
| 68 // Called after a task with |shutdown_behavior| has been run by RunTask(). |
| 69 // Updates |num_tasks_blocking_shutdown_| and signals |shutdown_cv_| if |
| 70 // necessary. |
| 71 void AfterRunTask(TaskShutdownBehavior shutdown_behavior); |
| 72 |
| 73 // Synchronizes access to all members. |
| 74 mutable SchedulerLock lock_; |
| 75 |
| 76 // Condition variable signaled when |num_tasks_blocking_shutdown_| reaches |
| 77 // zero while shutdown is in progress. Null if shutdown isn't in progress. |
| 78 scoped_ptr<ConditionVariable> shutdown_cv_; |
| 79 |
| 80 // Number of tasks blocking shutdown. |
| 81 size_t num_tasks_blocking_shutdown_ = 0; |
| 82 |
| 83 // Number of BLOCK_SHUTDOWN tasks posted during shutdown. |
| 84 HistogramBase::Sample num_block_shutdown_tasks_posted_during_shutdown_ = 0; |
| 85 |
| 86 // True once Shutdown() has returned. No new task can be scheduled after this. |
| 87 bool shutdown_completed_ = false; |
| 88 |
| 89 DISALLOW_COPY_AND_ASSIGN(TaskTracker); |
| 90 }; |
| 91 |
| 92 } // namespace internal |
| 93 } // namespace base |
| 94 |
| 95 #endif // BASE_TASK_SCHEDULER_TASK_TRACKER_H_ |
OLD | NEW |