blob: 8c080673f93e5b2743d2968fb933eb99773ef106 [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 Chartier02b6a782012-10-26 13:51:26 -070039 // Called when references reaches 0.
40 virtual void Finalize() { }
41};
42
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070043class ThreadPoolWorker {
44 public:
45 static const size_t kDefaultStackSize = 1 * MB;
46
47 size_t GetStackSize() const {
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080048 DCHECK(stack_.get() != nullptr);
49 return stack_->Size();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070050 }
51
52 virtual ~ThreadPoolWorker();
53
Mathieu Chartier02b6a782012-10-26 13:51:26 -070054 protected:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070055 ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
56 static void* Callback(void* arg) LOCKS_EXCLUDED(Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -070057 virtual void Run();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070058
Ian Rogersd914eb22013-04-18 16:11:15 -070059 ThreadPool* const thread_pool_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070060 const std::string name_;
Ian Rogers700a4022014-05-19 16:49:03 -070061 std::unique_ptr<MemMap> stack_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070062 pthread_t pthread_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070063
Mathieu Chartier02e25112013-08-14 16:14:24 -070064 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070065 friend class ThreadPool;
66 DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker);
67};
68
69class ThreadPool {
70 public:
71 // Returns the number of threads in the thread pool.
72 size_t GetThreadCount() const {
73 return threads_.size();
74 }
75
76 // Broadcast to the workers and tell them to empty out the work queue.
77 void StartWorkers(Thread* self);
78
79 // Do not allow workers to grab any new tasks.
80 void StopWorkers(Thread* self);
81
82 // Add a new task, the first available started worker will process it. Does not delete the task
83 // after running it, it is the caller's responsibility.
Mathieu Chartier02b6a782012-10-26 13:51:26 -070084 void AddTask(Thread* self, Task* task);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070085
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080086 explicit ThreadPool(const char* name, size_t num_threads);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070087 virtual ~ThreadPool();
88
89 // Wait for all tasks currently on queue to get completed.
Ian Rogers1d54e732013-05-02 21:10:01 -070090 void Wait(Thread* self, bool do_work, bool may_hold_locks);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070091
Mathieu Chartier02b6a782012-10-26 13:51:26 -070092 size_t GetTaskCount(Thread* self);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070093
Mathieu Chartier02b6a782012-10-26 13:51:26 -070094 // Returns the total amount of workers waited for tasks.
95 uint64_t GetWaitTime() const {
96 return total_wait_time_;
97 }
98
Mathieu Chartier2775ee42013-08-20 17:43:47 -070099 // Provides a way to bound the maximum number of worker threads, threads must be less the the
100 // thread count of the thread pool.
101 void SetMaxActiveWorkers(size_t threads);
102
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700103 protected:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700104 // get a task to run, blocks if there are no tasks left
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700105 virtual Task* GetTask(Thread* self);
106
107 // Try to get a task, returning NULL if there is none available.
108 Task* TryGetTask(Thread* self);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700109 Task* TryGetTaskLocked() EXCLUSIVE_LOCKS_REQUIRED(task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700110
111 // Are we shutting down?
112 bool IsShuttingDown() const EXCLUSIVE_LOCKS_REQUIRED(task_queue_lock_) {
113 return shutting_down_;
114 }
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700115
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800116 const std::string name_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700117 Mutex task_queue_lock_;
118 ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_);
119 ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_);
120 volatile bool started_ GUARDED_BY(task_queue_lock_);
121 volatile bool shutting_down_ GUARDED_BY(task_queue_lock_);
122 // How many worker threads are waiting on the condition.
123 volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700124 std::deque<Task*> tasks_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700125 // TODO: make this immutable/const?
126 std::vector<ThreadPoolWorker*> threads_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700127 // Work balance detection.
128 uint64_t start_time_ GUARDED_BY(task_queue_lock_);
129 uint64_t total_wait_time_;
Mathieu Chartier35883cc2012-11-13 14:08:12 -0800130 Barrier creation_barier_;
Mathieu Chartier2775ee42013-08-20 17:43:47 -0700131 size_t max_active_workers_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700132
Mathieu Chartier02e25112013-08-14 16:14:24 -0700133 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700134 friend class ThreadPoolWorker;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700135 friend class WorkStealingWorker;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700136 DISALLOW_COPY_AND_ASSIGN(ThreadPool);
137};
138
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700139class WorkStealingTask : public Task {
140 public:
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700141 WorkStealingTask() : ref_count_(0) {}
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700142
143 size_t GetRefCount() const {
144 return ref_count_;
145 }
146
147 virtual void StealFrom(Thread* self, WorkStealingTask* source) = 0;
148
149 private:
150 // How many people are referencing this task.
151 size_t ref_count_;
152
153 friend class WorkStealingWorker;
154};
155
156class WorkStealingWorker : public ThreadPoolWorker {
157 public:
158 virtual ~WorkStealingWorker();
159
160 bool IsRunningTask() const {
161 return task_ != NULL;
162 }
163
164 protected:
165 WorkStealingTask* task_;
166
167 WorkStealingWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
168 virtual void Run();
169
Mathieu Chartier02e25112013-08-14 16:14:24 -0700170 private:
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700171 friend class WorkStealingThreadPool;
172 DISALLOW_COPY_AND_ASSIGN(WorkStealingWorker);
173};
174
175class WorkStealingThreadPool : public ThreadPool {
176 public:
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800177 explicit WorkStealingThreadPool(const char* name, size_t num_threads);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700178 virtual ~WorkStealingThreadPool();
179
180 private:
181 Mutex work_steal_lock_;
182 // Which thread we are stealing from (round robin).
183 size_t steal_index_;
184
185 // Find a task to steal from
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700186 WorkStealingTask* FindTaskToStealFrom() EXCLUSIVE_LOCKS_REQUIRED(work_steal_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700187
188 friend class WorkStealingWorker;
189};
190
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700191} // namespace art
192
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700193#endif // ART_RUNTIME_THREAD_POOL_H_