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