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

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: self review 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/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/synchronization/condition_variable.h"
18 #include "base/task_runner.h"
19 #include "base/task_scheduler/priority_queue.h"
20 #include "base/task_scheduler/scheduler_lock.h"
21 #include "base/task_scheduler/scheduler_worker_thread.h"
22 #include "base/task_scheduler/sequence.h"
23 #include "base/task_scheduler/task_traits.h"
24 #include "base/threading/platform_thread.h"
25 #include "base/threading/thread_local.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 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority
37 // |thread_priority|. |ran_task_from_sequence_callback| is invoked after a
38 // thread of this thread pool tries to run a Task from a Sequence.
39 // |task_tracker| is used to handle shutdown behavior of Tasks. Returns
40 // nullptr when it's not possible to create a thread pool with at least one
41 // thread.
42 static std::unique_ptr<SchedulerThreadPool> CreateThreadPool(
43 ThreadPriority thread_priority,
44 size_t max_threads,
45 const SchedulerWorkerThread::RanTaskFromSequenceCallback&
46 ran_task_from_sequence_callback,
47 TaskTracker* task_tracker);
48
49 // Destroying a SchedulerThreadPool in production is not allowed; it is always
50 // leaked. In tests, it can only be destroyed after JoinForTesting() has
51 // returned.
52 ~SchedulerThreadPool();
53
54 // Returns a TaskRunner whose PostTask invocations will result in scheduling
55 // Tasks with |traits| and |execution_mode| in this thread pool.
56 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
57 const TaskTraits& traits,
58 ExecutionMode execution_mode);
59
60 // Reinserts |sequence| into this thread pool's shared priority queue with
danakj 2016/04/05 00:27:59 Can you explain in the comment why this method is
fdoray 2016/04/06 19:31:02 Renamed ReinsertSequence -> InsertSequenceAfterTas
61 // |sequence_sort_key|.
62 void ReinsertSequence(scoped_refptr<Sequence> sequence,
63 const SequenceSortKey& sequence_sort_key);
64
65 // Waits until all threads are idle.
66 void WaitForAllWorkerThreadsIdleForTesting();
67
68 // Joins all threads of this thread pool. Tasks that are already running are
69 // allowed to complete their execution. This can only be called once.
70 void JoinForTesting();
71
72 private:
73 SchedulerThreadPool(ThreadPriority thread_priority,
74 size_t max_threads,
75 const SchedulerWorkerThread::RanTaskFromSequenceCallback&
76 ran_task_from_sequence_callback,
77 TaskTracker* task_tracker);
78
79 // Wakes up one thread from this thread pool if they aren't all busy.
80 void WakeUpOneThread();
81
82 // Adds |worker_thread| to |idle_worker_threads_stack_|.
83 void AddToIdleSchedulerWorkerThreadsStack(
danakj 2016/04/05 00:27:59 Quite the mouthful. Maybe just AddToIdleWorkerThre
fdoray 2016/04/06 19:31:02 Done.
84 SchedulerWorkerThread* worker_thread);
85
86 // Invoked when a sequence is inserted in |shared_priority_queue_|.
87 void SequenceInsertedInSharedPriorityQueueCallback();
88
89 // Invoked when the main function of a worker thread is entered.
90 void MainEntryCallback() const;
91
92 // Invoked by a worker thread to get a Sequence from which to run a Task.
93 scoped_refptr<Sequence> GetWorkCallback(SchedulerWorkerThread* worker_thread);
94
95 // PriorityQueue from which all threads of this thread pool get work.
96 PriorityQueue shared_priority_queue_;
97
98 // All worker threads owned by this thread pool. This is only modified by the
99 // constructor.
100 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_;
101
102 // True if WakeUpOneThread() shouldn't be called when a Sequence is inserted
103 // in |shared_priority_queue_| by a given thread.
104 ThreadLocalBoolean no_wake_up_on_sequence_insertion_;
105
106 // Synchronizes access to |idle_worker_threads_stack_| and
107 // |idle_worker_threads_cv_|.
108 SchedulerLock idle_worker_threads_stack_lock_;
109
110 // Stack of idle worker threads. The last worker thread that became idle is on
111 // top of the stack.
112 std::stack<SchedulerWorkerThread*> idle_worker_threads_stack_;
113
114 // Signaled when all worker threads become idle.
115 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_;
116
117 // Synchronizes access to |join_for_testing_returned_|.
118 SchedulerLock join_for_testing_returned_lock_;
119
120 // True once JoinForTesting() has returned.
121 bool join_for_testing_returned_ = false;
122
123 TaskTracker* const task_tracker_;
124
125 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool);
126 };
127
128 // Helper for posting |task| to the provided |sequence| and |priority_queue|
129 // conditional on |task_tracker|. Exposed for testing.
130 void BASE_EXPORT PostTaskHelper(std::unique_ptr<Task> task,
131 scoped_refptr<Sequence> sequence,
132 PriorityQueue* priority_queue,
133 TaskTracker* task_tracker);
134
135 } // namespace internal
136 } // namespace base
137
138 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698