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