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

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: CR from gab #18 Created 4 years, 9 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 <iosfwd>
9
10 #include "base/atomicops.h"
11 #include "base/base_export.h"
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/task_scheduler/priority_queue.h"
19 #include "base/task_scheduler/sequence.h"
20 #include "base/task_scheduler/task_traits.h"
21 #include "base/threading/platform_thread.h"
22
23 namespace base {
24 namespace internal {
25
26 class TaskTracker;
27
28 // A thread that runs Tasks from a shared and a single-threaded PriorityQueue.
29 //
30 // There is no guarantee that a WorkerThread will run work from its shared
31 // PriorityQueue unless WakeUp() is called after the work is added. This allows
32 // a ThreadPool to choose which WorkerThread should run the new work. On the
33 // other hand, a WorkerThread is woken up automatically when Tasks are added to
34 // its single-threaded PriorityQueue.
35 //
36 // This class is thread-safe.
37 class BASE_EXPORT WorkerThread : public PlatformThread::Delegate {
38 public:
39 // Possible states for a WorkerThread.
40 enum class State {
41 BUSY,
42 IDLE,
43 };
44
45 // Callback invoked to indicate that a shared |sequence| is not empty after
46 // |worker_thread| has popped a Task from it.
47 using SharedSequenceStillHasTasksCallback =
48 Callback<void(const WorkerThread* worker_thread,
49 scoped_refptr<Sequence> sequence)>;
50
51 // Callback invoked when the state of |worker_thread| becomes |state|.
52 using StateChangedCallback =
53 Callback<void(WorkerThread* worker_thread, State state)>;
54
55 // Creates a WorkerThread with priority |thread_priority| that runs Tasks from
56 // its single-threaded PriorityQueue and from |shared_priority_queue|.
robliao 2016/03/24 23:03:26 Nit: |single_thread_priority_queue_| and from |sha
fdoray 2016/03/29 18:33:34 The intent of this comment is to describe the argu
57 // |shared_sequence_still_has_tasks_callback| is invoked when a Sequence
58 // extracted from |shared_priority_queue| isn't empty after the WorkerThread
59 // has popped a task from it. |state_changed_callback| is invoked when the
60 // WorkerThread becomes IDLE or BUSY. |task_tracker| is used to handle
61 // shutdown behavior of Tasks.
62 static scoped_ptr<WorkerThread> CreateWorkerThread(
63 ThreadPriority thread_priority,
64 PriorityQueue* shared_priority_queue,
65 const SharedSequenceStillHasTasksCallback&
66 shared_sequence_still_has_tasks_callback,
67 const StateChangedCallback& state_changed_callback,
68 TaskTracker* task_tracker);
69
70 // Destroying a WorkerThread in production is not allowed. In tests, it can
robliao 2016/03/24 23:03:26 How are we planning on reclaiming WorkerThreads in
fdoray 2016/03/29 18:33:34 Once a WorkerThread has been created in production
robliao 2016/03/30 00:42:42 I think this will be hard to determine until we ac
71 // only be destroyed after JoinForTesting() has returned.
72 ~WorkerThread() override;
73
74 // Wakes up this WorkerThread. When this is called, this WorkerThread will run
75 // until both its PriorityQueues become empty.
76 void WakeUp();
77
78 // Returns a SingleThreadTaskRunner whose PostTask invocations will result in
79 // scheduling Tasks with |traits| on this WorkerThread.
80 scoped_refptr<SingleThreadTaskRunner> CreateTaskRunnerWithTraits(
81 const TaskTraits& traits);
82
83 // Joins this WorkerThread. If a Task is already running, it will be allowed
84 // to complete its execution. This can only be called once.
85 void JoinForTesting();
86
87 private:
88 WorkerThread(ThreadPriority thread_priority,
89 PriorityQueue* shared_priority_queue,
90 const SharedSequenceStillHasTasksCallback&
91 shared_sequence_still_has_tasks_callback,
92 const StateChangedCallback& state_changed_callback,
93 TaskTracker* task_tracker);
94
95 // Extracts the Sequence with the highest priority from
96 // |shared_priority_queue_| or |single_thread_priority_queue_|.
97 // |is_single_threaded| is set to true if the returned Sequence comes from
98 // |single_thread_priority_queue_|.
99 scoped_refptr<Sequence> GetWork(bool* is_single_threaded);
100
101 // Sets the state of this WorkerThread.
102 void SetState(State state);
103
104 // PlatformThread::Delegate:
105 void ThreadMain() override;
106
107 bool should_exit_for_testing() const {
108 base::subtle::MemoryBarrier();
109 return should_exit_for_testing_;
110 }
111
112 // Platform thread managed by this WorkerThread.
113 PlatformThreadHandle thread_handle_;
114
115 // Current state of this WorkerThread.
116 State state_ = State::BUSY;
117
118 // Event signaled to wake up this WorkerThread.
119 WaitableEvent wake_up_event_;
120
121 // The single-threaded PriorityQueue from which this WorkerThread gets work.
122 PriorityQueue single_thread_priority_queue_;
123
124 // The shared PriorityQueue from which this WorkerThread gets work.
125 PriorityQueue* const shared_priority_queue_;
126
127 const SharedSequenceStillHasTasksCallback
128 shared_sequence_still_has_tasks_callback_;
129 const StateChangedCallback state_changed_callback_;
130 TaskTracker* const task_tracker_;
131
132 // True once JoinForTesting() has been called.
133 bool should_exit_for_testing_ = false;
134
135 DISALLOW_COPY_AND_ASSIGN(WorkerThread);
136 };
137
138 BASE_EXPORT std::ostream& operator<<(std::ostream& os,
139 WorkerThread::State state);
140
141 } // namespace internal
142 } // namespace base
143
144 #endif // BASE_TASK_SCHEDULER_WORKER_THREAD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698