blob: f55d72ec30fe020ccc41548eceade5e28f8b5abf [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
Nicolas Geoffray340dafa2016-11-18 16:03:10 +0000104 const std::vector<ThreadPoolWorker*>& GetWorkers() const {
105 return threads_;
106 }
107
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700108 // Broadcast to the workers and tell them to empty out the work queue.
Mathieu Chartier90443472015-07-16 20:32:27 -0700109 void StartWorkers(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700110
111 // Do not allow workers to grab any new tasks.
Mathieu Chartier90443472015-07-16 20:32:27 -0700112 void StopWorkers(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700113
114 // Add a new task, the first available started worker will process it. Does not delete the task
115 // after running it, it is the caller's responsibility.
Mathieu Chartier90443472015-07-16 20:32:27 -0700116 void AddTask(Thread* self, Task* task) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700117
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000118 // Remove all tasks in the queue.
119 void RemoveAllTasks(Thread* self) REQUIRES(!task_queue_lock_);
120
Andreas Gampeb15de0c2017-01-24 13:12:19 -0800121 // Create a named thread pool with the given number of threads.
122 //
123 // If create_peers is true, all worker threads will have a Java peer object. Note that if the
124 // pool is asked to do work on the current thread (see Wait), a peer may not be available. Wait
125 // will conservatively abort if create_peers and do_work are true.
Mathieu Chartiereac4d6a2018-12-05 12:33:46 -0800126 ThreadPool(const char* name,
127 size_t num_threads,
128 bool create_peers = false,
129 size_t worker_stack_size = ThreadPoolWorker::kDefaultStackSize);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700130 virtual ~ThreadPool();
131
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000132 // Create the threads of this pool.
133 void CreateThreads();
134
135 // Stops and deletes all threads in this pool.
136 void DeleteThreads();
137
Andreas Gampe6f3a70f2016-11-16 13:58:05 -0800138 // Wait for all tasks currently on queue to get completed. If the pool has been stopped, only
139 // wait till all already running tasks are done.
Andreas Gampeb15de0c2017-01-24 13:12:19 -0800140 // When the pool was created with peers for workers, do_work must not be true (see ThreadPool()).
Mathieu Chartier90443472015-07-16 20:32:27 -0700141 void Wait(Thread* self, bool do_work, bool may_hold_locks) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700142
Mathieu Chartier90443472015-07-16 20:32:27 -0700143 size_t GetTaskCount(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700144
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700145 // Returns the total amount of workers waited for tasks.
146 uint64_t GetWaitTime() const {
147 return total_wait_time_;
148 }
149
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700150 // Provides a way to bound the maximum number of worker threads, threads must be less the the
151 // thread count of the thread pool.
Mathieu Chartier90443472015-07-16 20:32:27 -0700152 void SetMaxActiveWorkers(size_t threads) REQUIRES(!task_queue_lock_);
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700153
Andreas Gampee701f082016-02-29 20:49:38 -0800154 // Set the "nice" priorty for threads in the pool.
155 void SetPthreadPriority(int priority);
156
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700157 protected:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700158 // get a task to run, blocks if there are no tasks left
Mathieu Chartier90443472015-07-16 20:32:27 -0700159 virtual Task* GetTask(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700160
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700161 // Try to get a task, returning null if there is none available.
Mathieu Chartier90443472015-07-16 20:32:27 -0700162 Task* TryGetTask(Thread* self) REQUIRES(!task_queue_lock_);
163 Task* TryGetTaskLocked() REQUIRES(task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700164
165 // Are we shutting down?
Mathieu Chartier90443472015-07-16 20:32:27 -0700166 bool IsShuttingDown() const REQUIRES(task_queue_lock_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700167 return shutting_down_;
168 }
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700169
Andreas Gampe6f3a70f2016-11-16 13:58:05 -0800170 bool HasOutstandingTasks() const REQUIRES(task_queue_lock_) {
171 return started_ && !tasks_.empty();
172 }
173
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800174 const std::string name_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700175 Mutex task_queue_lock_;
176 ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_);
177 ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_);
178 volatile bool started_ GUARDED_BY(task_queue_lock_);
179 volatile bool shutting_down_ GUARDED_BY(task_queue_lock_);
180 // How many worker threads are waiting on the condition.
181 volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700182 std::deque<Task*> tasks_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700183 std::vector<ThreadPoolWorker*> threads_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700184 // Work balance detection.
185 uint64_t start_time_ GUARDED_BY(task_queue_lock_);
186 uint64_t total_wait_time_;
Mathieu Chartier35883cc2012-11-13 14:08:12 -0800187 Barrier creation_barier_;
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700188 size_t max_active_workers_ GUARDED_BY(task_queue_lock_);
Andreas Gampeb15de0c2017-01-24 13:12:19 -0800189 const bool create_peers_;
Nicolas Geoffrayce9ed362018-11-29 03:19:28 +0000190 const size_t worker_stack_size_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700191
Mathieu Chartier02e25112013-08-14 16:14:24 -0700192 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700193 friend class ThreadPoolWorker;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700194 friend class WorkStealingWorker;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700195 DISALLOW_COPY_AND_ASSIGN(ThreadPool);
196};
197
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700198} // namespace art
199
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700200#endif // ART_RUNTIME_THREAD_POOL_H_