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

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

Issue 1708773002: TaskScheduler [7] SchedulerThreadPool (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_5_worker_thread
Patch Set: add SchedulerWorkerThreadDelegateImpl 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
(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_SCHEDULER_THREAD_POOL_H_
6 #define BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_
7
8 #include <stddef.h>
9
10 #include <memory>
11 #include <stack>
12 #include <vector>
13
14 #include "base/base_export.h"
15 #include "base/callback.h"
16 #include "base/macros.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/synchronization/condition_variable.h"
19 #include "base/task_runner.h"
20 #include "base/task_scheduler/priority_queue.h"
21 #include "base/task_scheduler/scheduler_lock.h"
22 #include "base/task_scheduler/scheduler_worker_thread.h"
23 #include "base/task_scheduler/sequence.h"
24 #include "base/task_scheduler/task_traits.h"
25 #include "base/threading/platform_thread.h"
26
27 namespace base {
28 namespace internal {
29
30 struct SequenceSortKey;
31 class TaskTracker;
32
33 // A pool of threads that run Tasks. This class is thread-safe.
34 class BASE_EXPORT SchedulerThreadPool {
35 public:
36 // Callback invoked when a Sequence isn't empty after a worker thread pops a
37 // Task from it.
38 using EnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>;
39
40 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority
41 // |thread_priority|. |enqueue_sequence_callback| will be invoked after a
42 // thread of this thread pool tries to run a Task. |task_tracker| is used to
43 // handle shutdown behavior of Tasks. Returns nullptr on failure to create a
44 // thread pool with at least one thread.
45 static std::unique_ptr<SchedulerThreadPool> CreateThreadPool(
46 ThreadPriority thread_priority,
47 size_t max_threads,
48 const EnqueueSequenceCallback& enqueue_sequence_callback,
49 TaskTracker* task_tracker);
50
51 // Destroying a SchedulerThreadPool returned by CreateThreadPool() is not
52 // allowed in production; it is always leaked. In tests, it can only be
53 // destroyed after JoinForTesting() has returned.
54 ~SchedulerThreadPool();
55
56 // Returns a TaskRunner whose PostTask invocations will result in scheduling
57 // Tasks with |traits| and |execution_mode| in this thread pool.
58 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
59 const TaskTraits& traits,
60 ExecutionMode execution_mode);
61
62 // Inserts |sequence| into this thread pool's shared priority queue with
63 // |sequence_sort_key|. Must only be called from a worker thread to put
64 // |sequence| back into a PriorityQueue after running a Task from it. The
65 // worker thread doesn't have to belong to this thread pool.
66 void EnqueueSequence(scoped_refptr<Sequence> sequence,
67 const SequenceSortKey& sequence_sort_key);
68
69 // Waits until all threads are idle.
70 void WaitForAllWorkerThreadsIdleForTesting();
71
72 // Joins all threads of this thread pool. Tasks that are already running are
73 // allowed to complete their execution. This can only be called once.
74 void JoinForTesting();
75
76 private:
77 class SchedulerWorkerThreadDelegateImpl
gab 2016/04/08 17:56:00 May even be able to only fwd-decl here?
fdoray 2016/04/08 19:00:05 Done.
78 : public SchedulerWorkerThread::Delegate {
79 public:
80 SchedulerWorkerThreadDelegateImpl(
81 SchedulerThreadPool* outer,
82 const EnqueueSequenceCallback enqueue_sequence_callback);
83 ~SchedulerWorkerThreadDelegateImpl() override;
84
85 // SchedulerWorkerThread::Delegate:
86 void OnMainEntry() override;
87 scoped_refptr<Sequence> GetWork(
88 SchedulerWorkerThread* worker_thread) override;
89 void EnqueueSequence(scoped_refptr<Sequence> sequence) override;
90
91 private:
92 SchedulerThreadPool* outer_;
93 const EnqueueSequenceCallback enqueue_sequence_callback_;
94 };
gab 2016/04/08 17:56:00 DISALLOW_COPY_AND_ASSIGN
fdoray 2016/04/08 19:00:05 Done.
95
96 SchedulerThreadPool(const EnqueueSequenceCallback& enqueue_sequence_callback,
97 TaskTracker* task_tracker);
98
99 bool Initialize(ThreadPriority thread_priority, size_t max_threads);
100
101 // Wakes up the last thread from this thread pool to go idle, if any.
102 void WakeUpOneThread();
103
104 // Adds |worker_thread| to |idle_worker_threads_stack_|.
105 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread);
106
107 // PriorityQueue from which all threads of this thread pool get work.
108 PriorityQueue shared_priority_queue_;
109
110 // Synchronizes access to |worker_threads_|, |idle_worker_threads_stack_| and
111 // |idle_worker_threads_cv_|. Has |shared_priority_queue_|'s lock as its
112 // predecessor so that a thread can be pushed to |idle_worker_threads_stack_|
113 // within the scope of a Transaction (more details in GetWork()).
114 SchedulerLock worker_threads_lock_;
115
116 // All worker threads owned by this thread pool.
117 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_;
118
119 // Stack of idle worker threads.
120 std::stack<SchedulerWorkerThread*> idle_worker_threads_stack_;
121
122 // Signaled when all worker threads become idle.
123 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_;
124
125 // Signaled once JoinForTesting() has returned.
126 WaitableEvent join_for_testing_returned_;
127
128 // Delegate for all worker threads in this pool.
129 SchedulerWorkerThreadDelegateImpl worker_thread_delegate_;
130
131 TaskTracker* const task_tracker_;
132
133 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool);
134 };
135
136 } // namespace internal
137 } // namespace base
138
139 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698