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

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: CR gab/danakj #25-26 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
« no previous file with comments | « base/task_scheduler/priority_queue.h ('k') | base/task_scheduler/scheduler_thread_pool.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
78
79 SchedulerThreadPool(const EnqueueSequenceCallback& enqueue_sequence_callback,
80 TaskTracker* task_tracker);
81
82 bool Initialize(ThreadPriority thread_priority, size_t max_threads);
83
84 // Wakes up the last thread from this thread pool to go idle, if any.
85 void WakeUpOneThread();
86
87 // Adds |worker_thread| to |idle_worker_threads_stack_|.
88 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread);
89
90 // PriorityQueue from which all threads of this thread pool get work.
91 PriorityQueue shared_priority_queue_;
92
93 // All worker threads owned by this thread pool. Is only modified during
94 // initialization of the thread pool.
95 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_;
96
97 // Synchronizes access to |idle_worker_threads_stack_| and
98 // |idle_worker_threads_cv_|. Has |shared_priority_queue_|'s lock as its
99 // predecessor so that a thread can be pushed to |idle_worker_threads_stack_|
100 // within the scope of a Transaction (more details in GetWork()).
101 SchedulerLock worker_threads_lock_;
gab 2016/04/08 21:10:51 Change the name back to |idle_worker_threads_lock_
fdoray 2016/04/11 13:52:23 Done.
102
103 // Stack of idle worker threads.
104 std::stack<SchedulerWorkerThread*> idle_worker_threads_stack_;
105
106 // Signaled when all worker threads become idle.
107 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_;
108
109 // Signaled once JoinForTesting() has returned.
110 WaitableEvent join_for_testing_returned_;
111
112 // Delegate for all worker threads in this pool.
113 std::unique_ptr<SchedulerWorkerThread::Delegate> worker_thread_delegate_;
114
115 TaskTracker* const task_tracker_;
116
117 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool);
118 };
119
120 } // namespace internal
121 } // namespace base
122
123 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_
OLDNEW
« no previous file with comments | « base/task_scheduler/priority_queue.h ('k') | base/task_scheduler/scheduler_thread_pool.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698