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_OS_THREAD_H_ | 5 #ifndef VM_OS_THREAD_H_ |
6 #define VM_OS_THREAD_H_ | 6 #define VM_OS_THREAD_H_ |
7 | 7 |
8 #include "platform/globals.h" | 8 #include "platform/globals.h" |
9 #include "vm/allocation.h" | 9 #include "vm/allocation.h" |
10 #include "vm/globals.h" | 10 #include "vm/globals.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 friend class Thread; | 43 friend class Thread; |
44 friend class OSThread; | 44 friend class OSThread; |
45 | 45 |
46 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseThread); | 46 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseThread); |
47 }; | 47 }; |
48 | 48 |
49 | 49 |
50 // Low-level operations on OS platform threads. | 50 // Low-level operations on OS platform threads. |
51 class OSThread : public BaseThread { | 51 class OSThread : public BaseThread { |
52 public: | 52 public: |
53 OSThread(); | 53 // The constructor of OSThread is never called directly, instead we call |
| 54 // this factory style method 'CreateOSThread' to create OSThread structures. |
| 55 // The method can return a NULL if the Dart VM is in shutdown mode. |
| 56 static OSThread* CreateOSThread(); |
54 ~OSThread(); | 57 ~OSThread(); |
55 | 58 |
56 ThreadId id() const { | 59 ThreadId id() const { |
57 ASSERT(id_ != OSThread::kInvalidThreadId); | 60 ASSERT(id_ != OSThread::kInvalidThreadId); |
58 return id_; | 61 return id_; |
59 } | 62 } |
60 | 63 |
61 ThreadJoinId join_id() const { | 64 ThreadJoinId join_id() const { |
62 ASSERT(join_id_ != OSThread::kInvalidThreadJoinId); | 65 ASSERT(join_id_ != OSThread::kInvalidThreadJoinId); |
63 return join_id_; | 66 return join_id_; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 static intptr_t ThreadIdToIntPtr(ThreadId id); | 171 static intptr_t ThreadIdToIntPtr(ThreadId id); |
169 static ThreadId ThreadIdFromIntPtr(intptr_t id); | 172 static ThreadId ThreadIdFromIntPtr(intptr_t id); |
170 static bool Compare(ThreadId a, ThreadId b); | 173 static bool Compare(ThreadId a, ThreadId b); |
171 static void GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage); | 174 static void GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage); |
172 | 175 |
173 // Called at VM startup and shutdown. | 176 // Called at VM startup and shutdown. |
174 static void InitOnce(); | 177 static void InitOnce(); |
175 | 178 |
176 static bool IsThreadInList(ThreadJoinId join_id); | 179 static bool IsThreadInList(ThreadJoinId join_id); |
177 | 180 |
| 181 static void DisableOSThreadCreation(); |
| 182 static void EnableOSThreadCreation(); |
| 183 |
178 static const intptr_t kStackSizeBuffer = (4 * KB * kWordSize); | 184 static const intptr_t kStackSizeBuffer = (4 * KB * kWordSize); |
179 | 185 |
180 static const ThreadId kInvalidThreadId; | 186 static const ThreadId kInvalidThreadId; |
181 static const ThreadJoinId kInvalidThreadJoinId; | 187 static const ThreadJoinId kInvalidThreadJoinId; |
182 | 188 |
183 private: | 189 private: |
| 190 // The constructor is private as CreateOSThread should be used |
| 191 // to create a new OSThread structure. |
| 192 OSThread(); |
| 193 |
184 // These methods should not be used in a generic way and hence | 194 // These methods should not be used in a generic way and hence |
185 // are private, they have been added to solve the problem of | 195 // are private, they have been added to solve the problem of |
186 // accessing the VM thread structure from an OSThread object | 196 // accessing the VM thread structure from an OSThread object |
187 // in the windows thread interrupter which is used for profiling. | 197 // in the windows thread interrupter which is used for profiling. |
188 // We could eliminate this requirement if the windows thread interrupter | 198 // We could eliminate this requirement if the windows thread interrupter |
189 // is implemented differently. | 199 // is implemented differently. |
190 Thread* thread() const { return thread_; } | 200 Thread* thread() const { return thread_; } |
191 void set_thread(Thread* value) { | 201 void set_thread(Thread* value) { |
192 thread_ = value; | 202 thread_ = value; |
193 } | 203 } |
194 | 204 |
195 static void Cleanup(); | 205 static void Cleanup(); |
196 static ThreadId GetCurrentThreadTraceId(); | 206 static ThreadId GetCurrentThreadTraceId(); |
197 static ThreadJoinId GetCurrentThreadJoinId(); | 207 static ThreadJoinId GetCurrentThreadJoinId(); |
198 static OSThread* GetOSThreadFromThread(Thread* thread); | 208 static OSThread* GetOSThreadFromThread(Thread* thread); |
199 static void AddThreadToList(OSThread* thread); | 209 static void AddThreadToListLocked(OSThread* thread); |
200 static void RemoveThreadFromList(OSThread* thread); | 210 static void RemoveThreadFromList(OSThread* thread); |
201 static OSThread* CreateAndSetUnknownThread(); | 211 static OSThread* CreateAndSetUnknownThread(); |
202 | 212 |
203 static ThreadLocalKey thread_key_; | 213 static ThreadLocalKey thread_key_; |
204 | 214 |
205 const ThreadId id_; | 215 const ThreadId id_; |
206 const ThreadJoinId join_id_; | 216 const ThreadJoinId join_id_; |
207 const ThreadId trace_id_; // Used to interface with tracing tools. | 217 const ThreadId trace_id_; // Used to interface with tracing tools. |
208 char* name_; // A name for this thread. | 218 char* name_; // A name for this thread. |
209 | 219 |
210 Mutex* timeline_block_lock_; | 220 Mutex* timeline_block_lock_; |
211 TimelineEventBlock* timeline_block_; | 221 TimelineEventBlock* timeline_block_; |
212 | 222 |
213 // All |Thread|s are registered in the thread list. | 223 // All |Thread|s are registered in the thread list. |
214 OSThread* thread_list_next_; | 224 OSThread* thread_list_next_; |
215 | 225 |
216 uintptr_t thread_interrupt_disabled_; | 226 uintptr_t thread_interrupt_disabled_; |
217 Log* log_; | 227 Log* log_; |
218 uword stack_base_; | 228 uword stack_base_; |
219 Thread* thread_; | 229 Thread* thread_; |
220 | 230 |
221 static OSThread* thread_list_head_; | 231 static OSThread* thread_list_head_; |
222 static Mutex* thread_list_lock_; | 232 static Mutex* thread_list_lock_; |
| 233 static bool creation_enabled_; |
223 | 234 |
224 friend class OSThreadIterator; | 235 friend class OSThreadIterator; |
225 friend class ThreadInterrupterWin; | 236 friend class ThreadInterrupterWin; |
226 friend class ThreadRegistry; | 237 friend class ThreadRegistry; |
227 }; | 238 }; |
228 | 239 |
229 | 240 |
230 // Note that this takes the thread list lock, prohibiting threads from coming | 241 // Note that this takes the thread list lock, prohibiting threads from coming |
231 // on- or off-line. | 242 // on- or off-line. |
232 class OSThreadIterator : public ValueObject { | 243 class OSThreadIterator : public ValueObject { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 #endif // defined(DEBUG) | 327 #endif // defined(DEBUG) |
317 | 328 |
318 DISALLOW_COPY_AND_ASSIGN(Monitor); | 329 DISALLOW_COPY_AND_ASSIGN(Monitor); |
319 }; | 330 }; |
320 | 331 |
321 | 332 |
322 } // namespace dart | 333 } // namespace dart |
323 | 334 |
324 | 335 |
325 #endif // VM_OS_THREAD_H_ | 336 #endif // VM_OS_THREAD_H_ |
OLD | NEW |