Chromium Code Reviews| 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" |
| 11 | 11 |
| 12 | 12 |
| 13 namespace dart { | 13 namespace dart { |
| 14 | 14 |
| 15 // The single thread local key which stores all the thread local data | 15 // The single thread local key which stores all the thread local data |
| 16 // for a thread. | 16 // for a thread. |
| 17 // TODO(koda): Can we merge this with ThreadInterrupter::thread_state_key_? | 17 // TODO(koda): Can we merge this with ThreadInterrupter::thread_state_key_? |
| 18 ThreadLocalKey Thread::thread_key_ = OSThread::kUnsetThreadLocalKey; | 18 ThreadLocalKey Thread::thread_key_ = OSThread::kUnsetThreadLocalKey; |
| 19 | 19 |
| 20 | 20 |
| 21 static void DeleteThread(void* thread) { | |
| 22 delete reinterpret_cast<Thread*>(thread); | |
| 23 } | |
| 24 | |
| 25 | |
| 21 void Thread::InitOnce() { | 26 void Thread::InitOnce() { |
| 22 ASSERT(thread_key_ == OSThread::kUnsetThreadLocalKey); | 27 ASSERT(thread_key_ == OSThread::kUnsetThreadLocalKey); |
| 23 thread_key_ = OSThread::CreateThreadLocal(); | 28 thread_key_ = OSThread::CreateThreadLocal(DeleteThread); |
| 24 ASSERT(thread_key_ != OSThread::kUnsetThreadLocalKey); | 29 ASSERT(thread_key_ != OSThread::kUnsetThreadLocalKey); |
| 25 } | 30 } |
| 26 | 31 |
| 27 | 32 |
| 28 void Thread::SetCurrent(Thread* current) { | 33 void Thread::SetCurrent(Thread* current) { |
| 29 OSThread::SetThreadLocal(thread_key_, reinterpret_cast<uword>(current)); | 34 OSThread::SetThreadLocal(thread_key_, reinterpret_cast<uword>(current)); |
| 30 } | 35 } |
| 31 | 36 |
| 32 | 37 |
| 33 void Thread::EnsureInit() { | 38 void Thread::EnsureInit() { |
| 34 if (Thread::Current() == NULL) { | 39 if (Thread::Current() == NULL) { |
| 35 SetCurrent(new Thread()); | 40 SetCurrent(new Thread()); |
| 36 } | 41 } |
| 37 } | 42 } |
| 38 | 43 |
| 39 | 44 |
| 45 #if defined(TARGET_OS_WINDOWS) | |
| 40 void Thread::CleanUp() { | 46 void Thread::CleanUp() { |
| 41 Thread* current = Current(); | 47 Thread* current = Current(); |
| 42 if (current != NULL) { | 48 if (current != NULL) { |
| 43 delete current; | 49 delete current; |
| 44 } | 50 } |
| 45 SetCurrent(NULL); | 51 SetCurrent(NULL); |
| 46 } | 52 } |
| 53 #endif | |
| 47 | 54 |
| 48 | 55 |
| 49 void Thread::EnterIsolate(Isolate* isolate) { | 56 void Thread::EnterIsolate(Isolate* isolate) { |
| 50 EnsureInit(); | 57 EnsureInit(); |
|
Ivan Posva
2015/05/15 15:40:52
How about initializing once in the thread pool and
koda
2015/05/15 17:30:04
Done (but not to the extreme).
| |
| 51 Thread* thread = Thread::Current(); | 58 Thread* thread = Thread::Current(); |
| 52 ASSERT(thread->isolate() == NULL); | 59 ASSERT(thread->isolate() == NULL); |
| 53 ASSERT(isolate->mutator_thread() == NULL); | 60 ASSERT(isolate->mutator_thread() == NULL); |
| 54 isolate->set_mutator_thread(thread); | 61 isolate->set_mutator_thread(thread); |
| 55 // TODO(koda): Migrate thread_state_ and profile_data_ to Thread, to allow | 62 // TODO(koda): Migrate thread_state_ and profile_data_ to Thread, to allow |
| 56 // helper threads concurrent with mutator. | 63 // helper threads concurrent with mutator. |
| 57 ASSERT(isolate->thread_state() == NULL); | 64 ASSERT(isolate->thread_state() == NULL); |
| 58 InterruptableThreadState* thread_state = | 65 InterruptableThreadState* thread_state = |
| 59 ThreadInterrupter::GetCurrentThreadState(); | 66 ThreadInterrupter::GetCurrentThreadState(); |
| 60 #if defined(DEBUG) | 67 #if defined(DEBUG) |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 80 } | 87 } |
| 81 isolate->set_thread_state(NULL); | 88 isolate->set_thread_state(NULL); |
| 82 Profiler::EndExecution(isolate); | 89 Profiler::EndExecution(isolate); |
| 83 isolate->set_mutator_thread(NULL); | 90 isolate->set_mutator_thread(NULL); |
| 84 thread->isolate_ = NULL; | 91 thread->isolate_ = NULL; |
| 85 ASSERT(Isolate::Current() == NULL); | 92 ASSERT(Isolate::Current() == NULL); |
| 86 } | 93 } |
| 87 | 94 |
| 88 | 95 |
| 89 void Thread::EnterIsolateAsHelper(Isolate* isolate) { | 96 void Thread::EnterIsolateAsHelper(Isolate* isolate) { |
| 90 EnsureInit(); | 97 EnsureInit(); |
|
Ivan Posva
2015/05/15 15:40:52
ditto
koda
2015/05/15 17:30:03
Done.
| |
| 91 Thread* thread = Thread::Current(); | 98 Thread* thread = Thread::Current(); |
| 92 ASSERT(thread->isolate() == NULL); | 99 ASSERT(thread->isolate() == NULL); |
| 93 thread->isolate_ = isolate; | 100 thread->isolate_ = isolate; |
| 94 // Do not update isolate->mutator_thread, but perform sanity check: | 101 // Do not update isolate->mutator_thread, but perform sanity check: |
| 95 // this thread should not be both the main mutator and helper. | 102 // this thread should not be both the main mutator and helper. |
| 96 ASSERT(isolate->mutator_thread() != thread); | 103 ASSERT(isolate->mutator_thread() != thread); |
| 97 } | 104 } |
| 98 | 105 |
| 99 | 106 |
| 100 void Thread::ExitIsolateAsHelper() { | 107 void Thread::ExitIsolateAsHelper() { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 111 return isolate_->cha_; | 118 return isolate_->cha_; |
| 112 } | 119 } |
| 113 | 120 |
| 114 | 121 |
| 115 void Thread::set_cha(CHA* value) { | 122 void Thread::set_cha(CHA* value) { |
| 116 ASSERT(isolate_ != NULL); | 123 ASSERT(isolate_ != NULL); |
| 117 isolate_->cha_ = value; | 124 isolate_->cha_ = value; |
| 118 } | 125 } |
| 119 | 126 |
| 120 } // namespace dart | 127 } // namespace dart |
| OLD | NEW |