Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_THREAD_POOL_H_ |
| 18 | #define ART_RUNTIME_THREAD_POOL_H_ |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include <deque> |
| 21 | #include <vector> |
| 22 | |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 23 | #include "barrier.h" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 24 | #include "base/mutex.h" |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 25 | #include "closure.h" |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 26 | #include "locks.h" |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 30 | class ThreadPool; |
| 31 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 32 | class Task : public Closure { |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 33 | public: |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 34 | // Called when references reaches 0. |
| 35 | virtual void Finalize() { } |
| 36 | }; |
| 37 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 38 | class ThreadPoolWorker { |
| 39 | public: |
| 40 | static const size_t kDefaultStackSize = 1 * MB; |
| 41 | |
| 42 | size_t GetStackSize() const { |
| 43 | return stack_size_; |
| 44 | } |
| 45 | |
| 46 | virtual ~ThreadPoolWorker(); |
| 47 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 48 | protected: |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 49 | ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size); |
| 50 | static void* Callback(void* arg) LOCKS_EXCLUDED(Locks::mutator_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 51 | virtual void Run(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 52 | |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 53 | ThreadPool* const thread_pool_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 54 | const std::string name_; |
| 55 | const size_t stack_size_; |
| 56 | pthread_t pthread_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 57 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 58 | private: |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 59 | friend class ThreadPool; |
| 60 | DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker); |
| 61 | }; |
| 62 | |
| 63 | class ThreadPool { |
| 64 | public: |
| 65 | // Returns the number of threads in the thread pool. |
| 66 | size_t GetThreadCount() const { |
| 67 | return threads_.size(); |
| 68 | } |
| 69 | |
| 70 | // Broadcast to the workers and tell them to empty out the work queue. |
| 71 | void StartWorkers(Thread* self); |
| 72 | |
| 73 | // Do not allow workers to grab any new tasks. |
| 74 | void StopWorkers(Thread* self); |
| 75 | |
| 76 | // Add a new task, the first available started worker will process it. Does not delete the task |
| 77 | // after running it, it is the caller's responsibility. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 78 | void AddTask(Thread* self, Task* task); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 79 | |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 80 | explicit ThreadPool(size_t num_threads); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 81 | virtual ~ThreadPool(); |
| 82 | |
| 83 | // Wait for all tasks currently on queue to get completed. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 84 | void Wait(Thread* self, bool do_work, bool may_hold_locks); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 85 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 86 | size_t GetTaskCount(Thread* self); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 87 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 88 | // Returns the total amount of workers waited for tasks. |
| 89 | uint64_t GetWaitTime() const { |
| 90 | return total_wait_time_; |
| 91 | } |
| 92 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 93 | // Provides a way to bound the maximum number of worker threads, threads must be less the the |
| 94 | // thread count of the thread pool. |
| 95 | void SetMaxActiveWorkers(size_t threads); |
| 96 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 97 | protected: |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 98 | // Get a task to run, blocks if there are no tasks left |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 99 | virtual Task* GetTask(Thread* self); |
| 100 | |
| 101 | // Try to get a task, returning NULL if there is none available. |
| 102 | Task* TryGetTask(Thread* self); |
| 103 | Task* TryGetTaskLocked(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(task_queue_lock_); |
| 104 | |
| 105 | // Are we shutting down? |
| 106 | bool IsShuttingDown() const EXCLUSIVE_LOCKS_REQUIRED(task_queue_lock_) { |
| 107 | return shutting_down_; |
| 108 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 109 | |
| 110 | Mutex task_queue_lock_; |
| 111 | ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_); |
| 112 | ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_); |
| 113 | volatile bool started_ GUARDED_BY(task_queue_lock_); |
| 114 | volatile bool shutting_down_ GUARDED_BY(task_queue_lock_); |
| 115 | // How many worker threads are waiting on the condition. |
| 116 | volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 117 | std::deque<Task*> tasks_ GUARDED_BY(task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 118 | // TODO: make this immutable/const? |
| 119 | std::vector<ThreadPoolWorker*> threads_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 120 | // Work balance detection. |
| 121 | uint64_t start_time_ GUARDED_BY(task_queue_lock_); |
| 122 | uint64_t total_wait_time_; |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 123 | Barrier creation_barier_; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 124 | size_t max_active_workers_ GUARDED_BY(task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 125 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 126 | private: |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 127 | friend class ThreadPoolWorker; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 128 | friend class WorkStealingWorker; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 129 | DISALLOW_COPY_AND_ASSIGN(ThreadPool); |
| 130 | }; |
| 131 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 132 | class WorkStealingTask : public Task { |
| 133 | public: |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 134 | WorkStealingTask() : ref_count_(0) {} |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 135 | |
| 136 | size_t GetRefCount() const { |
| 137 | return ref_count_; |
| 138 | } |
| 139 | |
| 140 | virtual void StealFrom(Thread* self, WorkStealingTask* source) = 0; |
| 141 | |
| 142 | private: |
| 143 | // How many people are referencing this task. |
| 144 | size_t ref_count_; |
| 145 | |
| 146 | friend class WorkStealingWorker; |
| 147 | }; |
| 148 | |
| 149 | class WorkStealingWorker : public ThreadPoolWorker { |
| 150 | public: |
| 151 | virtual ~WorkStealingWorker(); |
| 152 | |
| 153 | bool IsRunningTask() const { |
| 154 | return task_ != NULL; |
| 155 | } |
| 156 | |
| 157 | protected: |
| 158 | WorkStealingTask* task_; |
| 159 | |
| 160 | WorkStealingWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size); |
| 161 | virtual void Run(); |
| 162 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 163 | private: |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 164 | friend class WorkStealingThreadPool; |
| 165 | DISALLOW_COPY_AND_ASSIGN(WorkStealingWorker); |
| 166 | }; |
| 167 | |
| 168 | class WorkStealingThreadPool : public ThreadPool { |
| 169 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 170 | explicit WorkStealingThreadPool(size_t num_threads); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 171 | virtual ~WorkStealingThreadPool(); |
| 172 | |
| 173 | private: |
| 174 | Mutex work_steal_lock_; |
| 175 | // Which thread we are stealing from (round robin). |
| 176 | size_t steal_index_; |
| 177 | |
| 178 | // Find a task to steal from |
| 179 | WorkStealingTask* FindTaskToStealFrom(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(work_steal_lock_); |
| 180 | |
| 181 | friend class WorkStealingWorker; |
| 182 | }; |
| 183 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 184 | } // namespace art |
| 185 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 186 | #endif // ART_RUNTIME_THREAD_POOL_H_ |