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

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

Issue 1704113002: TaskScheduler [6] SchedulerWorkerThread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_4_shutdown
Patch Set: Created 4 years, 10 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef BASE_TASK_SCHEDULER_WORKER_THREAD_H_
6 #define BASE_TASK_SCHEDULER_WORKER_THREAD_H_
7
8 #include "base/base_export.h"
9 #include "base/callback_forward.h"
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/task_scheduler/priority_queue.h"
15 #include "base/task_scheduler/sequence.h"
16 #include "base/task_scheduler/task_traits.h"
17 #include "base/threading/platform_thread.h"
18
19 namespace base {
20 namespace internal {
21
22 class DelayedTaskManager;
23 class ShutdownManager;
24
25 // A thread that runs tasks from a shared and a single-threaded priority queue.
26 // This public methods of this class are thread-safe.
27 class BASE_EXPORT WorkerThread : public PlatformThread::Delegate {
28 public:
29 // Callback invoked when the thread enters its main function.
30 using MainEntryCallback = Callback<void()>;
31
32 // Callback to reinsert |sequence| in the appropriate priority queue after one
33 // of its tasks has been executed. |worker_thread| is the worker thread that
34 // invokes the callback.
35 using ReinsertSequenceCallback =
36 Callback<void(scoped_refptr<Sequence> sequence,
37 const WorkerThread* worker_thread)>;
38
39 // Callback invoked when the worker thread becomes idle. |worker_thread| is
40 // the worker thread that invokes the callback.
41 using BecomesIdleCallback = Callback<void(WorkerThread* worker_thread)>;
42
43 // Creates a worker thread that runs tasks from a single-thread priority queue
44 // and from |shared_priority_queue| with |thread_priority|.
45 // |reinsert_sequence_callback| is invoked to reinsert a sequence in the
46 // appropriate priority queue after one of its tasks has been executed.
47 // |delayed_task_manager| is used to handle delayed tasks. |shutdown_manager|
48 // is used to handle shutdown behavior of tasks.
49 static scoped_ptr<WorkerThread> CreateWorkerThread(
50 ThreadPriority thread_priority,
51 PriorityQueue* shared_priority_queue,
52 const MainEntryCallback& main_entry_callback,
53 const ReinsertSequenceCallback& reinsert_sequence_callback,
54 const BecomesIdleCallback& becomes_idle_callback,
55 DelayedTaskManager* delayed_task_manager,
56 ShutdownManager* shutdown_manager);
57
58 ~WorkerThread() override;
59
60 // Wakes up the worker thread. Soon after this is called, the thread starts
61 // running tasks from its priority queue. It continues to run tasks until its
62 // priority queue is empty. Has no effect if the worker thread is already
63 // running tasks.
64 void WakeUp();
65
66 // Returns a SingleThreadTaskRunner whose PostTask invocations will result in
67 // scheduling tasks on this worker thread with traits |traits|.
68 scoped_refptr<SingleThreadTaskRunner> CreateTaskRunnerWithTraits(
69 const TaskTraits& traits,
70 ExecutionMode execution_mode);
71
72 // Returns true if the worker thread has single-threaded tasks (running or
73 // pending). The returned result can be invalidated at any time by single-
74 // threaded tasks being posted or executed.
75 bool HasSingleThreadedTasks() const;
76
77 // Initiates shutdown and waits until the thread exits. This method must not
78 // be called more than once in the lifetime of the WorkerThread.
79 void ShutdownAndJoinForTesting();
80
81 const PriorityQueue* shared_priority_queue() const {
82 return shared_priority_queue_;
83 }
84
85 private:
86 WorkerThread(ThreadPriority thread_priority,
87 PriorityQueue* shared_priority_queue,
88 const MainEntryCallback& main_entry_callback,
89 const ReinsertSequenceCallback& reinsert_sequence_callback,
90 const BecomesIdleCallback& becomes_idle_callback,
91 DelayedTaskManager* delayed_task_manager,
92 ShutdownManager* shutdown_manager);
93
94 // Returns true if a thread has been successfully created by the constructor.
95 bool IsValid() const;
96
97 // Extracts the sequence with the highest priority from
98 // |shared_priority_queue_| or |single_thread_priority_queue_|.
99 scoped_refptr<Sequence> GetWork();
100
101 // Reinserts |sequence| in |single_thread_priority_queue_|.
102 void ReinsertSequenceInSingleThreadPriorityQueue(
103 scoped_refptr<Sequence> sequence);
104
105 // Waits until either |wakeup_event_| is signaled or a task from
106 // |delayed_task_manager_| becomes ready for execution.
107 void WaitUntilWorkIsAvailable();
108
109 // PlatformThread::Delegate:
110 void ThreadMain() override;
111
112 // Platform thread managed by this object.
113 PlatformThreadHandle thread_handle_;
114
115 // Event signaled to wake up the thread.
116 mutable WaitableEvent wakeup_event_;
117
118 // True when the worker thread is running a single-threaded task.
119 bool is_running_single_threaded_task_;
120
121 // The single-threaded priority queue from which the worker thread gets work.
122 // Tasks posted to a SINGLE_THREADED* task runner returned by this
123 // WorkerThread end up in this priority queue.
124 PriorityQueue single_thread_priority_queue_;
125
126 // The shared priority queue from which the worker thread gets work.
127 PriorityQueue* const shared_priority_queue_;
128
129 const MainEntryCallback main_entry_callback_;
130
131 const ReinsertSequenceCallback reinsert_sequence_callback_;
132
133 const BecomesIdleCallback becomes_idle_callback_;
134
135 DelayedTaskManager* const delayed_task_manager_;
136
137 ShutdownManager* const shutdown_manager_;
138
139 DISALLOW_COPY_AND_ASSIGN(WorkerThread);
140 };
141
142 } // namespace internal
143 } // namespace base
144
145 #endif // BASE_TASK_SCHEDULER_WORKER_THREAD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698