OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 FreeStoreAllocationPolicy::Delete(p); | 330 FreeStoreAllocationPolicy::Delete(p); |
331 return; | 331 return; |
332 } | 332 } |
333 PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1; | 333 PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1; |
334 ASSERT(storage->next_->previous_ == storage); | 334 ASSERT(storage->next_->previous_ == storage); |
335 ASSERT(storage->previous_->next_ == storage); | 335 ASSERT(storage->previous_->next_ == storage); |
336 storage->Unlink(); | 336 storage->Unlink(); |
337 storage->LinkTo(&free_list_); | 337 storage->LinkTo(&free_list_); |
338 } | 338 } |
339 | 339 |
340 Isolate* Isolate::default_isolate_ = NULL; | |
341 Thread::LocalStorageKey Isolate::isolate_key_; | 340 Thread::LocalStorageKey Isolate::isolate_key_; |
342 Thread::LocalStorageKey Isolate::thread_id_key_; | 341 Thread::LocalStorageKey Isolate::thread_id_key_; |
343 Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_; | 342 Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_; |
344 #ifdef DEBUG | 343 #ifdef DEBUG |
345 Thread::LocalStorageKey PerThreadAssertScopeBase::thread_local_key; | 344 Thread::LocalStorageKey PerThreadAssertScopeBase::thread_local_key; |
346 #endif // DEBUG | 345 #endif // DEBUG |
347 Mutex Isolate::process_wide_mutex_; | 346 Mutex Isolate::process_wide_mutex_; |
348 // TODO(dcarney): Remove with default isolate. | 347 // TODO(dcarney): Remove with default isolate. |
349 enum DefaultIsolateStatus { | 348 enum DefaultIsolateStatus { |
350 kDefaultIsolateUninitialized, | 349 kDefaultIsolateUninitialized, |
351 kDefaultIsolateInitialized, | 350 kDefaultIsolateInitialized, |
352 kDefaultIsolateCrashIfInitialized | 351 kDefaultIsolateCrashIfInitialized |
353 }; | 352 }; |
354 static DefaultIsolateStatus default_isolate_status_ | 353 static DefaultIsolateStatus default_isolate_status_ |
355 = kDefaultIsolateUninitialized; | 354 = kDefaultIsolateUninitialized; |
356 Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL; | 355 Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL; |
357 Atomic32 Isolate::isolate_counter_ = 0; | 356 Atomic32 Isolate::isolate_counter_ = 0; |
| 357 Atomic32 Isolate::living_isolates_ = 0; |
358 | 358 |
359 Isolate::PerIsolateThreadData* | 359 Isolate::PerIsolateThreadData* |
360 Isolate::FindOrAllocatePerThreadDataForThisThread() { | 360 Isolate::FindOrAllocatePerThreadDataForThisThread() { |
361 ThreadId thread_id = ThreadId::Current(); | 361 ThreadId thread_id = ThreadId::Current(); |
362 PerIsolateThreadData* per_thread = NULL; | 362 PerIsolateThreadData* per_thread = NULL; |
363 { | 363 { |
364 LockGuard<Mutex> lock_guard(&process_wide_mutex_); | 364 LockGuard<Mutex> lock_guard(&process_wide_mutex_); |
365 per_thread = thread_data_table_->Lookup(this, thread_id); | 365 per_thread = thread_data_table_->Lookup(this, thread_id); |
366 if (per_thread == NULL) { | 366 if (per_thread == NULL) { |
367 per_thread = new PerIsolateThreadData(this, thread_id); | 367 per_thread = new PerIsolateThreadData(this, thread_id); |
(...skipping 22 matching lines...) Expand all Loading... |
390 } | 390 } |
391 | 391 |
392 | 392 |
393 void Isolate::SetCrashIfDefaultIsolateInitialized() { | 393 void Isolate::SetCrashIfDefaultIsolateInitialized() { |
394 LockGuard<Mutex> lock_guard(&process_wide_mutex_); | 394 LockGuard<Mutex> lock_guard(&process_wide_mutex_); |
395 CHECK(default_isolate_status_ != kDefaultIsolateInitialized); | 395 CHECK(default_isolate_status_ != kDefaultIsolateInitialized); |
396 default_isolate_status_ = kDefaultIsolateCrashIfInitialized; | 396 default_isolate_status_ = kDefaultIsolateCrashIfInitialized; |
397 } | 397 } |
398 | 398 |
399 | 399 |
400 void Isolate::EnsureDefaultIsolate() { | 400 Isolate* Isolate::EnsureDefaultIsolate() { |
| 401 static Isolate* default_isolate_ = NULL; |
401 LockGuard<Mutex> lock_guard(&process_wide_mutex_); | 402 LockGuard<Mutex> lock_guard(&process_wide_mutex_); |
402 CHECK(default_isolate_status_ != kDefaultIsolateCrashIfInitialized); | 403 CHECK(default_isolate_status_ != kDefaultIsolateCrashIfInitialized); |
403 if (default_isolate_ == NULL) { | 404 if (default_isolate_ == NULL) { |
404 isolate_key_ = Thread::CreateThreadLocalKey(); | 405 default_isolate_ = new Isolate(true); |
405 thread_id_key_ = Thread::CreateThreadLocalKey(); | |
406 per_isolate_thread_data_key_ = Thread::CreateThreadLocalKey(); | |
407 #ifdef DEBUG | |
408 PerThreadAssertScopeBase::thread_local_key = Thread::CreateThreadLocalKey(); | |
409 #endif // DEBUG | |
410 thread_data_table_ = new Isolate::ThreadDataTable(); | |
411 default_isolate_ = new Isolate(); | |
412 } | 406 } |
413 // Can't use SetIsolateThreadLocals(default_isolate_, NULL) here | 407 // Can't use SetIsolateThreadLocals(default_isolate_, NULL) here |
414 // because a non-null thread data may be already set. | 408 // because a non-null thread data may be already set. |
415 if (Thread::GetThreadLocal(isolate_key_) == NULL) { | 409 if (Thread::GetThreadLocal(isolate_key_) == NULL) { |
416 Thread::SetThreadLocal(isolate_key_, default_isolate_); | 410 Thread::SetThreadLocal(isolate_key_, default_isolate_); |
417 } | 411 } |
| 412 |
| 413 return default_isolate_; |
418 } | 414 } |
419 | 415 |
| 416 |
| 417 void Isolate::InitializeThreadLocalStorage() { |
| 418 // Double checked locking on existence of thread_data_table_. |
| 419 if (thread_data_table_ != NULL) return; |
| 420 LockGuard<Mutex> lock_guard(&process_wide_mutex_); |
| 421 if (thread_data_table_ != NULL) return; |
| 422 isolate_key_ = Thread::CreateThreadLocalKey(); |
| 423 thread_id_key_ = Thread::CreateThreadLocalKey(); |
| 424 per_isolate_thread_data_key_ = Thread::CreateThreadLocalKey(); |
| 425 #ifdef DEBUG |
| 426 PerThreadAssertScopeBase::thread_local_key = Thread::CreateThreadLocalKey(); |
| 427 #endif // DEBUG |
| 428 thread_data_table_ = new Isolate::ThreadDataTable(); |
| 429 CHECK(thread_data_table_ != NULL); |
| 430 } |
| 431 |
| 432 |
| 433 // TODO(dcarney): Remove this with default_isolate_ and Isolate::Current. |
420 struct StaticInitializer { | 434 struct StaticInitializer { |
421 StaticInitializer() { | 435 StaticInitializer() { |
422 Isolate::EnsureDefaultIsolate(); | 436 Isolate::InitializeThreadLocalStorage(); |
423 } | 437 } |
424 } static_initializer; | 438 } static_initializer; |
425 | 439 |
426 #ifdef ENABLE_DEBUGGER_SUPPORT | 440 #ifdef ENABLE_DEBUGGER_SUPPORT |
427 Debugger* Isolate::GetDefaultIsolateDebugger() { | 441 Debugger* Isolate::GetDefaultIsolateDebugger() { |
428 EnsureDefaultIsolate(); | 442 return EnsureDefaultIsolate()->debugger(); |
429 return default_isolate_->debugger(); | |
430 } | 443 } |
431 #endif | 444 #endif |
432 | 445 |
433 | 446 |
434 StackGuard* Isolate::GetDefaultIsolateStackGuard() { | 447 StackGuard* Isolate::GetDefaultIsolateStackGuard() { |
435 EnsureDefaultIsolate(); | 448 return EnsureDefaultIsolate()->stack_guard(); |
436 return default_isolate_->stack_guard(); | |
437 } | 449 } |
438 | 450 |
439 | 451 |
440 void Isolate::EnterDefaultIsolate() { | 452 void Isolate::EnterDefaultIsolate() { |
441 EnsureDefaultIsolate(); | 453 Isolate* default_isolate = EnsureDefaultIsolate(); |
442 ASSERT(default_isolate_ != NULL); | 454 ASSERT(default_isolate != NULL); |
443 | 455 |
444 PerIsolateThreadData* data = CurrentPerIsolateThreadData(); | 456 PerIsolateThreadData* data = CurrentPerIsolateThreadData(); |
445 // If not yet in default isolate - enter it. | 457 // If not yet in default isolate - enter it. |
446 if (data == NULL || data->isolate() != default_isolate_) { | 458 if (data == NULL || data->isolate() != default_isolate) { |
447 default_isolate_->Enter(); | 459 default_isolate->Enter(); |
448 } | 460 } |
449 } | 461 } |
450 | 462 |
451 | 463 |
452 v8::Isolate* Isolate::GetDefaultIsolateForLocking() { | 464 v8::Isolate* Isolate::GetDefaultIsolateForLocking() { |
453 EnsureDefaultIsolate(); | 465 return reinterpret_cast<v8::Isolate*>(EnsureDefaultIsolate()); |
454 return reinterpret_cast<v8::Isolate*>(default_isolate_); | |
455 } | 466 } |
456 | 467 |
457 | 468 |
458 Address Isolate::get_address_from_id(Isolate::AddressId id) { | 469 Address Isolate::get_address_from_id(Isolate::AddressId id) { |
459 return isolate_addresses_[id]; | 470 return isolate_addresses_[id]; |
460 } | 471 } |
461 | 472 |
462 | 473 |
463 char* Isolate::Iterate(ObjectVisitor* v, char* thread_storage) { | 474 char* Isolate::Iterate(ObjectVisitor* v, char* thread_storage) { |
464 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(thread_storage); | 475 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(thread_storage); |
(...skipping 1273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1738 if (FLAG_trace_isolates) { \ | 1749 if (FLAG_trace_isolates) { \ |
1739 PrintF("Isolate %p (id %d)" #tag "\n", \ | 1750 PrintF("Isolate %p (id %d)" #tag "\n", \ |
1740 reinterpret_cast<void*>(this), id()); \ | 1751 reinterpret_cast<void*>(this), id()); \ |
1741 } \ | 1752 } \ |
1742 } while (false) | 1753 } while (false) |
1743 #else | 1754 #else |
1744 #define TRACE_ISOLATE(tag) | 1755 #define TRACE_ISOLATE(tag) |
1745 #endif | 1756 #endif |
1746 | 1757 |
1747 | 1758 |
1748 Isolate::Isolate() | 1759 Isolate::Isolate(bool is_default_isolate) |
1749 : state_(UNINITIALIZED), | 1760 : state_(UNINITIALIZED), |
1750 embedder_data_(NULL), | 1761 embedder_data_(NULL), |
| 1762 is_default_isolate_(is_default_isolate), |
1751 entry_stack_(NULL), | 1763 entry_stack_(NULL), |
1752 stack_trace_nesting_level_(0), | 1764 stack_trace_nesting_level_(0), |
1753 incomplete_message_(NULL), | 1765 incomplete_message_(NULL), |
1754 preallocated_memory_thread_(NULL), | 1766 preallocated_memory_thread_(NULL), |
1755 preallocated_message_space_(NULL), | 1767 preallocated_message_space_(NULL), |
1756 bootstrapper_(NULL), | 1768 bootstrapper_(NULL), |
1757 runtime_profiler_(NULL), | 1769 runtime_profiler_(NULL), |
1758 compilation_cache_(NULL), | 1770 compilation_cache_(NULL), |
1759 counters_(NULL), | 1771 counters_(NULL), |
1760 code_range_(NULL), | 1772 code_range_(NULL), |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1796 use_crankshaft_(true), | 1808 use_crankshaft_(true), |
1797 initialized_from_snapshot_(false), | 1809 initialized_from_snapshot_(false), |
1798 cpu_profiler_(NULL), | 1810 cpu_profiler_(NULL), |
1799 heap_profiler_(NULL), | 1811 heap_profiler_(NULL), |
1800 function_entry_hook_(NULL), | 1812 function_entry_hook_(NULL), |
1801 deferred_handles_head_(NULL), | 1813 deferred_handles_head_(NULL), |
1802 optimizing_compiler_thread_(NULL), | 1814 optimizing_compiler_thread_(NULL), |
1803 marking_thread_(NULL), | 1815 marking_thread_(NULL), |
1804 sweeper_thread_(NULL), | 1816 sweeper_thread_(NULL), |
1805 stress_deopt_count_(0) { | 1817 stress_deopt_count_(0) { |
| 1818 InitializeThreadLocalStorage(); |
| 1819 |
1806 id_ = NoBarrier_AtomicIncrement(&isolate_counter_, 1); | 1820 id_ = NoBarrier_AtomicIncrement(&isolate_counter_, 1); |
| 1821 NoBarrier_AtomicIncrement(&living_isolates_, 1); |
1807 TRACE_ISOLATE(constructor); | 1822 TRACE_ISOLATE(constructor); |
1808 | 1823 |
1809 memset(isolate_addresses_, 0, | 1824 memset(isolate_addresses_, 0, |
1810 sizeof(isolate_addresses_[0]) * (kIsolateAddressCount + 1)); | 1825 sizeof(isolate_addresses_[0]) * (kIsolateAddressCount + 1)); |
1811 | 1826 |
1812 heap_.isolate_ = this; | 1827 heap_.isolate_ = this; |
1813 stack_guard_.isolate_ = this; | 1828 stack_guard_.isolate_ = this; |
1814 | 1829 |
1815 // ThreadManager is initialized early to support locking an isolate | 1830 // ThreadManager is initialized early to support locking an isolate |
1816 // before it is entered. | 1831 // before it is entered. |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1987 | 2002 |
1988 Isolate::~Isolate() { | 2003 Isolate::~Isolate() { |
1989 TRACE_ISOLATE(destructor); | 2004 TRACE_ISOLATE(destructor); |
1990 | 2005 |
1991 // Has to be called while counters_ are still alive | 2006 // Has to be called while counters_ are still alive |
1992 runtime_zone_.DeleteKeptSegment(); | 2007 runtime_zone_.DeleteKeptSegment(); |
1993 | 2008 |
1994 // The entry stack must be empty when we get here, | 2009 // The entry stack must be empty when we get here, |
1995 // except for the default isolate, where it can | 2010 // except for the default isolate, where it can |
1996 // still contain up to one entry stack item | 2011 // still contain up to one entry stack item |
1997 ASSERT(entry_stack_ == NULL || this == default_isolate_); | 2012 ASSERT(entry_stack_ == NULL || is_default_isolate_); |
1998 ASSERT(entry_stack_ == NULL || entry_stack_->previous_item == NULL); | 2013 ASSERT(entry_stack_ == NULL || entry_stack_->previous_item == NULL); |
1999 | 2014 |
2000 delete entry_stack_; | 2015 delete entry_stack_; |
2001 entry_stack_ = NULL; | 2016 entry_stack_ = NULL; |
2002 | 2017 |
2003 delete[] assembler_spare_buffer_; | 2018 delete[] assembler_spare_buffer_; |
2004 assembler_spare_buffer_ = NULL; | 2019 assembler_spare_buffer_ = NULL; |
2005 | 2020 |
2006 delete unicode_cache_; | 2021 delete unicode_cache_; |
2007 unicode_cache_ = NULL; | 2022 unicode_cache_ = NULL; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2072 | 2087 |
2073 delete random_number_generator_; | 2088 delete random_number_generator_; |
2074 random_number_generator_ = NULL; | 2089 random_number_generator_ = NULL; |
2075 | 2090 |
2076 #ifdef ENABLE_DEBUGGER_SUPPORT | 2091 #ifdef ENABLE_DEBUGGER_SUPPORT |
2077 delete debugger_; | 2092 delete debugger_; |
2078 debugger_ = NULL; | 2093 debugger_ = NULL; |
2079 delete debug_; | 2094 delete debug_; |
2080 debug_ = NULL; | 2095 debug_ = NULL; |
2081 #endif | 2096 #endif |
| 2097 |
| 2098 NoBarrier_AtomicIncrement(&living_isolates_, -1); |
2082 } | 2099 } |
2083 | 2100 |
2084 | 2101 |
2085 void Isolate::InitializeThreadLocal() { | 2102 void Isolate::InitializeThreadLocal() { |
2086 thread_local_top_.isolate_ = this; | 2103 thread_local_top_.isolate_ = this; |
2087 thread_local_top_.Initialize(); | 2104 thread_local_top_.Initialize(); |
2088 } | 2105 } |
2089 | 2106 |
2090 | 2107 |
2091 void Isolate::PropagatePendingExceptionToExternalTryCatch() { | 2108 void Isolate::PropagatePendingExceptionToExternalTryCatch() { |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2542 | 2559 |
2543 #ifdef DEBUG | 2560 #ifdef DEBUG |
2544 #define ISOLATE_FIELD_OFFSET(type, name, ignored) \ | 2561 #define ISOLATE_FIELD_OFFSET(type, name, ignored) \ |
2545 const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_); | 2562 const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_); |
2546 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET) | 2563 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET) |
2547 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET) | 2564 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET) |
2548 #undef ISOLATE_FIELD_OFFSET | 2565 #undef ISOLATE_FIELD_OFFSET |
2549 #endif | 2566 #endif |
2550 | 2567 |
2551 } } // namespace v8::internal | 2568 } } // namespace v8::internal |
OLD | NEW |