blob: a465e110557a00c64b0984800dee73d6b0d5bb47 [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>
21#include <vector>
22
Mathieu Chartier35883cc2012-11-13 14:08:12 -080023#include "barrier.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080024#include "base/mutex.h"
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080025#include "mem_map.h"
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070026
27namespace art {
28
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070029class ThreadPool;
30
Ian Rogers14317f02014-12-03 10:48:05 -080031class Closure {
32 public:
33 virtual ~Closure() { }
34 virtual void Run(Thread* self) = 0;
35};
36
Mathieu Chartier02b6a782012-10-26 13:51:26 -070037class Task : public Closure {
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070038 public:
Mathieu Chartiera5eae692014-12-17 17:56:03 -080039 // Called after Closure::Run has been called.
Mathieu Chartier02b6a782012-10-26 13:51:26 -070040 virtual void Finalize() { }
41};
42
Mathieu Chartiera5eae692014-12-17 17:56:03 -080043class SelfDeletingTask : public Task {
44 public:
45 virtual ~SelfDeletingTask() { }
46 virtual void Finalize() {
47 delete this;
48 }
49};
50
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070051class ThreadPoolWorker {
52 public:
53 static const size_t kDefaultStackSize = 1 * MB;
54
55 size_t GetStackSize() const {
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080056 DCHECK(stack_.get() != nullptr);
57 return stack_->Size();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070058 }
59
60 virtual ~ThreadPoolWorker();
61
Andreas Gampee701f082016-02-29 20:49:38 -080062 // Set the "nice" priorty for this worker.
63 void SetPthreadPriority(int priority);
64
Nicolas Geoffray340dafa2016-11-18 16:03:10 +000065 Thread* GetThread() const { return thread_; }
66
Mathieu Chartier02b6a782012-10-26 13:51:26 -070067 protected:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070068 ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
Mathieu Chartier90443472015-07-16 20:32:27 -070069 static void* Callback(void* arg) REQUIRES(!Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -070070 virtual void Run();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070071
Ian Rogersd914eb22013-04-18 16:11:15 -070072 ThreadPool* const thread_pool_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070073 const std::string name_;
Ian Rogers700a4022014-05-19 16:49:03 -070074 std::unique_ptr<MemMap> stack_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070075 pthread_t pthread_;
Nicolas Geoffray340dafa2016-11-18 16:03:10 +000076 Thread* thread_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070077
Mathieu Chartier02e25112013-08-14 16:14:24 -070078 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070079 friend class ThreadPool;
80 DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker);
81};
82
Calin Juravleccd56952016-12-15 17:57:38 +000083// Note that thread pool workers will set Thread#setCanCallIntoJava to false.
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070084class ThreadPool {
85 public:
86 // Returns the number of threads in the thread pool.
87 size_t GetThreadCount() const {
88 return threads_.size();
89 }
90
Nicolas Geoffray340dafa2016-11-18 16:03:10 +000091 const std::vector<ThreadPoolWorker*>& GetWorkers() const {
92 return threads_;
93 }
94
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070095 // Broadcast to the workers and tell them to empty out the work queue.
Mathieu Chartier90443472015-07-16 20:32:27 -070096 void StartWorkers(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070097
98 // Do not allow workers to grab any new tasks.
Mathieu Chartier90443472015-07-16 20:32:27 -070099 void StopWorkers(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700100
101 // Add a new task, the first available started worker will process it. Does not delete the task
102 // after running it, it is the caller's responsibility.
Mathieu Chartier90443472015-07-16 20:32:27 -0700103 void AddTask(Thread* self, Task* task) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700104
Nicolas Geoffray629e9352015-11-04 17:22:16 +0000105 // Remove all tasks in the queue.
106 void RemoveAllTasks(Thread* self) REQUIRES(!task_queue_lock_);
107
Andreas Gampeb15de0c2017-01-24 13:12:19 -0800108 // Create a named thread pool with the given number of threads.
109 //
110 // If create_peers is true, all worker threads will have a Java peer object. Note that if the
111 // pool is asked to do work on the current thread (see Wait), a peer may not be available. Wait
112 // will conservatively abort if create_peers and do_work are true.
113 ThreadPool(const char* name, size_t num_threads, bool create_peers = false);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700114 virtual ~ThreadPool();
115
Andreas Gampe6f3a70f2016-11-16 13:58:05 -0800116 // Wait for all tasks currently on queue to get completed. If the pool has been stopped, only
117 // wait till all already running tasks are done.
Andreas Gampeb15de0c2017-01-24 13:12:19 -0800118 // When the pool was created with peers for workers, do_work must not be true (see ThreadPool()).
Mathieu Chartier90443472015-07-16 20:32:27 -0700119 void Wait(Thread* self, bool do_work, bool may_hold_locks) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700120
Mathieu Chartier90443472015-07-16 20:32:27 -0700121 size_t GetTaskCount(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700122
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700123 // Returns the total amount of workers waited for tasks.
124 uint64_t GetWaitTime() const {
125 return total_wait_time_;
126 }
127
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700128 // Provides a way to bound the maximum number of worker threads, threads must be less the the
129 // thread count of the thread pool.
Mathieu Chartier90443472015-07-16 20:32:27 -0700130 void SetMaxActiveWorkers(size_t threads) REQUIRES(!task_queue_lock_);
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700131
Andreas Gampee701f082016-02-29 20:49:38 -0800132 // Set the "nice" priorty for threads in the pool.
133 void SetPthreadPriority(int priority);
134
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700135 protected:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700136 // get a task to run, blocks if there are no tasks left
Mathieu Chartier90443472015-07-16 20:32:27 -0700137 virtual Task* GetTask(Thread* self) REQUIRES(!task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700138
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700139 // Try to get a task, returning null if there is none available.
Mathieu Chartier90443472015-07-16 20:32:27 -0700140 Task* TryGetTask(Thread* self) REQUIRES(!task_queue_lock_);
141 Task* TryGetTaskLocked() REQUIRES(task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700142
143 // Are we shutting down?
Mathieu Chartier90443472015-07-16 20:32:27 -0700144 bool IsShuttingDown() const REQUIRES(task_queue_lock_) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700145 return shutting_down_;
146 }
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700147
Andreas Gampe6f3a70f2016-11-16 13:58:05 -0800148 bool HasOutstandingTasks() const REQUIRES(task_queue_lock_) {
149 return started_ && !tasks_.empty();
150 }
151
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800152 const std::string name_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700153 Mutex task_queue_lock_;
154 ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_);
155 ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_);
156 volatile bool started_ GUARDED_BY(task_queue_lock_);
157 volatile bool shutting_down_ GUARDED_BY(task_queue_lock_);
158 // How many worker threads are waiting on the condition.
159 volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700160 std::deque<Task*> tasks_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700161 // TODO: make this immutable/const?
162 std::vector<ThreadPoolWorker*> threads_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700163 // Work balance detection.
164 uint64_t start_time_ GUARDED_BY(task_queue_lock_);
165 uint64_t total_wait_time_;
Mathieu Chartier35883cc2012-11-13 14:08:12 -0800166 Barrier creation_barier_;
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700167 size_t max_active_workers_ GUARDED_BY(task_queue_lock_);
Andreas Gampeb15de0c2017-01-24 13:12:19 -0800168 const bool create_peers_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700169
Mathieu Chartier02e25112013-08-14 16:14:24 -0700170 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700171 friend class ThreadPoolWorker;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700172 friend class WorkStealingWorker;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700173 DISALLOW_COPY_AND_ASSIGN(ThreadPool);
174};
175
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700176} // namespace art
177
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700178#endif // ART_RUNTIME_THREAD_POOL_H_