| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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_H_ | 5 #ifndef VM_THREAD_H_ |
| 6 #define VM_THREAD_H_ | 6 #define VM_THREAD_H_ |
| 7 | 7 |
| 8 #include "vm/base_isolate.h" | 8 #include "vm/base_isolate.h" |
| 9 #include "vm/globals.h" | 9 #include "vm/globals.h" |
| 10 #include "vm/os_thread.h" | 10 #include "vm/os_thread.h" |
| 11 | 11 |
| 12 namespace dart { | 12 namespace dart { |
| 13 | 13 |
| 14 class CHA; | 14 class CHA; |
| 15 class Isolate; | 15 class Isolate; |
| 16 | 16 |
| 17 // A VM thread; may be executing Dart code or performing helper tasks like | 17 // A VM thread; may be executing Dart code or performing helper tasks like |
| 18 // garbage collection or compilation. The Thread structure associated with | 18 // garbage collection or compilation. The Thread structure associated with |
| 19 // a thread is allocated when the thread enters an isolate, and destroyed | 19 // a thread is allocated when the thread enters an isolate, and destroyed |
| 20 // upon an explicit call to CleanUp. | 20 // when the underlying OS thread exits. NOTE: On Windows, Thread::CleanUp |
| 21 // | 21 // must currently be called manually (issue 23474). |
| 22 // NOTE: When the underlying OS thread is started by the embedder, i.e., not | |
| 23 // part of a ThreadPool managed by the VM, we currently leak the Thread | |
| 24 // structure after its last isolate exit. We could add an explicit cleanup API, | |
| 25 // or, on platforms that support it, register a cleanup callback. | |
| 26 class Thread { | 22 class Thread { |
| 27 public: | 23 public: |
| 28 // The currently executing thread, or NULL if not yet initialized. | 24 // The currently executing thread, or NULL if not yet initialized. |
| 29 static Thread* Current() { | 25 static Thread* Current() { |
| 30 return reinterpret_cast<Thread*>(OSThread::GetThreadLocal(thread_key_)); | 26 return reinterpret_cast<Thread*>(OSThread::GetThreadLocal(thread_key_)); |
| 31 } | 27 } |
| 32 | 28 |
| 33 // Makes the current thread enter 'isolate'. Also calls EnsureInit. | 29 // Makes the current thread enter 'isolate'. |
| 34 static void EnterIsolate(Isolate* isolate); | 30 static void EnterIsolate(Isolate* isolate); |
| 35 // Makes the current thread exit its isolate. | 31 // Makes the current thread exit its isolate. |
| 36 static void ExitIsolate(); | 32 static void ExitIsolate(); |
| 37 | 33 |
| 38 // A VM thread other than the main mutator thread can enter an isolate as a | 34 // A VM thread other than the main mutator thread can enter an isolate as a |
| 39 // "helper" to gain limited concurrent access to the isolate. One example is | 35 // "helper" to gain limited concurrent access to the isolate. One example is |
| 40 // SweeperTask (which uses the class table, which is copy-on-write). | 36 // SweeperTask (which uses the class table, which is copy-on-write). |
| 41 // TODO(koda): Properly synchronize heap access to expand allowed operations. | 37 // TODO(koda): Properly synchronize heap access to expand allowed operations. |
| 42 static void EnterIsolateAsHelper(Isolate* isolate); | 38 static void EnterIsolateAsHelper(Isolate* isolate); |
| 43 static void ExitIsolateAsHelper(); | 39 static void ExitIsolateAsHelper(); |
| 44 | 40 |
| 45 // Initializes the current thread as a VM thread, if not already done. | 41 #if defined(TARGET_OS_WINDOWS) |
| 46 static void EnsureInit(); | 42 // Clears the state of the current thread and frees the allocation. |
| 47 // Clears the state of the current thread. | |
| 48 static void CleanUp(); | 43 static void CleanUp(); |
| 44 #endif |
| 49 | 45 |
| 50 // Called at VM startup. | 46 // Called at VM startup. |
| 51 static void InitOnce(); | 47 static void InitOnce(); |
| 52 | 48 |
| 53 // The topmost zone used for allocation in this thread. | 49 // The topmost zone used for allocation in this thread. |
| 54 Zone* zone() { | 50 Zone* zone() { |
| 55 return reinterpret_cast<BaseIsolate*>(isolate())->current_zone(); | 51 return reinterpret_cast<BaseIsolate*>(isolate())->current_zone(); |
| 56 } | 52 } |
| 57 | 53 |
| 58 // The isolate that this thread is operating on, or NULL if none. | 54 // The isolate that this thread is operating on, or NULL if none. |
| 59 Isolate* isolate() const { return isolate_; } | 55 Isolate* isolate() const { return isolate_; } |
| 60 | 56 |
| 61 // The (topmost) CHA for the compilation in the isolate of this thread. | 57 // The (topmost) CHA for the compilation in the isolate of this thread. |
| 62 // TODO(23153): Move this out of Isolate/Thread. | 58 // TODO(23153): Move this out of Isolate/Thread. |
| 63 CHA* cha() const; | 59 CHA* cha() const; |
| 64 void set_cha(CHA* value); | 60 void set_cha(CHA* value); |
| 65 | 61 |
| 66 private: | 62 private: |
| 63 // Initializes the current thread as a VM thread, if not already done. |
| 64 static void EnsureInit(); |
| 65 |
| 67 static ThreadLocalKey thread_key_; | 66 static ThreadLocalKey thread_key_; |
| 68 | 67 |
| 69 Isolate* isolate_; | 68 Isolate* isolate_; |
| 70 | 69 |
| 71 Thread() | 70 Thread() |
| 72 : isolate_(NULL) {} | 71 : isolate_(NULL) {} |
| 73 | 72 |
| 74 static void SetCurrent(Thread* current); | 73 static void SetCurrent(Thread* current); |
| 75 | 74 |
| 76 DISALLOW_COPY_AND_ASSIGN(Thread); | 75 DISALLOW_COPY_AND_ASSIGN(Thread); |
| 77 }; | 76 }; |
| 78 | 77 |
| 79 } // namespace dart | 78 } // namespace dart |
| 80 | 79 |
| 81 #endif // VM_THREAD_H_ | 80 #endif // VM_THREAD_H_ |
| OLD | NEW |