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 #include "vm/thread.h" | 5 #include "vm/thread.h" |
6 | 6 |
7 #include "vm/isolate.h" | 7 #include "vm/isolate.h" |
8 #include "vm/os_thread.h" | 8 #include "vm/os_thread.h" |
9 #include "vm/profiler.h" | 9 #include "vm/profiler.h" |
10 #include "vm/thread_interrupter.h" | 10 #include "vm/thread_interrupter.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 | 32 |
33 void Thread::EnsureInit() { | 33 void Thread::EnsureInit() { |
34 if (Thread::Current() == NULL) { | 34 if (Thread::Current() == NULL) { |
35 SetCurrent(new Thread()); | 35 SetCurrent(new Thread()); |
36 } | 36 } |
37 } | 37 } |
38 | 38 |
39 | 39 |
40 void Thread::CleanUp() { | 40 void Thread::CleanUp() { |
41 // We currently deallocate the Thread, to ensure that embedder threads don't | |
42 // leak the Thread structure. An alternative approach would be to clear and | |
43 // reuse it, but register a destructor at the OS level. | |
44 Thread* current = Current(); | 41 Thread* current = Current(); |
45 if (current != NULL) { | 42 if (current != NULL) { |
46 delete current; | 43 delete current; |
47 } | 44 } |
48 SetCurrent(NULL); | 45 SetCurrent(NULL); |
49 } | 46 } |
50 | 47 |
51 | 48 |
52 void Thread::EnterIsolate(Isolate* isolate) { | 49 void Thread::EnterIsolate(Isolate* isolate) { |
53 EnsureInit(); | 50 EnsureInit(); |
(...skipping 13 matching lines...) Expand all Loading... |
67 Profiler::BeginExecution(isolate); | 64 Profiler::BeginExecution(isolate); |
68 isolate->set_thread_state(thread_state); | 65 isolate->set_thread_state(thread_state); |
69 isolate->set_vm_tag(VMTag::kVMTagId); | 66 isolate->set_vm_tag(VMTag::kVMTagId); |
70 thread->isolate_ = isolate; | 67 thread->isolate_ = isolate; |
71 } | 68 } |
72 | 69 |
73 | 70 |
74 void Thread::ExitIsolate() { | 71 void Thread::ExitIsolate() { |
75 Thread* thread = Thread::Current(); | 72 Thread* thread = Thread::Current(); |
76 // TODO(koda): Audit callers; they should know whether they're in an isolate. | 73 // TODO(koda): Audit callers; they should know whether they're in an isolate. |
77 if (thread == NULL) return; | 74 if (thread == NULL || thread->isolate() == NULL) return; |
78 Isolate* isolate = thread->isolate(); | 75 Isolate* isolate = thread->isolate(); |
79 ASSERT(isolate != NULL); | |
80 if (isolate->is_runnable()) { | 76 if (isolate->is_runnable()) { |
81 isolate->set_vm_tag(VMTag::kIdleTagId); | 77 isolate->set_vm_tag(VMTag::kIdleTagId); |
82 } else { | 78 } else { |
83 isolate->set_vm_tag(VMTag::kLoadWaitTagId); | 79 isolate->set_vm_tag(VMTag::kLoadWaitTagId); |
84 } | 80 } |
85 isolate->set_thread_state(NULL); | 81 isolate->set_thread_state(NULL); |
86 Profiler::EndExecution(isolate); | 82 Profiler::EndExecution(isolate); |
87 isolate->set_mutator_thread(NULL); | 83 isolate->set_mutator_thread(NULL); |
88 thread->isolate_ = NULL; | 84 thread->isolate_ = NULL; |
89 ASSERT(Isolate::Current() == NULL); | 85 ASSERT(Isolate::Current() == NULL); |
90 CleanUp(); | 86 } |
| 87 |
| 88 |
| 89 void Thread::EnterIsolateAsHelper(Isolate* isolate) { |
| 90 EnsureInit(); |
| 91 Thread* thread = Thread::Current(); |
| 92 ASSERT(thread->isolate() == NULL); |
| 93 thread->isolate_ = isolate; |
| 94 // Do not update isolate->mutator_thread, but perform sanity check: |
| 95 // this thread should not be both the main mutator and helper. |
| 96 ASSERT(isolate->mutator_thread() != thread); |
| 97 } |
| 98 |
| 99 |
| 100 void Thread::ExitIsolateAsHelper() { |
| 101 Thread* thread = Thread::Current(); |
| 102 Isolate* isolate = thread->isolate(); |
| 103 ASSERT(isolate != NULL); |
| 104 thread->isolate_ = NULL; |
| 105 ASSERT(isolate->mutator_thread() != thread); |
91 } | 106 } |
92 | 107 |
93 | 108 |
94 CHA* Thread::cha() const { | 109 CHA* Thread::cha() const { |
95 ASSERT(isolate_ != NULL); | 110 ASSERT(isolate_ != NULL); |
96 return isolate_->cha_; | 111 return isolate_->cha_; |
97 } | 112 } |
98 | 113 |
99 | 114 |
100 void Thread::set_cha(CHA* value) { | 115 void Thread::set_cha(CHA* value) { |
101 ASSERT(isolate_ != NULL); | 116 ASSERT(isolate_ != NULL); |
102 isolate_->cha_ = value; | 117 isolate_->cha_ = value; |
103 } | 118 } |
104 | 119 |
105 } // namespace dart | 120 } // namespace dart |
OLD | NEW |