blob: 9c6d47b0a9efee153c9165905be272e64539dcdc [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 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 {
43 return stack_size_;
44 }
45
46 virtual ~ThreadPoolWorker();
47
Mathieu Chartier02b6a782012-10-26 13:51:26 -070048 protected:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070049 ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
50 static void* Callback(void* arg) LOCKS_EXCLUDED(Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -070051 virtual void Run();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070052
Ian Rogersd914eb22013-04-18 16:11:15 -070053 ThreadPool* const thread_pool_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070054 const std::string name_;
55 const size_t stack_size_;
56 pthread_t pthread_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070057
Mathieu Chartier02e25112013-08-14 16:14:24 -070058 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070059 friend class ThreadPool;
60 DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker);
61};
62
63class ThreadPool {
64 public:
65 // Returns the number of threads in the thread pool.
66 size_t GetThreadCount() const {
67 return threads_.size();
68 }
69
70 // Broadcast to the workers and tell them to empty out the work queue.
71 void StartWorkers(Thread* self);
72
73 // Do not allow workers to grab any new tasks.
74 void StopWorkers(Thread* self);
75
76 // Add a new task, the first available started worker will process it. Does not delete the task
77 // after running it, it is the caller's responsibility.
Mathieu Chartier02b6a782012-10-26 13:51:26 -070078 void AddTask(Thread* self, Task* task);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070079
Brian Carlstrom93ba8932013-07-17 21:31:49 -070080 explicit ThreadPool(size_t num_threads);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070081 virtual ~ThreadPool();
82
83 // Wait for all tasks currently on queue to get completed.
Ian Rogers1d54e732013-05-02 21:10:01 -070084 void Wait(Thread* self, bool do_work, bool may_hold_locks);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070085
Mathieu Chartier02b6a782012-10-26 13:51:26 -070086 size_t GetTaskCount(Thread* self);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070087
Mathieu Chartier02b6a782012-10-26 13:51:26 -070088 // Returns the total amount of workers waited for tasks.
89 uint64_t GetWaitTime() const {
90 return total_wait_time_;
91 }
92
93 protected:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070094 // Get a task to run, blocks if there are no tasks left
Mathieu Chartier02b6a782012-10-26 13:51:26 -070095 virtual Task* GetTask(Thread* self);
96
97 // Try to get a task, returning NULL if there is none available.
98 Task* TryGetTask(Thread* self);
99 Task* TryGetTaskLocked(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(task_queue_lock_);
100
101 // Are we shutting down?
102 bool IsShuttingDown() const EXCLUSIVE_LOCKS_REQUIRED(task_queue_lock_) {
103 return shutting_down_;
104 }
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700105
106 Mutex task_queue_lock_;
107 ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_);
108 ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_);
109 volatile bool started_ GUARDED_BY(task_queue_lock_);
110 volatile bool shutting_down_ GUARDED_BY(task_queue_lock_);
111 // How many worker threads are waiting on the condition.
112 volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700113 std::deque<Task*> tasks_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700114 // TODO: make this immutable/const?
115 std::vector<ThreadPoolWorker*> threads_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700116 // Work balance detection.
117 uint64_t start_time_ GUARDED_BY(task_queue_lock_);
118 uint64_t total_wait_time_;
Mathieu Chartier35883cc2012-11-13 14:08:12 -0800119 Barrier creation_barier_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700120
Mathieu Chartier02e25112013-08-14 16:14:24 -0700121 private:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700122 friend class ThreadPoolWorker;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700123 friend class WorkStealingWorker;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700124 DISALLOW_COPY_AND_ASSIGN(ThreadPool);
125};
126
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700127class WorkStealingTask : public Task {
128 public:
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700129 WorkStealingTask() : ref_count_(0) {}
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700130
131 size_t GetRefCount() const {
132 return ref_count_;
133 }
134
135 virtual void StealFrom(Thread* self, WorkStealingTask* source) = 0;
136
137 private:
138 // How many people are referencing this task.
139 size_t ref_count_;
140
141 friend class WorkStealingWorker;
142};
143
144class WorkStealingWorker : public ThreadPoolWorker {
145 public:
146 virtual ~WorkStealingWorker();
147
148 bool IsRunningTask() const {
149 return task_ != NULL;
150 }
151
152 protected:
153 WorkStealingTask* task_;
154
155 WorkStealingWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
156 virtual void Run();
157
Mathieu Chartier02e25112013-08-14 16:14:24 -0700158 private:
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700159 friend class WorkStealingThreadPool;
160 DISALLOW_COPY_AND_ASSIGN(WorkStealingWorker);
161};
162
163class WorkStealingThreadPool : public ThreadPool {
164 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700165 explicit WorkStealingThreadPool(size_t num_threads);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700166 virtual ~WorkStealingThreadPool();
167
168 private:
169 Mutex work_steal_lock_;
170 // Which thread we are stealing from (round robin).
171 size_t steal_index_;
172
173 // Find a task to steal from
174 WorkStealingTask* FindTaskToStealFrom(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(work_steal_lock_);
175
176 friend class WorkStealingWorker;
177};
178
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700179} // namespace art
180
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700181#endif // ART_RUNTIME_THREAD_POOL_H_