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_H_ |
6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_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" |
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 thread that runs Tasks from Sequences returned by a delegate. |
25 // | 25 // |
26 // A SchedulerWorkerThread starts out sleeping. It is woken up by a call to | 26 // A SchedulerWorker starts out sleeping. It is woken up by a call to WakeUp(). |
27 // WakeUp(). After a wake-up, a SchedulerWorkerThread runs Tasks from Sequences | 27 // After a wake-up, a SchedulerWorker runs Tasks from Sequences returned by the |
28 // returned by the GetWork() method of its delegate as long as it doesn't return | 28 // GetWork() method of its delegate as long as it doesn't return nullptr. It |
29 // nullptr. It also periodically checks with its TaskTracker whether shutdown | 29 // also periodically checks with its TaskTracker whether shutdown has completed |
30 // has completed and exits when it has. | 30 // and exits when it has. |
31 // | 31 // |
32 // This class is thread-safe. | 32 // This class is thread-safe. |
33 class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate { | 33 class BASE_EXPORT SchedulerWorker : public PlatformThread::Delegate { |
34 public: | 34 public: |
35 // Delegate interface for SchedulerWorkerThread. The methods are always called | 35 // Delegate interface for SchedulerWorker. The methods are always called from |
36 // from the thread managed by the SchedulerWorkerThread instance. | 36 // the thread managed by the SchedulerWorker instance. |
fdoray
2016/06/20 15:08:39
*a* thread
robliao
2016/06/20 17:50:08
Done.
| |
37 class Delegate { | 37 class Delegate { |
38 public: | 38 public: |
39 virtual ~Delegate() = default; | 39 virtual ~Delegate() = default; |
40 | 40 |
41 // Called by |worker_thread| when it enters its main function. | 41 // Called by |worker| when it enters its main function. |
fdoray
2016/06/20 15:08:39
when *a thread it manages* enters
robliao
2016/06/20 17:50:08
Done.
| |
42 virtual void OnMainEntry(SchedulerWorkerThread* worker_thread) = 0; | 42 virtual void OnMainEntry(SchedulerWorker* worker) = 0; |
43 | 43 |
44 // Called by |worker_thread| to get a Sequence from which to run a Task. | 44 // Called by |worker| to get a Sequence from which to run a Task. |
45 virtual scoped_refptr<Sequence> GetWork( | 45 virtual scoped_refptr<Sequence> GetWork(SchedulerWorker* worker) = 0; |
46 SchedulerWorkerThread* worker_thread) = 0; | |
47 | 46 |
48 // Called when |sequence| isn't empty after the SchedulerWorkerThread pops a | 47 // Called when |sequence| isn't empty after the SchedulerWorker pops a Task |
49 // Task from it. |sequence| is the last Sequence returned by GetWork(). | 48 // from it. |sequence| is the last Sequence returned by GetWork(). |
50 virtual void ReEnqueueSequence(scoped_refptr<Sequence> sequence) = 0; | 49 virtual void ReEnqueueSequence(scoped_refptr<Sequence> sequence) = 0; |
51 | 50 |
52 // Called by |worker_thread| to determine how long to sleep before the next | 51 // Called by |worker| to determine how long to sleep before the next |
53 // call to GetWork(). GetWork() may be called before this timeout expires | 52 // call to GetWork(). GetWork() may be called before this timeout expires |
54 // if the thread's WakeUp() method is called. | 53 // if the thread's WakeUp() method is called. |
fdoray
2016/06/20 15:08:39
worker's
robliao
2016/06/20 17:50:08
Done.
| |
55 virtual TimeDelta GetSleepTimeout() = 0; | 54 virtual TimeDelta GetSleepTimeout() = 0; |
56 }; | 55 }; |
57 | 56 |
58 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs | 57 // Creates a SchedulerWorker with priority |thread_priority| that runs Tasks |
59 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to | 58 // from Sequences returned by |delegate|. |task_tracker| is used to handle |
60 // handle shutdown behavior of Tasks. Returns nullptr if creating the | 59 // shutdown behavior of Tasks. Returns nullptr if creating the underlying |
61 // underlying platform thread fails. | 60 // platform thread fails. |
62 static std::unique_ptr<SchedulerWorkerThread> Create( | 61 static std::unique_ptr<SchedulerWorker> Create( |
63 ThreadPriority thread_priority, | 62 ThreadPriority thread_priority, |
64 std::unique_ptr<Delegate> delegate, | 63 std::unique_ptr<Delegate> delegate, |
65 TaskTracker* task_tracker); | 64 TaskTracker* task_tracker); |
66 | 65 |
67 // Destroying a SchedulerWorkerThread in production is not allowed; it is | 66 // Destroying a SchedulerWorker in production is not allowed; it is always |
68 // always leaked. In tests, it can only be destroyed after JoinForTesting() | 67 // leaked. In tests, it can only be destroyed after JoinForTesting() has |
69 // has returned. | 68 // returned. |
70 ~SchedulerWorkerThread() override; | 69 ~SchedulerWorker() override; |
71 | 70 |
72 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this | 71 // Wakes up this SchedulerWorker if it wasn't already awake. After this |
73 // is called, this SchedulerWorkerThread will run Tasks from Sequences | 72 // is called, this SchedulerWorker will run Tasks from Sequences |
74 // returned by the GetWork() method of its delegate until it returns nullptr. | 73 // returned by the GetWork() method of its delegate until it returns nullptr. |
75 void WakeUp(); | 74 void WakeUp(); |
76 | 75 |
77 SchedulerWorkerThread::Delegate* delegate() { return delegate_.get(); } | 76 SchedulerWorker::Delegate* delegate() { return delegate_.get(); } |
78 | 77 |
79 // Joins this SchedulerWorkerThread. If a Task is already running, it will be | 78 // Joins this SchedulerWorker. If a Task is already running, it will be |
80 // allowed to complete its execution. This can only be called once. | 79 // allowed to complete its execution. This can only be called once. |
81 void JoinForTesting(); | 80 void JoinForTesting(); |
82 | 81 |
83 private: | 82 private: |
84 SchedulerWorkerThread(ThreadPriority thread_priority, | 83 SchedulerWorker(ThreadPriority thread_priority, |
85 std::unique_ptr<Delegate> delegate, | 84 std::unique_ptr<Delegate> delegate, |
86 TaskTracker* task_tracker); | 85 TaskTracker* task_tracker); |
87 | 86 |
88 // PlatformThread::Delegate: | 87 // PlatformThread::Delegate: |
89 void ThreadMain() override; | 88 void ThreadMain() override; |
90 | 89 |
91 bool ShouldExitForTesting() const; | 90 bool ShouldExitForTesting() const; |
92 | 91 |
93 // Platform thread managed by this SchedulerWorkerThread. | 92 // Platform thread managed by this SchedulerWorker. |
94 PlatformThreadHandle thread_handle_; | 93 PlatformThreadHandle thread_handle_; |
95 | 94 |
96 // Event signaled to wake up this SchedulerWorkerThread. | 95 // Event signaled to wake up this SchedulerWorker. |
97 WaitableEvent wake_up_event_; | 96 WaitableEvent wake_up_event_; |
98 | 97 |
99 const std::unique_ptr<Delegate> delegate_; | 98 const std::unique_ptr<Delegate> delegate_; |
100 TaskTracker* const task_tracker_; | 99 TaskTracker* const task_tracker_; |
101 | 100 |
102 // Synchronizes access to |should_exit_for_testing_|. | 101 // Synchronizes access to |should_exit_for_testing_|. |
103 mutable SchedulerLock should_exit_for_testing_lock_; | 102 mutable SchedulerLock should_exit_for_testing_lock_; |
104 | 103 |
105 // True once JoinForTesting() has been called. | 104 // True once JoinForTesting() has been called. |
106 bool should_exit_for_testing_ = false; | 105 bool should_exit_for_testing_ = false; |
107 | 106 |
108 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); | 107 DISALLOW_COPY_AND_ASSIGN(SchedulerWorker); |
109 }; | 108 }; |
110 | 109 |
111 } // namespace internal | 110 } // namespace internal |
112 } // namespace base | 111 } // namespace base |
113 | 112 |
114 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ | 113 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ |
OLD | NEW |