| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ |
| 6 #define BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 // allowed in production; it is always leaked. In tests, it can only be | 44 // allowed in production; it is always leaked. In tests, it can only be |
| 45 // destroyed after JoinForTesting() has returned. | 45 // destroyed after JoinForTesting() has returned. |
| 46 ~SchedulerThreadPoolImpl() override; | 46 ~SchedulerThreadPoolImpl() override; |
| 47 | 47 |
| 48 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority | 48 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority |
| 49 // |thread_priority|. |re_enqueue_sequence_callback| will be invoked after a | 49 // |thread_priority|. |re_enqueue_sequence_callback| will be invoked after a |
| 50 // thread of this thread pool tries to run a Task. |task_tracker| is used to | 50 // thread of this thread pool tries to run a Task. |task_tracker| is used to |
| 51 // handle shutdown behavior of Tasks. |delayed_task_manager| handles Tasks | 51 // handle shutdown behavior of Tasks. |delayed_task_manager| handles Tasks |
| 52 // posted with a delay. Returns nullptr on failure to create a thread pool | 52 // posted with a delay. Returns nullptr on failure to create a thread pool |
| 53 // with at least one thread. | 53 // with at least one thread. |
| 54 static std::unique_ptr<SchedulerThreadPoolImpl> CreateThreadPool( | 54 static std::unique_ptr<SchedulerThreadPoolImpl> Create( |
| 55 ThreadPriority thread_priority, | 55 ThreadPriority thread_priority, |
| 56 size_t max_threads, | 56 size_t max_threads, |
| 57 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | 57 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
| 58 TaskTracker* task_tracker, | 58 TaskTracker* task_tracker, |
| 59 DelayedTaskManager* delayed_task_manager); | 59 DelayedTaskManager* delayed_task_manager); |
| 60 | 60 |
| 61 // Waits until all threads are idle. | 61 // Waits until all threads are idle. |
| 62 void WaitForAllWorkerThreadsIdleForTesting(); | 62 void WaitForAllWorkerThreadsIdleForTesting(); |
| 63 | 63 |
| 64 // Joins all threads of this thread pool. Tasks that are already running are | 64 // Joins all threads of this thread pool. Tasks that are already running are |
| 65 // allowed to complete their execution. This can only be called once. | 65 // allowed to complete their execution. This can only be called once. |
| 66 void JoinForTesting(); | 66 void JoinForTesting(); |
| 67 | 67 |
| 68 // SchedulerThreadPool: | 68 // SchedulerThreadPool: |
| 69 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 69 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
| 70 const TaskTraits& traits, | 70 const TaskTraits& traits, |
| 71 ExecutionMode execution_mode) override; | 71 ExecutionMode execution_mode) override; |
| 72 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, | 72 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, |
| 73 const SequenceSortKey& sequence_sort_key) override; | 73 const SequenceSortKey& sequence_sort_key) override; |
| 74 bool PostTaskWithSequence(std::unique_ptr<Task> task, | 74 bool PostTaskWithSequence(std::unique_ptr<Task> task, |
| 75 scoped_refptr<Sequence> sequence) override; | 75 scoped_refptr<Sequence> sequence) override; |
| 76 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, | 76 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, |
| 77 scoped_refptr<Sequence> sequence) override; | 77 scoped_refptr<Sequence> sequence) override; |
| 78 | 78 |
| 79 private: | 79 private: |
| 80 class SchedulerWorkerThreadDelegateImpl; | 80 class SchedulerWorkerThreadDelegateImpl; |
| 81 | 81 |
| 82 SchedulerThreadPoolImpl( | 82 SchedulerThreadPoolImpl( |
| 83 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | |
| 84 TaskTracker* task_tracker, | 83 TaskTracker* task_tracker, |
| 85 DelayedTaskManager* delayed_task_manager); | 84 DelayedTaskManager* delayed_task_manager); |
| 86 | 85 |
| 87 bool Initialize(ThreadPriority thread_priority, size_t max_threads); | 86 bool Initialize( |
| 87 ThreadPriority thread_priority, |
| 88 size_t max_threads, |
| 89 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); |
| 88 | 90 |
| 89 // Wakes up the last thread from this thread pool to go idle, if any. | 91 // Wakes up the last thread from this thread pool to go idle, if any. |
| 90 void WakeUpOneThread(); | 92 void WakeUpOneThread(); |
| 91 | 93 |
| 92 // Adds |worker_thread| to |idle_worker_threads_stack_|. | 94 // Adds |worker_thread| to |idle_worker_threads_stack_|. |
| 93 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); | 95 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
| 94 | 96 |
| 95 // PriorityQueue from which all threads of this thread pool get work. | 97 // PriorityQueue from which all threads of this thread pool get work. |
| 96 PriorityQueue shared_priority_queue_; | 98 PriorityQueue shared_priority_queue_; |
| 97 | 99 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 108 | 110 |
| 109 // Stack of idle worker threads. | 111 // Stack of idle worker threads. |
| 110 SchedulerWorkerThreadStack idle_worker_threads_stack_; | 112 SchedulerWorkerThreadStack idle_worker_threads_stack_; |
| 111 | 113 |
| 112 // Signaled when all worker threads become idle. | 114 // Signaled when all worker threads become idle. |
| 113 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; | 115 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; |
| 114 | 116 |
| 115 // Signaled once JoinForTesting() has returned. | 117 // Signaled once JoinForTesting() has returned. |
| 116 WaitableEvent join_for_testing_returned_; | 118 WaitableEvent join_for_testing_returned_; |
| 117 | 119 |
| 118 // Delegate for all worker threads in this pool. | |
| 119 std::unique_ptr<SchedulerWorkerThread::Delegate> worker_thread_delegate_; | |
| 120 | |
| 121 TaskTracker* const task_tracker_; | 120 TaskTracker* const task_tracker_; |
| 122 DelayedTaskManager* const delayed_task_manager_; | 121 DelayedTaskManager* const delayed_task_manager_; |
| 123 | 122 |
| 124 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPoolImpl); | 123 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPoolImpl); |
| 125 }; | 124 }; |
| 126 | 125 |
| 127 } // namespace internal | 126 } // namespace internal |
| 128 } // namespace base | 127 } // namespace base |
| 129 | 128 |
| 130 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ | 129 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ |
| OLD | NEW |