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_SCHEDULER_WORKER_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ |
6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 // also periodically checks with its TaskTracker whether shutdown has completed | 31 // also periodically checks with its TaskTracker whether shutdown has completed |
32 // and exits when it has. | 32 // and exits when it has. |
33 // | 33 // |
34 // The worker is free to release and reallocate the platform thread with | 34 // The worker is free to release and reallocate the platform thread with |
35 // guidance from the delegate. | 35 // guidance from the delegate. |
36 // | 36 // |
37 // This class is thread-safe. | 37 // This class is thread-safe. |
38 class BASE_EXPORT SchedulerWorker { | 38 class BASE_EXPORT SchedulerWorker { |
39 public: | 39 public: |
40 // Delegate interface for SchedulerWorker. The methods are always called from | 40 // Delegate interface for SchedulerWorker. The methods are always called from |
41 // a thread managed by the SchedulerWorker instance. | 41 // the thread managed by the SchedulerWorker instance. |
42 class Delegate { | 42 class Delegate { |
43 public: | 43 public: |
44 virtual ~Delegate() = default; | 44 virtual ~Delegate() = default; |
45 | 45 |
46 // Called by a thread managed by |worker| when it enters its main function. | 46 // Called by a thread managed by |worker| when it enters its main function. |
47 // If a thread is recreated after detachment, |detach_duration| is the time | 47 // If a thread is recreated after detachment, |detach_duration| is the time |
48 // elapsed since detachment. Otherwise, if this is the first thread created | 48 // elapsed since detachment. Otherwise, if this is the first thread created |
49 // for |worker|, |detach_duration| is TimeDelta::Max(). | 49 // for |worker|, |detach_duration| is TimeDelta::Max(). |
50 virtual void OnMainEntry(SchedulerWorker* worker, | 50 virtual void OnMainEntry(SchedulerWorker* worker) = 0; |
51 const TimeDelta& detach_duration) = 0; | |
52 | 51 |
53 // Called by a thread managed by |worker| to get a Sequence from which to | 52 // Called by a thread managed by |worker| to get a Sequence from which to |
54 // run a Task. | 53 // run a Task. |
55 virtual scoped_refptr<Sequence> GetWork(SchedulerWorker* worker) = 0; | 54 virtual scoped_refptr<Sequence> GetWork(SchedulerWorker* worker) = 0; |
56 | 55 |
57 // Called by the SchedulerWorker after it ran a task with |task_priority|. | 56 // Called by the SchedulerWorker after it ran a task with |task_priority|. |
58 // |task_latency| is the time elapsed between when the task was posted and | 57 // |task_latency| is the time elapsed between when the task was posted and |
59 // when it started to run. | 58 // when it started to run. |
60 virtual void DidRunTaskWithPriority(TaskPriority task_priority, | 59 virtual void DidRunTaskWithPriority(TaskPriority task_priority, |
61 const TimeDelta& task_latency) = 0; | 60 const TimeDelta& task_latency) = 0; |
(...skipping 14 matching lines...) Expand all Loading... |
76 // safe. If the delegate is responsible for thread-affine work, detachment | 75 // safe. If the delegate is responsible for thread-affine work, detachment |
77 // is generally not safe. | 76 // is generally not safe. |
78 // | 77 // |
79 // When true is returned: | 78 // When true is returned: |
80 // - The next WakeUp() could be more costly due to new thread creation. | 79 // - The next WakeUp() could be more costly due to new thread creation. |
81 // - The worker will take this as a signal that it can detach, but it is not | 80 // - The worker will take this as a signal that it can detach, but it is not |
82 // obligated to do so. | 81 // obligated to do so. |
83 // This MUST return false if SchedulerWorker::JoinForTesting() is in | 82 // This MUST return false if SchedulerWorker::JoinForTesting() is in |
84 // progress. | 83 // progress. |
85 virtual bool CanDetach(SchedulerWorker* worker) = 0; | 84 virtual bool CanDetach(SchedulerWorker* worker) = 0; |
| 85 |
| 86 // Called by a thread before it detaches. This method is not allowed to |
| 87 // acquire a SchedulerLock because it is called within the scope of another |
| 88 // SchedulerLock. |
| 89 virtual void OnDetach() = 0; |
86 }; | 90 }; |
87 | 91 |
88 enum class InitialState { ALIVE, DETACHED }; | 92 enum class InitialState { ALIVE, DETACHED }; |
89 | 93 |
90 // Creates a SchedulerWorker that runs Tasks from Sequences returned by | 94 // Creates a SchedulerWorker that runs Tasks from Sequences returned by |
91 // |delegate|. |priority_hint| is the preferred thread priority; the actual | 95 // |delegate|. |priority_hint| is the preferred thread priority; the actual |
92 // thread priority depends on shutdown state and platform capabilities. | 96 // thread priority depends on shutdown state and platform capabilities. |
93 // |task_tracker| is used to handle shutdown behavior of Tasks. If | 97 // |task_tracker| is used to handle shutdown behavior of Tasks. If |
94 // |worker_state| is DETACHED, the thread will be created upon a WakeUp(). | 98 // |worker_state| is DETACHED, the thread will be created upon a WakeUp(). |
95 // Returns nullptr if creating the underlying platform thread fails during | 99 // Returns nullptr if creating the underlying platform thread fails during |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 void CreateThread(); | 140 void CreateThread(); |
137 | 141 |
138 void CreateThreadAssertSynchronized(); | 142 void CreateThreadAssertSynchronized(); |
139 | 143 |
140 // Synchronizes access to |thread_|. | 144 // Synchronizes access to |thread_|. |
141 mutable SchedulerLock thread_lock_; | 145 mutable SchedulerLock thread_lock_; |
142 | 146 |
143 // The underlying thread for this SchedulerWorker. | 147 // The underlying thread for this SchedulerWorker. |
144 std::unique_ptr<Thread> thread_; | 148 std::unique_ptr<Thread> thread_; |
145 | 149 |
146 // Time of the last successful Detach(). Is only accessed from the thread | |
147 // managed by this SchedulerWorker. | |
148 TimeTicks last_detach_time_; | |
149 | |
150 const ThreadPriority priority_hint_; | 150 const ThreadPriority priority_hint_; |
151 const std::unique_ptr<Delegate> delegate_; | 151 const std::unique_ptr<Delegate> delegate_; |
152 TaskTracker* const task_tracker_; | 152 TaskTracker* const task_tracker_; |
153 | 153 |
154 // Set once JoinForTesting() has been called. | 154 // Set once JoinForTesting() has been called. |
155 AtomicFlag should_exit_for_testing_; | 155 AtomicFlag should_exit_for_testing_; |
156 | 156 |
157 DISALLOW_COPY_AND_ASSIGN(SchedulerWorker); | 157 DISALLOW_COPY_AND_ASSIGN(SchedulerWorker); |
158 }; | 158 }; |
159 | 159 |
160 } // namespace internal | 160 } // namespace internal |
161 } // namespace base | 161 } // namespace base |
162 | 162 |
163 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ | 163 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ |
OLD | NEW |