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_THREAD_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ |
6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
14 #include "base/task_scheduler/scheduler_lock.h" | 14 #include "base/task_scheduler/scheduler_lock.h" |
15 #include "base/task_scheduler/sequence.h" | 15 #include "base/task_scheduler/sequence.h" |
16 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 | 18 |
19 namespace base { | 19 namespace base { |
20 namespace internal { | 20 namespace internal { |
21 | 21 |
22 class TaskTracker; | 22 class TaskTracker; |
23 | 23 |
24 // A thread that runs Tasks from Sequences returned by a delegate. | 24 // A worker that manages at most one thread to run Tasks from Sequences returned |
fdoray
2016/06/13 20:31:17
one thread at a time?
robliao
2016/06/13 23:23:28
Changed to a single thread.
| |
25 // by a delegate. | |
25 // | 26 // |
26 // A SchedulerWorkerThread starts out sleeping. It is woken up by a call to | 27 // A SchedulerWorkerThread starts out sleeping. It is woken up by a call to |
27 // WakeUp(). After a wake-up, a SchedulerWorkerThread runs Tasks from Sequences | 28 // WakeUp(). After a wake-up, a SchedulerWorkerThread runs Tasks from Sequences |
28 // returned by the GetWork() method of its delegate as long as it doesn't return | 29 // returned by the GetWork() method of its delegate as long as it doesn't return |
29 // nullptr. It also periodically checks with its TaskTracker whether shutdown | 30 // nullptr. It also periodically checks with its TaskTracker whether shutdown |
30 // has completed and exits when it has. | 31 // has completed and exits when it has. |
31 // | 32 // |
33 // The worker is free to release and reallocate the platform thread with | |
34 // guidance from the delegate. | |
35 // | |
32 // This class is thread-safe. | 36 // This class is thread-safe. |
33 class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate { | 37 class BASE_EXPORT SchedulerWorkerThread { |
34 public: | 38 public: |
35 // Delegate interface for SchedulerWorkerThread. The methods are always called | 39 // Delegate interface for SchedulerWorkerThread. The methods are always called |
36 // from the thread managed by the SchedulerWorkerThread instance. | 40 // from the thread managed by the SchedulerWorkerThread instance. |
37 class Delegate { | 41 class Delegate { |
38 public: | 42 public: |
39 virtual ~Delegate() = default; | 43 virtual ~Delegate() = default; |
40 | 44 |
41 // Called by |worker_thread| when it enters its main function. | 45 // Called when |worker_thread|'s worker enters the main function. |
46 // If a thread is recreated after detachment, this call will occur again. | |
42 virtual void OnMainEntry(SchedulerWorkerThread* worker_thread) = 0; | 47 virtual void OnMainEntry(SchedulerWorkerThread* worker_thread) = 0; |
43 | 48 |
44 // Called by |worker_thread| to get a Sequence from which to run a Task. | 49 // Called by |worker_thread| to get a Sequence from which to run a Task. |
45 virtual scoped_refptr<Sequence> GetWork( | 50 virtual scoped_refptr<Sequence> GetWork( |
46 SchedulerWorkerThread* worker_thread) = 0; | 51 SchedulerWorkerThread* worker_thread) = 0; |
47 | 52 |
48 // Called when |sequence| isn't empty after the SchedulerWorkerThread pops a | 53 // Called when |sequence| isn't empty after the SchedulerWorkerThread pops a |
49 // Task from it. |sequence| is the last Sequence returned by GetWork(). | 54 // Task from it. |sequence| is the last Sequence returned by GetWork(). |
50 virtual void ReEnqueueSequence(scoped_refptr<Sequence> sequence) = 0; | 55 virtual void ReEnqueueSequence(scoped_refptr<Sequence> sequence) = 0; |
51 | 56 |
52 // Called by |worker_thread| to determine how long to sleep before the next | 57 // Called by |worker_thread| to determine how long to sleep before the next |
53 // call to GetWork(). GetWork() may be called before this timeout expires | 58 // call to GetWork(). GetWork() may be called before this timeout expires |
54 // if the thread's WakeUp() method is called. | 59 // if the thread's WakeUp() method is called. |
55 virtual TimeDelta GetSleepTimeout() = 0; | 60 virtual TimeDelta GetSleepTimeout() = 0; |
61 | |
62 // Called by |worker_thread| to see if it is allowed to detach if the last | |
63 // call to GetWork() returned nullptr. | |
fdoray
2016/06/13 20:31:17
No new line?
robliao
2016/06/13 23:23:28
Done.
| |
64 // A SchedulerWorkerThread reserves the right to ignore the return value of | |
65 // this function. | |
fdoray
2016/06/13 20:31:17
How will the thread pool keep one active (not deta
robliao
2016/06/13 23:23:28
The threadpool will simply make sure it returns fa
| |
66 // | |
67 // It is the responsibility of the delegate to determine if detachment is | |
68 // safe. If |worker_thread| is responsible for thread-affine work, | |
69 // detachment is generally not safe. | |
70 // | |
71 // When true is returned... | |
gab
2016/06/13 19:23:06
s/.../:/
robliao
2016/06/13 23:23:28
Done.
| |
72 // - The next WakeUp() could be more costly due to new thread creation. | |
73 // - The worker will free resources if it can. | |
gab
2016/06/13 19:23:05
The comment further above initially says "reserves
robliao
2016/06/13 23:23:28
Updated this comment and removed the statement abo
| |
74 // False must be returned if SchedulerWorkerThread::JoinForTesting is in | |
gab
2016/06/13 19:23:06
s/False must be returned/This *must* return false
robliao
2016/06/13 23:23:28
Done.
| |
75 // progress. | |
76 virtual bool CanDetach(SchedulerWorkerThread* worker_thread) = 0; | |
56 }; | 77 }; |
57 | 78 |
79 enum class InitialState { ALIVE, DETACHED }; | |
80 | |
58 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs | 81 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs |
59 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to | 82 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to |
60 // handle shutdown behavior of Tasks. Returns nullptr if creating the | 83 // handle shutdown behavior of Tasks. If |worker_state| is DETACHED, the |
61 // underlying platform thread fails. | 84 // worker will be created upon a WakeUp. Returns nullptr if creating the |
85 // underlying platform thread fails during Create. | |
62 static std::unique_ptr<SchedulerWorkerThread> Create( | 86 static std::unique_ptr<SchedulerWorkerThread> Create( |
63 ThreadPriority thread_priority, | 87 ThreadPriority thread_priority, |
64 std::unique_ptr<Delegate> delegate, | 88 std::unique_ptr<Delegate> delegate, |
65 TaskTracker* task_tracker); | 89 TaskTracker* task_tracker, |
90 InitialState initial_state); | |
66 | 91 |
67 // Destroying a SchedulerWorkerThread in production is not allowed; it is | 92 // Destroying a SchedulerWorkerThread in production is not allowed; it is |
68 // always leaked. In tests, it can only be destroyed after JoinForTesting() | 93 // always leaked. In tests, it can only be destroyed after JoinForTesting() |
69 // has returned. | 94 // has returned. |
70 ~SchedulerWorkerThread() override; | 95 ~SchedulerWorkerThread(); |
71 | 96 |
72 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this | 97 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this |
73 // is called, this SchedulerWorkerThread will run Tasks from Sequences | 98 // is called, this SchedulerWorkerThread will run Tasks from Sequences |
74 // returned by the GetWork() method of its delegate until it returns nullptr. | 99 // returned by the GetWork() method of its delegate until it returns nullptr. |
100 // May fail if the worker is detached. On failure the worker will not be able | |
gab
2016/06/13 19:23:06
"the worker will not be able to call" : unclear fo
robliao
2016/06/13 23:23:28
fdoray wanted to emphasize that WakeUp is generall
| |
101 // to call Delegate::GetWork(). | |
75 void WakeUp(); | 102 void WakeUp(); |
76 | 103 |
77 SchedulerWorkerThread::Delegate* delegate() { return delegate_.get(); } | 104 SchedulerWorkerThread::Delegate* delegate() { return delegate_.get(); } |
78 | 105 |
79 // Joins this SchedulerWorkerThread. If a Task is already running, it will be | 106 // Joins this SchedulerWorkerThread. If a Task is already running, it will be |
80 // allowed to complete its execution. This can only be called once. | 107 // allowed to complete its execution. This can only be called once. |
81 void JoinForTesting(); | 108 void JoinForTesting(); |
82 | 109 |
110 // Returns ture if the worker is alive. | |
fdoray
2016/06/13 20:31:17
*true
robliao
2016/06/13 23:23:28
Done.
| |
111 bool WorkerAliveForTesting() const; | |
112 | |
83 private: | 113 private: |
114 class Worker; | |
115 | |
84 SchedulerWorkerThread(ThreadPriority thread_priority, | 116 SchedulerWorkerThread(ThreadPriority thread_priority, |
85 std::unique_ptr<Delegate> delegate, | 117 std::unique_ptr<Delegate> delegate, |
86 TaskTracker* task_tracker); | 118 TaskTracker* task_tracker); |
87 | 119 |
88 // PlatformThread::Delegate: | 120 // Returns the worker instance if the detach was successful so that the worker |
89 void ThreadMain() override; | 121 // instance can be freed upon termination of the thread. |
122 // If the detach is not possible, returns nullptr. | |
123 std::unique_ptr<SchedulerWorkerThread::Worker> Detach(); | |
124 | |
125 void CreateWorker(); | |
126 | |
127 void CreateWorkerAssertSynchronized(); | |
90 | 128 |
91 bool ShouldExitForTesting() const; | 129 bool ShouldExitForTesting() const; |
92 | 130 |
93 // Platform thread managed by this SchedulerWorkerThread. | 131 // Synchronizes access to |worker_| |
94 PlatformThreadHandle thread_handle_; | 132 mutable SchedulerLock worker_lock_; |
95 | 133 |
96 // Event signaled to wake up this SchedulerWorkerThread. | 134 // The underlying thread for this SchedulerWorkerThread. |
97 WaitableEvent wake_up_event_; | 135 std::unique_ptr<Worker> worker_; |
98 | 136 |
137 const ThreadPriority thread_priority_; | |
99 const std::unique_ptr<Delegate> delegate_; | 138 const std::unique_ptr<Delegate> delegate_; |
100 TaskTracker* const task_tracker_; | 139 TaskTracker* const task_tracker_; |
101 | 140 |
102 // Synchronizes access to |should_exit_for_testing_|. | 141 // Synchronizes access to |should_exit_for_testing_|. |
103 mutable SchedulerLock should_exit_for_testing_lock_; | 142 mutable SchedulerLock should_exit_for_testing_lock_; |
104 | 143 |
105 // True once JoinForTesting() has been called. | 144 // True once JoinForTesting() has been called. |
106 bool should_exit_for_testing_ = false; | 145 bool should_exit_for_testing_ = false; |
107 | 146 |
108 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); | 147 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); |
109 }; | 148 }; |
110 | 149 |
111 } // namespace internal | 150 } // namespace internal |
112 } // namespace base | 151 } // namespace base |
113 | 152 |
114 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ | 153 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ |
OLD | NEW |