blob: e8f9afe62dcddb55cc305e548649b7d4a410cc50 [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 Chartier0e4627e2012-10-23 16:13:36 -070026#include "locks.h"
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080027#include "mem_map.h"
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070028
29namespace art {
30
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070031class ThreadPool;
32
Mathieu Chartier02b6a782012-10-26 13:51:26 -070033class Task : public Closure {
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070034 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -070035 // Called when references reaches 0.
36 virtual void Finalize() { }
37};
38
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070039class ThreadPoolWorker {
40 public:
41 static const size_t kDefaultStackSize = 1 * MB;
42
43 size_t GetStackSize() const {
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080044 DCHECK(stack_.get() != nullptr);
45 return stack_->Size();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070046 }
47
48 virtual ~ThreadPoolWorker();
49
Mathieu Chartier02b6a782012-10-26 13:51:26 -070050 protected:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070051 ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
52 static void* Callback(void* arg) LOCKS_EXCLUDED(Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -070053 virtual void Run();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070054
Ian Rogersd914eb22013-04-18 16:11:15 -070055 ThreadPool* const thread_pool_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070056 const std::string name_;
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080057 UniquePtr<MemMap> stack_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070058 pthread_t pthread_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070059
Mathieu Chartier02e25112013-08-14 16:14:24 -070060 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070061 friend class ThreadPool;
62 DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker);
63};
64
65class ThreadPool {
66 public:
67 // Returns the number of threads in the thread pool.
68 size_t GetThreadCount() const {
69 return threads_.size();
70 }
71
72 // Broadcast to the workers and tell them to empty out the work queue.
73 void StartWorkers(Thread* self);
74
75 // Do not allow workers to grab any new tasks.
76 void StopWorkers(Thread* self);
77
78 // Add a new task, the first available started worker will process it. Does not delete the task
79 // after running it, it is the caller's responsibility.
Mathieu Chartier02b6a782012-10-26 13:51:26 -070080 void AddTask(Thread* self, Task* task);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070081
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080082 explicit ThreadPool(const char* name, size_t num_threads);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070083 virtual ~ThreadPool();
84
85 // Wait for all tasks currently on queue to get completed.
Ian Rogers1d54e732013-05-02 21:10:01 -070086 void Wait(Thread* self, bool do_work, bool may_hold_locks);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070087
Mathieu Chartier02b6a782012-10-26 13:51:26 -070088 size_t GetTaskCount(Thread* self);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070089
Mathieu Chartier02b6a782012-10-26 13:51:26 -070090 // Returns the total amount of workers waited for tasks.
91 uint64_t GetWaitTime() const {
92 return total_wait_time_;
93 }
94
Mathieu Chartier2775ee42013-08-20 17:43:47 -070095 // Provides a way to bound the maximum number of worker threads, threads must be less the the
96 // thread count of the thread pool.
97 void SetMaxActiveWorkers(size_t threads);
98
Mathieu Chartier02b6a782012-10-26 13:51:26 -070099 protected:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700100 // Get a task to run, blocks if there are no tasks left
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700101 virtual Task* GetTask(Thread* self);
102
103 // Try to get a task, returning NULL if there is none available.
104 Task* TryGetTask(Thread* self);
105 Task* TryGetTaskLocked(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(task_queue_lock_);
106
107 // Are we shutting down?
108 bool IsShuttingDown() const EXCLUSIVE_LOCKS_REQUIRED(task_queue_lock_) {
109 return shutting_down_;
110 }
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700111
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800112 const std::string name_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700113 Mutex task_queue_lock_;
114 ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_);
115 ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_);
116 volatile bool started_ GUARDED_BY(task_queue_lock_);
117 volatile bool shutting_down_ GUARDED_BY(task_queue_lock_);
118 // How many worker threads are waiting on the condition.
119 volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700120 std::deque<Task*> tasks_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700121 // TODO: make this immutable/const?
122 std::vector<ThreadPoolWorker*> threads_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700123 // Work balance detection.
124 uint64_t start_time_ GUARDED_BY(task_queue_lock_);
125 uint64_t total_wait_time_;
Mathieu Chartier35883cc2012-11-13 14:08:12 -0800126 Barrier creation_barier_;
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700127 size_t max_active_workers_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700128
Mathieu Chartier02e25112013-08-14 16:14:24 -0700129 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700130 friend class ThreadPoolWorker;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700131 friend class WorkStealingWorker;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700132 DISALLOW_COPY_AND_ASSIGN(ThreadPool);
133};
134
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700135class WorkStealingTask : public Task {
136 public:
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700137 WorkStealingTask() : ref_count_(0) {}
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700138
139 size_t GetRefCount() const {
140 return ref_count_;
141 }
142
143 virtual void StealFrom(Thread* self, WorkStealingTask* source) = 0;
144
145 private:
146 // How many people are referencing this task.
147 size_t ref_count_;
148
149 friend class WorkStealingWorker;
150};
151
152class WorkStealingWorker : public ThreadPoolWorker {
153 public:
154 virtual ~WorkStealingWorker();
155
156 bool IsRunningTask() const {
157 return task_ != NULL;
158 }
159
160 protected:
161 WorkStealingTask* task_;
162
163 WorkStealingWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
164 virtual void Run();
165
Mathieu Chartier02e25112013-08-14 16:14:24 -0700166 private:
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700167 friend class WorkStealingThreadPool;
168 DISALLOW_COPY_AND_ASSIGN(WorkStealingWorker);
169};
170
171class WorkStealingThreadPool : public ThreadPool {
172 public:
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800173 explicit WorkStealingThreadPool(const char* name, size_t num_threads);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700174 virtual ~WorkStealingThreadPool();
175
176 private:
177 Mutex work_steal_lock_;
178 // Which thread we are stealing from (round robin).
179 size_t steal_index_;
180
181 // Find a task to steal from
182 WorkStealingTask* FindTaskToStealFrom(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(work_steal_lock_);
183
184 friend class WorkStealingWorker;
185};
186
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700187} // namespace art
188
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700189#endif // ART_RUNTIME_THREAD_POOL_H_