| 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). |
| 52 // The Task is still in |sequence| when this is called. |
| 53 virtual void RanTaskFromSequence(scoped_refptr<Sequence> sequence) = 0; |
| 54 }; |
| 45 | 55 |
| 46 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs | 56 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs |
| 47 // Tasks from Sequences returned by |get_work_callback|. |main_entry_callback| | 57 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to |
| 48 // is invoked when the main function of the SchedulerWorkerThread is entered. | 58 // handle shutdown behavior of Tasks. Returns nullptr if creating the |
| 49 // |ran_task_from_sequence_callback| is invoked after the | 59 // 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( | 60 static std::unique_ptr<SchedulerWorkerThread> CreateSchedulerWorkerThread( |
| 54 ThreadPriority thread_priority, | 61 ThreadPriority thread_priority, |
| 55 const Closure& main_entry_callback, | 62 Delegate* delegate, |
| 56 const GetWorkCallback& get_work_callback, | |
| 57 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
| 58 TaskTracker* task_tracker); | 63 TaskTracker* task_tracker); |
| 59 | 64 |
| 60 // Destroying a SchedulerWorkerThread in production is not allowed; it is | 65 // Destroying a SchedulerWorkerThread in production is not allowed; it is |
| 61 // always leaked. In tests, it can only be destroyed after JoinForTesting() | 66 // always leaked. In tests, it can only be destroyed after JoinForTesting() |
| 62 // has returned. | 67 // has returned. |
| 63 ~SchedulerWorkerThread() override; | 68 ~SchedulerWorkerThread() override; |
| 64 | 69 |
| 65 // Wakes up this SchedulerWorkerThread. After this is called, this | 70 // Wakes up this SchedulerWorkerThread. After this is called, this |
| 66 // SchedulerWorkerThread will run Tasks from Sequences returned by | 71 // SchedulerWorkerThread will run Tasks from Sequences returned by the |
| 67 // |get_work_callback_| until it returns nullptr. | 72 // GetWork() method of its delegate until it returns nullptr. |
| 68 void WakeUp(); | 73 void WakeUp(); |
| 69 | 74 |
| 70 // Joins this SchedulerWorkerThread. If a Task is already running, it will be | 75 // Joins this SchedulerWorkerThread. If a Task is already running, it will be |
| 71 // allowed to complete its execution. This can only be called once. | 76 // allowed to complete its execution. This can only be called once. |
| 72 void JoinForTesting(); | 77 void JoinForTesting(); |
| 73 | 78 |
| 74 private: | 79 private: |
| 75 SchedulerWorkerThread( | 80 SchedulerWorkerThread(ThreadPriority thread_priority, |
| 76 ThreadPriority thread_priority, | 81 Delegate* delegate, |
| 77 const Closure& main_entry_callback, | 82 TaskTracker* task_tracker); |
| 78 const GetWorkCallback& get_work_callback, | |
| 79 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
| 80 TaskTracker* task_tracker); | |
| 81 | 83 |
| 82 // PlatformThread::Delegate: | 84 // PlatformThread::Delegate: |
| 83 void ThreadMain() override; | 85 void ThreadMain() override; |
| 84 | 86 |
| 85 bool ShouldExitForTesting() const; | 87 bool ShouldExitForTesting() const; |
| 86 | 88 |
| 87 // Platform thread managed by this SchedulerWorkerThread. | 89 // Platform thread managed by this SchedulerWorkerThread. |
| 88 PlatformThreadHandle thread_handle_; | 90 PlatformThreadHandle thread_handle_; |
| 89 | 91 |
| 90 // Event signaled to wake up this SchedulerWorkerThread. | 92 // Event signaled to wake up this SchedulerWorkerThread. |
| 91 WaitableEvent wake_up_event_; | 93 WaitableEvent wake_up_event_; |
| 92 | 94 |
| 93 const Closure main_entry_callback_; | 95 Delegate* const delegate_; |
| 94 const GetWorkCallback get_work_callback_; | |
| 95 const RanTaskFromSequenceCallback ran_task_from_sequence_callback_; | |
| 96 TaskTracker* const task_tracker_; | 96 TaskTracker* const task_tracker_; |
| 97 | 97 |
| 98 // Synchronizes access to |should_exit_for_testing_|. | 98 // Synchronizes access to |should_exit_for_testing_|. |
| 99 mutable SchedulerLock should_exit_for_testing_lock_; | 99 mutable SchedulerLock should_exit_for_testing_lock_; |
| 100 | 100 |
| 101 // True once JoinForTesting() has been called. | 101 // True once JoinForTesting() has been called. |
| 102 bool should_exit_for_testing_ = false; | 102 bool should_exit_for_testing_ = false; |
| 103 | 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); | 104 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); |
| 105 }; | 105 }; |
| 106 | 106 |
| 107 } // namespace internal | 107 } // namespace internal |
| 108 } // namespace base | 108 } // namespace base |
| 109 | 109 |
| 110 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ | 110 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ |
| OLD | NEW |