blob: 1d0f85deead9f79f2223fef77a2b3b54104f3c4a [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
23#include "locks.h"
24#include "../src/mutex.h"
25
26namespace art {
27
28class Closure;
29class ThreadPool;
30
31class 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_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070050
51 friend class ThreadPool;
52 DISALLOW_COPY_AND_ASSIGN(ThreadPoolWorker);
53};
54
55class ThreadPool {
56 public:
57 // Returns the number of threads in the thread pool.
58 size_t GetThreadCount() const {
59 return threads_.size();
60 }
61
62 // Broadcast to the workers and tell them to empty out the work queue.
63 void StartWorkers(Thread* self);
64
65 // Do not allow workers to grab any new tasks.
66 void StopWorkers(Thread* self);
67
68 // Add a new task, the first available started worker will process it. Does not delete the task
69 // after running it, it is the caller's responsibility.
70 void AddTask(Thread* self, Closure* task);
71
72 ThreadPool(size_t num_threads);
73 virtual ~ThreadPool();
74
75 // Wait for all tasks currently on queue to get completed.
76 void Wait(Thread* self);
77
78 private:
79 // Add a new task.
80 void AddThread(size_t stack_size);
81
82 // Get a task to run, blocks if there are no tasks left
83 Closure* GetTask(Thread* self);
84
85 Mutex task_queue_lock_;
86 ConditionVariable task_queue_condition_ GUARDED_BY(task_queue_lock_);
87 ConditionVariable completion_condition_ GUARDED_BY(task_queue_lock_);
88 volatile bool started_ GUARDED_BY(task_queue_lock_);
89 volatile bool shutting_down_ GUARDED_BY(task_queue_lock_);
90 // How many worker threads are waiting on the condition.
91 volatile size_t waiting_count_ GUARDED_BY(task_queue_lock_);
92 std::deque<Closure*> tasks_ GUARDED_BY(task_queue_lock_);
93 // TODO: make this immutable/const?
94 std::vector<ThreadPoolWorker*> threads_;
95
96 friend class ThreadPoolWorker;
97 DISALLOW_COPY_AND_ASSIGN(ThreadPool);
98};
99
100} // namespace art
101
102#endif // ART_SRC_THREAD_POOL_H_