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 of the scheduler go through a 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 task tracker. 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 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.
| |
39 void PostTask(const Callback<void(scoped_ptr<Task>)>& post_task_callback, | |
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 bool shutdown_completed() const { | |
47 AutoSchedulerLock auto_lock(lock_); | |
48 return shutdown_completed_; | |
49 } | |
50 | |
51 bool is_shutting_down_for_testing() const { | |
52 AutoSchedulerLock auto_lock(lock_); | |
53 return is_shutting_down_; | |
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 // and returns true if the current shutdown state allows the task to be | |
60 // 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_| and returns true if the current shutdown | |
65 // 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 |cv_| when appropriate. | |
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 |is_shutting_down_|. | |
77 scoped_ptr<ConditionVariable> cv_; | |
78 | |
79 // Number of tasks blocking shutdown. | |
80 size_t num_tasks_blocking_shutdown_; | |
81 | |
82 // True when Shutdown() is waiting for tasks blocking shutdown to complete | |
83 // their execution. | |
84 bool is_shutting_down_; | |
85 | |
86 // True once Shutdown() has returned. No new task can be scheduled after this. | |
87 bool shutdown_completed_; | |
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 |