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