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 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 FreeStoreAllocationPolicy::Delete(p); | 323 FreeStoreAllocationPolicy::Delete(p); |
324 return; | 324 return; |
325 } | 325 } |
326 PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1; | 326 PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1; |
327 ASSERT(storage->next_->previous_ == storage); | 327 ASSERT(storage->next_->previous_ == storage); |
328 ASSERT(storage->previous_->next_ == storage); | 328 ASSERT(storage->previous_->next_ == storage); |
329 storage->Unlink(); | 329 storage->Unlink(); |
330 storage->LinkTo(&free_list_); | 330 storage->LinkTo(&free_list_); |
331 } | 331 } |
332 | 332 |
333 Isolate* Isolate::default_isolate_ = NULL; | |
334 Thread::LocalStorageKey Isolate::isolate_key_; | 333 Thread::LocalStorageKey Isolate::isolate_key_; |
335 Thread::LocalStorageKey Isolate::thread_id_key_; | 334 Thread::LocalStorageKey Isolate::thread_id_key_; |
336 Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_; | 335 Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_; |
337 #ifdef DEBUG | 336 #ifdef DEBUG |
338 Thread::LocalStorageKey PerThreadAssertScopeBase::thread_local_key; | 337 Thread::LocalStorageKey PerThreadAssertScopeBase::thread_local_key; |
339 #endif // DEBUG | 338 #endif // DEBUG |
340 Mutex Isolate::process_wide_mutex_; | 339 Mutex Isolate::process_wide_mutex_; |
341 // TODO(dcarney): Remove with default isolate. | 340 // TODO(dcarney): Remove with default isolate. |
342 enum DefaultIsolateStatus { | 341 enum DefaultIsolateStatus { |
343 kDefaultIsolateUninitialized, | 342 kDefaultIsolateUninitialized, |
344 kDefaultIsolateInitialized, | 343 kDefaultIsolateInitialized, |
345 kDefaultIsolateCrashIfInitialized | 344 kDefaultIsolateCrashIfInitialized |
346 }; | 345 }; |
347 static DefaultIsolateStatus default_isolate_status_ | 346 static DefaultIsolateStatus default_isolate_status_ |
348 = kDefaultIsolateUninitialized; | 347 = kDefaultIsolateUninitialized; |
349 Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL; | 348 Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL; |
350 Atomic32 Isolate::isolate_counter_ = 0; | 349 Atomic32 Isolate::isolate_counter_ = 0; |
| 350 Atomic32 Isolate::living_isolates_ = 0; |
351 | 351 |
352 Isolate::PerIsolateThreadData* | 352 Isolate::PerIsolateThreadData* |
353 Isolate::FindOrAllocatePerThreadDataForThisThread() { | 353 Isolate::FindOrAllocatePerThreadDataForThisThread() { |
354 ThreadId thread_id = ThreadId::Current(); | 354 ThreadId thread_id = ThreadId::Current(); |
355 PerIsolateThreadData* per_thread = NULL; | 355 PerIsolateThreadData* per_thread = NULL; |
356 { | 356 { |
357 LockGuard<Mutex> lock_guard(&process_wide_mutex_); | 357 LockGuard<Mutex> lock_guard(&process_wide_mutex_); |
358 per_thread = thread_data_table_->Lookup(this, thread_id); | 358 per_thread = thread_data_table_->Lookup(this, thread_id); |
359 if (per_thread == NULL) { | 359 if (per_thread == NULL) { |
360 per_thread = new PerIsolateThreadData(this, thread_id); | 360 per_thread = new PerIsolateThreadData(this, thread_id); |
(...skipping 22 matching lines...) Expand all Loading... |
383 } | 383 } |
384 | 384 |
385 | 385 |
386 void Isolate::SetCrashIfDefaultIsolateInitialized() { | 386 void Isolate::SetCrashIfDefaultIsolateInitialized() { |
387 LockGuard<Mutex> lock_guard(&process_wide_mutex_); | 387 LockGuard<Mutex> lock_guard(&process_wide_mutex_); |
388 CHECK(default_isolate_status_ != kDefaultIsolateInitialized); | 388 CHECK(default_isolate_status_ != kDefaultIsolateInitialized); |
389 default_isolate_status_ = kDefaultIsolateCrashIfInitialized; | 389 default_isolate_status_ = kDefaultIsolateCrashIfInitialized; |
390 } | 390 } |
391 | 391 |
392 | 392 |
393 void Isolate::EnsureDefaultIsolate() { | 393 Isolate* Isolate::EnsureDefaultIsolate() { |
| 394 static Isolate* default_isolate_ = NULL; |
394 LockGuard<Mutex> lock_guard(&process_wide_mutex_); | 395 LockGuard<Mutex> lock_guard(&process_wide_mutex_); |
395 CHECK(default_isolate_status_ != kDefaultIsolateCrashIfInitialized); | 396 CHECK(default_isolate_status_ != kDefaultIsolateCrashIfInitialized); |
396 if (default_isolate_ == NULL) { | 397 if (default_isolate_ == NULL) { |
397 isolate_key_ = Thread::CreateThreadLocalKey(); | 398 default_isolate_ = new Isolate(true); |
398 thread_id_key_ = Thread::CreateThreadLocalKey(); | |
399 per_isolate_thread_data_key_ = Thread::CreateThreadLocalKey(); | |
400 #ifdef DEBUG | |
401 PerThreadAssertScopeBase::thread_local_key = Thread::CreateThreadLocalKey(); | |
402 #endif // DEBUG | |
403 thread_data_table_ = new Isolate::ThreadDataTable(); | |
404 default_isolate_ = new Isolate(); | |
405 } | 399 } |
406 // Can't use SetIsolateThreadLocals(default_isolate_, NULL) here | 400 // Can't use SetIsolateThreadLocals(default_isolate_, NULL) here |
407 // because a non-null thread data may be already set. | 401 // because a non-null thread data may be already set. |
408 if (Thread::GetThreadLocal(isolate_key_) == NULL) { | 402 if (Thread::GetThreadLocal(isolate_key_) == NULL) { |
409 Thread::SetThreadLocal(isolate_key_, default_isolate_); | 403 Thread::SetThreadLocal(isolate_key_, default_isolate_); |
410 } | 404 } |
| 405 |
| 406 return default_isolate_; |
411 } | 407 } |
412 | 408 |
| 409 |
| 410 void Isolate::InitializeThreadLocalStorage() { |
| 411 // Double checked locking on existence of thread_data_table_. |
| 412 if (thread_data_table_ != NULL) return; |
| 413 LockGuard<Mutex> lock_guard(&process_wide_mutex_); |
| 414 if (thread_data_table_ != NULL) return; |
| 415 isolate_key_ = Thread::CreateThreadLocalKey(); |
| 416 thread_id_key_ = Thread::CreateThreadLocalKey(); |
| 417 per_isolate_thread_data_key_ = Thread::CreateThreadLocalKey(); |
| 418 #ifdef DEBUG |
| 419 PerThreadAssertScopeBase::thread_local_key = Thread::CreateThreadLocalKey(); |
| 420 #endif // DEBUG |
| 421 thread_data_table_ = new Isolate::ThreadDataTable(); |
| 422 CHECK(thread_data_table_ != NULL); |
| 423 } |
| 424 |
| 425 |
| 426 // TODO(dcarney): Remove this with default_isolate_ and Isolate::Current. |
413 struct StaticInitializer { | 427 struct StaticInitializer { |
414 StaticInitializer() { | 428 StaticInitializer() { |
415 Isolate::EnsureDefaultIsolate(); | 429 Isolate::InitializeThreadLocalStorage(); |
416 } | 430 } |
417 } static_initializer; | 431 } static_initializer; |
418 | 432 |
419 #ifdef ENABLE_DEBUGGER_SUPPORT | 433 #ifdef ENABLE_DEBUGGER_SUPPORT |
420 Debugger* Isolate::GetDefaultIsolateDebugger() { | 434 Debugger* Isolate::GetDefaultIsolateDebugger() { |
421 EnsureDefaultIsolate(); | 435 return EnsureDefaultIsolate()->debugger(); |
422 return default_isolate_->debugger(); | |
423 } | 436 } |
424 #endif | 437 #endif |
425 | 438 |
426 | 439 |
427 StackGuard* Isolate::GetDefaultIsolateStackGuard() { | 440 StackGuard* Isolate::GetDefaultIsolateStackGuard() { |
428 EnsureDefaultIsolate(); | 441 return EnsureDefaultIsolate()->stack_guard(); |
429 return default_isolate_->stack_guard(); | |
430 } | 442 } |
431 | 443 |
432 | 444 |
433 void Isolate::EnterDefaultIsolate() { | 445 void Isolate::EnterDefaultIsolate() { |
434 EnsureDefaultIsolate(); | 446 Isolate* default_isolate = EnsureDefaultIsolate(); |
435 ASSERT(default_isolate_ != NULL); | 447 ASSERT(default_isolate != NULL); |
436 | 448 |
437 PerIsolateThreadData* data = CurrentPerIsolateThreadData(); | 449 PerIsolateThreadData* data = CurrentPerIsolateThreadData(); |
438 // If not yet in default isolate - enter it. | 450 // If not yet in default isolate - enter it. |
439 if (data == NULL || data->isolate() != default_isolate_) { | 451 if (data == NULL || data->isolate() != default_isolate) { |
440 default_isolate_->Enter(); | 452 default_isolate->Enter(); |
441 } | 453 } |
442 } | 454 } |
443 | 455 |
444 | 456 |
445 v8::Isolate* Isolate::GetDefaultIsolateForLocking() { | 457 v8::Isolate* Isolate::GetDefaultIsolateForLocking() { |
446 EnsureDefaultIsolate(); | 458 return reinterpret_cast<v8::Isolate*>(EnsureDefaultIsolate()); |
447 return reinterpret_cast<v8::Isolate*>(default_isolate_); | |
448 } | 459 } |
449 | 460 |
450 | 461 |
451 Address Isolate::get_address_from_id(Isolate::AddressId id) { | 462 Address Isolate::get_address_from_id(Isolate::AddressId id) { |
452 return isolate_addresses_[id]; | 463 return isolate_addresses_[id]; |
453 } | 464 } |
454 | 465 |
455 | 466 |
456 char* Isolate::Iterate(ObjectVisitor* v, char* thread_storage) { | 467 char* Isolate::Iterate(ObjectVisitor* v, char* thread_storage) { |
457 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(thread_storage); | 468 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(thread_storage); |
(...skipping 1269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1727 if (FLAG_trace_isolates) { \ | 1738 if (FLAG_trace_isolates) { \ |
1728 PrintF("Isolate %p (id %d)" #tag "\n", \ | 1739 PrintF("Isolate %p (id %d)" #tag "\n", \ |
1729 reinterpret_cast<void*>(this), id()); \ | 1740 reinterpret_cast<void*>(this), id()); \ |
1730 } \ | 1741 } \ |
1731 } while (false) | 1742 } while (false) |
1732 #else | 1743 #else |
1733 #define TRACE_ISOLATE(tag) | 1744 #define TRACE_ISOLATE(tag) |
1734 #endif | 1745 #endif |
1735 | 1746 |
1736 | 1747 |
1737 Isolate::Isolate() | 1748 Isolate::Isolate(bool is_default_isolate) |
1738 : state_(UNINITIALIZED), | 1749 : state_(UNINITIALIZED), |
1739 embedder_data_(NULL), | 1750 embedder_data_(NULL), |
1740 entry_stack_(NULL), | 1751 entry_stack_(NULL), |
1741 stack_trace_nesting_level_(0), | 1752 stack_trace_nesting_level_(0), |
1742 incomplete_message_(NULL), | 1753 incomplete_message_(NULL), |
1743 preallocated_memory_thread_(NULL), | 1754 preallocated_memory_thread_(NULL), |
1744 preallocated_message_space_(NULL), | 1755 preallocated_message_space_(NULL), |
1745 bootstrapper_(NULL), | 1756 bootstrapper_(NULL), |
1746 runtime_profiler_(NULL), | 1757 runtime_profiler_(NULL), |
1747 compilation_cache_(NULL), | 1758 compilation_cache_(NULL), |
(...skipping 29 matching lines...) Expand all Loading... |
1777 string_tracker_(NULL), | 1788 string_tracker_(NULL), |
1778 regexp_stack_(NULL), | 1789 regexp_stack_(NULL), |
1779 date_cache_(NULL), | 1790 date_cache_(NULL), |
1780 code_stub_interface_descriptors_(NULL), | 1791 code_stub_interface_descriptors_(NULL), |
1781 // TODO(bmeurer) Initialized lazily because it depends on flags; can | 1792 // TODO(bmeurer) Initialized lazily because it depends on flags; can |
1782 // be fixed once the default isolate cleanup is done. | 1793 // be fixed once the default isolate cleanup is done. |
1783 random_number_generator_(NULL), | 1794 random_number_generator_(NULL), |
1784 has_fatal_error_(false), | 1795 has_fatal_error_(false), |
1785 use_crankshaft_(true), | 1796 use_crankshaft_(true), |
1786 initialized_from_snapshot_(false), | 1797 initialized_from_snapshot_(false), |
| 1798 is_default_isolate_(is_default_isolate), |
1787 cpu_profiler_(NULL), | 1799 cpu_profiler_(NULL), |
1788 heap_profiler_(NULL), | 1800 heap_profiler_(NULL), |
1789 function_entry_hook_(NULL), | 1801 function_entry_hook_(NULL), |
1790 deferred_handles_head_(NULL), | 1802 deferred_handles_head_(NULL), |
1791 optimizing_compiler_thread_(NULL), | 1803 optimizing_compiler_thread_(NULL), |
1792 sweeper_thread_(NULL), | 1804 sweeper_thread_(NULL), |
1793 stress_deopt_count_(0) { | 1805 stress_deopt_count_(0) { |
| 1806 InitializeThreadLocalStorage(); |
| 1807 |
1794 id_ = NoBarrier_AtomicIncrement(&isolate_counter_, 1); | 1808 id_ = NoBarrier_AtomicIncrement(&isolate_counter_, 1); |
| 1809 NoBarrier_AtomicIncrement(&living_isolates_, 1); |
1795 TRACE_ISOLATE(constructor); | 1810 TRACE_ISOLATE(constructor); |
1796 | 1811 |
1797 memset(isolate_addresses_, 0, | 1812 memset(isolate_addresses_, 0, |
1798 sizeof(isolate_addresses_[0]) * (kIsolateAddressCount + 1)); | 1813 sizeof(isolate_addresses_[0]) * (kIsolateAddressCount + 1)); |
1799 | 1814 |
1800 heap_.isolate_ = this; | 1815 heap_.isolate_ = this; |
1801 stack_guard_.isolate_ = this; | 1816 stack_guard_.isolate_ = this; |
1802 | 1817 |
1803 // ThreadManager is initialized early to support locking an isolate | 1818 // ThreadManager is initialized early to support locking an isolate |
1804 // before it is entered. | 1819 // before it is entered. |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1967 | 1982 |
1968 Isolate::~Isolate() { | 1983 Isolate::~Isolate() { |
1969 TRACE_ISOLATE(destructor); | 1984 TRACE_ISOLATE(destructor); |
1970 | 1985 |
1971 // Has to be called while counters_ are still alive | 1986 // Has to be called while counters_ are still alive |
1972 runtime_zone_.DeleteKeptSegment(); | 1987 runtime_zone_.DeleteKeptSegment(); |
1973 | 1988 |
1974 // The entry stack must be empty when we get here, | 1989 // The entry stack must be empty when we get here, |
1975 // except for the default isolate, where it can | 1990 // except for the default isolate, where it can |
1976 // still contain up to one entry stack item | 1991 // still contain up to one entry stack item |
1977 ASSERT(entry_stack_ == NULL || this == default_isolate_); | 1992 ASSERT(entry_stack_ == NULL || is_default_isolate_); |
1978 ASSERT(entry_stack_ == NULL || entry_stack_->previous_item == NULL); | 1993 ASSERT(entry_stack_ == NULL || entry_stack_->previous_item == NULL); |
1979 | 1994 |
1980 delete entry_stack_; | 1995 delete entry_stack_; |
1981 entry_stack_ = NULL; | 1996 entry_stack_ = NULL; |
1982 | 1997 |
1983 delete[] assembler_spare_buffer_; | 1998 delete[] assembler_spare_buffer_; |
1984 assembler_spare_buffer_ = NULL; | 1999 assembler_spare_buffer_ = NULL; |
1985 | 2000 |
1986 delete unicode_cache_; | 2001 delete unicode_cache_; |
1987 unicode_cache_ = NULL; | 2002 unicode_cache_ = NULL; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2052 | 2067 |
2053 delete random_number_generator_; | 2068 delete random_number_generator_; |
2054 random_number_generator_ = NULL; | 2069 random_number_generator_ = NULL; |
2055 | 2070 |
2056 #ifdef ENABLE_DEBUGGER_SUPPORT | 2071 #ifdef ENABLE_DEBUGGER_SUPPORT |
2057 delete debugger_; | 2072 delete debugger_; |
2058 debugger_ = NULL; | 2073 debugger_ = NULL; |
2059 delete debug_; | 2074 delete debug_; |
2060 debug_ = NULL; | 2075 debug_ = NULL; |
2061 #endif | 2076 #endif |
| 2077 |
| 2078 NoBarrier_AtomicIncrement(&living_isolates_, -1); |
2062 } | 2079 } |
2063 | 2080 |
2064 | 2081 |
2065 void Isolate::InitializeThreadLocal() { | 2082 void Isolate::InitializeThreadLocal() { |
2066 thread_local_top_.isolate_ = this; | 2083 thread_local_top_.isolate_ = this; |
2067 thread_local_top_.Initialize(); | 2084 thread_local_top_.Initialize(); |
2068 } | 2085 } |
2069 | 2086 |
2070 | 2087 |
2071 void Isolate::PropagatePendingExceptionToExternalTryCatch() { | 2088 void Isolate::PropagatePendingExceptionToExternalTryCatch() { |
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2514 | 2531 |
2515 #ifdef DEBUG | 2532 #ifdef DEBUG |
2516 #define ISOLATE_FIELD_OFFSET(type, name, ignored) \ | 2533 #define ISOLATE_FIELD_OFFSET(type, name, ignored) \ |
2517 const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_); | 2534 const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_); |
2518 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET) | 2535 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET) |
2519 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET) | 2536 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET) |
2520 #undef ISOLATE_FIELD_OFFSET | 2537 #undef ISOLATE_FIELD_OFFSET |
2521 #endif | 2538 #endif |
2522 | 2539 |
2523 } } // namespace v8::internal | 2540 } } // namespace v8::internal |
OLD | NEW |