blob: b9e5a97cb536066b2472d86d00c754e93b78005d [file] [log] [blame]
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_THREAD_POOL_H_
18#define ART_RUNTIME_THREAD_POOL_H_
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070019
20#include <deque>
Mathieu Chartierc6068c72018-11-13 16:00:58 -080021#include <functional>
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070022#include <vector>
23
Mathieu Chartier35883cc2012-11-13 14:08:12 -080024#include "barrier.h"
David Sehr79e26072018-04-06 17:58:50 -070025#include "base/mem_map.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080026#include "base/mutex.h"
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070027
28namespace art {
29
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070030class ThreadPool;
31
Ian Rogers14317f02014-12-03 10:48:05 -080032class Closure {
33 public:
34 virtual ~Closure() { }
35 virtual void Run(Thread* self) = 0;
36};
37
Alex Lighta4cdd362019-04-18 09:17:10 -070038class 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 Chartier02b6a782012-10-26 13:51:26 -070049class Task : public Closure {
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070050 public:
Mathieu Chartiera5eae692014-12-17 17:56:03 -080051 // Called after Closure::Run has been called.
Mathieu Chartier02b6a782012-10-26 13:51:26 -070052 virtual void Finalize() { }
53};
54
Mathieu Chartiera5eae692014-12-17 17:56:03 -080055class SelfDeletingTask : public Task {
56 public:
57 virtual ~SelfDeletingTask() { }
58 virtual void Finalize() {
59 delete this;
60 }
61};
62
Mathieu Chartierc6068c72018-11-13 16:00:58 -080063class 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 Chartier0e4627e2012-10-23 16:13:36 -070075class ThreadPoolWorker {
76 public:
77 static const size_t kDefaultStackSize = 1 * MB;
78
79 size_t GetStackSize() const {
Vladimir Markoc34bebf2018-08-16 16:12:49 +010080 DCHECK(stack_.IsValid());
81 return stack_.Size();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070082 }
83
84 virtual ~ThreadPoolWorker();
85
Nicolas Geoffrayf9dbb972020-08-27 15:21:11 +010086 // Set the "nice" priority for this worker.
Andreas Gampee701f082016-02-29 20:49:38 -080087 void SetPthreadPriority(int priority);
88
Nicolas Geoffrayf9dbb972020-08-27 15:21:11 +010089 // Get the "nice" priority for this worker.
90 int GetPthreadPriority();
91
Nicolas Geoffray340dafa2016-11-18 16:03:10 +000092 Thread* GetThread() const { return thread_; }
93
Mathieu Chartier02b6a782012-10-26 13:51:26 -070094 protected:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070095 ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
Mathieu Chartier90443472015-07-16 20:32:27 -070096 static void* Callback(void* arg) REQUIRES(!Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -070097 virtual void Run();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070098
Ian Rogersd914eb22013-04-18 16:11:15 -070099 ThreadPool* const thread_pool_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700100 const std::string name_;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100101 MemMap stack_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700102 pthread_t pthread_;
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000103 Thread* thread_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700104
Mathieu Chartier02e25112013-08-14 16:14:24 -0700105 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700106 friend class ThreadPool;
107 DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker);
108};
109
Calin Juravleccd56952016-12-15 17:57:38 +0000110// Note that thread pool workers will set Thread#setCanCallIntoJava to false.
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700111class ThreadPool {
112 public:
113 // Returns the number of threads in the thread pool.
114 size_t GetThreadCount() const {
115 return threads_.size();
116 }
117
Mathieu Chartier93c21ba2018-12-10 13:08:30 -0800118 const std::vector<ThreadPoolWorker*>& GetWorkers();
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000119
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700120 // Broadcast to the workers and tell them to empty out the work queue.
Mathieu Chartier90443472015-07-16 20:32:27 -0700121 void StartWorkers(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700122
123 // Do not allow workers to grab any new tasks.
Mathieu Chartier90443472015-07-16 20:32:27 -0700124 void StopWorkers(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700125
126 // Add a new task, the first available started worker will process it. Does not delete the task
127 // after running it, it is the caller's responsibility.
Mathieu Chartier90443472015-07-16 20:32:27 -0700128 void AddTask(Thread* self, Task* task) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700129
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000130 // Remove all tasks in the queue.
131 void RemoveAllTasks(Thread* self) REQUIRES(!task_queue_lock_);
132
Andreas Gampeb15de0c2017-01-24 13:12:19 -0800133 // Create a named thread pool with the given number of threads.
134 //
135 // If create_peers is true, all worker threads will have a Java peer object. Note that if the
136 // pool is asked to do work on the current thread (see Wait), a peer may not be available. Wait
137 // will conservatively abort if create_peers and do_work are true.
Mathieu Chartiereac4d6a2018-12-05 12:33:46 -0800138 ThreadPool(const char* name,
139 size_t num_threads,
140 bool create_peers = false,
141 size_t worker_stack_size = ThreadPoolWorker::kDefaultStackSize);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700142 virtual ~ThreadPool();
143
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000144 // Create the threads of this pool.
145 void CreateThreads();
146
147 // Stops and deletes all threads in this pool.
148 void DeleteThreads();
149
Andreas Gampe6f3a70f2016-11-16 13:58:05 -0800150 // Wait for all tasks currently on queue to get completed. If the pool has been stopped, only
151 // wait till all already running tasks are done.
Andreas Gampeb15de0c2017-01-24 13:12:19 -0800152 // When the pool was created with peers for workers, do_work must not be true (see ThreadPool()).
Mathieu Chartier90443472015-07-16 20:32:27 -0700153 void Wait(Thread* self, bool do_work, bool may_hold_locks) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700154
Mathieu Chartier90443472015-07-16 20:32:27 -0700155 size_t GetTaskCount(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700156
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700157 // Returns the total amount of workers waited for tasks.
158 uint64_t GetWaitTime() const {
159 return total_wait_time_;
160 }
161
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700162 // Provides a way to bound the maximum number of worker threads, threads must be less the the
163 // thread count of the thread pool.
Mathieu Chartier90443472015-07-16 20:32:27 -0700164 void SetMaxActiveWorkers(size_t threads) REQUIRES(!task_queue_lock_);
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700165
Nicolas Geoffrayf9dbb972020-08-27 15:21:11 +0100166 // Set the "nice" priority for threads in the pool.
Andreas Gampee701f082016-02-29 20:49:38 -0800167 void SetPthreadPriority(int priority);
168
Nicolas Geoffrayf9dbb972020-08-27 15:21:11 +0100169 // CHECK that the "nice" priority of threads in the pool is the given
170 // `priority`.
171 void CheckPthreadPriority(int priority);
172
Mathieu Chartier93c21ba2018-12-10 13:08:30 -0800173 // Wait for workers to be created.
174 void WaitForWorkersToBeCreated();
175
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700176 protected:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700177 // get a task to run, blocks if there are no tasks left
Mathieu Chartier90443472015-07-16 20:32:27 -0700178 virtual Task* GetTask(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700179
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700180 // Try to get a task, returning null if there is none available.
Mathieu Chartier90443472015-07-16 20:32:27 -0700181 Task* TryGetTask(Thread* self) REQUIRES(!task_queue_lock_);
182 Task* TryGetTaskLocked() REQUIRES(task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700183
184 // Are we shutting down?
Mathieu Chartier90443472015-07-16 20:32:27 -0700185 bool IsShuttingDown() const REQUIRES(task_queue_lock_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700186 return shutting_down_;
187 }
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700188
Andreas Gampe6f3a70f2016-11-16 13:58:05 -0800189 bool HasOutstandingTasks() const REQUIRES(task_queue_lock_) {
190 return started_ && !tasks_.empty();
191 }
192
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800193 const std::string name_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700194 Mutex task_queue_lock_;
195 ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_);
196 ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_);
197 volatile bool started_ GUARDED_BY(task_queue_lock_);
198 volatile bool shutting_down_ GUARDED_BY(task_queue_lock_);
199 // How many worker threads are waiting on the condition.
200 volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700201 std::deque<Task*> tasks_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700202 std::vector<ThreadPoolWorker*> threads_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700203 // Work balance detection.
204 uint64_t start_time_ GUARDED_BY(task_queue_lock_);
205 uint64_t total_wait_time_;
Mathieu Chartier35883cc2012-11-13 14:08:12 -0800206 Barrier creation_barier_;
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700207 size_t max_active_workers_ GUARDED_BY(task_queue_lock_);
Andreas Gampeb15de0c2017-01-24 13:12:19 -0800208 const bool create_peers_;
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000209 const size_t worker_stack_size_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700210
Mathieu Chartier02e25112013-08-14 16:14:24 -0700211 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700212 friend class ThreadPoolWorker;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700213 friend class WorkStealingWorker;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700214 DISALLOW_COPY_AND_ASSIGN(ThreadPool);
215};
216
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700217} // namespace art
218
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700219#endif // ART_RUNTIME_THREAD_POOL_H_