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 | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 25 | #include "mem_map.h" |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 29 | class ThreadPool; |
| 30 | |
Ian Rogers | 14317f0 | 2014-12-03 10:48:05 -0800 | [diff] [blame] | 31 | class Closure { |
| 32 | public: |
| 33 | virtual ~Closure() { } |
| 34 | virtual void Run(Thread* self) = 0; |
| 35 | }; |
| 36 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 37 | class Task : public Closure { |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 38 | public: |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 39 | // Called after Closure::Run has been called. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 40 | virtual void Finalize() { } |
| 41 | }; |
| 42 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 43 | class SelfDeletingTask : public Task { |
| 44 | public: |
| 45 | virtual ~SelfDeletingTask() { } |
| 46 | virtual void Finalize() { |
| 47 | delete this; |
| 48 | } |
| 49 | }; |
| 50 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 51 | class ThreadPoolWorker { |
| 52 | public: |
| 53 | static const size_t kDefaultStackSize = 1 * MB; |
| 54 | |
| 55 | size_t GetStackSize() const { |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 56 | DCHECK(stack_.get() != nullptr); |
| 57 | return stack_->Size(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | virtual ~ThreadPoolWorker(); |
| 61 | |
Andreas Gampe | e701f08 | 2016-02-29 20:49:38 -0800 | [diff] [blame] | 62 | // Set the "nice" priorty for this worker. |
| 63 | void SetPthreadPriority(int priority); |
| 64 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 65 | protected: |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 66 | ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 67 | static void* Callback(void* arg) REQUIRES(!Locks::mutator_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 68 | virtual void Run(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 69 | |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 70 | ThreadPool* const thread_pool_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 71 | const std::string name_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 72 | std::unique_ptr<MemMap> stack_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 73 | pthread_t pthread_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 74 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 75 | private: |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 76 | friend class ThreadPool; |
| 77 | DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker); |
| 78 | }; |
| 79 | |
| 80 | class ThreadPool { |
| 81 | public: |
| 82 | // Returns the number of threads in the thread pool. |
| 83 | size_t GetThreadCount() const { |
| 84 | return threads_.size(); |
| 85 | } |
| 86 | |
| 87 | // Broadcast to the workers and tell them to empty out the work queue. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 88 | void StartWorkers(Thread* self) REQUIRES(!task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 89 | |
| 90 | // Do not allow workers to grab any new tasks. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 91 | void StopWorkers(Thread* self) REQUIRES(!task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 92 | |
| 93 | // Add a new task, the first available started worker will process it. Does not delete the task |
| 94 | // after running it, it is the caller's responsibility. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 95 | void AddTask(Thread* self, Task* task) REQUIRES(!task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 96 | |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 97 | // Remove all tasks in the queue. |
| 98 | void RemoveAllTasks(Thread* self) REQUIRES(!task_queue_lock_); |
| 99 | |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 100 | ThreadPool(const char* name, size_t num_threads); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 101 | virtual ~ThreadPool(); |
| 102 | |
| 103 | // Wait for all tasks currently on queue to get completed. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 104 | void Wait(Thread* self, bool do_work, bool may_hold_locks) REQUIRES(!task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 105 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 106 | size_t GetTaskCount(Thread* self) REQUIRES(!task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 107 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 108 | // Returns the total amount of workers waited for tasks. |
| 109 | uint64_t GetWaitTime() const { |
| 110 | return total_wait_time_; |
| 111 | } |
| 112 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 113 | // Provides a way to bound the maximum number of worker threads, threads must be less the the |
| 114 | // thread count of the thread pool. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 115 | void SetMaxActiveWorkers(size_t threads) REQUIRES(!task_queue_lock_); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 116 | |
Andreas Gampe | e701f08 | 2016-02-29 20:49:38 -0800 | [diff] [blame] | 117 | // Set the "nice" priorty for threads in the pool. |
| 118 | void SetPthreadPriority(int priority); |
| 119 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 120 | protected: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 121 | // get a task to run, blocks if there are no tasks left |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 122 | virtual Task* GetTask(Thread* self) REQUIRES(!task_queue_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 123 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 124 | // Try to get a task, returning null if there is none available. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 125 | Task* TryGetTask(Thread* self) REQUIRES(!task_queue_lock_); |
| 126 | Task* TryGetTaskLocked() REQUIRES(task_queue_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 127 | |
| 128 | // Are we shutting down? |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 129 | bool IsShuttingDown() const REQUIRES(task_queue_lock_) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 130 | return shutting_down_; |
| 131 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 132 | |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 133 | const std::string name_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 134 | Mutex task_queue_lock_; |
| 135 | ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_); |
| 136 | ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_); |
| 137 | volatile bool started_ GUARDED_BY(task_queue_lock_); |
| 138 | volatile bool shutting_down_ GUARDED_BY(task_queue_lock_); |
| 139 | // How many worker threads are waiting on the condition. |
| 140 | volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 141 | std::deque<Task*> tasks_ GUARDED_BY(task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 142 | // TODO: make this immutable/const? |
| 143 | std::vector<ThreadPoolWorker*> threads_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 144 | // Work balance detection. |
| 145 | uint64_t start_time_ GUARDED_BY(task_queue_lock_); |
| 146 | uint64_t total_wait_time_; |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 147 | Barrier creation_barier_; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 148 | size_t max_active_workers_ GUARDED_BY(task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 149 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 150 | private: |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 151 | friend class ThreadPoolWorker; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 152 | friend class WorkStealingWorker; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 153 | DISALLOW_COPY_AND_ASSIGN(ThreadPool); |
| 154 | }; |
| 155 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 156 | } // namespace art |
| 157 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 158 | #endif // ART_RUNTIME_THREAD_POOL_H_ |