Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(745)

Side by Side Diff: base/task_scheduler/scheduler_worker_thread.h

Issue 1876363004: TaskScheduler [11] Support ExecutionMode::SINGLE_THREADED. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@8_delayed
Patch Set: rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/single_thread_task_runner.h"
13 #include "base/synchronization/waitable_event.h" 14 #include "base/synchronization/waitable_event.h"
15 #include "base/task_scheduler/priority_queue.h"
14 #include "base/task_scheduler/scheduler_lock.h" 16 #include "base/task_scheduler/scheduler_lock.h"
17 #include "base/task_scheduler/scheduler_task_executor.h"
15 #include "base/task_scheduler/sequence.h" 18 #include "base/task_scheduler/sequence.h"
16 #include "base/threading/platform_thread.h" 19 #include "base/threading/platform_thread.h"
17 20
18 namespace base { 21 namespace base {
19 namespace internal { 22 namespace internal {
20 23
24 class DelayedTaskManager;
21 class TaskTracker; 25 class TaskTracker;
22 26
23 // A thread that runs Tasks from Sequences returned by a delegate. 27 // A thread that runs Tasks from Sequences returned by a delegate.
24 // 28 //
25 // A SchedulerWorkerThread starts out sleeping. It is woken up by a call to 29 // A SchedulerWorkerThread starts out sleeping. It is woken up by a call to
26 // WakeUp(). After a wake-up, a SchedulerWorkerThread runs Tasks from Sequences 30 // WakeUp(). After a wake-up, a SchedulerWorkerThread runs Tasks from Sequences
27 // returned by the GetWork() method of its delegate as long as it doesn't return 31 // returned by the GetWork() method of its delegate as long as it doesn't return
28 // nullptr. It also periodically checks with its TaskTracker whether shutdown 32 // nullptr. It also periodically checks with its TaskTracker whether shutdown
29 // has completed and exits when it has. 33 // has completed and exits when it has.
30 // 34 //
31 // This class is thread-safe. 35 // This class is thread-safe.
32 class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate { 36 class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate,
37 public SchedulerTaskExecutor {
33 public: 38 public:
34 // Delegate interface for SchedulerWorkerThread. The methods are always called 39 // Delegate interface for SchedulerWorkerThread. The methods are always called
35 // from the thread managed by the SchedulerWorkerThread instance. 40 // from the thread managed by the SchedulerWorkerThread instance.
36 class Delegate { 41 class Delegate {
37 public: 42 public:
38 virtual ~Delegate() = default; 43 virtual ~Delegate() = default;
39 44
40 // Called when the main function of the SchedulerWorkerThread enters. 45 // Called when the main function of the SchedulerWorkerThread enters.
41 virtual void OnMainEntry() = 0; 46 virtual void OnMainEntry() = 0;
42 47
43 // Called by |worker_thread| to get a Sequence from which to run a Task. 48 // Called by |worker_thread| to get a Sequence from which to run a Task.
49 // |alternate_priority_queue| will be considered as an alternate source of
50 // work. |alternate_priority_queue_used| is set to true if the returned
51 // Sequence was selected from it.
44 virtual scoped_refptr<Sequence> GetWork( 52 virtual scoped_refptr<Sequence> GetWork(
45 SchedulerWorkerThread* worker_thread) = 0; 53 SchedulerWorkerThread* worker_thread,
54 PriorityQueue* alternate_priority_queue,
55 bool* alternate_priority_queue_used) = 0;
46 56
47 // Called when |sequence| isn't empty after the SchedulerWorkerThread pops a 57 // Called when a Sequence returned by GetWork() that wasn't selected from
48 // Task from it. |sequence| is the last Sequence returned by GetWork(). 58 // the alternate priority queue isn't empty after the SchedulerWorkerThread
59 // pops a Task from it.
49 virtual void EnqueueSequence(scoped_refptr<Sequence> sequence) = 0; 60 virtual void EnqueueSequence(scoped_refptr<Sequence> sequence) = 0;
50 }; 61 };
51 62
52 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs 63 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs
53 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to 64 // Tasks from Sequences returned by |delegate|. |task_tracker| handles
54 // handle shutdown behavior of Tasks. Returns nullptr if creating the 65 // shutdown behavior of Tasks. |delayed_task_manager| handles Tasks posted
55 // underlying platform thread fails. 66 // with a delay. |predecessor_priority_queue| is a PriorityQueue whose
67 // transactions may overlap this thread's PriorityQueue's transactions.
68 // Returns nullptr if creating the underlying platform thread fails.
56 static std::unique_ptr<SchedulerWorkerThread> CreateSchedulerWorkerThread( 69 static std::unique_ptr<SchedulerWorkerThread> CreateSchedulerWorkerThread(
57 ThreadPriority thread_priority, 70 ThreadPriority thread_priority,
58 Delegate* delegate, 71 Delegate* delegate,
59 TaskTracker* task_tracker); 72 TaskTracker* task_tracker,
73 DelayedTaskManager* delayed_task_manager,
74 const PriorityQueue* predecessor_priority_queue);
60 75
61 // Destroying a SchedulerWorkerThread in production is not allowed; it is 76 // Destroying a SchedulerWorkerThread in production is not allowed; it is
62 // always leaked. In tests, it can only be destroyed after JoinForTesting() 77 // always leaked. In tests, it can only be destroyed after JoinForTesting()
63 // has returned. 78 // has returned.
64 ~SchedulerWorkerThread() override; 79 ~SchedulerWorkerThread() override;
65 80
81 // Returns a SingleThreadTaskRunner whose PostTask invocations will result in
82 // scheduling Tasks with |traits| on this worker thread.
83 scoped_refptr<SingleThreadTaskRunner> CreateTaskRunnerWithTraits(
84 const TaskTraits& traits);
85
66 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this 86 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this
67 // is called, this SchedulerWorkerThread will run Tasks from Sequences 87 // is called, this SchedulerWorkerThread will run Tasks from Sequences
68 // returned by the GetWork() method of its delegate until it returns nullptr. 88 // returned by the GetWork() method of its delegate until it returns nullptr.
69 void WakeUp(); 89 void WakeUp();
70 90
71 // Joins this SchedulerWorkerThread. If a Task is already running, it will be 91 // Joins this SchedulerWorkerThread. If a Task is already running, it will be
72 // allowed to complete its execution. This can only be called once. 92 // allowed to complete its execution. This can only be called once.
73 void JoinForTesting(); 93 void JoinForTesting();
74 94
95 // SchedulerTaskExecutor:
96 void PostTaskWithSequence(std::unique_ptr<Task> task,
97 scoped_refptr<Sequence> sequence) override;
98
75 private: 99 private:
76 SchedulerWorkerThread(ThreadPriority thread_priority, 100 SchedulerWorkerThread(ThreadPriority thread_priority,
77 Delegate* delegate, 101 Delegate* delegate,
78 TaskTracker* task_tracker); 102 TaskTracker* task_tracker,
103 DelayedTaskManager* delayed_task_manager,
104 const PriorityQueue* predecessor_priority_queue);
79 105
80 // PlatformThread::Delegate: 106 // PlatformThread::Delegate:
81 void ThreadMain() override; 107 void ThreadMain() override;
82 108
83 bool ShouldExitForTesting() const; 109 bool ShouldExitForTesting() const;
84 110
85 // Platform thread managed by this SchedulerWorkerThread. 111 // Platform thread managed by this SchedulerWorkerThread.
86 PlatformThreadHandle thread_handle_; 112 PlatformThreadHandle thread_handle_;
87 113
88 // Event signaled to wake up this SchedulerWorkerThread. 114 // Event signaled to wake up this SchedulerWorkerThread.
89 WaitableEvent wake_up_event_; 115 WaitableEvent wake_up_event_;
90 116
117 // PriorityQueue for Tasks/Sequences posted through SingleThreadTaskRunners
118 // returned by CreateTaskRunnerWithTraits.
119 PriorityQueue single_threaded_priority_queue_;
120
91 Delegate* const delegate_; 121 Delegate* const delegate_;
92 TaskTracker* const task_tracker_; 122 TaskTracker* const task_tracker_;
123 DelayedTaskManager* const delayed_task_manager_;
93 124
94 // Synchronizes access to |should_exit_for_testing_|. 125 // Synchronizes access to |should_exit_for_testing_|.
95 mutable SchedulerLock should_exit_for_testing_lock_; 126 mutable SchedulerLock should_exit_for_testing_lock_;
96 127
97 // True once JoinForTesting() has been called. 128 // True once JoinForTesting() has been called.
98 bool should_exit_for_testing_ = false; 129 bool should_exit_for_testing_ = false;
99 130
100 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); 131 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread);
101 }; 132 };
102 133
103 } // namespace internal 134 } // namespace internal
104 } // namespace base 135 } // namespace base
105 136
106 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ 137 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698