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

Unified Diff: base/task_scheduler/thread_pool.h

Issue 1685423002: Task Scheduler. (Closed) Base URL: https://luckyluke-private.googlesource.com/src@a_master
Patch Set: Created 4 years, 10 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 side-by-side diff with in-line comments
Download patch
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..e6066c7de3a2266bf2f5a5e8f81d80986195e07f
--- /dev/null
+++ b/base/task_scheduler/thread_pool.h
@@ -0,0 +1,125 @@
+// 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/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 task_scheduler {
+
+class SequenceSortKey;
+class ShutdownManager;
+
+// A pool of threads that run tasks. Unless otherwise noted, all public methods
+// of this class are 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);
+
+ // Waits until all threads have exited. The Shutdown() method of
+ // |shutdown_manager_| must have been called before this is called, otherwise
+ // the threads won't exit. This method is not thread-safe.
fdoray 2016/02/11 17:30:33 "This method can only be called once per ThreadPoo
fdoray 2016/02/12 04:16:20 Done.
+ void JoinAllThreadsForTesting();
+
+ 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();
+
+ // 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 task_scheduler
+} // namespace base
+
+#endif // BASE_TASK_SCHEDULER_THREAD_POOL_H_

Powered by Google App Engine
This is Rietveld 408576698