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

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

Issue 1685423002: Task Scheduler. (Closed) Base URL: https://luckyluke-private.googlesource.com/src@a_master
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
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/task_scheduler/delayed_task_manager.h"
17 #include "base/task_scheduler/priority_queue.h"
18 #include "base/task_scheduler/scheduler_lock.h"
19 #include "base/task_scheduler/sequence.h"
20 #include "base/task_scheduler/worker_thread.h"
21 #include "base/threading/platform_thread.h"
22 #include "base/threading/thread_local.h"
23
24 namespace base {
25 struct TaskTraits;
26 } // namespace base
27
28 namespace base {
29 namespace task_scheduler {
30
31 class SequenceSortKey;
32 class ShutdownManager;
33
34 // A pool of threads that run tasks. Unless otherwise noted, all public methods
35 // of this class are 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 // Waits until all threads have exited. The Shutdown() method of
71 // |shutdown_manager_| must have been called before this is called, otherwise
72 // the threads won't exit. This method is not thread-safe.
fdoray 2016/02/11 17:30:33 "This method can only be called once per ThreadPoo
fdoray 2016/02/12 04:16:20 Done.
73 void JoinAllThreadsForTesting();
74
75 private:
76 ThreadPool(
77 ThreadPriority thread_priority,
78 size_t num_threads,
79 const WorkerThread::ReinsertSequenceCallback& reinsert_sequence_callback,
80 ShutdownManager* shutdown_manager);
81
82 // Invoked by |worker_thread| when it becomes idle. |worker_thread| has to
83 // belong to this thread pool.
84 void WorkerThreadBecomesIdleCallback(WorkerThread* worker_thread);
85
86 // Wakes up 1 thread from the pool if not all threads are busy.
87 void WakeUpOneThread();
88
89 // Callback invoked when a sequence is inserted in |priority_queue_|.
90 void OnSequenceInsertedInPriorityQueue();
91
92 // Priority queue from which all worker threads of this pool get work.
93 PriorityQueue priority_queue_;
94
95 // All the worker threads owned by this thread pool.
96 std::vector<scoped_ptr<WorkerThread>> worker_threads_;
97
98 // Lock protecting |idle_worker_threads_stack_| and |idle_threads_set_|.
99 SchedulerLock idle_worker_threads_lock_;
100
101 // Stack of possibly idle worker threads. The last worker thread that became
102 // idle is on top of the stack. When a thread is woken up to execute single-
103 // threaded tasks, it is not removed from this stack. That means that a thread
104 // can be on this stack without actually being idle.
105 std::stack<WorkerThread*> idle_worker_threads_stack_;
106
107 // Set of worker threads that are in |idle_worker_threads_stack_|.
108 std::set<WorkerThread*> idle_worker_threads_set_;
109
110 // Indicates that no worker thread should be woken up when a sequence is
111 // inserted in |priority_queue_| by a given thread.
112 ThreadLocalBoolean disable_wake_up_thread_on_sequence_insertion_;
113
114 ShutdownManager* const shutdown_manager_;
115
116 // Manages delayed tasks posted to this thread pool.
117 DelayedTaskManager delayed_task_manager_;
118
119 DISALLOW_COPY_AND_ASSIGN(ThreadPool);
120 };
121
122 } // namespace task_scheduler
123 } // namespace base
124
125 #endif // BASE_TASK_SCHEDULER_THREAD_POOL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698