OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_THREAD_POOL_H_ | 5 #ifndef VM_THREAD_POOL_H_ |
6 #define VM_THREAD_POOL_H_ | 6 #define VM_THREAD_POOL_H_ |
7 | 7 |
8 #include "vm/allocation.h" | |
8 #include "vm/globals.h" | 9 #include "vm/globals.h" |
9 #include "vm/os_thread.h" | 10 #include "vm/os_thread.h" |
10 | 11 |
11 namespace dart { | 12 namespace dart { |
12 | 13 |
13 class ThreadPool { | 14 class ThreadPool { |
14 public: | 15 public: |
15 // Subclasses of Task are able to run on a ThreadPool. | 16 // Subclasses of Task are able to run on a ThreadPool. |
16 class Task { | 17 class Task { |
17 protected: | 18 protected: |
18 Task(); | 19 Task(); |
19 | 20 |
20 public: | 21 public: |
21 virtual ~Task(); | 22 virtual ~Task(); |
22 | 23 |
23 // Override this to provide task-specific behavior. | 24 // Override this to provide task-specific behavior. |
24 virtual void Run() = 0; | 25 virtual void Run() = 0; |
25 | 26 |
26 private: | 27 private: |
27 DISALLOW_COPY_AND_ASSIGN(Task); | 28 DISALLOW_COPY_AND_ASSIGN(Task); |
28 }; | 29 }; |
29 | 30 |
30 ThreadPool(); | 31 ThreadPool(); |
31 | 32 |
32 // Shuts down this thread pool. Causes workers to terminate | 33 // Shuts down this thread pool. Causes workers to terminate |
33 // themselves when they are active again. | 34 // themselves when they are active again. |
34 ~ThreadPool(); | 35 ~ThreadPool(); |
35 | 36 |
36 // Runs a task on the thread pool. | 37 // Runs a task on the thread pool. |
37 void Run(Task* task); | 38 bool Run(Task* task); |
38 | 39 |
39 // Some simple stats. | 40 // Some simple stats. |
40 uint64_t workers_running() const { return count_running_; } | 41 uint64_t workers_running() const { return count_running_; } |
41 uint64_t workers_idle() const { return count_idle_; } | 42 uint64_t workers_idle() const { return count_idle_; } |
42 uint64_t workers_started() const { return count_started_; } | 43 uint64_t workers_started() const { return count_started_; } |
43 uint64_t workers_stopped() const { return count_stopped_; } | 44 uint64_t workers_stopped() const { return count_stopped_; } |
44 | 45 |
45 private: | 46 private: |
46 friend class ThreadPoolTestPeer; | |
47 | |
48 class Worker { | 47 class Worker { |
49 public: | 48 public: |
50 explicit Worker(ThreadPool* pool); | 49 explicit Worker(ThreadPool* pool); |
51 | 50 |
52 // Sets a task on the worker. | 51 // Sets a task on the worker. |
53 void SetTask(Task* task); | 52 void SetTask(Task* task); |
54 | 53 |
55 // Starts the thread for the worker. This should only be called | 54 // Starts the thread for the worker. This should only be called |
56 // after a task has been set by the initial call to SetTask(). | 55 // after a task has been set by the initial call to SetTask(). |
57 void StartThread(); | 56 void StartThread(); |
58 | 57 |
59 // Main loop for a worker. | 58 // Main loop for a worker. Returns true if worker is removed from thread |
60 void Loop(); | 59 // lists, false otherwise. |
60 bool Loop(); | |
61 | 61 |
62 // Causes worker to terminate eventually. | 62 // Causes worker to terminate eventually. |
63 void Shutdown(); | 63 void Shutdown(); |
64 | 64 |
65 // Get the Worker's thread id. | |
66 ThreadId id() const { return id_; } | |
turnidge
2015/09/15 17:46:42
From offline: iposva suggests putting locking insi
| |
67 ThreadJoinId join_id() const { return join_id_; } | |
turnidge
2015/09/15 17:46:42
From offline discussions with zra, iposva, and me
| |
68 | |
65 private: | 69 private: |
66 friend class ThreadPool; | 70 friend class ThreadPool; |
67 | 71 |
68 // The main entry point for new worker threads. | 72 // The main entry point for new worker threads. |
69 static void Main(uword args); | 73 static void Main(uword args); |
70 | 74 |
71 bool IsDone() const { return pool_ == NULL; } | 75 bool IsDone() const { return done_; } |
72 | 76 |
73 // Fields owned by Worker. | 77 // Fields owned by Worker. |
74 Monitor monitor_; | 78 Monitor monitor_; |
75 ThreadPool* pool_; | 79 ThreadPool* pool_; |
76 Task* task_; | 80 Task* task_; |
81 ThreadId id_; | |
82 ThreadJoinId join_id_; | |
83 bool done_; | |
77 | 84 |
78 // Fields owned by ThreadPool. Workers should not look at these | 85 // Fields owned by ThreadPool. Workers should not look at these |
79 // directly. It's like looking at the sun. | 86 // directly. It's like looking at the sun. |
80 bool owned_; // Protected by ThreadPool::mutex_ | 87 bool owned_; // Protected by ThreadPool::mutex_ |
81 Worker* all_next_; // Protected by ThreadPool::mutex_ | 88 Worker* all_next_; // Protected by ThreadPool::mutex_ |
82 Worker* idle_next_; // Protected by ThreadPool::mutex_ | 89 Worker* idle_next_; // Protected by ThreadPool::mutex_ |
83 | 90 |
91 Worker* shutdown_next_; // Protected by ThreadPool::exit_monitor | |
92 | |
84 DISALLOW_COPY_AND_ASSIGN(Worker); | 93 DISALLOW_COPY_AND_ASSIGN(Worker); |
85 }; | 94 }; |
86 | 95 |
96 class JoinList { | |
97 public: | |
98 explicit JoinList(ThreadJoinId id, JoinList* next) : id_(id), next_(next) { | |
99 } | |
100 | |
101 // The thread pool's mutex_ must be held when calling this. | |
102 static void AddLocked(ThreadJoinId id, JoinList** list); | |
103 | |
104 static void Join(JoinList** list); | |
105 | |
106 ThreadJoinId id() const { return id_; } | |
107 JoinList* next() const { return next_; } | |
108 | |
109 private: | |
110 ThreadJoinId id_; | |
111 JoinList* next_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(JoinList); | |
114 }; | |
115 | |
87 void Shutdown(); | 116 void Shutdown(); |
88 | 117 |
89 // Expensive. Use only in assertions. | 118 // Expensive. Use only in assertions. |
90 bool IsIdle(Worker* worker); | 119 bool IsIdle(Worker* worker); |
91 | 120 |
92 bool RemoveWorkerFromIdleList(Worker* worker); | 121 bool RemoveWorkerFromIdleList(Worker* worker); |
93 bool RemoveWorkerFromAllList(Worker* worker); | 122 bool RemoveWorkerFromAllList(Worker* worker); |
94 | 123 |
124 void AddWorkerToShutdownList(Worker* worker); | |
125 bool RemoveWorkerFromShutdownList(Worker* worker); | |
126 | |
127 void ReapExitedIdleThreads(); | |
128 | |
95 // Worker operations. | 129 // Worker operations. |
96 void SetIdle(Worker* worker); | 130 void SetIdleAndReapExited(Worker* worker); |
97 bool ReleaseIdleWorker(Worker* worker); | 131 bool ReleaseIdleWorker(Worker* worker); |
98 | 132 |
99 Mutex mutex_; | 133 Mutex mutex_; |
100 bool shutting_down_; | 134 bool shutting_down_; |
101 Worker* all_workers_; | 135 Worker* all_workers_; |
102 Worker* idle_workers_; | 136 Worker* idle_workers_; |
103 uint64_t count_started_; | 137 uint64_t count_started_; |
104 uint64_t count_stopped_; | 138 uint64_t count_stopped_; |
105 uint64_t count_running_; | 139 uint64_t count_running_; |
106 uint64_t count_idle_; | 140 uint64_t count_idle_; |
107 | 141 |
108 static Monitor* exit_monitor_; // Used only in testing. | 142 Monitor exit_monitor_; |
109 static int* exit_count_; // Used only in testing. | 143 Worker* shutting_down_workers_; |
144 JoinList* join_list_; | |
110 | 145 |
111 DISALLOW_COPY_AND_ASSIGN(ThreadPool); | 146 DISALLOW_COPY_AND_ASSIGN(ThreadPool); |
112 }; | 147 }; |
113 | 148 |
114 } // namespace dart | 149 } // namespace dart |
115 | 150 |
116 #endif // VM_THREAD_POOL_H_ | 151 #endif // VM_THREAD_POOL_H_ |
OLD | NEW |