| 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(); |  | 
| 51   Thread* thread = Thread::Current(); | 57   Thread* thread = Thread::Current(); | 
|  | 58   ASSERT(thread != NULL); | 
| 52   ASSERT(thread->isolate() == NULL); | 59   ASSERT(thread->isolate() == NULL); | 
| 53   ASSERT(isolate->mutator_thread() == NULL); | 60   ASSERT(isolate->mutator_thread() == NULL); | 
| 54   thread->isolate_ = isolate; | 61   thread->isolate_ = isolate; | 
| 55   isolate->set_mutator_thread(thread); | 62   isolate->set_mutator_thread(thread); | 
| 56   // TODO(koda): Migrate thread_state_ and profile_data_ to Thread, to allow | 63   // TODO(koda): Migrate thread_state_ and profile_data_ to Thread, to allow | 
| 57   // helper threads concurrent with mutator. | 64   // helper threads concurrent with mutator. | 
| 58   ASSERT(isolate->thread_state() == NULL); | 65   ASSERT(isolate->thread_state() == NULL); | 
| 59   InterruptableThreadState* thread_state = | 66   InterruptableThreadState* thread_state = | 
| 60       ThreadInterrupter::GetCurrentThreadState(); | 67       ThreadInterrupter::GetCurrentThreadState(); | 
| 61 #if defined(DEBUG) | 68 #if defined(DEBUG) | 
| (...skipping 18 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(); |  | 
| 91   Thread* thread = Thread::Current(); | 97   Thread* thread = Thread::Current(); | 
|  | 98   ASSERT(thread != NULL); | 
| 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() { | 
| 101   Thread* thread = Thread::Current(); | 108   Thread* thread = Thread::Current(); | 
| 102   Isolate* isolate = thread->isolate(); | 109   Isolate* isolate = thread->isolate(); | 
| 103   ASSERT(isolate != NULL); | 110   ASSERT(isolate != NULL); | 
| 104   thread->isolate_ = NULL; | 111   thread->isolate_ = NULL; | 
| 105   ASSERT(isolate->mutator_thread() != thread); | 112   ASSERT(isolate->mutator_thread() != thread); | 
| 106 } | 113 } | 
| 107 | 114 | 
| 108 | 115 | 
| 109 CHA* Thread::cha() const { | 116 CHA* Thread::cha() const { | 
| 110   ASSERT(isolate_ != NULL); | 117   ASSERT(isolate_ != NULL); | 
| 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 | 
|---|