OLD | NEW |
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/atomicops.h" |
10 #include "base/base_export.h" | 11 #include "base/base_export.h" |
11 #include "base/macros.h" | 12 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
14 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
15 #include "base/task_scheduler/priority_queue.h" | 16 #include "base/task_scheduler/priority_queue.h" |
16 #include "base/task_scheduler/scheduler_lock.h" | 17 #include "base/task_scheduler/scheduler_lock.h" |
17 #include "base/task_scheduler/scheduler_task_executor.h" | 18 #include "base/task_scheduler/scheduler_task_executor.h" |
18 #include "base/task_scheduler/sequence.h" | 19 #include "base/task_scheduler/sequence.h" |
19 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 // Destroying a SchedulerWorkerThread in production is not allowed; it is | 76 // Destroying a SchedulerWorkerThread in production is not allowed; it is |
76 // always leaked. In tests, it can only be destroyed after JoinForTesting() | 77 // always leaked. In tests, it can only be destroyed after JoinForTesting() |
77 // has returned. | 78 // has returned. |
78 ~SchedulerWorkerThread() override; | 79 ~SchedulerWorkerThread() override; |
79 | 80 |
80 // Returns a SingleThreadTaskRunner whose PostTask invocations will result in | 81 // Returns a SingleThreadTaskRunner whose PostTask invocations will result in |
81 // scheduling Tasks with |traits| on this worker thread. | 82 // scheduling Tasks with |traits| on this worker thread. |
82 scoped_refptr<SingleThreadTaskRunner> CreateTaskRunnerWithTraits( | 83 scoped_refptr<SingleThreadTaskRunner> CreateTaskRunnerWithTraits( |
83 const TaskTraits& traits); | 84 const TaskTraits& traits); |
84 | 85 |
| 86 // Returns the number of single-thread TaskRunners associated with this |
| 87 // SchedulerWorkerThread. |
| 88 size_t GetNumSingleThreadTaskRunners() const; |
| 89 |
85 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this | 90 // Wakes up this SchedulerWorkerThread if it wasn't already awake. After this |
86 // is called, this SchedulerWorkerThread will run Tasks from Sequences | 91 // is called, this SchedulerWorkerThread will run Tasks from Sequences |
87 // returned by the GetWork() method of its delegate until it returns nullptr. | 92 // returned by the GetWork() method of its delegate until it returns nullptr. |
88 void WakeUp(); | 93 void WakeUp(); |
89 | 94 |
90 // Joins this SchedulerWorkerThread. If a Task is already running, it will be | 95 // Joins this SchedulerWorkerThread. If a Task is already running, it will be |
91 // allowed to complete its execution. This can only be called once. | 96 // allowed to complete its execution. This can only be called once. |
92 void JoinForTesting(); | 97 void JoinForTesting(); |
93 | 98 |
94 // SchedulerTaskExecutor: | 99 // SchedulerTaskExecutor: |
(...skipping 11 matching lines...) Expand all Loading... |
106 void ThreadMain() override; | 111 void ThreadMain() override; |
107 | 112 |
108 bool ShouldExitForTesting() const; | 113 bool ShouldExitForTesting() const; |
109 | 114 |
110 // Platform thread managed by this SchedulerWorkerThread. | 115 // Platform thread managed by this SchedulerWorkerThread. |
111 PlatformThreadHandle thread_handle_; | 116 PlatformThreadHandle thread_handle_; |
112 | 117 |
113 // Event signaled to wake up this SchedulerWorkerThread. | 118 // Event signaled to wake up this SchedulerWorkerThread. |
114 WaitableEvent wake_up_event_; | 119 WaitableEvent wake_up_event_; |
115 | 120 |
| 121 // Number of single-thread TaskRunners associated with this |
| 122 // SchedulerWorkerThread. |
| 123 base::subtle::Atomic32 num_single_thread_task_runners_; |
| 124 |
116 // PriorityQueue for Tasks/Sequences posted through SingleThreadTaskRunners | 125 // PriorityQueue for Tasks/Sequences posted through SingleThreadTaskRunners |
117 // returned by CreateTaskRunnerWithTraits. | 126 // returned by CreateTaskRunnerWithTraits. |
118 PriorityQueue single_threaded_priority_queue_; | 127 PriorityQueue single_threaded_priority_queue_; |
119 | 128 |
120 Delegate* const delegate_; | 129 Delegate* const delegate_; |
121 TaskTracker* const task_tracker_; | 130 TaskTracker* const task_tracker_; |
122 DelayedTaskManager* const delayed_task_manager_; | 131 DelayedTaskManager* const delayed_task_manager_; |
123 | 132 |
124 // Synchronizes access to |should_exit_for_testing_|. | 133 // Synchronizes access to |should_exit_for_testing_|. |
125 mutable SchedulerLock should_exit_for_testing_lock_; | 134 mutable SchedulerLock should_exit_for_testing_lock_; |
126 | 135 |
127 // True once JoinForTesting() has been called. | 136 // True once JoinForTesting() has been called. |
128 bool should_exit_for_testing_ = false; | 137 bool should_exit_for_testing_ = false; |
129 | 138 |
130 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); | 139 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThread); |
131 }; | 140 }; |
132 | 141 |
133 } // namespace internal | 142 } // namespace internal |
134 } // namespace base | 143 } // namespace base |
135 | 144 |
136 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ | 145 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_THREAD_H_ |
OLD | NEW |