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 // 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.
| |
29 // 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| 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.
| |
37 // task involves updating the internal state of the TaskTracker, generating a | |
38 // 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)
| |
39 // add the task it receives to a pool from which tasks are run until | |
40 // 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.
| |
41 void PostTask(const Callback<void(scoped_ptr<Task>)>& post_task_callback, | |
42 scoped_ptr<Task> task); | |
43 | |
44 // Runs |task| unless the current shutdown state prevents that. |task| must | |
45 // have been successfully posted via PostTask(). | |
gab
2016/03/01 22:18:47
s/./ first./
fdoray
2016/03/02 00:38:41
Done.
| |
46 void RunTask(const Task* task); | |
47 | |
48 bool shutdown_completed() const { | |
49 AutoSchedulerLock auto_lock(lock_); | |
50 return shutdown_completed_; | |
51 } | |
52 | |
53 bool is_shutting_down_for_testing() const { | |
54 AutoSchedulerLock auto_lock(lock_); | |
55 return is_shutting_down_; | |
56 } | |
57 | |
58 private: | |
59 // Informs TaskTracker that a task with |shutdown_behavior| is about to be | |
60 // posted. Updates |num_tasks_blocking_shutdown_| and returns true if this | |
61 // is allowed by the current shutdown state. | |
62 bool RequestPostTask(TaskShutdownBehavior shutdown_behavior); | |
63 | |
64 // Informs TaskTracker that a task with |shutdown_behavior| is about to be | |
65 // scheduled. Updates |num_tasks_blocking_shutdown_| and returns true if this | |
66 // is allowed by the current shutdown state. | |
67 bool RequestScheduleTask(TaskShutdownBehavior shutdown_behavior); | |
68 | |
69 // Informs TaskTracker that a task with |shutdown_behavior| has been executed. | |
70 // Updates |num_tasks_blocking_shutdown_| and signals |cv_| when appropriate. | |
71 void DidExecuteTask(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 |is_shutting_down_|. | |
78 scoped_ptr<ConditionVariable> cv_; | |
79 | |
80 // Number of tasks blocking shutdown. | |
81 size_t num_tasks_blocking_shutdown_; | |
82 | |
83 // True when Shutdown() is waiting for tasks blocking shutdown to complete | |
84 // their execution. | |
85 bool is_shutting_down_; | |
86 | |
87 // True once Shutdown() has returned. No new task can be scheduled after this. | |
88 bool shutdown_completed_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(TaskTracker); | |
91 }; | |
92 | |
93 } // namespace internal | |
94 } // namespace base | |
95 | |
96 #endif // BASE_TASK_SCHEDULER_TASK_TRACKER_H_ | |
OLD | NEW |