Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame^] | 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_SRC_THREAD_POOL_H_ |
| 18 | #define ART_SRC_THREAD_POOL_H_ |
| 19 | |
| 20 | #include <deque> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "locks.h" |
| 24 | #include "../src/mutex.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | class Closure; |
| 29 | class ThreadPool; |
| 30 | |
| 31 | class ThreadPoolWorker { |
| 32 | public: |
| 33 | static const size_t kDefaultStackSize = 1 * MB; |
| 34 | |
| 35 | size_t GetStackSize() const { |
| 36 | return stack_size_; |
| 37 | } |
| 38 | |
| 39 | virtual ~ThreadPoolWorker(); |
| 40 | |
| 41 | private: |
| 42 | ThreadPoolWorker(ThreadPool* thread_pool, const std::string& name, size_t stack_size); |
| 43 | static void* Callback(void* arg) LOCKS_EXCLUDED(Locks::mutator_lock_); |
| 44 | void Run(); |
| 45 | |
| 46 | ThreadPool* thread_pool_; |
| 47 | const std::string name_; |
| 48 | const size_t stack_size_; |
| 49 | pthread_t pthread_; |
| 50 | pthread_attr_t attr; |
| 51 | |
| 52 | friend class ThreadPool; |
| 53 | DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker); |
| 54 | }; |
| 55 | |
| 56 | class ThreadPool { |
| 57 | public: |
| 58 | // Returns the number of threads in the thread pool. |
| 59 | size_t GetThreadCount() const { |
| 60 | return threads_.size(); |
| 61 | } |
| 62 | |
| 63 | // Broadcast to the workers and tell them to empty out the work queue. |
| 64 | void StartWorkers(Thread* self); |
| 65 | |
| 66 | // Do not allow workers to grab any new tasks. |
| 67 | void StopWorkers(Thread* self); |
| 68 | |
| 69 | // Add a new task, the first available started worker will process it. Does not delete the task |
| 70 | // after running it, it is the caller's responsibility. |
| 71 | void AddTask(Thread* self, Closure* task); |
| 72 | |
| 73 | ThreadPool(size_t num_threads); |
| 74 | virtual ~ThreadPool(); |
| 75 | |
| 76 | // Wait for all tasks currently on queue to get completed. |
| 77 | void Wait(Thread* self); |
| 78 | |
| 79 | private: |
| 80 | // Add a new task. |
| 81 | void AddThread(size_t stack_size); |
| 82 | |
| 83 | // Get a task to run, blocks if there are no tasks left |
| 84 | Closure* GetTask(Thread* self); |
| 85 | |
| 86 | Mutex task_queue_lock_; |
| 87 | ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_); |
| 88 | ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_); |
| 89 | volatile bool started_ GUARDED_BY(task_queue_lock_); |
| 90 | volatile bool shutting_down_ GUARDED_BY(task_queue_lock_); |
| 91 | // How many worker threads are waiting on the condition. |
| 92 | volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_); |
| 93 | std::deque<Closure*> tasks_ GUARDED_BY(task_queue_lock_); |
| 94 | // TODO: make this immutable/const? |
| 95 | std::vector<ThreadPoolWorker*> threads_; |
| 96 | |
| 97 | friend class ThreadPoolWorker; |
| 98 | DISALLOW_COPY_AND_ASSIGN(ThreadPool); |
| 99 | }; |
| 100 | |
| 101 | } // namespace art |
| 102 | |
| 103 | #endif // ART_SRC_THREAD_POOL_H_ |