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

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: rebase 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 "base/base_export.h"
9 #include "base/callback.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/condition_variable.h"
14 #include "base/task_scheduler/priority_queue.h"
15 #include "base/task_scheduler/scheduler_lock.h"
16 #include "base/task_scheduler/sequence.h"
17 #include "base/task_scheduler/task_traits.h"
18 #include "base/threading/platform_thread.h"
19
20 namespace base {
21 namespace internal {
22
23 class TaskTracker;
24
25 // A thread that runs tasks from a shared and a single-threaded priority queue.
26 // This class is thread-safe.
27 class BASE_EXPORT WorkerThread : public PlatformThread::Delegate {
28 public:
29 // Callback invoked by |worker_thread| to reinsert |sequence| in the
30 // appropriate priority queue it has executed one of its tasks.
31 using ReinsertSequenceCallback =
32 Callback<void(scoped_refptr<Sequence> sequence,
33 const WorkerThread* worker_thread)>;
34
35 // Callback invoked when |worker_thread| becomes idle.
36 using BecomesIdleCallback = Callback<void(WorkerThread* worker_thread)>;
37
38 // Creates a WorkerThread with priority |thread_priority| that runs tasks from
39 // a single-thread priority queue and from |shared_priority_queue|.
40 // |reinsert_sequence_callback| is invoked to reinsert a sequence in the
41 // appropriate priority queue after one of its tasks has been executed.
42 // |task_tracker| is used to handle shutdown behavior of tasks.
43 static scoped_ptr<WorkerThread> CreateWorkerThread(
44 ThreadPriority thread_priority,
45 PriorityQueue* shared_priority_queue,
46 const ReinsertSequenceCallback& reinsert_sequence_callback,
47 const BecomesIdleCallback& becomes_idle_callback,
48 TaskTracker* task_tracker);
49
50 // A WorkerThread must only be destroyed after JoinForTesting() has returned.
51 ~WorkerThread() override;
52
53 // Wakes up the WorkerThread. Returns false if it was already awake.
54 bool WakeUp();
55
56 // Returns a SingleThreadTaskRunner whose PostTask invocations will result in
57 // scheduling tasks on this WorkerThread with |traits|.
58 scoped_refptr<SingleThreadTaskRunner> CreateTaskRunnerWithTraits(
59 const TaskTraits& traits);
60
61 // Joins the WorkerThread. If a task is already running, it will be allowed
62 // to complete its execution. This can only be called once.
63 void JoinForTesting();
64
65 private:
66 WorkerThread(ThreadPriority thread_priority,
67 PriorityQueue* shared_priority_queue,
68 const ReinsertSequenceCallback& reinsert_sequence_callback,
69 const BecomesIdleCallback& becomes_idle_callback,
70 TaskTracker* task_tracker);
71
72 // Extracts the sequence with the highest priority from
73 // |shared_priority_queue_| or |single_thread_priority_queue_|.
74 // |single_thread| is set to true if the returned task comes from
75 // |single_thread_priority_queue_|.
76 scoped_refptr<Sequence> GetWork(bool* single_thread);
77
78 // Waits until |is_awake_| becomes true.
79 void WaitUntilWakeUp();
80
81 // PlatformThread::Delegate:
82 void ThreadMain() override;
83
84 // Platform thread managed by this WorkerThread.
85 PlatformThreadHandle thread_handle_;
86
87 // Synchronizes access to |wake_up_cv_|, |is_awake_| and
88 // |should_exit_for_testing_|.
89 SchedulerLock lock_;
90
91 // Condition variable signaled when |is_awake_| becomes true.
92 mutable scoped_ptr<ConditionVariable> wake_up_cv_;
93
94 // True from the time WakeUp() is called to the time the WorkerThread doesn't
95 // have any remaining work to do.
96 bool is_awake_;
97
98 // True once JoinForTesting() has been called.
99 bool should_exit_for_testing_;
100
101 // The single-threaded priority queue from which the WorkerThread gets work.
102 PriorityQueue single_thread_priority_queue_;
103
104 // The shared priority queue from which the WorkerThread gets work.
105 PriorityQueue* const shared_priority_queue_;
106
107 const ReinsertSequenceCallback reinsert_sequence_callback_;
108 const BecomesIdleCallback becomes_idle_callback_;
109 TaskTracker* const task_tracker_;
110
111 DISALLOW_COPY_AND_ASSIGN(WorkerThread);
112 };
113
114 } // namespace internal
115 } // namespace base
116
117 #endif // BASE_TASK_SCHEDULER_WORKER_THREAD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698