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

Side by Side Diff: base/threading/sequenced_worker_pool.h

Issue 2177373005: Revert "Allow SequencedTaskRunnerHandle::Get() while running unsequenced tasks." (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge up to r408965 Created 4 years, 4 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
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 <stddef.h> 8 #include <stddef.h>
9 9
10 #include <cstddef> 10 #include <cstddef>
(...skipping 30 matching lines...) Expand all
41 // 41 //
42 // - No two tasks with the same token will run at the same time. 42 // - No two tasks with the same token will run at the same time.
43 // 43 //
44 // - Given two tasks T1 and T2 with the same token such that T2 will 44 // - Given two tasks T1 and T2 with the same token such that T2 will
45 // run after T1, then T2 will start after T1 is destroyed. 45 // run after T1, then T2 will start after T1 is destroyed.
46 // 46 //
47 // - If T2 will run after T1, then all memory changes in T1 and T1's 47 // - If T2 will run after T1, then all memory changes in T1 and T1's
48 // destruction will be visible to T2. 48 // destruction will be visible to T2.
49 // 49 //
50 // Example: 50 // Example:
51 // SequencedWorkerPool::SequenceToken token = 51 // SequencedWorkerPool::SequenceToken token = pool.GetSequenceToken();
52 // SequencedWorkerPool::GetSequenceToken();
53 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, 52 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN,
54 // FROM_HERE, base::Bind(...)); 53 // FROM_HERE, base::Bind(...));
55 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN, 54 // pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN,
56 // FROM_HERE, base::Bind(...)); 55 // FROM_HERE, base::Bind(...));
57 // 56 //
58 // You can make named sequence tokens to make it easier to share a token 57 // You can make named sequence tokens to make it easier to share a token
59 // across different components. 58 // across different components.
60 // 59 //
61 // You can also post tasks to the pool without ordering using PostWorkerTask. 60 // You can also post tasks to the pool without ordering using PostWorkerTask.
62 // These will be executed in an unspecified order. The order of execution 61 // These will be executed in an unspecified order. The order of execution
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 virtual void OnHasWork() = 0; 157 virtual void OnHasWork() = 0;
159 virtual void WillWaitForShutdown() = 0; 158 virtual void WillWaitForShutdown() = 0;
160 virtual void OnDestruct() = 0; 159 virtual void OnDestruct() = 0;
161 }; 160 };
162 161
163 // Gets the SequencedToken of the current thread. 162 // Gets the SequencedToken of the current thread.
164 // If current thread is not a SequencedWorkerPool worker thread or is running 163 // If current thread is not a SequencedWorkerPool worker thread or is running
165 // an unsequenced task, returns an invalid SequenceToken. 164 // an unsequenced task, returns an invalid SequenceToken.
166 static SequenceToken GetSequenceTokenForCurrentThread(); 165 static SequenceToken GetSequenceTokenForCurrentThread();
167 166
168 // Gets a SequencedTaskRunner for the current thread. If the current thread is
169 // running an unsequenced task, a new SequenceToken will be generated and set,
170 // so that the returned SequencedTaskRunner is guaranteed to run tasks after
171 // the current task has finished running.
172 static scoped_refptr<SequencedTaskRunner>
173 GetSequencedTaskRunnerForCurrentThread();
174
175 // Returns a unique token that can be used to sequence tasks posted to
176 // PostSequencedWorkerTask(). Valid tokens are always nonzero.
177 // TODO(bauerb): Rename this to better differentiate from
178 // GetSequenceTokenForCurrentThread().
179 static SequenceToken GetSequenceToken();
180
181 // Returns the SequencedWorkerPool that owns this thread, or null if the 167 // Returns the SequencedWorkerPool that owns this thread, or null if the
182 // current thread is not a SequencedWorkerPool worker thread. 168 // current thread is not a SequencedWorkerPool worker thread.
183 static scoped_refptr<SequencedWorkerPool> GetWorkerPoolForCurrentThread(); 169 static scoped_refptr<SequencedWorkerPool> GetWorkerPoolForCurrentThread();
184 170
171 // Returns a unique token that can be used to sequence tasks posted to
172 // PostSequencedWorkerTask(). Valid tokens are always nonzero.
173 static SequenceToken GetSequenceToken();
174
185 // When constructing a SequencedWorkerPool, there must be a 175 // When constructing a SequencedWorkerPool, there must be a
186 // ThreadTaskRunnerHandle on the current thread unless you plan to 176 // ThreadTaskRunnerHandle on the current thread unless you plan to
187 // deliberately leak it. 177 // deliberately leak it.
188 178
189 // Pass the maximum number of threads (they will be lazily created as needed) 179 // Pass the maximum number of threads (they will be lazily created as needed)
190 // and a prefix for the thread name to aid in debugging. |task_priority| will 180 // and a prefix for the thread name to aid in debugging. |task_priority| will
191 // be used to hint base::TaskScheduler for an experiment in which all 181 // be used to hint base::TaskScheduler for an experiment in which all
192 // SequencedWorkerPool tasks will be redirected to it in processes where a 182 // SequencedWorkerPool tasks will be redirected to it in processes where a
193 // base::TaskScheduler was instantiated. 183 // base::TaskScheduler was instantiated.
194 SequencedWorkerPool(size_t max_threads, 184 SequencedWorkerPool(size_t max_threads,
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 // TaskRunner implementation. Forwards to PostDelayedWorkerTask(). 316 // TaskRunner implementation. Forwards to PostDelayedWorkerTask().
327 bool PostDelayedTask(const tracked_objects::Location& from_here, 317 bool PostDelayedTask(const tracked_objects::Location& from_here,
328 const Closure& task, 318 const Closure& task,
329 TimeDelta delay) override; 319 TimeDelta delay) override;
330 bool RunsTasksOnCurrentThread() const override; 320 bool RunsTasksOnCurrentThread() const override;
331 321
332 // Returns true if the current thread is processing a task with the given 322 // Returns true if the current thread is processing a task with the given
333 // sequence_token. 323 // sequence_token.
334 bool IsRunningSequenceOnCurrentThread(SequenceToken sequence_token) const; 324 bool IsRunningSequenceOnCurrentThread(SequenceToken sequence_token) const;
335 325
336 // Returns true if any thread is currently processing a task with the given
337 // sequence token. Should only be called with a valid sequence token.
338 bool IsRunningSequence(SequenceToken sequence_token) const;
339
340 // Blocks until all pending tasks are complete. This should only be called in 326 // Blocks until all pending tasks are complete. This should only be called in
341 // unit tests when you want to validate something that should have happened. 327 // unit tests when you want to validate something that should have happened.
342 // This will not flush delayed tasks; delayed tasks get deleted. 328 // This will not flush delayed tasks; delayed tasks get deleted.
343 // 329 //
344 // Note that calling this will not prevent other threads from posting work to 330 // Note that calling this will not prevent other threads from posting work to
345 // the queue while the calling thread is waiting on Flush(). In this case, 331 // the queue while the calling thread is waiting on Flush(). In this case,
346 // Flush will return only when there's no more work in the queue. Normally, 332 // Flush will return only when there's no more work in the queue. Normally,
347 // this doesn't come up since in a test, all the work is being posted from 333 // this doesn't come up since in a test, all the work is being posted from
348 // the main thread. 334 // the main thread.
349 void FlushForTesting(); 335 void FlushForTesting();
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 // Avoid pulling in too many headers by putting (almost) everything 374 // Avoid pulling in too many headers by putting (almost) everything
389 // into |inner_|. 375 // into |inner_|.
390 const std::unique_ptr<Inner> inner_; 376 const std::unique_ptr<Inner> inner_;
391 377
392 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool); 378 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPool);
393 }; 379 };
394 380
395 } // namespace base 381 } // namespace base
396 382
397 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_ 383 #endif // BASE_THREADING_SEQUENCED_WORKER_POOL_H_
OLDNEW
« no previous file with comments | « base/threading/sequenced_task_runner_handle_unittest.cc ('k') | base/threading/sequenced_worker_pool.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698