| Index: base/task_scheduler/thread_pool.h
|
| diff --git a/base/task_scheduler/thread_pool.h b/base/task_scheduler/thread_pool.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..37243607d139a77f4f56dcc750eddca375a4ac3b
|
| --- /dev/null
|
| +++ b/base/task_scheduler/thread_pool.h
|
| @@ -0,0 +1,127 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#ifndef BASE_TASK_SCHEDULER_THREAD_POOL_H_
|
| +#define BASE_TASK_SCHEDULER_THREAD_POOL_H_
|
| +
|
| +#include <set>
|
| +#include <stack>
|
| +#include <vector>
|
| +
|
| +#include "base/base_export.h"
|
| +#include "base/macros.h"
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "base/synchronization/waitable_event.h"
|
| +#include "base/task_scheduler/delayed_task_manager.h"
|
| +#include "base/task_scheduler/priority_queue.h"
|
| +#include "base/task_scheduler/scheduler_lock.h"
|
| +#include "base/task_scheduler/sequence.h"
|
| +#include "base/task_scheduler/worker_thread.h"
|
| +#include "base/threading/platform_thread.h"
|
| +#include "base/threading/thread_local.h"
|
| +
|
| +namespace base {
|
| +struct TaskTraits;
|
| +} // namespace base
|
| +
|
| +namespace base {
|
| +namespace internal {
|
| +
|
| +class SequenceSortKey;
|
| +class ShutdownManager;
|
| +
|
| +// A pool of threads that run tasks. This class is thread-safe.
|
| +class BASE_EXPORT ThreadPool {
|
| + public:
|
| + ~ThreadPool();
|
| +
|
| + // Creates a pool with |num_threads| threads of priority |thread_priority|.
|
| + // |reinsert_sequence_callback| is invoked to reinsert a sequence in the
|
| + // appropriate priority queue after one of its tasks has been executed.
|
| + // |shutdown_manager| is used to handle shutdown behavior of tasks. Returns
|
| + // nullptr if it wasn't possible to create at least 1 thread.
|
| + static scoped_ptr<ThreadPool> CreateThreadPool(
|
| + ThreadPriority thread_priority,
|
| + size_t num_threads,
|
| + const WorkerThread::ReinsertSequenceCallback& reinsert_sequence_callback,
|
| + ShutdownManager* shutdown_manager);
|
| +
|
| + // Returns the number of threads in the pool. This can be less than the number
|
| + // of threads requested when the pool was created.
|
| + size_t GetNumThreads() const;
|
| +
|
| + // Returns a TaskRunner whose PostTask invocations will result in scheduling
|
| + // tasks within this thread pool with traits |traits| and execution mode
|
| + // |execution_mode|.
|
| + scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
|
| + const TaskTraits& traits,
|
| + ExecutionMode execution_mode);
|
| +
|
| + // Reinserts |sequence| in the priority queue of this thread pool with sort
|
| + // key |sequence_sort_key| after one of its tasks has been executed. Note that
|
| + // the task could have been executed by a worker thread from this pool or from
|
| + // another pool.
|
| + void ReinsertSequence(scoped_refptr<Sequence> sequence,
|
| + const SequenceSortKey& sequence_sort_key,
|
| + const WorkerThread* worker_thread);
|
| +
|
| + // Initiates shutdown and waits until all threads have exited. This method
|
| + // must not be called more than once in the lifetime of the ThreadPool.
|
| + void ShutdownAndJoinAllThreadsForTesting();
|
| +
|
| + private:
|
| + ThreadPool(
|
| + ThreadPriority thread_priority,
|
| + size_t num_threads,
|
| + const WorkerThread::ReinsertSequenceCallback& reinsert_sequence_callback,
|
| + ShutdownManager* shutdown_manager);
|
| +
|
| + // Invoked by |worker_thread| when it becomes idle. |worker_thread| has to
|
| + // belong to this thread pool.
|
| + void WorkerThreadBecomesIdleCallback(WorkerThread* worker_thread);
|
| +
|
| + // Wakes up 1 thread from the pool if not all threads are busy.
|
| + void WakeUpOneThread();
|
| +
|
| + // Callback invoked when a sequence is inserted in |priority_queue_|.
|
| + void OnSequenceInsertedInPriorityQueue();
|
| +
|
| + // Signaled when the threadpool data structures are ready.
|
| + WaitableEvent thread_pool_ready_;
|
| +
|
| + // Priority queue from which all worker threads of this pool get work.
|
| + PriorityQueue priority_queue_;
|
| +
|
| + // All the worker threads owned by this thread pool.
|
| + std::vector<scoped_ptr<WorkerThread>> worker_threads_;
|
| +
|
| + // Lock protecting |idle_worker_threads_stack_| and |idle_threads_set_|.
|
| + SchedulerLock idle_worker_threads_lock_;
|
| +
|
| + // Stack of possibly idle worker threads. The last worker thread that became
|
| + // idle is on top of the stack. When a thread is woken up to execute single-
|
| + // threaded tasks, it is not removed from this stack. That means that a thread
|
| + // can be on this stack without actually being idle.
|
| + std::stack<WorkerThread*> idle_worker_threads_stack_;
|
| +
|
| + // Set of worker threads that are in |idle_worker_threads_stack_|.
|
| + std::set<WorkerThread*> idle_worker_threads_set_;
|
| +
|
| + // Indicates that no worker thread should be woken up when a sequence is
|
| + // inserted in |priority_queue_| by a given thread.
|
| + ThreadLocalBoolean disable_wake_up_thread_on_sequence_insertion_;
|
| +
|
| + ShutdownManager* const shutdown_manager_;
|
| +
|
| + // Manages delayed tasks posted to this thread pool.
|
| + DelayedTaskManager delayed_task_manager_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(ThreadPool);
|
| +};
|
| +
|
| +} // namespace internal
|
| +} // namespace base
|
| +
|
| +#endif // BASE_TASK_SCHEDULER_THREAD_POOL_H_
|
|
|