blob: 0a2a50c9e18dcbf19e31d21c792ff350559857a2 [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
Mathieu Chartier02b6a782012-10-26 13:51:26 -070038class Task : public Closure {
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070039 public:
Mathieu Chartiera5eae692014-12-17 17:56:03 -080040 // Called after Closure::Run has been called.
Mathieu Chartier02b6a782012-10-26 13:51:26 -070041 virtual void Finalize() { }
42};
43
Mathieu Chartiera5eae692014-12-17 17:56:03 -080044class SelfDeletingTask : public Task {
45 public:
46 virtual ~SelfDeletingTask() { }
47 virtual void Finalize() {
48 delete this;
49 }
50};
51
Mathieu Chartierc6068c72018-11-13 16:00:58 -080052class 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 Chartier0e4627e2012-10-23 16:13:36 -070064class ThreadPoolWorker {
65 public:
66 static const size_t kDefaultStackSize = 1 * MB;
67
68 size_t GetStackSize() const {
Vladimir Markoc34bebf2018-08-16 16:12:49 +010069 DCHECK(stack_.IsValid());
70 return stack_.Size();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070071 }
72
73 virtual ~ThreadPoolWorker();
74
Andreas Gampee701f082016-02-29 20:49:38 -080075 // Set the "nice" priorty for this worker.
76 void SetPthreadPriority(int priority);
77
Nicolas Geoffray340dafa2016-11-18 16:03:10 +000078 Thread* GetThread() const { return thread_; }
79
Mathieu Chartier02b6a782012-10-26 13:51:26 -070080 protected:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070081 ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
Mathieu Chartier90443472015-07-16 20:32:27 -070082 static void* Callback(void* arg) REQUIRES(!Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -070083 virtual void Run();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070084
Ian Rogersd914eb22013-04-18 16:11:15 -070085 ThreadPool* const thread_pool_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070086 const std::string name_;
Vladimir Markoc34bebf2018-08-16 16:12:49 +010087 MemMap stack_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070088 pthread_t pthread_;
Nicolas Geoffray340dafa2016-11-18 16:03:10 +000089 Thread* thread_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070090
Mathieu Chartier02e25112013-08-14 16:14:24 -070091 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070092 friend class ThreadPool;
93 DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker);
94};
95
Calin Juravleccd56952016-12-15 17:57:38 +000096// Note that thread pool workers will set Thread#setCanCallIntoJava to false.
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070097class 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 Chartier93c21ba2018-12-10 13:08:30 -0800104 const std::vector<ThreadPoolWorker*>& GetWorkers();
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000105
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700106 // Broadcast to the workers and tell them to empty out the work queue.
Mathieu Chartier90443472015-07-16 20:32:27 -0700107 void StartWorkers(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700108
109 // Do not allow workers to grab any new tasks.
Mathieu Chartier90443472015-07-16 20:32:27 -0700110 void StopWorkers(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700111
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 Chartier90443472015-07-16 20:32:27 -0700114 void AddTask(Thread* self, Task* task) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700115
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000116 // Remove all tasks in the queue.
117 void RemoveAllTasks(Thread* self) REQUIRES(!task_queue_lock_);
118
Andreas Gampeb15de0c2017-01-24 13:12:19 -0800119 // 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 Chartiereac4d6a2018-12-05 12:33:46 -0800124 ThreadPool(const char* name,
125 size_t num_threads,
126 bool create_peers = false,
127 size_t worker_stack_size = ThreadPoolWorker::kDefaultStackSize);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700128 virtual ~ThreadPool();
129
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000130 // Create the threads of this pool.
131 void CreateThreads();
132
133 // Stops and deletes all threads in this pool.
134 void DeleteThreads();
135
Andreas Gampe6f3a70f2016-11-16 13:58:05 -0800136 // 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 Gampeb15de0c2017-01-24 13:12:19 -0800138 // When the pool was created with peers for workers, do_work must not be true (see ThreadPool()).
Mathieu Chartier90443472015-07-16 20:32:27 -0700139 void Wait(Thread* self, bool do_work, bool may_hold_locks) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700140
Mathieu Chartier90443472015-07-16 20:32:27 -0700141 size_t GetTaskCount(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700142
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700143 // Returns the total amount of workers waited for tasks.
144 uint64_t GetWaitTime() const {
145 return total_wait_time_;
146 }
147
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700148 // 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 Chartier90443472015-07-16 20:32:27 -0700150 void SetMaxActiveWorkers(size_t threads) REQUIRES(!task_queue_lock_);
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700151
Andreas Gampee701f082016-02-29 20:49:38 -0800152 // Set the "nice" priorty for threads in the pool.
153 void SetPthreadPriority(int priority);
154
Mathieu Chartier93c21ba2018-12-10 13:08:30 -0800155 // Wait for workers to be created.
156 void WaitForWorkersToBeCreated();
157
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700158 protected:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700159 // get a task to run, blocks if there are no tasks left
Mathieu Chartier90443472015-07-16 20:32:27 -0700160 virtual Task* GetTask(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700161
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700162 // Try to get a task, returning null if there is none available.
Mathieu Chartier90443472015-07-16 20:32:27 -0700163 Task* TryGetTask(Thread* self) REQUIRES(!task_queue_lock_);
164 Task* TryGetTaskLocked() REQUIRES(task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700165
166 // Are we shutting down?
Mathieu Chartier90443472015-07-16 20:32:27 -0700167 bool IsShuttingDown() const REQUIRES(task_queue_lock_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700168 return shutting_down_;
169 }
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700170
Andreas Gampe6f3a70f2016-11-16 13:58:05 -0800171 bool HasOutstandingTasks() const REQUIRES(task_queue_lock_) {
172 return started_ && !tasks_.empty();
173 }
174
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800175 const std::string name_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700176 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 Chartier02b6a782012-10-26 13:51:26 -0700183 std::deque<Task*> tasks_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700184 std::vector<ThreadPoolWorker*> threads_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700185 // Work balance detection.
186 uint64_t start_time_ GUARDED_BY(task_queue_lock_);
187 uint64_t total_wait_time_;
Mathieu Chartier35883cc2012-11-13 14:08:12 -0800188 Barrier creation_barier_;
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700189 size_t max_active_workers_ GUARDED_BY(task_queue_lock_);
Andreas Gampeb15de0c2017-01-24 13:12:19 -0800190 const bool create_peers_;
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000191 const size_t worker_stack_size_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700192
Mathieu Chartier02e25112013-08-14 16:14:24 -0700193 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700194 friend class ThreadPoolWorker;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700195 friend class WorkStealingWorker;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700196 DISALLOW_COPY_AND_ASSIGN(ThreadPool);
197};
198
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700199} // namespace art
200
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700201#endif // ART_RUNTIME_THREAD_POOL_H_