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