1 /*
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_RUNTIME_THREAD_POOL_H_
18 #define ART_RUNTIME_THREAD_POOL_H_
19 
20 #include <deque>
21 #include <vector>
22 
23 #include "barrier.h"
24 #include "base/mutex.h"
25 #include "mem_map.h"
26 
27 namespace art {
28 
29 class ThreadPool;
30 
31 class Closure {
32  public:
~Closure()33   virtual ~Closure() { }
34   virtual void Run(Thread* self) = 0;
35 };
36 
37 class Task : public Closure {
38  public:
39   // Called after Closure::Run has been called.
Finalize()40   virtual void Finalize() { }
41 };
42 
43 class SelfDeletingTask : public Task {
44  public:
~SelfDeletingTask()45   virtual ~SelfDeletingTask() { }
Finalize()46   virtual void Finalize() {
47     delete this;
48   }
49 };
50 
51 class ThreadPoolWorker {
52  public:
53   static const size_t kDefaultStackSize = 1 * MB;
54 
GetStackSize()55   size_t GetStackSize() const {
56     DCHECK(stack_.get() != nullptr);
57     return stack_->Size();
58   }
59 
60   virtual ~ThreadPoolWorker();
61 
62   // Set the "nice" priorty for this worker.
63   void SetPthreadPriority(int priority);
64 
GetThread()65   Thread* GetThread() const { return thread_; }
66 
67  protected:
68   ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size);
69   static void* Callback(void* arg) REQUIRES(!Locks::mutator_lock_);
70   virtual void Run();
71 
72   ThreadPool* const thread_pool_;
73   const std::string name_;
74   std::unique_ptr<MemMap> stack_;
75   pthread_t pthread_;
76   Thread* thread_;
77 
78  private:
79   friend class ThreadPool;
80   DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker);
81 };
82 
83 // Note that thread pool workers will set Thread#setCanCallIntoJava to false.
84 class ThreadPool {
85  public:
86   // Returns the number of threads in the thread pool.
GetThreadCount()87   size_t GetThreadCount() const {
88     return threads_.size();
89   }
90 
GetWorkers()91   const std::vector<ThreadPoolWorker*>& GetWorkers() const {
92     return threads_;
93   }
94 
95   // Broadcast to the workers and tell them to empty out the work queue.
96   void StartWorkers(Thread* self) REQUIRES(!task_queue_lock_);
97 
98   // Do not allow workers to grab any new tasks.
99   void StopWorkers(Thread* self) REQUIRES(!task_queue_lock_);
100 
101   // Add a new task, the first available started worker will process it. Does not delete the task
102   // after running it, it is the caller's responsibility.
103   void AddTask(Thread* self, Task* task) REQUIRES(!task_queue_lock_);
104 
105   // Remove all tasks in the queue.
106   void RemoveAllTasks(Thread* self) REQUIRES(!task_queue_lock_);
107 
108   // Create a named thread pool with the given number of threads.
109   //
110   // If create_peers is true, all worker threads will have a Java peer object. Note that if the
111   // pool is asked to do work on the current thread (see Wait), a peer may not be available. Wait
112   // will conservatively abort if create_peers and do_work are true.
113   ThreadPool(const char* name, size_t num_threads, bool create_peers = false);
114   virtual ~ThreadPool();
115 
116   // Wait for all tasks currently on queue to get completed. If the pool has been stopped, only
117   // wait till all already running tasks are done.
118   // When the pool was created with peers for workers, do_work must not be true (see ThreadPool()).
119   void Wait(Thread* self, bool do_work, bool may_hold_locks) REQUIRES(!task_queue_lock_);
120 
121   size_t GetTaskCount(Thread* self) REQUIRES(!task_queue_lock_);
122 
123   // Returns the total amount of workers waited for tasks.
GetWaitTime()124   uint64_t GetWaitTime() const {
125     return total_wait_time_;
126   }
127 
128   // Provides a way to bound the maximum number of worker threads, threads must be less the the
129   // thread count of the thread pool.
130   void SetMaxActiveWorkers(size_t threads) REQUIRES(!task_queue_lock_);
131 
132   // Set the "nice" priorty for threads in the pool.
133   void SetPthreadPriority(int priority);
134 
135  protected:
136   // get a task to run, blocks if there are no tasks left
137   virtual Task* GetTask(Thread* self) REQUIRES(!task_queue_lock_);
138 
139   // Try to get a task, returning null if there is none available.
140   Task* TryGetTask(Thread* self) REQUIRES(!task_queue_lock_);
141   Task* TryGetTaskLocked() REQUIRES(task_queue_lock_);
142 
143   // Are we shutting down?
IsShuttingDown()144   bool IsShuttingDown() const REQUIRES(task_queue_lock_) {
145     return shutting_down_;
146   }
147 
HasOutstandingTasks()148   bool HasOutstandingTasks() const REQUIRES(task_queue_lock_) {
149     return started_ && !tasks_.empty();
150   }
151 
152   const std::string name_;
153   Mutex task_queue_lock_;
154   ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_);
155   ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_);
156   volatile bool started_ GUARDED_BY(task_queue_lock_);
157   volatile bool shutting_down_ GUARDED_BY(task_queue_lock_);
158   // How many worker threads are waiting on the condition.
159   volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_);
160   std::deque<Task*> tasks_ GUARDED_BY(task_queue_lock_);
161   // TODO: make this immutable/const?
162   std::vector<ThreadPoolWorker*> threads_;
163   // Work balance detection.
164   uint64_t start_time_ GUARDED_BY(task_queue_lock_);
165   uint64_t total_wait_time_;
166   Barrier creation_barier_;
167   size_t max_active_workers_ GUARDED_BY(task_queue_lock_);
168   const bool create_peers_;
169 
170  private:
171   friend class ThreadPoolWorker;
172   friend class WorkStealingWorker;
173   DISALLOW_COPY_AND_ASSIGN(ThreadPool);
174 };
175 
176 }  // namespace art
177 
178 #endif  // ART_RUNTIME_THREAD_POOL_H_
179