Index: base/task_scheduler/scheduler_thread_pool.h |
diff --git a/base/task_scheduler/scheduler_thread_pool.h b/base/task_scheduler/scheduler_thread_pool.h |
index 6cbf6f47411c2b590c7a9d085775b9146273881e..0c7e650eb1f9b83ecef4c3b4cbb5b64eaa4950dc 100644 |
--- a/base/task_scheduler/scheduler_thread_pool.h |
+++ b/base/task_scheduler/scheduler_thread_pool.h |
@@ -8,6 +8,7 @@ |
#include <stddef.h> |
#include <memory> |
+#include <unordered_map> |
#include <vector> |
#include "base/base_export.h" |
@@ -36,8 +37,8 @@ class TaskTracker; |
// A pool of threads that run Tasks. This class is thread-safe. |
class BASE_EXPORT SchedulerThreadPool : public SchedulerThreadPoolInterface { |
public: |
- // Callback invoked when a Sequence isn't empty after a worker thread pops a |
- // Task from it. |
+ // Callback invoked when a non-single-threaded Sequence isn't empty after a |
+ // worker thread pops a Task from it. |
using EnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; |
// Destroying a SchedulerThreadPool returned by CreateThreadPool() is not |
@@ -46,11 +47,11 @@ class BASE_EXPORT SchedulerThreadPool : public SchedulerThreadPoolInterface { |
~SchedulerThreadPool() override; |
// Creates a SchedulerThreadPool with up to |max_threads| threads of priority |
- // |thread_priority|. |enqueue_sequence_callback| will be invoked after a |
- // thread of this thread pool tries to run a Task. |task_tracker| is used to |
- // handle shutdown behavior of Tasks. |delayed_task_manager| handles Tasks |
- // posted with a delay. Returns nullptr on failure to create a thread pool |
- // with at least one thread. |
+ // |thread_priority|. |enqueue_sequence_callback| will be invoked when a non- |
+ // single-threaded Sequence isn't empty after a worker thread pops a Task from |
+ // it. |task_tracker| is used to handle shutdown behavior of Tasks. |
+ // |delayed_task_manager| handles Tasks posted with a delay. Returns nullptr |
+ // on failure to create a thread pool with at least one thread. |
static std::unique_ptr<SchedulerThreadPool> CreateThreadPool( |
ThreadPriority thread_priority, |
size_t max_threads, |
@@ -78,14 +79,17 @@ class BASE_EXPORT SchedulerThreadPool : public SchedulerThreadPoolInterface { |
// allowed to complete their execution. This can only be called once. |
void JoinForTesting(); |
- // Posts |task| to be executed as part of |sequence|. Returns true if |task| |
- // is posted. |
+ // Posts |task| to be executed as part of |sequence|. |task| will run on |
+ // |worker_thread| if specified; otherwise it can run on a any thread owned by |
danakj
2016/04/27 18:28:43
"on any"
fdoray
2016/04/27 19:17:04
n/a with latest patch set
|
+ // this pool. Returns true if |task| is posted. |
bool PostTaskWithSequence(std::unique_ptr<Task> task, |
- scoped_refptr<Sequence> sequence); |
+ scoped_refptr<Sequence> sequence, |
+ SchedulerWorkerThread* worker_thread); |
// SchedulerThreadPoolInterface: |
void PostTaskWithSequenceNow(std::unique_ptr<Task> task, |
- scoped_refptr<Sequence> sequence) override; |
+ scoped_refptr<Sequence> sequence, |
+ SchedulerWorkerThread* worker_thread) override; |
private: |
class SchedulerWorkerThreadDelegateImpl; |
@@ -103,17 +107,31 @@ class BASE_EXPORT SchedulerThreadPool : public SchedulerThreadPoolInterface { |
// Adds |worker_thread| to |idle_worker_threads_stack_|. |
void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
+ // Removes |worker_thread| from |idle_worker_threads_stack_|. |
+ void RemoveFromIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
+ |
// Pops one idle worker thread from |idle_worker_thread_stack_| and returns |
// it. Returns nullptr if |idle_worker_thread_stack_| is empty. |
SchedulerWorkerThread* PopOneIdleWorkerThread(); |
- // PriorityQueue from which all threads of this thread pool get work. |
- PriorityQueue shared_priority_queue_; |
- |
// All worker threads owned by this thread pool. Only modified during |
// initialization of the thread pool. |
std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; |
+ // Synchronizes access to |next_worker_thread_index_|. |
+ SchedulerLock next_worker_thread_index_lock_; |
+ |
+ // Index of the worker thread that will be assigned to the next single- |
+ // threaded TaskRunner returned by this pool. |
+ size_t next_worker_thread_index_ = 0; |
+ |
+ // PriorityQueue from which all threads of this thread pool get work. |
+ PriorityQueue shared_priority_queue_; |
+ |
+ // Single-threaded PriorityQueues. |
+ std::unordered_map<SchedulerWorkerThread*, std::unique_ptr<PriorityQueue>> |
+ single_threaded_priority_queues_; |
+ |
// Synchronizes access to |idle_worker_threads_stack_| and |
// |idle_worker_threads_stack_cv_for_testing_|. Has |shared_priority_queue_|'s |
// lock as its predecessor so that a thread can be pushed to |