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> |
Mathieu Chartier | c6068c7 | 2018-11-13 16:00:58 -0800 | [diff] [blame] | 21 | #include <functional> |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 24 | #include "barrier.h" |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 25 | #include "base/mem_map.h" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 26 | #include "base/mutex.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 | |
Ian Rogers | 14317f0 | 2014-12-03 10:48:05 -0800 | [diff] [blame] | 32 | class Closure { |
| 33 | public: |
| 34 | virtual ~Closure() { } |
| 35 | virtual void Run(Thread* self) = 0; |
| 36 | }; |
| 37 | |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 38 | class FunctionClosure : public Closure { |
| 39 | public: |
| 40 | explicit FunctionClosure(std::function<void(Thread*)>&& f) : func_(std::move(f)) {} |
| 41 | void Run(Thread* self) override { |
| 42 | func_(self); |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | std::function<void(Thread*)> func_; |
| 47 | }; |
| 48 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 49 | class Task : public Closure { |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 50 | public: |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 51 | // Called after Closure::Run has been called. |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 52 | virtual void Finalize() { } |
| 53 | }; |
| 54 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 55 | class SelfDeletingTask : public Task { |
| 56 | public: |
| 57 | virtual ~SelfDeletingTask() { } |
| 58 | virtual void Finalize() { |
| 59 | delete this; |
| 60 | } |
| 61 | }; |
| 62 | |
Mathieu Chartier | c6068c7 | 2018-11-13 16:00:58 -0800 | [diff] [blame] | 63 | class FunctionTask : public SelfDeletingTask { |
| 64 | public: |
| 65 | explicit FunctionTask(std::function<void(Thread*)>&& func) : func_(std::move(func)) {} |
| 66 | |
| 67 | void Run(Thread* self) override { |
| 68 | func_(self); |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | std::function<void(Thread*)> func_; |
| 73 | }; |
| 74 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 75 | class ThreadPoolWorker { |
| 76 | public: |
| 77 | static const size_t kDefaultStackSize = 1 * MB; |
| 78 | |
| 79 | size_t GetStackSize() const { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 80 | DCHECK(stack_.IsValid()); |
| 81 | return stack_.Size(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | virtual ~ThreadPoolWorker(); |
| 85 | |
Andreas Gampe | e701f08 | 2016-02-29 20:49:38 -0800 | [diff] [blame] | 86 | // Set the "nice" priorty for this worker. |
| 87 | void SetPthreadPriority(int priority); |
| 88 | |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 89 | Thread* GetThread() const { return thread_; } |
| 90 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 91 | protected: |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 92 | ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 93 | static void* Callback(void* arg) REQUIRES(!Locks::mutator_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 94 | virtual void Run(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 95 | |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 96 | ThreadPool* const thread_pool_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 97 | const std::string name_; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 98 | MemMap stack_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 99 | pthread_t pthread_; |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 100 | Thread* thread_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 101 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 102 | private: |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 103 | friend class ThreadPool; |
| 104 | DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker); |
| 105 | }; |
| 106 | |
Calin Juravle | ccd5695 | 2016-12-15 17:57:38 +0000 | [diff] [blame] | 107 | // Note that thread pool workers will set Thread#setCanCallIntoJava to false. |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 108 | class ThreadPool { |
| 109 | public: |
| 110 | // Returns the number of threads in the thread pool. |
| 111 | size_t GetThreadCount() const { |
| 112 | return threads_.size(); |
| 113 | } |
| 114 | |
Mathieu Chartier | 93c21ba | 2018-12-10 13:08:30 -0800 | [diff] [blame] | 115 | const std::vector<ThreadPoolWorker*>& GetWorkers(); |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 116 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 117 | // 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] | 118 | void StartWorkers(Thread* self) REQUIRES(!task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 119 | |
| 120 | // Do not allow workers to grab any new tasks. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 121 | void StopWorkers(Thread* self) REQUIRES(!task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 122 | |
| 123 | // Add a new task, the first available started worker will process it. Does not delete the task |
| 124 | // after running it, it is the caller's responsibility. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 125 | void AddTask(Thread* self, Task* task) REQUIRES(!task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 126 | |
Nicolas Geoffray | 629e935 | 2015-11-04 17:22:16 +0000 | [diff] [blame] | 127 | // Remove all tasks in the queue. |
| 128 | void RemoveAllTasks(Thread* self) REQUIRES(!task_queue_lock_); |
| 129 | |
Andreas Gampe | b15de0c | 2017-01-24 13:12:19 -0800 | [diff] [blame] | 130 | // Create a named thread pool with the given number of threads. |
| 131 | // |
| 132 | // If create_peers is true, all worker threads will have a Java peer object. Note that if the |
| 133 | // pool is asked to do work on the current thread (see Wait), a peer may not be available. Wait |
| 134 | // will conservatively abort if create_peers and do_work are true. |
Mathieu Chartier | eac4d6a | 2018-12-05 12:33:46 -0800 | [diff] [blame] | 135 | ThreadPool(const char* name, |
| 136 | size_t num_threads, |
| 137 | bool create_peers = false, |
| 138 | size_t worker_stack_size = ThreadPoolWorker::kDefaultStackSize); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 139 | virtual ~ThreadPool(); |
| 140 | |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 141 | // Create the threads of this pool. |
| 142 | void CreateThreads(); |
| 143 | |
| 144 | // Stops and deletes all threads in this pool. |
| 145 | void DeleteThreads(); |
| 146 | |
Andreas Gampe | 6f3a70f | 2016-11-16 13:58:05 -0800 | [diff] [blame] | 147 | // Wait for all tasks currently on queue to get completed. If the pool has been stopped, only |
| 148 | // wait till all already running tasks are done. |
Andreas Gampe | b15de0c | 2017-01-24 13:12:19 -0800 | [diff] [blame] | 149 | // When the pool was created with peers for workers, do_work must not be true (see ThreadPool()). |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 150 | 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] | 151 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 152 | size_t GetTaskCount(Thread* self) REQUIRES(!task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 153 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 154 | // Returns the total amount of workers waited for tasks. |
| 155 | uint64_t GetWaitTime() const { |
| 156 | return total_wait_time_; |
| 157 | } |
| 158 | |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 159 | // Provides a way to bound the maximum number of worker threads, threads must be less the the |
| 160 | // thread count of the thread pool. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 161 | void SetMaxActiveWorkers(size_t threads) REQUIRES(!task_queue_lock_); |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 162 | |
Andreas Gampe | e701f08 | 2016-02-29 20:49:38 -0800 | [diff] [blame] | 163 | // Set the "nice" priorty for threads in the pool. |
| 164 | void SetPthreadPriority(int priority); |
| 165 | |
Mathieu Chartier | 93c21ba | 2018-12-10 13:08:30 -0800 | [diff] [blame] | 166 | // Wait for workers to be created. |
| 167 | void WaitForWorkersToBeCreated(); |
| 168 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 169 | protected: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 170 | // get a task to run, blocks if there are no tasks left |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 171 | virtual Task* GetTask(Thread* self) REQUIRES(!task_queue_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 172 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 173 | // Try to get a task, returning null if there is none available. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 174 | Task* TryGetTask(Thread* self) REQUIRES(!task_queue_lock_); |
| 175 | Task* TryGetTaskLocked() REQUIRES(task_queue_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 176 | |
| 177 | // Are we shutting down? |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 178 | bool IsShuttingDown() const REQUIRES(task_queue_lock_) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 179 | return shutting_down_; |
| 180 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 181 | |
Andreas Gampe | 6f3a70f | 2016-11-16 13:58:05 -0800 | [diff] [blame] | 182 | bool HasOutstandingTasks() const REQUIRES(task_queue_lock_) { |
| 183 | return started_ && !tasks_.empty(); |
| 184 | } |
| 185 | |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 186 | const std::string name_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 187 | Mutex task_queue_lock_; |
| 188 | ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_); |
| 189 | ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_); |
| 190 | volatile bool started_ GUARDED_BY(task_queue_lock_); |
| 191 | volatile bool shutting_down_ GUARDED_BY(task_queue_lock_); |
| 192 | // How many worker threads are waiting on the condition. |
| 193 | volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 194 | std::deque<Task*> tasks_ GUARDED_BY(task_queue_lock_); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 195 | std::vector<ThreadPoolWorker*> threads_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 196 | // Work balance detection. |
| 197 | uint64_t start_time_ GUARDED_BY(task_queue_lock_); |
| 198 | uint64_t total_wait_time_; |
Mathieu Chartier | 35883cc | 2012-11-13 14:08:12 -0800 | [diff] [blame] | 199 | Barrier creation_barier_; |
Mathieu Chartier | 2775ee4 | 2013-08-20 17:43:47 -0700 | [diff] [blame] | 200 | size_t max_active_workers_ GUARDED_BY(task_queue_lock_); |
Andreas Gampe | b15de0c | 2017-01-24 13:12:19 -0800 | [diff] [blame] | 201 | const bool create_peers_; |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 202 | const size_t worker_stack_size_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 203 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 204 | private: |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 205 | friend class ThreadPoolWorker; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 206 | friend class WorkStealingWorker; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 207 | DISALLOW_COPY_AND_ASSIGN(ThreadPool); |
| 208 | }; |
| 209 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 210 | } // namespace art |
| 211 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 212 | #endif // ART_RUNTIME_THREAD_POOL_H_ |