OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 | 6 |
7 #include "src/v8.h" | 7 #include "src/v8.h" |
8 | 8 |
9 #include "src/ast.h" | 9 #include "src/ast.h" |
10 #include "src/base/platform/platform.h" | 10 #include "src/base/platform/platform.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 | 39 |
40 base::Atomic32 ThreadId::highest_thread_id_ = 0; | 40 base::Atomic32 ThreadId::highest_thread_id_ = 0; |
41 | 41 |
42 int ThreadId::AllocateThreadId() { | 42 int ThreadId::AllocateThreadId() { |
43 int new_id = base::NoBarrier_AtomicIncrement(&highest_thread_id_, 1); | 43 int new_id = base::NoBarrier_AtomicIncrement(&highest_thread_id_, 1); |
44 return new_id; | 44 return new_id; |
45 } | 45 } |
46 | 46 |
47 | 47 |
48 int ThreadId::GetCurrentThreadId() { | 48 int ThreadId::GetCurrentThreadId() { |
| 49 Isolate::EnsureInitialized(); |
49 int thread_id = base::Thread::GetThreadLocalInt(Isolate::thread_id_key_); | 50 int thread_id = base::Thread::GetThreadLocalInt(Isolate::thread_id_key_); |
50 if (thread_id == 0) { | 51 if (thread_id == 0) { |
51 thread_id = AllocateThreadId(); | 52 thread_id = AllocateThreadId(); |
52 base::Thread::SetThreadLocalInt(Isolate::thread_id_key_, thread_id); | 53 base::Thread::SetThreadLocalInt(Isolate::thread_id_key_, thread_id); |
53 } | 54 } |
54 return thread_id; | 55 return thread_id; |
55 } | 56 } |
56 | 57 |
57 | 58 |
58 ThreadLocalTop::ThreadLocalTop() { | 59 ThreadLocalTop::ThreadLocalTop() { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 thread_id_ = ThreadId::Current(); | 98 thread_id_ = ThreadId::Current(); |
98 } | 99 } |
99 | 100 |
100 | 101 |
101 base::Thread::LocalStorageKey Isolate::isolate_key_; | 102 base::Thread::LocalStorageKey Isolate::isolate_key_; |
102 base::Thread::LocalStorageKey Isolate::thread_id_key_; | 103 base::Thread::LocalStorageKey Isolate::thread_id_key_; |
103 base::Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_; | 104 base::Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_; |
104 #ifdef DEBUG | 105 #ifdef DEBUG |
105 base::Thread::LocalStorageKey PerThreadAssertScopeBase::thread_local_key; | 106 base::Thread::LocalStorageKey PerThreadAssertScopeBase::thread_local_key; |
106 #endif // DEBUG | 107 #endif // DEBUG |
107 base::Mutex Isolate::process_wide_mutex_; | 108 base::LazyMutex Isolate::process_wide_mutex_ = LAZY_MUTEX_INITIALIZER; |
108 // TODO(dcarney): Remove with default isolate. | |
109 enum DefaultIsolateStatus { | |
110 kDefaultIsolateUninitialized, | |
111 kDefaultIsolateInitialized, | |
112 kDefaultIsolateCrashIfInitialized | |
113 }; | |
114 static DefaultIsolateStatus default_isolate_status_ | |
115 = kDefaultIsolateUninitialized; | |
116 Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL; | 109 Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL; |
117 base::Atomic32 Isolate::isolate_counter_ = 0; | 110 base::Atomic32 Isolate::isolate_counter_ = 0; |
118 | 111 |
119 Isolate::PerIsolateThreadData* | 112 Isolate::PerIsolateThreadData* |
120 Isolate::FindOrAllocatePerThreadDataForThisThread() { | 113 Isolate::FindOrAllocatePerThreadDataForThisThread() { |
| 114 EnsureInitialized(); |
121 ThreadId thread_id = ThreadId::Current(); | 115 ThreadId thread_id = ThreadId::Current(); |
122 PerIsolateThreadData* per_thread = NULL; | 116 PerIsolateThreadData* per_thread = NULL; |
123 { | 117 { |
124 base::LockGuard<base::Mutex> lock_guard(&process_wide_mutex_); | 118 base::LockGuard<base::Mutex> lock_guard(process_wide_mutex_.Pointer()); |
125 per_thread = thread_data_table_->Lookup(this, thread_id); | 119 per_thread = thread_data_table_->Lookup(this, thread_id); |
126 if (per_thread == NULL) { | 120 if (per_thread == NULL) { |
127 per_thread = new PerIsolateThreadData(this, thread_id); | 121 per_thread = new PerIsolateThreadData(this, thread_id); |
128 thread_data_table_->Insert(per_thread); | 122 thread_data_table_->Insert(per_thread); |
129 } | 123 } |
130 ASSERT(thread_data_table_->Lookup(this, thread_id) == per_thread); | 124 ASSERT(thread_data_table_->Lookup(this, thread_id) == per_thread); |
131 } | 125 } |
132 return per_thread; | 126 return per_thread; |
133 } | 127 } |
134 | 128 |
135 | 129 |
136 Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThisThread() { | 130 Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThisThread() { |
137 ThreadId thread_id = ThreadId::Current(); | 131 ThreadId thread_id = ThreadId::Current(); |
138 return FindPerThreadDataForThread(thread_id); | 132 return FindPerThreadDataForThread(thread_id); |
139 } | 133 } |
140 | 134 |
141 | 135 |
142 Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThread( | 136 Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThread( |
143 ThreadId thread_id) { | 137 ThreadId thread_id) { |
| 138 EnsureInitialized(); |
144 PerIsolateThreadData* per_thread = NULL; | 139 PerIsolateThreadData* per_thread = NULL; |
145 { | 140 { |
146 base::LockGuard<base::Mutex> lock_guard(&process_wide_mutex_); | 141 base::LockGuard<base::Mutex> lock_guard(process_wide_mutex_.Pointer()); |
147 per_thread = thread_data_table_->Lookup(this, thread_id); | 142 per_thread = thread_data_table_->Lookup(this, thread_id); |
148 } | 143 } |
149 return per_thread; | 144 return per_thread; |
150 } | 145 } |
151 | 146 |
152 | 147 |
153 void Isolate::SetCrashIfDefaultIsolateInitialized() { | 148 void Isolate::EnsureInitialized() { |
154 base::LockGuard<base::Mutex> lock_guard(&process_wide_mutex_); | 149 base::LockGuard<base::Mutex> lock_guard(process_wide_mutex_.Pointer()); |
155 CHECK(default_isolate_status_ != kDefaultIsolateInitialized); | |
156 default_isolate_status_ = kDefaultIsolateCrashIfInitialized; | |
157 } | |
158 | |
159 | |
160 void Isolate::EnsureDefaultIsolate() { | |
161 base::LockGuard<base::Mutex> lock_guard(&process_wide_mutex_); | |
162 CHECK(default_isolate_status_ != kDefaultIsolateCrashIfInitialized); | |
163 if (thread_data_table_ == NULL) { | 150 if (thread_data_table_ == NULL) { |
164 isolate_key_ = base::Thread::CreateThreadLocalKey(); | 151 isolate_key_ = base::Thread::CreateThreadLocalKey(); |
165 thread_id_key_ = base::Thread::CreateThreadLocalKey(); | 152 thread_id_key_ = base::Thread::CreateThreadLocalKey(); |
166 per_isolate_thread_data_key_ = base::Thread::CreateThreadLocalKey(); | 153 per_isolate_thread_data_key_ = base::Thread::CreateThreadLocalKey(); |
167 #ifdef DEBUG | 154 #ifdef DEBUG |
168 PerThreadAssertScopeBase::thread_local_key = | 155 PerThreadAssertScopeBase::thread_local_key = |
169 base::Thread::CreateThreadLocalKey(); | 156 base::Thread::CreateThreadLocalKey(); |
170 #endif // DEBUG | 157 #endif // DEBUG |
171 thread_data_table_ = new Isolate::ThreadDataTable(); | 158 thread_data_table_ = new Isolate::ThreadDataTable(); |
172 } | 159 } |
173 } | 160 } |
174 | 161 |
175 struct StaticInitializer { | |
176 StaticInitializer() { | |
177 Isolate::EnsureDefaultIsolate(); | |
178 } | |
179 } static_initializer; | |
180 | |
181 | 162 |
182 Address Isolate::get_address_from_id(Isolate::AddressId id) { | 163 Address Isolate::get_address_from_id(Isolate::AddressId id) { |
183 return isolate_addresses_[id]; | 164 return isolate_addresses_[id]; |
184 } | 165 } |
185 | 166 |
186 | 167 |
187 char* Isolate::Iterate(ObjectVisitor* v, char* thread_storage) { | 168 char* Isolate::Iterate(ObjectVisitor* v, char* thread_storage) { |
188 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(thread_storage); | 169 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(thread_storage); |
189 Iterate(v, thread); | 170 Iterate(v, thread); |
190 return thread_storage + sizeof(ThreadLocalTop); | 171 return thread_storage + sizeof(ThreadLocalTop); |
(...skipping 1329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1520 // Temporarily set this isolate as current so that various parts of | 1501 // Temporarily set this isolate as current so that various parts of |
1521 // the isolate can access it in their destructors without having a | 1502 // the isolate can access it in their destructors without having a |
1522 // direct pointer. We don't use Enter/Exit here to avoid | 1503 // direct pointer. We don't use Enter/Exit here to avoid |
1523 // initializing the thread data. | 1504 // initializing the thread data. |
1524 PerIsolateThreadData* saved_data = CurrentPerIsolateThreadData(); | 1505 PerIsolateThreadData* saved_data = CurrentPerIsolateThreadData(); |
1525 Isolate* saved_isolate = UncheckedCurrent(); | 1506 Isolate* saved_isolate = UncheckedCurrent(); |
1526 SetIsolateThreadLocals(this, NULL); | 1507 SetIsolateThreadLocals(this, NULL); |
1527 | 1508 |
1528 Deinit(); | 1509 Deinit(); |
1529 | 1510 |
1530 { base::LockGuard<base::Mutex> lock_guard(&process_wide_mutex_); | 1511 { base::LockGuard<base::Mutex> lock_guard(process_wide_mutex_.Pointer()); |
1531 thread_data_table_->RemoveAllThreads(this); | 1512 thread_data_table_->RemoveAllThreads(this); |
1532 } | 1513 } |
1533 | 1514 |
1534 if (serialize_partial_snapshot_cache_ != NULL) { | 1515 if (serialize_partial_snapshot_cache_ != NULL) { |
1535 delete[] serialize_partial_snapshot_cache_; | 1516 delete[] serialize_partial_snapshot_cache_; |
1536 serialize_partial_snapshot_cache_ = NULL; | 1517 serialize_partial_snapshot_cache_ = NULL; |
1537 } | 1518 } |
1538 | 1519 |
1539 delete this; | 1520 delete this; |
1540 | 1521 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1621 set_serialize_partial_snapshot_cache_capacity(new_capacity); | 1602 set_serialize_partial_snapshot_cache_capacity(new_capacity); |
1622 } | 1603 } |
1623 | 1604 |
1624 serialize_partial_snapshot_cache()[length] = obj; | 1605 serialize_partial_snapshot_cache()[length] = obj; |
1625 set_serialize_partial_snapshot_cache_length(length + 1); | 1606 set_serialize_partial_snapshot_cache_length(length + 1); |
1626 } | 1607 } |
1627 | 1608 |
1628 | 1609 |
1629 void Isolate::SetIsolateThreadLocals(Isolate* isolate, | 1610 void Isolate::SetIsolateThreadLocals(Isolate* isolate, |
1630 PerIsolateThreadData* data) { | 1611 PerIsolateThreadData* data) { |
| 1612 EnsureInitialized(); |
1631 base::Thread::SetThreadLocal(isolate_key_, isolate); | 1613 base::Thread::SetThreadLocal(isolate_key_, isolate); |
1632 base::Thread::SetThreadLocal(per_isolate_thread_data_key_, data); | 1614 base::Thread::SetThreadLocal(per_isolate_thread_data_key_, data); |
1633 } | 1615 } |
1634 | 1616 |
1635 | 1617 |
1636 Isolate::~Isolate() { | 1618 Isolate::~Isolate() { |
1637 TRACE_ISOLATE(destructor); | 1619 TRACE_ISOLATE(destructor); |
1638 | 1620 |
1639 // Has to be called while counters_ are still alive | 1621 // Has to be called while counters_ are still alive |
1640 runtime_zone_.DeleteKeptSegment(); | 1622 runtime_zone_.DeleteKeptSegment(); |
(...skipping 721 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2362 // The simulator uses a separate JS stack. | 2344 // The simulator uses a separate JS stack. |
2363 Address jssp_address = Simulator::current(isolate_)->get_sp(); | 2345 Address jssp_address = Simulator::current(isolate_)->get_sp(); |
2364 uintptr_t jssp = reinterpret_cast<uintptr_t>(jssp_address); | 2346 uintptr_t jssp = reinterpret_cast<uintptr_t>(jssp_address); |
2365 if (jssp < stack_guard->real_jslimit()) return true; | 2347 if (jssp < stack_guard->real_jslimit()) return true; |
2366 #endif // USE_SIMULATOR | 2348 #endif // USE_SIMULATOR |
2367 return reinterpret_cast<uintptr_t>(this) < stack_guard->real_climit(); | 2349 return reinterpret_cast<uintptr_t>(this) < stack_guard->real_climit(); |
2368 } | 2350 } |
2369 | 2351 |
2370 | 2352 |
2371 } } // namespace v8::internal | 2353 } } // namespace v8::internal |
OLD | NEW |