blob: c816c84561da8399bdc32ecf4b8ec9e899444558 [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 Chartier02b6a782012-10-26 13:51:26 -070025#include "closure.h"
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080026#include "mem_map.h"
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070027
28namespace art {
29
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070030class ThreadPool;
31
Mathieu Chartier02b6a782012-10-26 13:51:26 -070032class Task : public Closure {
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070033 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -070034 // Called when references reaches 0.
35 virtual void Finalize() { }
36};
37
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070038class ThreadPoolWorker {
39 public:
40 static const size_t kDefaultStackSize = 1 * MB;
41
42 size_t GetStackSize() const {
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080043 DCHECK(stack_.get() != nullptr);
44 return stack_->Size();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070045 }
46
47 virtual ~ThreadPoolWorker();
48
Mathieu Chartier02b6a782012-10-26 13:51:26 -070049 protected:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070050 ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
51 static void* Callback(void* arg) LOCKS_EXCLUDED(Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -070052 virtual void Run();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070053
Ian Rogersd914eb22013-04-18 16:11:15 -070054 ThreadPool* const thread_pool_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070055 const std::string name_;
Ian Rogers700a4022014-05-19 16:49:03 -070056 std::unique_ptr<MemMap> stack_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070057 pthread_t pthread_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070058
Mathieu Chartier02e25112013-08-14 16:14:24 -070059 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070060 friend class ThreadPool;
61 DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker);
62};
63
64class ThreadPool {
65 public:
66 // Returns the number of threads in the thread pool.
67 size_t GetThreadCount() const {
68 return threads_.size();
69 }
70
71 // Broadcast to the workers and tell them to empty out the work queue.
72 void StartWorkers(Thread* self);
73
74 // Do not allow workers to grab any new tasks.
75 void StopWorkers(Thread* self);
76
77 // Add a new task, the first available started worker will process it. Does not delete the task
78 // after running it, it is the caller's responsibility.
Mathieu Chartier02b6a782012-10-26 13:51:26 -070079 void AddTask(Thread* self, Task* task);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070080
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080081 explicit ThreadPool(const char* name, size_t num_threads);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070082 virtual ~ThreadPool();
83
84 // Wait for all tasks currently on queue to get completed.
Ian Rogers1d54e732013-05-02 21:10:01 -070085 void Wait(Thread* self, bool do_work, bool may_hold_locks);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070086
Mathieu Chartier02b6a782012-10-26 13:51:26 -070087 size_t GetTaskCount(Thread* self);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070088
Mathieu Chartier02b6a782012-10-26 13:51:26 -070089 // Returns the total amount of workers waited for tasks.
90 uint64_t GetWaitTime() const {
91 return total_wait_time_;
92 }
93
Mathieu Chartier2775ee42013-08-20 17:43:47 -070094 // Provides a way to bound the maximum number of worker threads, threads must be less the the
95 // thread count of the thread pool.
96 void SetMaxActiveWorkers(size_t threads);
97
Mathieu Chartier02b6a782012-10-26 13:51:26 -070098 protected:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070099 // get a task to run, blocks if there are no tasks left
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700100 virtual Task* GetTask(Thread* self);
101
102 // Try to get a task, returning NULL if there is none available.
103 Task* TryGetTask(Thread* self);
104 Task* TryGetTaskLocked(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(task_queue_lock_);
105
106 // Are we shutting down?
107 bool IsShuttingDown() const EXCLUSIVE_LOCKS_REQUIRED(task_queue_lock_) {
108 return shutting_down_;
109 }
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700110
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800111 const std::string name_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700112 Mutex task_queue_lock_;
113 ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_);
114 ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_);
115 volatile bool started_ GUARDED_BY(task_queue_lock_);
116 volatile bool shutting_down_ GUARDED_BY(task_queue_lock_);
117 // How many worker threads are waiting on the condition.
118 volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700119 std::deque<Task*> tasks_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700120 // TODO: make this immutable/const?
121 std::vector<ThreadPoolWorker*> threads_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700122 // Work balance detection.
123 uint64_t start_time_ GUARDED_BY(task_queue_lock_);
124 uint64_t total_wait_time_;
Mathieu Chartier35883cc2012-11-13 14:08:12 -0800125 Barrier creation_barier_;
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700126 size_t max_active_workers_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700127
Mathieu Chartier02e25112013-08-14 16:14:24 -0700128 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700129 friend class ThreadPoolWorker;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700130 friend class WorkStealingWorker;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700131 DISALLOW_COPY_AND_ASSIGN(ThreadPool);
132};
133
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700134class WorkStealingTask : public Task {
135 public:
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700136 WorkStealingTask() : ref_count_(0) {}
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700137
138 size_t GetRefCount() const {
139 return ref_count_;
140 }
141
142 virtual void StealFrom(Thread* self, WorkStealingTask* source) = 0;
143
144 private:
145 // How many people are referencing this task.
146 size_t ref_count_;
147
148 friend class WorkStealingWorker;
149};
150
151class WorkStealingWorker : public ThreadPoolWorker {
152 public:
153 virtual ~WorkStealingWorker();
154
155 bool IsRunningTask() const {
156 return task_ != NULL;
157 }
158
159 protected:
160 WorkStealingTask* task_;
161
162 WorkStealingWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
163 virtual void Run();
164
Mathieu Chartier02e25112013-08-14 16:14:24 -0700165 private:
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700166 friend class WorkStealingThreadPool;
167 DISALLOW_COPY_AND_ASSIGN(WorkStealingWorker);
168};
169
170class WorkStealingThreadPool : public ThreadPool {
171 public:
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800172 explicit WorkStealingThreadPool(const char* name, size_t num_threads);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700173 virtual ~WorkStealingThreadPool();
174
175 private:
176 Mutex work_steal_lock_;
177 // Which thread we are stealing from (round robin).
178 size_t steal_index_;
179
180 // Find a task to steal from
181 WorkStealingTask* FindTaskToStealFrom(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(work_steal_lock_);
182
183 friend class WorkStealingWorker;
184};
185
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700186} // namespace art
187
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700188#endif // ART_RUNTIME_THREAD_POOL_H_