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

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: self review (remove unused headers) 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 // Sets |is_single_threaded_sequence| to true if the returned Sequence comes
50 // from |single_threaded_priority_queue|.
44 virtual scoped_refptr<Sequence> GetWork( 51 virtual scoped_refptr<Sequence> GetWork(
45 SchedulerWorkerThread* worker_thread) = 0; 52 SchedulerWorkerThread* worker_thread,
53 PriorityQueue* single_threaded_priority_queue,
54 bool* is_single_threaded_sequence) = 0;
gab 2016/04/18 18:04:02 I don't think the Delegate needs to know that this
fdoray 2016/04/18 19:04:50 Done.
46 55
47 // Called when |sequence| isn't empty after the SchedulerWorkerThread pops a 56 // Called when a non-single-threaded Sequence returned by GetWork() isn't
gab 2016/04/18 18:04:02 Idem: re-word without enforcing "single-threaded"
fdoray 2016/04/18 19:04:50 Done.
48 // Task from it. |sequence| is the last Sequence returned by GetWork(). 57 // empty after the SchedulerWorkerThread pops a Task from it.
49 virtual void EnqueueSequence(scoped_refptr<Sequence> sequence) = 0; 58 virtual void EnqueueSequence(scoped_refptr<Sequence> sequence) = 0;
50 }; 59 };
51 60
52 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs 61 // Creates a SchedulerWorkerThread with priority |thread_priority| that runs
53 // Tasks from Sequences returned by |delegate|. |task_tracker| is used to 62 // Tasks from Sequences returned by |delegate|. |task_tracker| handles
54 // handle shutdown behavior of Tasks. Returns nullptr if creating the 63 // shutdown behavior of Tasks. |delayed_task_manager| handles Tasks posted
55 // underlying platform thread fails. 64 // with a delay. |predecessor_priority_queue| is a PriorityQueue for which a
65 // thread is allowed to have an active Transaction when its creates a
gab 2016/04/18 18:04:02 s/its creates/it creates/ but also "it" doesn't c
fdoray 2016/04/18 19:04:50 Done.
66 // Transaction for this worker thread's single-threaded PriorityQueue. Returns
67 // nullptr if creating the underlying platform thread fails.
56 static std::unique_ptr<SchedulerWorkerThread> CreateSchedulerWorkerThread( 68 static std::unique_ptr<SchedulerWorkerThread> CreateSchedulerWorkerThread(
57 ThreadPriority thread_priority, 69 ThreadPriority thread_priority,
58 Delegate* delegate, 70 Delegate* delegate,
59 TaskTracker* task_tracker); 71 TaskTracker* task_tracker,
72 DelayedTaskManager* delayed_task_manager,
73 const PriorityQueue* predecessor_priority_queue);
60 74
61 // Destroying a SchedulerWorkerThread in production is not allowed; it is 75 // Destroying a SchedulerWorkerThread in production is not allowed; it is
62 // always leaked. In tests, it can only be destroyed after JoinForTesting() 76 // always leaked. In tests, it can only be destroyed after JoinForTesting()
63 // has returned. 77 // has returned.
64 ~SchedulerWorkerThread() override; 78 ~SchedulerWorkerThread() override;
65 79
80 // Returns a SingleThreadTaskRunner whose PostTask invocations will result in
81 // scheduling Tasks with |traits| on this worker thread.
82 scoped_refptr<SingleThreadTaskRunner> CreateTaskRunnerWithTraits(
83 const TaskTraits& traits);
84
66 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this 85 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this
67 // is called, this SchedulerWorkerThread will run Tasks from Sequences 86 // is called, this SchedulerWorkerThread will run Tasks from Sequences
68 // returned by the GetWork() method of its delegate until it returns nullptr. 87 // returned by the GetWork() method of its delegate until it returns nullptr.
69 void WakeUp(); 88 void WakeUp();
70 89
71 // Joins this SchedulerWorkerThread. If a Task is already running, it will be 90 // Joins this SchedulerWorkerThread. If a Task is already running, it will be
72 // allowed to complete its execution. This can only be called once. 91 // allowed to complete its execution. This can only be called once.
73 void JoinForTesting(); 92 void JoinForTesting();
74 93
94 // SchedulerTaskExecutor:
95 void PostTaskWithSequence(std::unique_ptr<Task> task,
96 scoped_refptr<Sequence> sequence) override;
97
75 private: 98 private:
76 SchedulerWorkerThread(ThreadPriority thread_priority, 99 SchedulerWorkerThread(ThreadPriority thread_priority,
77 Delegate* delegate, 100 Delegate* delegate,
78 TaskTracker* task_tracker); 101 TaskTracker* task_tracker,
102 DelayedTaskManager* delayed_task_manager,
103 const PriorityQueue* predecessor_priority_queue);
79 104
80 // PlatformThread::Delegate: 105 // PlatformThread::Delegate:
81 void ThreadMain() override; 106 void ThreadMain() override;
82 107
83 bool ShouldExitForTesting() const; 108 bool ShouldExitForTesting() const;
84 109
85 // Platform thread managed by this SchedulerWorkerThread. 110 // Platform thread managed by this SchedulerWorkerThread.
86 PlatformThreadHandle thread_handle_; 111 PlatformThreadHandle thread_handle_;
87 112
88 // Event signaled to wake up this SchedulerWorkerThread. 113 // Event signaled to wake up this SchedulerWorkerThread.
89 WaitableEvent wake_up_event_; 114 WaitableEvent wake_up_event_;
90 115
116 // PriorityQueue for Tasks/Sequences posted through SingleThreadTaskRunners
117 // returned by CreateTaskRunnerWithTraits.
118 PriorityQueue single_threaded_priority_queue_;
119
91 Delegate* const delegate_; 120 Delegate* const delegate_;
92 TaskTracker* const task_tracker_; 121 TaskTracker* const task_tracker_;
122 DelayedTaskManager* const delayed_task_manager_;
93 123
94 // Synchronizes access to |should_exit_for_testing_|. 124 // Synchronizes access to |should_exit_for_testing_|.
95 mutable SchedulerLock should_exit_for_testing_lock_; 125 mutable SchedulerLock should_exit_for_testing_lock_;
96 126
97 // True once JoinForTesting() has been called. 127 // True once JoinForTesting() has been called.
98 bool should_exit_for_testing_ = false; 128 bool should_exit_for_testing_ = false;
99 129
100 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); 130 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread);
101 }; 131 };
102 132
103 } // namespace internal 133 } // namespace internal
104 } // namespace base 134 } // namespace base
105 135
106 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ 136 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698