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

Side by Side Diff: base/task_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_THREAD_POOL_H_
6 #define BASE_TASK_SCHEDULER_THREAD_POOL_H_
7
8 #include <stddef.h>
9
10 #include <stack>
11 #include <vector>
12
13 #include "base/base_export.h"
14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.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 ThreadPool {
35 public:
36 // Creates a ThreadPool with |num_threads| threads of priority
37 // |thread_priority|. |ran_task_from_sequence_callback| is invoked after a
38 // thread of this ThreadPool tries to run a Task from a Sequence.
39 // |task_tracker| is used to handle shutdown behavior of Tasks. Returns
40 // nullptr if when it fails to create a ThreadPool with at least one thread.
41 static scoped_ptr<ThreadPool> CreateThreadPool(
42 ThreadPriority thread_priority,
43 size_t num_threads,
44 const SchedulerWorkerThread::RanTaskFromSequenceCallback&
45 ran_task_from_sequence_callback,
46 TaskTracker* task_tracker);
47
48 // Destroying a ThreadPool in production is not allowed; it is always leaked.
49 // In tests, it can only be destroyed after JoinForTesting() has returned.
50 ~ThreadPool();
51
52 // Returns a TaskRunner whose PostTask invocations will result in scheduling
53 // Tasks with |traits| and |execution_mode| in this ThreadPool.
54 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
55 const TaskTraits& traits,
56 ExecutionMode execution_mode);
57
58 // Reinserts |sequence| in |shared_priority_queue_| with |sequence_sort_key|
59 // after |worker_thread| has run a Task from it. Note that |worker_thread|
robliao 2016/03/31 22:48:56 The second part of this is an implementation detai
fdoray 2016/04/01 16:02:52 Done.
robliao 2016/04/01 19:14:49 Looking at the code side-by-side, do we need worke
fdoray 2016/04/01 20:16:45 Removed it.
60 // doesn't necessarily belong to this ThreadPool.
61 void ReinsertSequence(const SchedulerWorkerThread* worker_thread,
62 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 ThreadPool. 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 ThreadPool(ThreadPriority thread_priority,
74 size_t num_threads,
75 const SchedulerWorkerThread::RanTaskFromSequenceCallback&
76 ran_task_from_sequence_callback,
77 TaskTracker* task_tracker);
78
79 // Wakes up one thread from this ThreadPool if they aren't all busy.
80 void WakeUpOneThread();
81
82 // Adds |worker_thread| to |idle_worker_threads_stack_|.
83 void AddToIdleSchedulerWorkerThreadsStack(
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 ThreadPool get work.
96 PriorityQueue shared_priority_queue_;
97
98 // All worker threads owned by this ThreadPool. This is only modified by the
99 // constructor.
100 std::vector<scoped_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 scoped_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(ThreadPool);
126 };
127
128 } // namespace internal
129 } // namespace base
130
131 #endif // BASE_TASK_SCHEDULER_THREAD_POOL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698