ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | |
| 30 | #include "v8.h" |
| 31 | |
| 32 | #include "ast.h" |
| 33 | #include "bootstrapper.h" |
| 34 | #include "codegen.h" |
| 35 | #include "compilation-cache.h" |
| 36 | #include "debug.h" |
| 37 | #include "deoptimizer.h" |
| 38 | #include "heap-profiler.h" |
| 39 | #include "hydrogen.h" |
| 40 | #include "isolate.h" |
| 41 | #include "lithium-allocator.h" |
| 42 | #include "log.h" |
| 43 | #include "regexp-stack.h" |
| 44 | #include "runtime-profiler.h" |
| 45 | #include "scanner.h" |
| 46 | #include "scopeinfo.h" |
| 47 | #include "serialize.h" |
| 48 | #include "simulator.h" |
| 49 | #include "spaces.h" |
| 50 | #include "stub-cache.h" |
| 51 | #include "version.h" |
| 52 | |
| 53 | |
| 54 | namespace v8 { |
| 55 | namespace internal { |
| 56 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 57 | Atomic32 ThreadId::highest_thread_id_ = 0; |
| 58 | |
| 59 | int ThreadId::AllocateThreadId() { |
| 60 | int new_id = NoBarrier_AtomicIncrement(&highest_thread_id_, 1); |
| 61 | return new_id; |
| 62 | } |
| 63 | |
| 64 | int ThreadId::GetCurrentThreadId() { |
| 65 | int thread_id = Thread::GetThreadLocalInt(Isolate::thread_id_key_); |
| 66 | if (thread_id == 0) { |
| 67 | thread_id = AllocateThreadId(); |
| 68 | Thread::SetThreadLocalInt(Isolate::thread_id_key_, thread_id); |
| 69 | } |
| 70 | return thread_id; |
| 71 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 72 | |
| 73 | // Create a dummy thread that will wait forever on a semaphore. The only |
| 74 | // purpose for this thread is to have some stack area to save essential data |
| 75 | // into for use by a stacks only core dump (aka minidump). |
| 76 | class PreallocatedMemoryThread: public Thread { |
| 77 | public: |
| 78 | char* data() { |
| 79 | if (data_ready_semaphore_ != NULL) { |
| 80 | // Initial access is guarded until the data has been published. |
| 81 | data_ready_semaphore_->Wait(); |
| 82 | delete data_ready_semaphore_; |
| 83 | data_ready_semaphore_ = NULL; |
| 84 | } |
| 85 | return data_; |
| 86 | } |
| 87 | |
| 88 | unsigned length() { |
| 89 | if (data_ready_semaphore_ != NULL) { |
| 90 | // Initial access is guarded until the data has been published. |
| 91 | data_ready_semaphore_->Wait(); |
| 92 | delete data_ready_semaphore_; |
| 93 | data_ready_semaphore_ = NULL; |
| 94 | } |
| 95 | return length_; |
| 96 | } |
| 97 | |
| 98 | // Stop the PreallocatedMemoryThread and release its resources. |
| 99 | void StopThread() { |
| 100 | keep_running_ = false; |
| 101 | wait_for_ever_semaphore_->Signal(); |
| 102 | |
| 103 | // Wait for the thread to terminate. |
| 104 | Join(); |
| 105 | |
| 106 | if (data_ready_semaphore_ != NULL) { |
| 107 | delete data_ready_semaphore_; |
| 108 | data_ready_semaphore_ = NULL; |
| 109 | } |
| 110 | |
| 111 | delete wait_for_ever_semaphore_; |
| 112 | wait_for_ever_semaphore_ = NULL; |
| 113 | } |
| 114 | |
| 115 | protected: |
| 116 | // When the thread starts running it will allocate a fixed number of bytes |
| 117 | // on the stack and publish the location of this memory for others to use. |
| 118 | void Run() { |
| 119 | EmbeddedVector<char, 15 * 1024> local_buffer; |
| 120 | |
| 121 | // Initialize the buffer with a known good value. |
| 122 | OS::StrNCpy(local_buffer, "Trace data was not generated.\n", |
| 123 | local_buffer.length()); |
| 124 | |
| 125 | // Publish the local buffer and signal its availability. |
| 126 | data_ = local_buffer.start(); |
| 127 | length_ = local_buffer.length(); |
| 128 | data_ready_semaphore_->Signal(); |
| 129 | |
| 130 | while (keep_running_) { |
| 131 | // This thread will wait here until the end of time. |
| 132 | wait_for_ever_semaphore_->Wait(); |
| 133 | } |
| 134 | |
| 135 | // Make sure we access the buffer after the wait to remove all possibility |
| 136 | // of it being optimized away. |
| 137 | OS::StrNCpy(local_buffer, "PreallocatedMemoryThread shutting down.\n", |
| 138 | local_buffer.length()); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | private: |
| 143 | explicit PreallocatedMemoryThread(Isolate* isolate) |
| 144 | : Thread(isolate, "v8:PreallocMem"), |
| 145 | keep_running_(true), |
| 146 | wait_for_ever_semaphore_(OS::CreateSemaphore(0)), |
| 147 | data_ready_semaphore_(OS::CreateSemaphore(0)), |
| 148 | data_(NULL), |
| 149 | length_(0) { |
| 150 | } |
| 151 | |
| 152 | // Used to make sure that the thread keeps looping even for spurious wakeups. |
| 153 | bool keep_running_; |
| 154 | |
| 155 | // This semaphore is used by the PreallocatedMemoryThread to wait for ever. |
| 156 | Semaphore* wait_for_ever_semaphore_; |
| 157 | // Semaphore to signal that the data has been initialized. |
| 158 | Semaphore* data_ready_semaphore_; |
| 159 | |
| 160 | // Location and size of the preallocated memory block. |
| 161 | char* data_; |
| 162 | unsigned length_; |
| 163 | |
| 164 | friend class Isolate; |
| 165 | |
| 166 | DISALLOW_COPY_AND_ASSIGN(PreallocatedMemoryThread); |
| 167 | }; |
| 168 | |
| 169 | |
| 170 | void Isolate::PreallocatedMemoryThreadStart() { |
| 171 | if (preallocated_memory_thread_ != NULL) return; |
| 172 | preallocated_memory_thread_ = new PreallocatedMemoryThread(this); |
| 173 | preallocated_memory_thread_->Start(); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | void Isolate::PreallocatedMemoryThreadStop() { |
| 178 | if (preallocated_memory_thread_ == NULL) return; |
| 179 | preallocated_memory_thread_->StopThread(); |
| 180 | // Done with the thread entirely. |
| 181 | delete preallocated_memory_thread_; |
| 182 | preallocated_memory_thread_ = NULL; |
| 183 | } |
| 184 | |
| 185 | |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 186 | void Isolate::PreallocatedStorageInit(size_t size) { |
| 187 | ASSERT(free_list_.next_ == &free_list_); |
| 188 | ASSERT(free_list_.previous_ == &free_list_); |
| 189 | PreallocatedStorage* free_chunk = |
| 190 | reinterpret_cast<PreallocatedStorage*>(new char[size]); |
| 191 | free_list_.next_ = free_list_.previous_ = free_chunk; |
| 192 | free_chunk->next_ = free_chunk->previous_ = &free_list_; |
| 193 | free_chunk->size_ = size - sizeof(PreallocatedStorage); |
| 194 | preallocated_storage_preallocated_ = true; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | void* Isolate::PreallocatedStorageNew(size_t size) { |
| 199 | if (!preallocated_storage_preallocated_) { |
| 200 | return FreeStoreAllocationPolicy::New(size); |
| 201 | } |
| 202 | ASSERT(free_list_.next_ != &free_list_); |
| 203 | ASSERT(free_list_.previous_ != &free_list_); |
| 204 | |
| 205 | size = (size + kPointerSize - 1) & ~(kPointerSize - 1); |
| 206 | // Search for exact fit. |
| 207 | for (PreallocatedStorage* storage = free_list_.next_; |
| 208 | storage != &free_list_; |
| 209 | storage = storage->next_) { |
| 210 | if (storage->size_ == size) { |
| 211 | storage->Unlink(); |
| 212 | storage->LinkTo(&in_use_list_); |
| 213 | return reinterpret_cast<void*>(storage + 1); |
| 214 | } |
| 215 | } |
| 216 | // Search for first fit. |
| 217 | for (PreallocatedStorage* storage = free_list_.next_; |
| 218 | storage != &free_list_; |
| 219 | storage = storage->next_) { |
| 220 | if (storage->size_ >= size + sizeof(PreallocatedStorage)) { |
| 221 | storage->Unlink(); |
| 222 | storage->LinkTo(&in_use_list_); |
| 223 | PreallocatedStorage* left_over = |
| 224 | reinterpret_cast<PreallocatedStorage*>( |
| 225 | reinterpret_cast<char*>(storage + 1) + size); |
| 226 | left_over->size_ = storage->size_ - size - sizeof(PreallocatedStorage); |
| 227 | ASSERT(size + left_over->size_ + sizeof(PreallocatedStorage) == |
| 228 | storage->size_); |
| 229 | storage->size_ = size; |
| 230 | left_over->LinkTo(&free_list_); |
| 231 | return reinterpret_cast<void*>(storage + 1); |
| 232 | } |
| 233 | } |
| 234 | // Allocation failure. |
| 235 | ASSERT(false); |
| 236 | return NULL; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | // We don't attempt to coalesce. |
| 241 | void Isolate::PreallocatedStorageDelete(void* p) { |
| 242 | if (p == NULL) { |
| 243 | return; |
| 244 | } |
| 245 | if (!preallocated_storage_preallocated_) { |
| 246 | FreeStoreAllocationPolicy::Delete(p); |
| 247 | return; |
| 248 | } |
| 249 | PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1; |
| 250 | ASSERT(storage->next_->previous_ == storage); |
| 251 | ASSERT(storage->previous_->next_ == storage); |
| 252 | storage->Unlink(); |
| 253 | storage->LinkTo(&free_list_); |
| 254 | } |
| 255 | |
| 256 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 257 | Isolate* Isolate::default_isolate_ = NULL; |
| 258 | Thread::LocalStorageKey Isolate::isolate_key_; |
| 259 | Thread::LocalStorageKey Isolate::thread_id_key_; |
| 260 | Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_; |
| 261 | Mutex* Isolate::process_wide_mutex_ = OS::CreateMutex(); |
| 262 | Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 263 | |
| 264 | |
| 265 | class IsolateInitializer { |
| 266 | public: |
| 267 | IsolateInitializer() { |
| 268 | Isolate::EnsureDefaultIsolate(); |
| 269 | } |
| 270 | }; |
| 271 | |
| 272 | static IsolateInitializer* EnsureDefaultIsolateAllocated() { |
| 273 | // TODO(isolates): Use the system threading API to do this once? |
| 274 | static IsolateInitializer static_initializer; |
| 275 | return &static_initializer; |
| 276 | } |
| 277 | |
| 278 | // This variable only needed to trigger static intialization. |
| 279 | static IsolateInitializer* static_initializer = EnsureDefaultIsolateAllocated(); |
| 280 | |
| 281 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 282 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 283 | |
| 284 | |
| 285 | Isolate::PerIsolateThreadData* Isolate::AllocatePerIsolateThreadData( |
| 286 | ThreadId thread_id) { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 287 | ASSERT(!thread_id.Equals(ThreadId::Invalid())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 288 | PerIsolateThreadData* per_thread = new PerIsolateThreadData(this, thread_id); |
| 289 | { |
| 290 | ScopedLock lock(process_wide_mutex_); |
| 291 | ASSERT(thread_data_table_->Lookup(this, thread_id) == NULL); |
| 292 | thread_data_table_->Insert(per_thread); |
| 293 | ASSERT(thread_data_table_->Lookup(this, thread_id) == per_thread); |
| 294 | } |
| 295 | return per_thread; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | Isolate::PerIsolateThreadData* |
| 300 | Isolate::FindOrAllocatePerThreadDataForThisThread() { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 301 | ThreadId thread_id = ThreadId::Current(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 302 | PerIsolateThreadData* per_thread = NULL; |
| 303 | { |
| 304 | ScopedLock lock(process_wide_mutex_); |
| 305 | per_thread = thread_data_table_->Lookup(this, thread_id); |
| 306 | if (per_thread == NULL) { |
| 307 | per_thread = AllocatePerIsolateThreadData(thread_id); |
| 308 | } |
| 309 | } |
| 310 | return per_thread; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | void Isolate::EnsureDefaultIsolate() { |
| 315 | ScopedLock lock(process_wide_mutex_); |
| 316 | if (default_isolate_ == NULL) { |
| 317 | isolate_key_ = Thread::CreateThreadLocalKey(); |
| 318 | thread_id_key_ = Thread::CreateThreadLocalKey(); |
| 319 | per_isolate_thread_data_key_ = Thread::CreateThreadLocalKey(); |
| 320 | thread_data_table_ = new Isolate::ThreadDataTable(); |
| 321 | default_isolate_ = new Isolate(); |
| 322 | } |
| 323 | // Can't use SetIsolateThreadLocals(default_isolate_, NULL) here |
| 324 | // becase a non-null thread data may be already set. |
| 325 | Thread::SetThreadLocal(isolate_key_, default_isolate_); |
| 326 | CHECK(default_isolate_->PreInit()); |
| 327 | } |
| 328 | |
| 329 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame^] | 330 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 331 | Debugger* Isolate::GetDefaultIsolateDebugger() { |
| 332 | EnsureDefaultIsolate(); |
| 333 | return default_isolate_->debugger(); |
| 334 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame^] | 335 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 336 | |
| 337 | |
| 338 | StackGuard* Isolate::GetDefaultIsolateStackGuard() { |
| 339 | EnsureDefaultIsolate(); |
| 340 | return default_isolate_->stack_guard(); |
| 341 | } |
| 342 | |
| 343 | |
| 344 | void Isolate::EnterDefaultIsolate() { |
| 345 | EnsureDefaultIsolate(); |
| 346 | ASSERT(default_isolate_ != NULL); |
| 347 | |
| 348 | PerIsolateThreadData* data = CurrentPerIsolateThreadData(); |
| 349 | // If not yet in default isolate - enter it. |
| 350 | if (data == NULL || data->isolate() != default_isolate_) { |
| 351 | default_isolate_->Enter(); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | |
| 356 | Isolate* Isolate::GetDefaultIsolateForLocking() { |
| 357 | EnsureDefaultIsolate(); |
| 358 | return default_isolate_; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | Isolate::ThreadDataTable::ThreadDataTable() |
| 363 | : list_(NULL) { |
| 364 | } |
| 365 | |
| 366 | |
| 367 | Isolate::PerIsolateThreadData* |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 368 | Isolate::ThreadDataTable::Lookup(Isolate* isolate, |
| 369 | ThreadId thread_id) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 370 | for (PerIsolateThreadData* data = list_; data != NULL; data = data->next_) { |
| 371 | if (data->Matches(isolate, thread_id)) return data; |
| 372 | } |
| 373 | return NULL; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | void Isolate::ThreadDataTable::Insert(Isolate::PerIsolateThreadData* data) { |
| 378 | if (list_ != NULL) list_->prev_ = data; |
| 379 | data->next_ = list_; |
| 380 | list_ = data; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | void Isolate::ThreadDataTable::Remove(PerIsolateThreadData* data) { |
| 385 | if (list_ == data) list_ = data->next_; |
| 386 | if (data->next_ != NULL) data->next_->prev_ = data->prev_; |
| 387 | if (data->prev_ != NULL) data->prev_->next_ = data->next_; |
| 388 | } |
| 389 | |
| 390 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 391 | void Isolate::ThreadDataTable::Remove(Isolate* isolate, |
| 392 | ThreadId thread_id) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 393 | PerIsolateThreadData* data = Lookup(isolate, thread_id); |
| 394 | if (data != NULL) { |
| 395 | Remove(data); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | |
| 400 | #ifdef DEBUG |
| 401 | #define TRACE_ISOLATE(tag) \ |
| 402 | do { \ |
| 403 | if (FLAG_trace_isolates) { \ |
| 404 | PrintF("Isolate %p " #tag "\n", reinterpret_cast<void*>(this)); \ |
| 405 | } \ |
| 406 | } while (false) |
| 407 | #else |
| 408 | #define TRACE_ISOLATE(tag) |
| 409 | #endif |
| 410 | |
| 411 | |
| 412 | Isolate::Isolate() |
| 413 | : state_(UNINITIALIZED), |
| 414 | entry_stack_(NULL), |
| 415 | stack_trace_nesting_level_(0), |
| 416 | incomplete_message_(NULL), |
| 417 | preallocated_memory_thread_(NULL), |
| 418 | preallocated_message_space_(NULL), |
| 419 | bootstrapper_(NULL), |
| 420 | runtime_profiler_(NULL), |
| 421 | compilation_cache_(NULL), |
| 422 | counters_(new Counters()), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 423 | code_range_(NULL), |
| 424 | break_access_(OS::CreateMutex()), |
| 425 | logger_(new Logger()), |
| 426 | stats_table_(new StatsTable()), |
| 427 | stub_cache_(NULL), |
| 428 | deoptimizer_data_(NULL), |
| 429 | capture_stack_trace_for_uncaught_exceptions_(false), |
| 430 | stack_trace_for_uncaught_exceptions_frame_limit_(0), |
| 431 | stack_trace_for_uncaught_exceptions_options_(StackTrace::kOverview), |
| 432 | transcendental_cache_(NULL), |
| 433 | memory_allocator_(NULL), |
| 434 | keyed_lookup_cache_(NULL), |
| 435 | context_slot_cache_(NULL), |
| 436 | descriptor_lookup_cache_(NULL), |
| 437 | handle_scope_implementer_(NULL), |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 438 | unicode_cache_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 439 | in_use_list_(0), |
| 440 | free_list_(0), |
| 441 | preallocated_storage_preallocated_(false), |
| 442 | pc_to_code_cache_(NULL), |
| 443 | write_input_buffer_(NULL), |
| 444 | global_handles_(NULL), |
| 445 | context_switcher_(NULL), |
| 446 | thread_manager_(NULL), |
| 447 | ast_sentinels_(NULL), |
| 448 | string_tracker_(NULL), |
| 449 | regexp_stack_(NULL), |
| 450 | frame_element_constant_list_(0), |
| 451 | result_constant_list_(0) { |
| 452 | TRACE_ISOLATE(constructor); |
| 453 | |
| 454 | memset(isolate_addresses_, 0, |
| 455 | sizeof(isolate_addresses_[0]) * (k_isolate_address_count + 1)); |
| 456 | |
| 457 | heap_.isolate_ = this; |
| 458 | zone_.isolate_ = this; |
| 459 | stack_guard_.isolate_ = this; |
| 460 | |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 461 | #if defined(V8_TARGET_ARCH_ARM) && !defined(__arm__) || \ |
| 462 | defined(V8_TARGET_ARCH_MIPS) && !defined(__mips__) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 463 | simulator_initialized_ = false; |
| 464 | simulator_i_cache_ = NULL; |
| 465 | simulator_redirection_ = NULL; |
| 466 | #endif |
| 467 | |
| 468 | #ifdef DEBUG |
| 469 | // heap_histograms_ initializes itself. |
| 470 | memset(&js_spill_information_, 0, sizeof(js_spill_information_)); |
| 471 | memset(code_kind_statistics_, 0, |
| 472 | sizeof(code_kind_statistics_[0]) * Code::NUMBER_OF_KINDS); |
| 473 | #endif |
| 474 | |
| 475 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 476 | debug_ = NULL; |
| 477 | debugger_ = NULL; |
| 478 | #endif |
| 479 | |
| 480 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 481 | producer_heap_profile_ = NULL; |
| 482 | #endif |
| 483 | |
| 484 | handle_scope_data_.Initialize(); |
| 485 | |
| 486 | #define ISOLATE_INIT_EXECUTE(type, name, initial_value) \ |
| 487 | name##_ = (initial_value); |
| 488 | ISOLATE_INIT_LIST(ISOLATE_INIT_EXECUTE) |
| 489 | #undef ISOLATE_INIT_EXECUTE |
| 490 | |
| 491 | #define ISOLATE_INIT_ARRAY_EXECUTE(type, name, length) \ |
| 492 | memset(name##_, 0, sizeof(type) * length); |
| 493 | ISOLATE_INIT_ARRAY_LIST(ISOLATE_INIT_ARRAY_EXECUTE) |
| 494 | #undef ISOLATE_INIT_ARRAY_EXECUTE |
| 495 | } |
| 496 | |
| 497 | void Isolate::TearDown() { |
| 498 | TRACE_ISOLATE(tear_down); |
| 499 | |
| 500 | // Temporarily set this isolate as current so that various parts of |
| 501 | // the isolate can access it in their destructors without having a |
| 502 | // direct pointer. We don't use Enter/Exit here to avoid |
| 503 | // initializing the thread data. |
| 504 | PerIsolateThreadData* saved_data = CurrentPerIsolateThreadData(); |
| 505 | Isolate* saved_isolate = UncheckedCurrent(); |
| 506 | SetIsolateThreadLocals(this, NULL); |
| 507 | |
| 508 | Deinit(); |
| 509 | |
| 510 | if (!IsDefaultIsolate()) { |
| 511 | delete this; |
| 512 | } |
| 513 | |
| 514 | // Restore the previous current isolate. |
| 515 | SetIsolateThreadLocals(saved_isolate, saved_data); |
| 516 | } |
| 517 | |
| 518 | |
| 519 | void Isolate::Deinit() { |
| 520 | if (state_ == INITIALIZED) { |
| 521 | TRACE_ISOLATE(deinit); |
| 522 | |
| 523 | if (FLAG_hydrogen_stats) HStatistics::Instance()->Print(); |
| 524 | |
| 525 | // We must stop the logger before we tear down other components. |
| 526 | logger_->EnsureTickerStopped(); |
| 527 | |
| 528 | delete deoptimizer_data_; |
| 529 | deoptimizer_data_ = NULL; |
| 530 | if (FLAG_preemption) { |
| 531 | v8::Locker locker; |
| 532 | v8::Locker::StopPreemption(); |
| 533 | } |
| 534 | builtins_.TearDown(); |
| 535 | bootstrapper_->TearDown(); |
| 536 | |
| 537 | // Remove the external reference to the preallocated stack memory. |
| 538 | delete preallocated_message_space_; |
| 539 | preallocated_message_space_ = NULL; |
| 540 | PreallocatedMemoryThreadStop(); |
| 541 | |
| 542 | HeapProfiler::TearDown(); |
| 543 | CpuProfiler::TearDown(); |
| 544 | if (runtime_profiler_ != NULL) { |
| 545 | runtime_profiler_->TearDown(); |
| 546 | delete runtime_profiler_; |
| 547 | runtime_profiler_ = NULL; |
| 548 | } |
| 549 | heap_.TearDown(); |
| 550 | logger_->TearDown(); |
| 551 | |
| 552 | // The default isolate is re-initializable due to legacy API. |
| 553 | state_ = PREINITIALIZED; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | |
| 558 | void Isolate::SetIsolateThreadLocals(Isolate* isolate, |
| 559 | PerIsolateThreadData* data) { |
| 560 | Thread::SetThreadLocal(isolate_key_, isolate); |
| 561 | Thread::SetThreadLocal(per_isolate_thread_data_key_, data); |
| 562 | } |
| 563 | |
| 564 | |
| 565 | Isolate::~Isolate() { |
| 566 | TRACE_ISOLATE(destructor); |
| 567 | |
| 568 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 569 | delete producer_heap_profile_; |
| 570 | producer_heap_profile_ = NULL; |
| 571 | #endif |
| 572 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 573 | delete unicode_cache_; |
| 574 | unicode_cache_ = NULL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 575 | |
| 576 | delete regexp_stack_; |
| 577 | regexp_stack_ = NULL; |
| 578 | |
| 579 | delete ast_sentinels_; |
| 580 | ast_sentinels_ = NULL; |
| 581 | |
| 582 | delete descriptor_lookup_cache_; |
| 583 | descriptor_lookup_cache_ = NULL; |
| 584 | delete context_slot_cache_; |
| 585 | context_slot_cache_ = NULL; |
| 586 | delete keyed_lookup_cache_; |
| 587 | keyed_lookup_cache_ = NULL; |
| 588 | |
| 589 | delete transcendental_cache_; |
| 590 | transcendental_cache_ = NULL; |
| 591 | delete stub_cache_; |
| 592 | stub_cache_ = NULL; |
| 593 | delete stats_table_; |
| 594 | stats_table_ = NULL; |
| 595 | |
| 596 | delete logger_; |
| 597 | logger_ = NULL; |
| 598 | |
| 599 | delete counters_; |
| 600 | counters_ = NULL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 601 | |
| 602 | delete handle_scope_implementer_; |
| 603 | handle_scope_implementer_ = NULL; |
| 604 | delete break_access_; |
| 605 | break_access_ = NULL; |
| 606 | |
| 607 | delete compilation_cache_; |
| 608 | compilation_cache_ = NULL; |
| 609 | delete bootstrapper_; |
| 610 | bootstrapper_ = NULL; |
| 611 | delete pc_to_code_cache_; |
| 612 | pc_to_code_cache_ = NULL; |
| 613 | delete write_input_buffer_; |
| 614 | write_input_buffer_ = NULL; |
| 615 | |
| 616 | delete context_switcher_; |
| 617 | context_switcher_ = NULL; |
| 618 | delete thread_manager_; |
| 619 | thread_manager_ = NULL; |
| 620 | |
| 621 | delete string_tracker_; |
| 622 | string_tracker_ = NULL; |
| 623 | |
| 624 | delete memory_allocator_; |
| 625 | memory_allocator_ = NULL; |
| 626 | delete code_range_; |
| 627 | code_range_ = NULL; |
| 628 | delete global_handles_; |
| 629 | global_handles_ = NULL; |
| 630 | |
| 631 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 632 | delete debugger_; |
| 633 | debugger_ = NULL; |
| 634 | delete debug_; |
| 635 | debug_ = NULL; |
| 636 | #endif |
| 637 | } |
| 638 | |
| 639 | |
| 640 | bool Isolate::PreInit() { |
| 641 | if (state_ != UNINITIALIZED) return true; |
| 642 | |
| 643 | TRACE_ISOLATE(preinit); |
| 644 | |
| 645 | ASSERT(Isolate::Current() == this); |
| 646 | |
| 647 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 648 | debug_ = new Debug(this); |
| 649 | debugger_ = new Debugger(); |
| 650 | debugger_->isolate_ = this; |
| 651 | #endif |
| 652 | |
| 653 | memory_allocator_ = new MemoryAllocator(); |
| 654 | memory_allocator_->isolate_ = this; |
| 655 | code_range_ = new CodeRange(); |
| 656 | code_range_->isolate_ = this; |
| 657 | |
| 658 | // Safe after setting Heap::isolate_, initializing StackGuard and |
| 659 | // ensuring that Isolate::Current() == this. |
| 660 | heap_.SetStackLimits(); |
| 661 | |
| 662 | #ifdef DEBUG |
| 663 | DisallowAllocationFailure disallow_allocation_failure; |
| 664 | #endif |
| 665 | |
| 666 | #define C(name) isolate_addresses_[Isolate::k_##name] = \ |
| 667 | reinterpret_cast<Address>(name()); |
| 668 | ISOLATE_ADDRESS_LIST(C) |
| 669 | ISOLATE_ADDRESS_LIST_PROF(C) |
| 670 | #undef C |
| 671 | |
| 672 | string_tracker_ = new StringTracker(); |
| 673 | string_tracker_->isolate_ = this; |
| 674 | thread_manager_ = new ThreadManager(); |
| 675 | thread_manager_->isolate_ = this; |
| 676 | compilation_cache_ = new CompilationCache(this); |
| 677 | transcendental_cache_ = new TranscendentalCache(); |
| 678 | keyed_lookup_cache_ = new KeyedLookupCache(); |
| 679 | context_slot_cache_ = new ContextSlotCache(); |
| 680 | descriptor_lookup_cache_ = new DescriptorLookupCache(); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 681 | unicode_cache_ = new UnicodeCache(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 682 | pc_to_code_cache_ = new PcToCodeCache(this); |
| 683 | write_input_buffer_ = new StringInputBuffer(); |
| 684 | global_handles_ = new GlobalHandles(this); |
| 685 | bootstrapper_ = new Bootstrapper(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 686 | handle_scope_implementer_ = new HandleScopeImplementer(); |
| 687 | stub_cache_ = new StubCache(this); |
| 688 | ast_sentinels_ = new AstSentinels(); |
| 689 | regexp_stack_ = new RegExpStack(); |
| 690 | regexp_stack_->isolate_ = this; |
| 691 | |
| 692 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 693 | producer_heap_profile_ = new ProducerHeapProfile(); |
| 694 | producer_heap_profile_->isolate_ = this; |
| 695 | #endif |
| 696 | |
| 697 | state_ = PREINITIALIZED; |
| 698 | return true; |
| 699 | } |
| 700 | |
| 701 | |
| 702 | void Isolate::InitializeThreadLocal() { |
| 703 | thread_local_top_.Initialize(); |
| 704 | clear_pending_exception(); |
| 705 | clear_pending_message(); |
| 706 | clear_scheduled_exception(); |
| 707 | } |
| 708 | |
| 709 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 710 | void Isolate::PropagatePendingExceptionToExternalTryCatch() { |
| 711 | ASSERT(has_pending_exception()); |
| 712 | |
| 713 | bool external_caught = IsExternallyCaught(); |
| 714 | thread_local_top_.external_caught_exception_ = external_caught; |
| 715 | |
| 716 | if (!external_caught) return; |
| 717 | |
| 718 | if (thread_local_top_.pending_exception_ == Failure::OutOfMemoryException()) { |
| 719 | // Do not propagate OOM exception: we should kill VM asap. |
| 720 | } else if (thread_local_top_.pending_exception_ == |
| 721 | heap()->termination_exception()) { |
| 722 | try_catch_handler()->can_continue_ = false; |
| 723 | try_catch_handler()->exception_ = heap()->null_value(); |
| 724 | } else { |
| 725 | // At this point all non-object (failure) exceptions have |
| 726 | // been dealt with so this shouldn't fail. |
| 727 | ASSERT(!pending_exception()->IsFailure()); |
| 728 | try_catch_handler()->can_continue_ = true; |
| 729 | try_catch_handler()->exception_ = pending_exception(); |
| 730 | if (!thread_local_top_.pending_message_obj_->IsTheHole()) { |
| 731 | try_catch_handler()->message_ = thread_local_top_.pending_message_obj_; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 737 | bool Isolate::Init(Deserializer* des) { |
| 738 | ASSERT(state_ != INITIALIZED); |
| 739 | |
| 740 | TRACE_ISOLATE(init); |
| 741 | |
| 742 | bool create_heap_objects = des == NULL; |
| 743 | |
| 744 | #ifdef DEBUG |
| 745 | // The initialization process does not handle memory exhaustion. |
| 746 | DisallowAllocationFailure disallow_allocation_failure; |
| 747 | #endif |
| 748 | |
| 749 | if (state_ == UNINITIALIZED && !PreInit()) return false; |
| 750 | |
| 751 | // Enable logging before setting up the heap |
| 752 | logger_->Setup(); |
| 753 | |
| 754 | CpuProfiler::Setup(); |
| 755 | HeapProfiler::Setup(); |
| 756 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 757 | // Initialize other runtime facilities |
| 758 | #if defined(USE_SIMULATOR) |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 759 | #if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_MIPS) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 760 | Simulator::Initialize(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 761 | #endif |
| 762 | #endif |
| 763 | |
| 764 | { // NOLINT |
| 765 | // Ensure that the thread has a valid stack guard. The v8::Locker object |
| 766 | // will ensure this too, but we don't have to use lockers if we are only |
| 767 | // using one thread. |
| 768 | ExecutionAccess lock(this); |
| 769 | stack_guard_.InitThread(lock); |
| 770 | } |
| 771 | |
| 772 | // Setup the object heap |
| 773 | ASSERT(!heap_.HasBeenSetup()); |
| 774 | if (!heap_.Setup(create_heap_objects)) { |
| 775 | V8::SetFatalError(); |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | bootstrapper_->Initialize(create_heap_objects); |
| 780 | builtins_.Setup(create_heap_objects); |
| 781 | |
| 782 | InitializeThreadLocal(); |
| 783 | |
| 784 | // Only preallocate on the first initialization. |
| 785 | if (FLAG_preallocate_message_memory && preallocated_message_space_ == NULL) { |
| 786 | // Start the thread which will set aside some memory. |
| 787 | PreallocatedMemoryThreadStart(); |
| 788 | preallocated_message_space_ = |
| 789 | new NoAllocationStringAllocator( |
| 790 | preallocated_memory_thread_->data(), |
| 791 | preallocated_memory_thread_->length()); |
| 792 | PreallocatedStorageInit(preallocated_memory_thread_->length() / 4); |
| 793 | } |
| 794 | |
| 795 | if (FLAG_preemption) { |
| 796 | v8::Locker locker; |
| 797 | v8::Locker::StartPreemption(100); |
| 798 | } |
| 799 | |
| 800 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 801 | debug_->Setup(create_heap_objects); |
| 802 | #endif |
| 803 | stub_cache_->Initialize(create_heap_objects); |
| 804 | |
| 805 | // If we are deserializing, read the state into the now-empty heap. |
| 806 | if (des != NULL) { |
| 807 | des->Deserialize(); |
| 808 | stub_cache_->Clear(); |
| 809 | } |
| 810 | |
| 811 | // Deserializing may put strange things in the root array's copy of the |
| 812 | // stack guard. |
| 813 | heap_.SetStackLimits(); |
| 814 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 815 | deoptimizer_data_ = new DeoptimizerData; |
| 816 | runtime_profiler_ = new RuntimeProfiler(this); |
| 817 | runtime_profiler_->Setup(); |
| 818 | |
| 819 | // If we are deserializing, log non-function code objects and compiled |
| 820 | // functions found in the snapshot. |
| 821 | if (des != NULL && FLAG_log_code) { |
| 822 | HandleScope scope; |
| 823 | LOG(this, LogCodeObjects()); |
| 824 | LOG(this, LogCompiledFunctions()); |
| 825 | } |
| 826 | |
| 827 | state_ = INITIALIZED; |
| 828 | return true; |
| 829 | } |
| 830 | |
| 831 | |
| 832 | void Isolate::Enter() { |
| 833 | Isolate* current_isolate = NULL; |
| 834 | PerIsolateThreadData* current_data = CurrentPerIsolateThreadData(); |
| 835 | if (current_data != NULL) { |
| 836 | current_isolate = current_data->isolate_; |
| 837 | ASSERT(current_isolate != NULL); |
| 838 | if (current_isolate == this) { |
| 839 | ASSERT(Current() == this); |
| 840 | ASSERT(entry_stack_ != NULL); |
| 841 | ASSERT(entry_stack_->previous_thread_data == NULL || |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 842 | entry_stack_->previous_thread_data->thread_id().Equals( |
| 843 | ThreadId::Current())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 844 | // Same thread re-enters the isolate, no need to re-init anything. |
| 845 | entry_stack_->entry_count++; |
| 846 | return; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | // Threads can have default isolate set into TLS as Current but not yet have |
| 851 | // PerIsolateThreadData for it, as it requires more advanced phase of the |
| 852 | // initialization. For example, a thread might be the one that system used for |
| 853 | // static initializers - in this case the default isolate is set in TLS but |
| 854 | // the thread did not yet Enter the isolate. If PerisolateThreadData is not |
| 855 | // there, use the isolate set in TLS. |
| 856 | if (current_isolate == NULL) { |
| 857 | current_isolate = Isolate::UncheckedCurrent(); |
| 858 | } |
| 859 | |
| 860 | PerIsolateThreadData* data = FindOrAllocatePerThreadDataForThisThread(); |
| 861 | ASSERT(data != NULL); |
| 862 | ASSERT(data->isolate_ == this); |
| 863 | |
| 864 | EntryStackItem* item = new EntryStackItem(current_data, |
| 865 | current_isolate, |
| 866 | entry_stack_); |
| 867 | entry_stack_ = item; |
| 868 | |
| 869 | SetIsolateThreadLocals(this, data); |
| 870 | |
| 871 | CHECK(PreInit()); |
| 872 | |
| 873 | // In case it's the first time some thread enters the isolate. |
| 874 | set_thread_id(data->thread_id()); |
| 875 | } |
| 876 | |
| 877 | |
| 878 | void Isolate::Exit() { |
| 879 | ASSERT(entry_stack_ != NULL); |
| 880 | ASSERT(entry_stack_->previous_thread_data == NULL || |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 881 | entry_stack_->previous_thread_data->thread_id().Equals( |
| 882 | ThreadId::Current())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 883 | |
| 884 | if (--entry_stack_->entry_count > 0) return; |
| 885 | |
| 886 | ASSERT(CurrentPerIsolateThreadData() != NULL); |
| 887 | ASSERT(CurrentPerIsolateThreadData()->isolate_ == this); |
| 888 | |
| 889 | // Pop the stack. |
| 890 | EntryStackItem* item = entry_stack_; |
| 891 | entry_stack_ = item->previous_item; |
| 892 | |
| 893 | PerIsolateThreadData* previous_thread_data = item->previous_thread_data; |
| 894 | Isolate* previous_isolate = item->previous_isolate; |
| 895 | |
| 896 | delete item; |
| 897 | |
| 898 | // Reinit the current thread for the isolate it was running before this one. |
| 899 | SetIsolateThreadLocals(previous_isolate, previous_thread_data); |
| 900 | } |
| 901 | |
| 902 | |
| 903 | void Isolate::ResetEagerOptimizingData() { |
| 904 | compilation_cache_->ResetEagerOptimizingData(); |
| 905 | } |
| 906 | |
| 907 | |
| 908 | #ifdef DEBUG |
| 909 | #define ISOLATE_FIELD_OFFSET(type, name, ignored) \ |
| 910 | const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_); |
| 911 | ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET) |
| 912 | ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET) |
| 913 | #undef ISOLATE_FIELD_OFFSET |
| 914 | #endif |
| 915 | |
| 916 | } } // namespace v8::internal |