blob: c8f6056f663d5e79519a180c2e0ddd9d429fdb6e [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
17#ifndef ART_SRC_THREAD_POOL_H_
18#define ART_SRC_THREAD_POOL_H_
19
20#include <deque>
21#include <vector>
22
Mathieu Chartier02b6a782012-10-26 13:51:26 -070023#include "closure.h"
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070024#include "locks.h"
25#include "../src/mutex.h"
26
27namespace art {
28
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070029class ThreadPool;
30
Mathieu Chartier02b6a782012-10-26 13:51:26 -070031class Task : public Closure {
32public:
33 // Called when references reaches 0.
34 virtual void Finalize() { }
35};
36
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070037class ThreadPoolWorker {
38 public:
39 static const size_t kDefaultStackSize = 1 * MB;
40
41 size_t GetStackSize() const {
42 return stack_size_;
43 }
44
45 virtual ~ThreadPoolWorker();
46
Mathieu Chartier02b6a782012-10-26 13:51:26 -070047 protected:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070048 ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
49 static void* Callback(void* arg) LOCKS_EXCLUDED(Locks::mutator_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -070050 virtual void Run();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070051
52 ThreadPool* thread_pool_;
53 const std::string name_;
54 const size_t stack_size_;
55 pthread_t pthread_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070056
57 friend class ThreadPool;
58 DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker);
59};
60
61class ThreadPool {
62 public:
63 // Returns the number of threads in the thread pool.
64 size_t GetThreadCount() const {
65 return threads_.size();
66 }
67
68 // Broadcast to the workers and tell them to empty out the work queue.
69 void StartWorkers(Thread* self);
70
71 // Do not allow workers to grab any new tasks.
72 void StopWorkers(Thread* self);
73
74 // Add a new task, the first available started worker will process it. Does not delete the task
75 // after running it, it is the caller's responsibility.
Mathieu Chartier02b6a782012-10-26 13:51:26 -070076 void AddTask(Thread* self, Task* task);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070077
78 ThreadPool(size_t num_threads);
79 virtual ~ThreadPool();
80
81 // Wait for all tasks currently on queue to get completed.
Mathieu Chartier02b6a782012-10-26 13:51:26 -070082 void Wait(Thread* self, bool do_work = true);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070083
Mathieu Chartier02b6a782012-10-26 13:51:26 -070084 size_t GetTaskCount(Thread* self);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070085
Mathieu Chartier02b6a782012-10-26 13:51:26 -070086 // Returns the total amount of workers waited for tasks.
87 uint64_t GetWaitTime() const {
88 return total_wait_time_;
89 }
90
91 protected:
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070092 // Get a task to run, blocks if there are no tasks left
Mathieu Chartier02b6a782012-10-26 13:51:26 -070093 virtual Task* GetTask(Thread* self);
94
95 // Try to get a task, returning NULL if there is none available.
96 Task* TryGetTask(Thread* self);
97 Task* TryGetTaskLocked(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(task_queue_lock_);
98
99 // Are we shutting down?
100 bool IsShuttingDown() const EXCLUSIVE_LOCKS_REQUIRED(task_queue_lock_) {
101 return shutting_down_;
102 }
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700103
104 Mutex task_queue_lock_;
105 ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_);
106 ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_);
107 volatile bool started_ GUARDED_BY(task_queue_lock_);
108 volatile bool shutting_down_ GUARDED_BY(task_queue_lock_);
109 // How many worker threads are waiting on the condition.
110 volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700111 std::deque<Task*> tasks_ GUARDED_BY(task_queue_lock_);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700112 // TODO: make this immutable/const?
113 std::vector<ThreadPoolWorker*> threads_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700114 // Work balance detection.
115 uint64_t start_time_ GUARDED_BY(task_queue_lock_);
116 uint64_t total_wait_time_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700117
118 friend class ThreadPoolWorker;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700119 friend class WorkStealingWorker;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700120 DISALLOW_COPY_AND_ASSIGN(ThreadPool);
121};
122
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700123class WorkStealingTask : public Task {
124 public:
125 WorkStealingTask() : ref_count_(0) {
126
127 }
128
129 size_t GetRefCount() const {
130 return ref_count_;
131 }
132
133 virtual void StealFrom(Thread* self, WorkStealingTask* source) = 0;
134
135 private:
136 // How many people are referencing this task.
137 size_t ref_count_;
138
139 friend class WorkStealingWorker;
140};
141
142class WorkStealingWorker : public ThreadPoolWorker {
143 public:
144 virtual ~WorkStealingWorker();
145
146 bool IsRunningTask() const {
147 return task_ != NULL;
148 }
149
150 protected:
151 WorkStealingTask* task_;
152
153 WorkStealingWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
154 virtual void Run();
155
156 friend class WorkStealingThreadPool;
157 DISALLOW_COPY_AND_ASSIGN(WorkStealingWorker);
158};
159
160class WorkStealingThreadPool : public ThreadPool {
161 public:
162 WorkStealingThreadPool(size_t num_threads);
163 virtual ~WorkStealingThreadPool();
164
165 private:
166 Mutex work_steal_lock_;
167 // Which thread we are stealing from (round robin).
168 size_t steal_index_;
169
170 // Find a task to steal from
171 WorkStealingTask* FindTaskToStealFrom(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(work_steal_lock_);
172
173 friend class WorkStealingWorker;
174};
175
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700176} // namespace art
177
178#endif // ART_SRC_THREAD_POOL_H_