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/callback.h" | |
12 #include "base/macros.h" | 11 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
14 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
15 #include "base/task_scheduler/scheduler_lock.h" | 14 #include "base/task_scheduler/scheduler_lock.h" |
16 #include "base/task_scheduler/sequence.h" | 15 #include "base/task_scheduler/sequence.h" |
17 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
18 | 17 |
19 namespace base { | 18 namespace base { |
20 namespace internal { | 19 namespace internal { |
21 | 20 |
21 class SchedulerWorkerThreadDelegate; | |
22 class TaskTracker; | 22 class TaskTracker; |
23 | 23 |
24 // A thread that runs Tasks from Sequences returned by a callback. | 24 // A thread that runs Tasks from Sequences returned by a delegate. |
25 // | 25 // |
26 // A SchedulerWorkerThread is woken up when its WakeUp() method is called. After | 26 // A SchedulerWorkerThread is woken up when its WakeUp() method is called. After |
27 // a wake- up, a SchedulerWorkerThread runs Tasks from Sequences returned by its | 27 // a wake-up, a SchedulerWorkerThread runs Tasks from Sequences returned by the |
28 // "get work" callback as long as it doesn't return nullptr. It also | 28 // GetWork() method of its delegate as long as it doesn't return nullptr. It |
29 // periodically checks with its TaskTracker whether shutdown has completed and | 29 // also periodically checks with its TaskTracker whether shutdown has completed |
30 // 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 SchedulerWorkerThread : public PlatformThread::Delegate { |
34 public: | 34 public: |
35 // Callback invoked to get a Sequence from which to run a Task on | |
36 // |worker_thread|. | |
37 using GetWorkCallback = | |
38 Callback<scoped_refptr<Sequence>(SchedulerWorkerThread* worker_thread)>; | |
39 | |
40 // Callback invoked after |worker_thread| has tried to run a Task from | |
41 // |sequence| (a TaskTracker might have prevented the Task from running). | |
42 using RanTaskFromSequenceCallback = | |
43 Callback<void(const SchedulerWorkerThread* worker_thread, | |
44 scoped_refptr<Sequence> sequence)>; | |
45 | |
46 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs | 35 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs |
47 // Tasks from Sequences returned by |get_work_callback|. |main_entry_callback| | 36 // Tasks from Sequences returned by the GetWork() method of |delegate|. |
48 // is invoked when the main function of the SchedulerWorkerThread is entered. | 37 // |delegate| is also notified when the thread's main function enters/exits |
robliao
2016/04/06 21:28:20
The |delegate| specific behavior can be removed he
fdoray
2016/04/07 13:53:41
Done.
| |
49 // |ran_task_from_sequence_callback| is invoked after the | 38 // and after the thread has tried to run a Task. |task_tracker| is used to |
50 // SchedulerWorkerThread has tried to run a Task from a Sequence returned by | 39 // handle shutdown behavior of Tasks. Returns nullptr if creating the |
51 // |get_work_callback|. |task_tracker| is used to handle shutdown behavior of | 40 // underlying platform thread fails. |
52 // Tasks. Returns nullptr if creating the underlying platform thread fails. | |
53 static std::unique_ptr<SchedulerWorkerThread> CreateSchedulerWorkerThread( | 41 static std::unique_ptr<SchedulerWorkerThread> CreateSchedulerWorkerThread( |
54 ThreadPriority thread_priority, | 42 ThreadPriority thread_priority, |
55 const Closure& main_entry_callback, | 43 SchedulerWorkerThreadDelegate* delegate, |
56 const GetWorkCallback& get_work_callback, | |
57 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
58 TaskTracker* task_tracker); | 44 TaskTracker* task_tracker); |
59 | 45 |
60 // Destroying a SchedulerWorkerThread in production is not allowed; it is | 46 // Destroying a SchedulerWorkerThread in production is not allowed; it is |
61 // always leaked. In tests, it can only be destroyed after JoinForTesting() | 47 // always leaked. In tests, it can only be destroyed after JoinForTesting() |
62 // has returned. | 48 // has returned. |
63 ~SchedulerWorkerThread() override; | 49 ~SchedulerWorkerThread() override; |
64 | 50 |
65 // Wakes up this SchedulerWorkerThread. After this is called, this | 51 // Wakes up this SchedulerWorkerThread. After this is called, this |
66 // SchedulerWorkerThread will run Tasks from Sequences returned by | 52 // SchedulerWorkerThread will run Tasks from Sequences returned by the |
67 // |get_work_callback_| until it returns nullptr. | 53 // GetWork() method of its delegate until it returns nullptr. |
68 void WakeUp(); | 54 void WakeUp(); |
69 | 55 |
70 // Joins this SchedulerWorkerThread. If a Task is already running, it will be | 56 // Joins this SchedulerWorkerThread. If a Task is already running, it will be |
71 // allowed to complete its execution. This can only be called once. | 57 // allowed to complete its execution. This can only be called once. |
72 void JoinForTesting(); | 58 void JoinForTesting(); |
73 | 59 |
74 private: | 60 private: |
75 SchedulerWorkerThread( | 61 SchedulerWorkerThread(ThreadPriority thread_priority, |
76 ThreadPriority thread_priority, | 62 SchedulerWorkerThreadDelegate* delegate, |
77 const Closure& main_entry_callback, | 63 TaskTracker* task_tracker); |
78 const GetWorkCallback& get_work_callback, | |
79 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
80 TaskTracker* task_tracker); | |
81 | 64 |
82 // PlatformThread::Delegate: | 65 // PlatformThread::Delegate: |
83 void ThreadMain() override; | 66 void ThreadMain() override; |
84 | 67 |
85 bool ShouldExitForTesting() const; | 68 bool ShouldExitForTesting() const; |
86 | 69 |
87 // Platform thread managed by this SchedulerWorkerThread. | 70 // Platform thread managed by this SchedulerWorkerThread. |
88 PlatformThreadHandle thread_handle_; | 71 PlatformThreadHandle thread_handle_; |
89 | 72 |
90 // Event signaled to wake up this SchedulerWorkerThread. | 73 // Event signaled to wake up this SchedulerWorkerThread. |
91 WaitableEvent wake_up_event_; | 74 WaitableEvent wake_up_event_; |
92 | 75 |
93 const Closure main_entry_callback_; | 76 SchedulerWorkerThreadDelegate* const delegate_; |
94 const GetWorkCallback get_work_callback_; | |
95 const RanTaskFromSequenceCallback ran_task_from_sequence_callback_; | |
96 TaskTracker* const task_tracker_; | 77 TaskTracker* const task_tracker_; |
97 | 78 |
98 // Synchronizes access to |should_exit_for_testing_|. | 79 // Synchronizes access to |should_exit_for_testing_|. |
99 mutable SchedulerLock should_exit_for_testing_lock_; | 80 mutable SchedulerLock should_exit_for_testing_lock_; |
100 | 81 |
101 // True once JoinForTesting() has been called. | 82 // True once JoinForTesting() has been called. |
102 bool should_exit_for_testing_ = false; | 83 bool should_exit_for_testing_ = false; |
103 | 84 |
104 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); | 85 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); |
105 }; | 86 }; |
106 | 87 |
107 } // namespace internal | 88 } // namespace internal |
108 } // namespace base | 89 } // namespace base |
109 | 90 |
110 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ | 91 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ |
OLD | NEW |