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