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/threading/sequenced_worker_pool.h

Issue 1423773003: Add SequencedTaskRunnerHandle to get a SequencedTaskRunner for the current thread / sequence. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comment Created 5 years, 1 month 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_THREADING_SEQUENCED_WORKER_POOL_H_ 5 #ifndef BASE_THREADING_SEQUENCED_WORKER_POOL_H_
6 #define BASE_THREADING_SEQUENCED_WORKER_POOL_H_ 6 #define BASE_THREADING_SEQUENCED_WORKER_POOL_H_
7 7
8 #include <cstddef> 8 #include <cstddef>
9 #include <string> 9 #include <string>
10 10
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 // pool. 123 // pool.
124 class SequenceToken { 124 class SequenceToken {
125 public: 125 public:
126 SequenceToken() : id_(0) {} 126 SequenceToken() : id_(0) {}
127 ~SequenceToken() {} 127 ~SequenceToken() {}
128 128
129 bool Equals(const SequenceToken& other) const { 129 bool Equals(const SequenceToken& other) const {
130 return id_ == other.id_; 130 return id_ == other.id_;
131 } 131 }
132 132
133 bool operator==(const SequenceToken& other) const {
danakj 2015/10/27 20:03:20 Why do we need this?
Bernhard Bauer 2015/10/28 13:36:28 {DCHECK,EXPECT,ASSERT}_EQ use the equality operato
danakj 2015/10/28 18:08:15 I might agree if the operator was only compiled in
Bernhard Bauer 2015/10/28 20:38:13 Ok. Turns out the easiest thing is to just compare
134 return Equals(other);
135 }
136
133 // Returns false if current thread is executing an unsequenced task. 137 // Returns false if current thread is executing an unsequenced task.
134 bool IsValid() const { 138 bool IsValid() const {
135 return id_ != 0; 139 return id_ != 0;
136 } 140 }
137 141
142 // Returns a string representation of this token. This method should only be
143 // used for debugging.
144 std::string ToString() const;
145
138 private: 146 private:
139 friend class SequencedWorkerPool; 147 friend class SequencedWorkerPool;
140 148
141 explicit SequenceToken(int id) : id_(id) {} 149 explicit SequenceToken(int id) : id_(id) {}
142 150
143 int id_; 151 int id_;
144 }; 152 };
145 153
146 // Allows tests to perform certain actions. 154 // Allows tests to perform certain actions.
147 class TestingObserver { 155 class TestingObserver {
148 public: 156 public:
149 virtual ~TestingObserver() {} 157 virtual ~TestingObserver() {}
150 virtual void OnHasWork() = 0; 158 virtual void OnHasWork() = 0;
151 virtual void WillWaitForShutdown() = 0; 159 virtual void WillWaitForShutdown() = 0;
152 virtual void OnDestruct() = 0; 160 virtual void OnDestruct() = 0;
153 }; 161 };
154 162
155 // Gets the SequencedToken of the current thread. 163 // Gets the SequencedToken of the current thread.
156 // If current thread is not a SequencedWorkerPool worker thread or is running 164 // If current thread is not a SequencedWorkerPool worker thread or is running
157 // an unsequenced task, returns an invalid SequenceToken. 165 // an unsequenced task, returns an invalid SequenceToken.
158 static SequenceToken GetSequenceTokenForCurrentThread(); 166 static SequenceToken GetSequenceTokenForCurrentThread();
danakj 2015/10/27 20:03:20 I think this changes the behaviour of this method
Bernhard Bauer 2015/10/28 13:36:28 Uh, possibly 😃 Basically, the value that is stored
danakj 2015/10/28 18:08:15 Ah, thanks :)
159 167
168 // Returns the SequencedWorkerPool that owns this thread, or null if the
169 // current thread is not a SequencedWorkerPool worker thread.
170 static scoped_refptr<SequencedWorkerPool> GetWorkerPoolForCurrentThread();
171
160 // When constructing a SequencedWorkerPool, there must be a 172 // When constructing a SequencedWorkerPool, there must be a
161 // MessageLoop on the current thread unless you plan to deliberately 173 // ThreadTaskRunnerHandle on the current thread unless you plan to
162 // leak it. 174 // deliberately leak it.
163 175
164 // Pass the maximum number of threads (they will be lazily created as needed) 176 // Pass the maximum number of threads (they will be lazily created as
danakj 2015/10/27 20:03:20 you can let comments wrap at 80
Bernhard Bauer 2015/10/28 13:36:28 Done.
165 // and a prefix for the thread name to aid in debugging. 177 // needed) and a prefix for the thread name to aid in debugging.
166 SequencedWorkerPool(size_t max_threads, 178 SequencedWorkerPool(size_t max_threads,
167 const std::string& thread_name_prefix); 179 const std::string& thread_name_prefix);
168 180
169 // Like above, but with |observer| for testing. Does not take 181 // Like above, but with |observer| for testing. Does not take
170 // ownership of |observer|. 182 // ownership of |observer|.
171 SequencedWorkerPool(size_t max_threads, 183 SequencedWorkerPool(size_t max_threads,
172 const std::string& thread_name_prefix, 184 const std::string& thread_name_prefix,
173 TestingObserver* observer); 185 TestingObserver* observer);
174 186
175 // Returns a unique token that can be used to sequence tasks posted to 187 // Returns a unique token that can be used to sequence tasks posted to
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 360
349 const scoped_refptr<SingleThreadTaskRunner> constructor_task_runner_; 361 const scoped_refptr<SingleThreadTaskRunner> constructor_task_runner_;
350 362
351 // Avoid pulling in too many headers by putting (almost) everything 363 // Avoid pulling in too many headers by putting (almost) everything
352 // into |inner_|. 364 // into |inner_|.
353 const scoped_ptr<Inner> inner_; 365 const scoped_ptr<Inner> inner_;
354 366
355 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); 367 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool);
356 }; 368 };
357 369
370 // Stream operator to allow using a SequenceToken in a DCHECK or testing
371 // assertion.
372 BASE_EXPORT std::ostream& operator<<(
danakj 2015/10/27 20:03:20 Do we need this?
Bernhard Bauer 2015/10/28 13:36:28 Same as above, this is handy for tests and DCHECKs
danakj 2015/10/28 18:08:15 Similar comment. Can you define this only in the t
373 std::ostream& out,
374 const SequencedWorkerPool::SequenceToken& token);
375
358 } // namespace base 376 } // namespace base
359 377
360 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ 378 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698