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

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: merge SchedulerWorkerThreadDelegate 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/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/scheduler_worker_thread_delegate.h"
24 #include "base/task_scheduler/sequence.h"
25 #include "base/task_scheduler/task_traits.h"
26 #include "base/threading/platform_thread.h"
27
28 namespace base {
29 namespace internal {
30
31 struct SequenceSortKey;
32 class TaskTracker;
33
34 // A pool of threads that run Tasks. This class is thread-safe.
35 class BASE_EXPORT SchedulerThreadPool : public SchedulerWorkerThreadDelegate {
36 public:
37 // Callback invoked after a worker thread has tried to run a Task from
38 // |sequence| (a TaskTracker might have prevented the Task from running).
39 using RanTaskFromSequenceCallback = Callback<void(scoped_refptr<Sequence>)>;
40
41 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority
42 // |thread_priority|. |ran_task_from_sequence_callback| will be invoked after
43 // a thread of this thread pool tries to run a Task. |task_tracker| is used to
44 // handle shutdown behavior of Tasks. Returns nullptr when it's not possible
45 // to create a thread pool with at least one thread.
gab 2016/04/07 20:32:48 s/when it's not possible to/on failure to/
fdoray 2016/04/08 14:53:03 Done.
46 static std::unique_ptr<SchedulerThreadPool> CreateThreadPool(
47 ThreadPriority thread_priority,
48 size_t max_threads,
49 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback,
50 TaskTracker* task_tracker);
51
52 // Destroying a SchedulerThreadPool returned by CreateThreadPool() is not
53 // allowed in production; it is always leaked. In tests, it can only be
54 // destroyed after JoinForTesting() has returned.
55 ~SchedulerThreadPool();
56
57 // Returns a TaskRunner whose PostTask invocations will result in scheduling
58 // Tasks with |traits| and |execution_mode| in this thread pool.
gab 2016/04/07 20:32:48 Does anything make sure that this STP is the right
fdoray 2016/04/08 14:53:03 It's up to the caller to decide. The mapping TaskT
59 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
60 const TaskTraits& traits,
61 ExecutionMode execution_mode);
62
63 // Inserts |sequence| into this thread pool's shared priority queue with
64 // |sequence_sort_key|. Must only be used by a worker thread to put |sequence|
gab 2016/04/07 20:32:48 I would assume SWT doesn't have a pointer back to
fdoray 2016/04/08 14:53:03 Currently, the stack to reinsert a Sequence in a s
65 // back into a PriorityQueue after running a Task from it.
66 void InsertSequenceAfterTaskRan(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 SchedulerThreadPool(
78 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback,
79 TaskTracker* task_tracker);
80
81 void Initialize(ThreadPriority thread_priority, size_t max_threads);
82
83 // Wakes up one thread from this thread pool if they aren't all busy.
gab 2016/04/07 20:32:47 How about: // Wakes up the last thread from this
fdoray 2016/04/08 14:53:02 Done.
84 void WakeUpOneThread();
85
86 // Adds |worker_thread| to |idle_worker_threads_stack_|.
87 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread);
88
89 // SchedulerWorkerThreadDelegate:
90 void OnMainEntry() override;
91 void OnMainExit() override;
92 scoped_refptr<Sequence> GetWork(
93 SchedulerWorkerThread* worker_thread) override;
94 void RanTaskFromSequence(scoped_refptr<Sequence> sequence) override;
95
96 // PriorityQueue from which all threads of this thread pool get work.
97 PriorityQueue shared_priority_queue_;
98
99 // All worker threads owned by this thread pool. This is only modified by the
100 // constructor.
gab 2016/04/07 20:32:48 I don't think this last requirement "This is only
fdoray 2016/04/08 14:53:02 Done. Without this comment, I have to hold a lock
gab 2016/04/08 17:56:00 Good point, but I think that's the right thing to
fdoray 2016/04/08 19:00:05 Ack.
101 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_;
102
103 // Synchronizes access to |idle_worker_threads_stack_| and
104 // |idle_worker_threads_cv_|.
gab 2016/04/07 20:32:48 Add a comment explaining why it takes the shared P
fdoray 2016/04/08 14:53:03 Done.
105 SchedulerLock idle_worker_threads_stack_lock_;
106
107 // Stack of idle worker threads. The last worker thread that became idle is on
108 // top of the stack.
gab 2016/04/07 20:32:48 rm "The last worker thread that became idle is top
fdoray 2016/04/08 14:53:03 Done.
109 std::stack<SchedulerWorkerThread*> idle_worker_threads_stack_;
110
111 // Signaled when all worker threads become idle.
112 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_;
gab 2016/04/07 20:32:48 Should we brand this with "for_testing"? I was con
danakj 2016/04/07 23:08:43 +1
fdoray 2016/04/08 14:53:03 Done.
113
114 // Synchronizes access to |join_for_testing_returned_|.
115 SchedulerLock join_for_testing_returned_lock_;
116
117 // True once JoinForTesting() has returned.
118 bool join_for_testing_returned_ = false;
119
120 const RanTaskFromSequenceCallback ran_task_from_sequence_callback_;
121 TaskTracker* const task_tracker_;
122
123 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool);
124 };
125
126 // Helper for posting |task| to the provided |sequence| and |priority_queue|
127 // conditional on |task_tracker|. Returns true if |task| is posted. Exposed for
128 // testing.
129 bool BASE_EXPORT PostTaskHelper(std::unique_ptr<Task> task,
130 scoped_refptr<Sequence> sequence,
131 PriorityQueue* priority_queue,
132 TaskTracker* task_tracker);
133
134 } // namespace internal
135 } // namespace base
136
137 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698