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

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: export SequenceToken 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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 // writing a preference file. 114 // writing a preference file.
115 // 115 //
116 // If a task is posted during shutdown, it will not get run since the 116 // If a task is posted during shutdown, it will not get run since the
117 // workers may already be stopped. In this case, the post operation will 117 // workers may already be stopped. In this case, the post operation will
118 // fail (return false) and the task will be deleted. 118 // fail (return false) and the task will be deleted.
119 BLOCK_SHUTDOWN, 119 BLOCK_SHUTDOWN,
120 }; 120 };
121 121
122 // Opaque identifier that defines sequencing of tasks posted to the worker 122 // Opaque identifier that defines sequencing of tasks posted to the worker
123 // pool. 123 // pool.
124 class SequenceToken { 124 class BASE_EXPORT 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 // Returns false if current thread is executing an unsequenced task. 133 // Returns false if current thread is executing an unsequenced task.
134 bool IsValid() const { 134 bool IsValid() const {
135 return id_ != 0; 135 return id_ != 0;
136 } 136 }
137 137
138 // Returns a string representation of this token. This method should only be
139 // used for debugging.
140 std::string ToString() const;
141
138 private: 142 private:
139 friend class SequencedWorkerPool; 143 friend class SequencedWorkerPool;
140 144
141 explicit SequenceToken(int id) : id_(id) {} 145 explicit SequenceToken(int id) : id_(id) {}
142 146
143 int id_; 147 int id_;
144 }; 148 };
145 149
146 // Allows tests to perform certain actions. 150 // Allows tests to perform certain actions.
147 class TestingObserver { 151 class TestingObserver {
148 public: 152 public:
149 virtual ~TestingObserver() {} 153 virtual ~TestingObserver() {}
150 virtual void OnHasWork() = 0; 154 virtual void OnHasWork() = 0;
151 virtual void WillWaitForShutdown() = 0; 155 virtual void WillWaitForShutdown() = 0;
152 virtual void OnDestruct() = 0; 156 virtual void OnDestruct() = 0;
153 }; 157 };
154 158
155 // Gets the SequencedToken of the current thread. 159 // Gets the SequencedToken of the current thread.
156 // If current thread is not a SequencedWorkerPool worker thread or is running 160 // If current thread is not a SequencedWorkerPool worker thread or is running
157 // an unsequenced task, returns an invalid SequenceToken. 161 // an unsequenced task, returns an invalid SequenceToken.
158 static SequenceToken GetSequenceTokenForCurrentThread(); 162 static SequenceToken GetSequenceTokenForCurrentThread();
159 163
164 // Returns the SequencedWorkerPool that owns this thread, or null if the
165 // current thread is not a SequencedWorkerPool worker thread.
166 static scoped_refptr<SequencedWorkerPool> GetWorkerPoolForCurrentThread();
167
160 // When constructing a SequencedWorkerPool, there must be a 168 // When constructing a SequencedWorkerPool, there must be a
161 // MessageLoop on the current thread unless you plan to deliberately 169 // ThreadTaskRunnerHandle on the current thread unless you plan to
162 // leak it. 170 // deliberately leak it.
163 171
164 // Pass the maximum number of threads (they will be lazily created as needed) 172 // Pass the maximum number of threads (they will be lazily created as needed)
165 // and a prefix for the thread name to aid in debugging. 173 // and a prefix for the thread name to aid in debugging.
166 SequencedWorkerPool(size_t max_threads, 174 SequencedWorkerPool(size_t max_threads,
167 const std::string& thread_name_prefix); 175 const std::string& thread_name_prefix);
168 176
169 // Like above, but with |observer| for testing. Does not take 177 // Like above, but with |observer| for testing. Does not take ownership of
170 // ownership of |observer|. 178 // |observer|.
171 SequencedWorkerPool(size_t max_threads, 179 SequencedWorkerPool(size_t max_threads,
172 const std::string& thread_name_prefix, 180 const std::string& thread_name_prefix,
173 TestingObserver* observer); 181 TestingObserver* observer);
174 182
175 // Returns a unique token that can be used to sequence tasks posted to 183 // Returns a unique token that can be used to sequence tasks posted to
176 // PostSequencedWorkerTask(). Valid tokens are always nonzero. 184 // PostSequencedWorkerTask(). Valid tokens are always nonzero.
177 SequenceToken GetSequenceToken(); 185 SequenceToken GetSequenceToken();
178 186
179 // Returns the sequence token associated with the given name. Calling this 187 // Returns the sequence token associated with the given name. Calling this
180 // function multiple times with the same string will always produce the 188 // function multiple times with the same string will always produce the
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 // Avoid pulling in too many headers by putting (almost) everything 359 // Avoid pulling in too many headers by putting (almost) everything
352 // into |inner_|. 360 // into |inner_|.
353 const scoped_ptr<Inner> inner_; 361 const scoped_ptr<Inner> inner_;
354 362
355 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); 363 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool);
356 }; 364 };
357 365
358 } // namespace base 366 } // namespace base
359 367
360 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ 368 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698