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