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" |
(...skipping 19 matching lines...) Expand all Loading... |
30 // | 30 // |
31 // This class is thread-safe. | 31 // This class is thread-safe. |
32 class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate { | 32 class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate { |
33 public: | 33 public: |
34 // Delegate interface for SchedulerWorkerThread. The methods are always called | 34 // Delegate interface for SchedulerWorkerThread. The methods are always called |
35 // from the thread managed by the SchedulerWorkerThread instance. | 35 // from the thread managed by the SchedulerWorkerThread instance. |
36 class Delegate { | 36 class Delegate { |
37 public: | 37 public: |
38 virtual ~Delegate() = default; | 38 virtual ~Delegate() = default; |
39 | 39 |
40 // Called when the main function of the SchedulerWorkerThread enters. | 40 // Called by |worker_thread| when it enters its main function. |
41 virtual void OnMainEntry() = 0; | 41 virtual void OnMainEntry(SchedulerWorkerThread* worker_thread) = 0; |
42 | 42 |
43 // Called by |worker_thread| to get a Sequence from which to run a Task. | 43 // Called by |worker_thread| to get a Sequence from which to run a Task. |
44 virtual scoped_refptr<Sequence> GetWork( | 44 virtual scoped_refptr<Sequence> GetWork( |
45 SchedulerWorkerThread* worker_thread) = 0; | 45 SchedulerWorkerThread* worker_thread) = 0; |
46 | 46 |
47 // Called when |sequence| isn't empty after the SchedulerWorkerThread pops a | 47 // Called when |sequence| isn't empty after the SchedulerWorkerThread pops a |
48 // Task from it. |sequence| is the last Sequence returned by GetWork(). | 48 // Task from it. |sequence| is the last Sequence returned by GetWork(). |
49 virtual void ReEnqueueSequence(scoped_refptr<Sequence> sequence) = 0; | 49 virtual void ReEnqueueSequence(scoped_refptr<Sequence> sequence) = 0; |
50 }; | 50 }; |
51 | 51 |
52 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs | 52 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs |
53 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to | 53 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to |
54 // handle shutdown behavior of Tasks. Returns nullptr if creating the | 54 // handle shutdown behavior of Tasks. Returns nullptr if creating the |
55 // underlying platform thread fails. | 55 // underlying platform thread fails. |
56 static std::unique_ptr<SchedulerWorkerThread> Create( | 56 static std::unique_ptr<SchedulerWorkerThread> Create( |
57 ThreadPriority thread_priority, | 57 ThreadPriority thread_priority, |
58 std::unique_ptr<Delegate> delegate, | 58 std::unique_ptr<Delegate> delegate, |
59 TaskTracker* task_tracker); | 59 TaskTracker* task_tracker); |
60 | 60 |
61 // Destroying a SchedulerWorkerThread in production is not allowed; it is | 61 // Destroying a SchedulerWorkerThread in production is not allowed; it is |
62 // always leaked. In tests, it can only be destroyed after JoinForTesting() | 62 // always leaked. In tests, it can only be destroyed after JoinForTesting() |
63 // has returned. | 63 // has returned. |
64 ~SchedulerWorkerThread() override; | 64 ~SchedulerWorkerThread() override; |
65 | 65 |
66 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this | 66 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this |
67 // is called, this SchedulerWorkerThread will run Tasks from Sequences | 67 // is called, this SchedulerWorkerThread will run Tasks from Sequences |
68 // returned by the GetWork() method of its delegate until it returns nullptr. | 68 // returned by the GetWork() method of its delegate until it returns nullptr. |
69 void WakeUp(); | 69 void WakeUp(); |
70 | 70 |
| 71 SchedulerWorkerThread::Delegate* delegate() { return delegate_.get(); } |
| 72 |
71 // Joins this SchedulerWorkerThread. If a Task is already running, it will be | 73 // Joins this SchedulerWorkerThread. If a Task is already running, it will be |
72 // allowed to complete its execution. This can only be called once. | 74 // allowed to complete its execution. This can only be called once. |
73 void JoinForTesting(); | 75 void JoinForTesting(); |
74 | 76 |
75 private: | 77 private: |
76 SchedulerWorkerThread(ThreadPriority thread_priority, | 78 SchedulerWorkerThread(ThreadPriority thread_priority, |
77 std::unique_ptr<Delegate> delegate, | 79 std::unique_ptr<Delegate> delegate, |
78 TaskTracker* task_tracker); | 80 TaskTracker* task_tracker); |
79 | 81 |
80 // PlatformThread::Delegate: | 82 // PlatformThread::Delegate: |
(...skipping 16 matching lines...) Expand all Loading... |
97 // True once JoinForTesting() has been called. | 99 // True once JoinForTesting() has been called. |
98 bool should_exit_for_testing_ = false; | 100 bool should_exit_for_testing_ = false; |
99 | 101 |
100 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); | 102 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); |
101 }; | 103 }; |
102 | 104 |
103 } // namespace internal | 105 } // namespace internal |
104 } // namespace base | 106 } // namespace base |
105 | 107 |
106 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ | 108 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ |
OLD | NEW |