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

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

Issue 1698183005: Reference CL for the new task scheduler. (Closed) Base URL: https://luckyluke-private.googlesource.com/src@bigmaster2
Patch Set: Created 4 years, 10 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/test_util.cc ('k') | base/task_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_THREAD_POOL_H_
6 #define BASE_TASK_SCHEDULER_THREAD_POOL_H_
7
8 #include <set>
9 #include <stack>
10 #include <vector>
11
12 #include "base/base_export.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/task_scheduler/delayed_task_manager.h"
18 #include "base/task_scheduler/priority_queue.h"
19 #include "base/task_scheduler/scheduler_lock.h"
20 #include "base/task_scheduler/sequence.h"
21 #include "base/task_scheduler/worker_thread.h"
22 #include "base/threading/platform_thread.h"
23 #include "base/threading/thread_local.h"
24
25 namespace base {
26 struct TaskTraits;
27 } // namespace base
28
29 namespace base {
30 namespace internal {
31
32 class SequenceSortKey;
33 class ShutdownManager;
34
35 // A pool of threads that run tasks. This class is thread-safe.
36 class BASE_EXPORT ThreadPool {
37 public:
38 ~ThreadPool();
39
40 // Creates a pool with |num_threads| threads of priority |thread_priority|.
41 // |reinsert_sequence_callback| is invoked to reinsert a sequence in the
42 // appropriate priority queue after one of its tasks has been executed.
43 // |shutdown_manager| is used to handle shutdown behavior of tasks. Returns
44 // nullptr if it wasn't possible to create at least 1 thread.
45 static scoped_ptr<ThreadPool> CreateThreadPool(
46 ThreadPriority thread_priority,
47 size_t num_threads,
48 const WorkerThread::ReinsertSequenceCallback& reinsert_sequence_callback,
49 ShutdownManager* shutdown_manager);
50
51 // Returns the number of threads in the pool. This can be less than the number
52 // of threads requested when the pool was created.
53 size_t GetNumThreads() const;
54
55 // Returns a TaskRunner whose PostTask invocations will result in scheduling
56 // tasks within this thread pool with traits |traits| and execution mode
57 // |execution_mode|.
58 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
59 const TaskTraits& traits,
60 ExecutionMode execution_mode);
61
62 // Reinserts |sequence| in the priority queue of this thread pool with sort
63 // key |sequence_sort_key| after one of its tasks has been executed. Note that
64 // the task could have been executed by a worker thread from this pool or from
65 // another pool.
66 void ReinsertSequence(scoped_refptr<Sequence> sequence,
67 const SequenceSortKey& sequence_sort_key,
68 const WorkerThread* worker_thread);
69
70 // Initiates shutdown and waits until all threads have exited. This method
71 // must not be called more than once in the lifetime of the ThreadPool.
72 void ShutdownAndJoinAllThreadsForTesting();
73
74 private:
75 ThreadPool(
76 ThreadPriority thread_priority,
77 size_t num_threads,
78 const WorkerThread::ReinsertSequenceCallback& reinsert_sequence_callback,
79 ShutdownManager* shutdown_manager);
80
81 // Invoked by |worker_thread| when it becomes idle. |worker_thread| has to
82 // belong to this thread pool.
83 void WorkerThreadBecomesIdleCallback(WorkerThread* worker_thread);
84
85 // Wakes up 1 thread from the pool if not all threads are busy.
86 void WakeUpOneThread();
87
88 // Callback invoked when a sequence is inserted in |priority_queue_|.
89 void OnSequenceInsertedInPriorityQueue();
90
91 // Signaled when the threadpool data structures are ready.
92 WaitableEvent thread_pool_ready_;
93
94 // Priority queue from which all worker threads of this pool get work.
95 PriorityQueue priority_queue_;
96
97 // All the worker threads owned by this thread pool.
98 std::vector<scoped_ptr<WorkerThread>> worker_threads_;
99
100 // Lock protecting |idle_worker_threads_stack_| and |idle_threads_set_|.
101 SchedulerLock idle_worker_threads_lock_;
102
103 // Stack of possibly idle worker threads. The last worker thread that became
104 // idle is on top of the stack. When a thread is woken up to execute single-
105 // threaded tasks, it is not removed from this stack. That means that a thread
106 // can be on this stack without actually being idle.
107 std::stack<WorkerThread*> idle_worker_threads_stack_;
108
109 // Set of worker threads that are in |idle_worker_threads_stack_|.
110 std::set<WorkerThread*> idle_worker_threads_set_;
111
112 // Indicates that no worker thread should be woken up when a sequence is
113 // inserted in |priority_queue_| by a given thread.
114 ThreadLocalBoolean disable_wake_up_thread_on_sequence_insertion_;
115
116 ShutdownManager* const shutdown_manager_;
117
118 // Manages delayed tasks posted to this thread pool.
119 DelayedTaskManager delayed_task_manager_;
120
121 DISALLOW_COPY_AND_ASSIGN(ThreadPool);
122 };
123
124 } // namespace internal
125 } // namespace base
126
127 #endif // BASE_TASK_SCHEDULER_THREAD_POOL_H_
OLDNEW
« no previous file with comments | « base/task_scheduler/test_util.cc ('k') | base/task_scheduler/thread_pool.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698