blob: 824cc37118b6df9c64ff8b6edd3d066bf0777ea3 [file] [log] [blame]
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001// Copyright 2006-2010 the V8 project authors. All rights reserved.
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
54namespace v8 {
55namespace internal {
56
57
58// Create a dummy thread that will wait forever on a semaphore. The only
59// purpose for this thread is to have some stack area to save essential data
60// into for use by a stacks only core dump (aka minidump).
61class PreallocatedMemoryThread: public Thread {
62 public:
63 char* data() {
64 if (data_ready_semaphore_ != NULL) {
65 // Initial access is guarded until the data has been published.
66 data_ready_semaphore_->Wait();
67 delete data_ready_semaphore_;
68 data_ready_semaphore_ = NULL;
69 }
70 return data_;
71 }
72
73 unsigned length() {
74 if (data_ready_semaphore_ != NULL) {
75 // Initial access is guarded until the data has been published.
76 data_ready_semaphore_->Wait();
77 delete data_ready_semaphore_;
78 data_ready_semaphore_ = NULL;
79 }
80 return length_;
81 }
82
83 // Stop the PreallocatedMemoryThread and release its resources.
84 void StopThread() {
85 keep_running_ = false;
86 wait_for_ever_semaphore_->Signal();
87
88 // Wait for the thread to terminate.
89 Join();
90
91 if (data_ready_semaphore_ != NULL) {
92 delete data_ready_semaphore_;
93 data_ready_semaphore_ = NULL;
94 }
95
96 delete wait_for_ever_semaphore_;
97 wait_for_ever_semaphore_ = NULL;
98 }
99
100 protected:
101 // When the thread starts running it will allocate a fixed number of bytes
102 // on the stack and publish the location of this memory for others to use.
103 void Run() {
104 EmbeddedVector<char, 15 * 1024> local_buffer;
105
106 // Initialize the buffer with a known good value.
107 OS::StrNCpy(local_buffer, "Trace data was not generated.\n",
108 local_buffer.length());
109
110 // Publish the local buffer and signal its availability.
111 data_ = local_buffer.start();
112 length_ = local_buffer.length();
113 data_ready_semaphore_->Signal();
114
115 while (keep_running_) {
116 // This thread will wait here until the end of time.
117 wait_for_ever_semaphore_->Wait();
118 }
119
120 // Make sure we access the buffer after the wait to remove all possibility
121 // of it being optimized away.
122 OS::StrNCpy(local_buffer, "PreallocatedMemoryThread shutting down.\n",
123 local_buffer.length());
124 }
125
126
127 private:
128 explicit PreallocatedMemoryThread(Isolate* isolate)
129 : Thread(isolate, "v8:PreallocMem"),
130 keep_running_(true),
131 wait_for_ever_semaphore_(OS::CreateSemaphore(0)),
132 data_ready_semaphore_(OS::CreateSemaphore(0)),
133 data_(NULL),
134 length_(0) {
135 }
136
137 // Used to make sure that the thread keeps looping even for spurious wakeups.
138 bool keep_running_;
139
140 // This semaphore is used by the PreallocatedMemoryThread to wait for ever.
141 Semaphore* wait_for_ever_semaphore_;
142 // Semaphore to signal that the data has been initialized.
143 Semaphore* data_ready_semaphore_;
144
145 // Location and size of the preallocated memory block.
146 char* data_;
147 unsigned length_;
148
149 friend class Isolate;
150
151 DISALLOW_COPY_AND_ASSIGN(PreallocatedMemoryThread);
152};
153
154
155void Isolate::PreallocatedMemoryThreadStart() {
156 if (preallocated_memory_thread_ != NULL) return;
157 preallocated_memory_thread_ = new PreallocatedMemoryThread(this);
158 preallocated_memory_thread_->Start();
159}
160
161
162void Isolate::PreallocatedMemoryThreadStop() {
163 if (preallocated_memory_thread_ == NULL) return;
164 preallocated_memory_thread_->StopThread();
165 // Done with the thread entirely.
166 delete preallocated_memory_thread_;
167 preallocated_memory_thread_ = NULL;
168}
169
170
171Isolate* Isolate::default_isolate_ = NULL;
172Thread::LocalStorageKey Isolate::isolate_key_;
173Thread::LocalStorageKey Isolate::thread_id_key_;
174Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_;
175Mutex* Isolate::process_wide_mutex_ = OS::CreateMutex();
176Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL;
177Isolate::ThreadId Isolate::highest_thread_id_ = 0;
178
179
180class IsolateInitializer {
181 public:
182 IsolateInitializer() {
183 Isolate::EnsureDefaultIsolate();
184 }
185};
186
187static IsolateInitializer* EnsureDefaultIsolateAllocated() {
188 // TODO(isolates): Use the system threading API to do this once?
189 static IsolateInitializer static_initializer;
190 return &static_initializer;
191}
192
193// This variable only needed to trigger static intialization.
194static IsolateInitializer* static_initializer = EnsureDefaultIsolateAllocated();
195
196
197Isolate::ThreadId Isolate::AllocateThreadId() {
198 ThreadId new_id;
199 {
200 ScopedLock lock(process_wide_mutex_);
201 new_id = ++highest_thread_id_;
202 }
203 return new_id;
204}
205
206
207Isolate::PerIsolateThreadData* Isolate::AllocatePerIsolateThreadData(
208 ThreadId thread_id) {
209 ASSERT(thread_id != 0);
210 ASSERT(Thread::GetThreadLocalInt(thread_id_key_) == thread_id);
211 PerIsolateThreadData* per_thread = new PerIsolateThreadData(this, thread_id);
212 {
213 ScopedLock lock(process_wide_mutex_);
214 ASSERT(thread_data_table_->Lookup(this, thread_id) == NULL);
215 thread_data_table_->Insert(per_thread);
216 ASSERT(thread_data_table_->Lookup(this, thread_id) == per_thread);
217 }
218 return per_thread;
219}
220
221
222Isolate::PerIsolateThreadData*
223 Isolate::FindOrAllocatePerThreadDataForThisThread() {
224 ThreadId thread_id = Thread::GetThreadLocalInt(thread_id_key_);
225 if (thread_id == 0) {
226 thread_id = AllocateThreadId();
227 Thread::SetThreadLocalInt(thread_id_key_, thread_id);
228 }
229 PerIsolateThreadData* per_thread = NULL;
230 {
231 ScopedLock lock(process_wide_mutex_);
232 per_thread = thread_data_table_->Lookup(this, thread_id);
233 if (per_thread == NULL) {
234 per_thread = AllocatePerIsolateThreadData(thread_id);
235 }
236 }
237 return per_thread;
238}
239
240
241void Isolate::EnsureDefaultIsolate() {
242 ScopedLock lock(process_wide_mutex_);
243 if (default_isolate_ == NULL) {
244 isolate_key_ = Thread::CreateThreadLocalKey();
245 thread_id_key_ = Thread::CreateThreadLocalKey();
246 per_isolate_thread_data_key_ = Thread::CreateThreadLocalKey();
247 thread_data_table_ = new Isolate::ThreadDataTable();
248 default_isolate_ = new Isolate();
249 }
250 // Can't use SetIsolateThreadLocals(default_isolate_, NULL) here
251 // becase a non-null thread data may be already set.
252 Thread::SetThreadLocal(isolate_key_, default_isolate_);
253 CHECK(default_isolate_->PreInit());
254}
255
256
257Debugger* Isolate::GetDefaultIsolateDebugger() {
258 EnsureDefaultIsolate();
259 return default_isolate_->debugger();
260}
261
262
263StackGuard* Isolate::GetDefaultIsolateStackGuard() {
264 EnsureDefaultIsolate();
265 return default_isolate_->stack_guard();
266}
267
268
269void Isolate::EnterDefaultIsolate() {
270 EnsureDefaultIsolate();
271 ASSERT(default_isolate_ != NULL);
272
273 PerIsolateThreadData* data = CurrentPerIsolateThreadData();
274 // If not yet in default isolate - enter it.
275 if (data == NULL || data->isolate() != default_isolate_) {
276 default_isolate_->Enter();
277 }
278}
279
280
281Isolate* Isolate::GetDefaultIsolateForLocking() {
282 EnsureDefaultIsolate();
283 return default_isolate_;
284}
285
286
287Isolate::ThreadDataTable::ThreadDataTable()
288 : list_(NULL) {
289}
290
291
292Isolate::PerIsolateThreadData*
293 Isolate::ThreadDataTable::Lookup(Isolate* isolate, ThreadId thread_id) {
294 for (PerIsolateThreadData* data = list_; data != NULL; data = data->next_) {
295 if (data->Matches(isolate, thread_id)) return data;
296 }
297 return NULL;
298}
299
300
301void Isolate::ThreadDataTable::Insert(Isolate::PerIsolateThreadData* data) {
302 if (list_ != NULL) list_->prev_ = data;
303 data->next_ = list_;
304 list_ = data;
305}
306
307
308void Isolate::ThreadDataTable::Remove(PerIsolateThreadData* data) {
309 if (list_ == data) list_ = data->next_;
310 if (data->next_ != NULL) data->next_->prev_ = data->prev_;
311 if (data->prev_ != NULL) data->prev_->next_ = data->next_;
312}
313
314
315void Isolate::ThreadDataTable::Remove(Isolate* isolate, ThreadId thread_id) {
316 PerIsolateThreadData* data = Lookup(isolate, thread_id);
317 if (data != NULL) {
318 Remove(data);
319 }
320}
321
322
323#ifdef DEBUG
324#define TRACE_ISOLATE(tag) \
325 do { \
326 if (FLAG_trace_isolates) { \
327 PrintF("Isolate %p " #tag "\n", reinterpret_cast<void*>(this)); \
328 } \
329 } while (false)
330#else
331#define TRACE_ISOLATE(tag)
332#endif
333
334
335Isolate::Isolate()
336 : state_(UNINITIALIZED),
337 entry_stack_(NULL),
338 stack_trace_nesting_level_(0),
339 incomplete_message_(NULL),
340 preallocated_memory_thread_(NULL),
341 preallocated_message_space_(NULL),
342 bootstrapper_(NULL),
343 runtime_profiler_(NULL),
344 compilation_cache_(NULL),
345 counters_(new Counters()),
346 cpu_features_(NULL),
347 code_range_(NULL),
348 break_access_(OS::CreateMutex()),
349 logger_(new Logger()),
350 stats_table_(new StatsTable()),
351 stub_cache_(NULL),
352 deoptimizer_data_(NULL),
353 capture_stack_trace_for_uncaught_exceptions_(false),
354 stack_trace_for_uncaught_exceptions_frame_limit_(0),
355 stack_trace_for_uncaught_exceptions_options_(StackTrace::kOverview),
356 transcendental_cache_(NULL),
357 memory_allocator_(NULL),
358 keyed_lookup_cache_(NULL),
359 context_slot_cache_(NULL),
360 descriptor_lookup_cache_(NULL),
361 handle_scope_implementer_(NULL),
362 scanner_constants_(NULL),
363 in_use_list_(0),
364 free_list_(0),
365 preallocated_storage_preallocated_(false),
366 pc_to_code_cache_(NULL),
367 write_input_buffer_(NULL),
368 global_handles_(NULL),
369 context_switcher_(NULL),
370 thread_manager_(NULL),
371 ast_sentinels_(NULL),
372 string_tracker_(NULL),
373 regexp_stack_(NULL),
374 frame_element_constant_list_(0),
375 result_constant_list_(0) {
376 TRACE_ISOLATE(constructor);
377
378 memset(isolate_addresses_, 0,
379 sizeof(isolate_addresses_[0]) * (k_isolate_address_count + 1));
380
381 heap_.isolate_ = this;
382 zone_.isolate_ = this;
383 stack_guard_.isolate_ = this;
384
385#if defined(V8_TARGET_ARCH_ARM) && !defined(__arm__)
386 simulator_initialized_ = false;
387 simulator_i_cache_ = NULL;
388 simulator_redirection_ = NULL;
389#endif
390
391#ifdef DEBUG
392 // heap_histograms_ initializes itself.
393 memset(&js_spill_information_, 0, sizeof(js_spill_information_));
394 memset(code_kind_statistics_, 0,
395 sizeof(code_kind_statistics_[0]) * Code::NUMBER_OF_KINDS);
396#endif
397
398#ifdef ENABLE_DEBUGGER_SUPPORT
399 debug_ = NULL;
400 debugger_ = NULL;
401#endif
402
403#ifdef ENABLE_LOGGING_AND_PROFILING
404 producer_heap_profile_ = NULL;
405#endif
406
407 handle_scope_data_.Initialize();
408
409#define ISOLATE_INIT_EXECUTE(type, name, initial_value) \
410 name##_ = (initial_value);
411 ISOLATE_INIT_LIST(ISOLATE_INIT_EXECUTE)
412#undef ISOLATE_INIT_EXECUTE
413
414#define ISOLATE_INIT_ARRAY_EXECUTE(type, name, length) \
415 memset(name##_, 0, sizeof(type) * length);
416 ISOLATE_INIT_ARRAY_LIST(ISOLATE_INIT_ARRAY_EXECUTE)
417#undef ISOLATE_INIT_ARRAY_EXECUTE
418}
419
420void Isolate::TearDown() {
421 TRACE_ISOLATE(tear_down);
422
423 // Temporarily set this isolate as current so that various parts of
424 // the isolate can access it in their destructors without having a
425 // direct pointer. We don't use Enter/Exit here to avoid
426 // initializing the thread data.
427 PerIsolateThreadData* saved_data = CurrentPerIsolateThreadData();
428 Isolate* saved_isolate = UncheckedCurrent();
429 SetIsolateThreadLocals(this, NULL);
430
431 Deinit();
432
433 if (!IsDefaultIsolate()) {
434 delete this;
435 }
436
437 // Restore the previous current isolate.
438 SetIsolateThreadLocals(saved_isolate, saved_data);
439}
440
441
442void Isolate::Deinit() {
443 if (state_ == INITIALIZED) {
444 TRACE_ISOLATE(deinit);
445
446 if (FLAG_hydrogen_stats) HStatistics::Instance()->Print();
447
448 // We must stop the logger before we tear down other components.
449 logger_->EnsureTickerStopped();
450
451 delete deoptimizer_data_;
452 deoptimizer_data_ = NULL;
453 if (FLAG_preemption) {
454 v8::Locker locker;
455 v8::Locker::StopPreemption();
456 }
457 builtins_.TearDown();
458 bootstrapper_->TearDown();
459
460 // Remove the external reference to the preallocated stack memory.
461 delete preallocated_message_space_;
462 preallocated_message_space_ = NULL;
463 PreallocatedMemoryThreadStop();
464
465 HeapProfiler::TearDown();
466 CpuProfiler::TearDown();
467 if (runtime_profiler_ != NULL) {
468 runtime_profiler_->TearDown();
469 delete runtime_profiler_;
470 runtime_profiler_ = NULL;
471 }
472 heap_.TearDown();
473 logger_->TearDown();
474
475 // The default isolate is re-initializable due to legacy API.
476 state_ = PREINITIALIZED;
477 }
478}
479
480
481void Isolate::SetIsolateThreadLocals(Isolate* isolate,
482 PerIsolateThreadData* data) {
483 Thread::SetThreadLocal(isolate_key_, isolate);
484 Thread::SetThreadLocal(per_isolate_thread_data_key_, data);
485}
486
487
488Isolate::~Isolate() {
489 TRACE_ISOLATE(destructor);
490
491#ifdef ENABLE_LOGGING_AND_PROFILING
492 delete producer_heap_profile_;
493 producer_heap_profile_ = NULL;
494#endif
495
496 delete scanner_constants_;
497 scanner_constants_ = NULL;
498
499 delete regexp_stack_;
500 regexp_stack_ = NULL;
501
502 delete ast_sentinels_;
503 ast_sentinels_ = NULL;
504
505 delete descriptor_lookup_cache_;
506 descriptor_lookup_cache_ = NULL;
507 delete context_slot_cache_;
508 context_slot_cache_ = NULL;
509 delete keyed_lookup_cache_;
510 keyed_lookup_cache_ = NULL;
511
512 delete transcendental_cache_;
513 transcendental_cache_ = NULL;
514 delete stub_cache_;
515 stub_cache_ = NULL;
516 delete stats_table_;
517 stats_table_ = NULL;
518
519 delete logger_;
520 logger_ = NULL;
521
522 delete counters_;
523 counters_ = NULL;
524 delete cpu_features_;
525 cpu_features_ = NULL;
526
527 delete handle_scope_implementer_;
528 handle_scope_implementer_ = NULL;
529 delete break_access_;
530 break_access_ = NULL;
531
532 delete compilation_cache_;
533 compilation_cache_ = NULL;
534 delete bootstrapper_;
535 bootstrapper_ = NULL;
536 delete pc_to_code_cache_;
537 pc_to_code_cache_ = NULL;
538 delete write_input_buffer_;
539 write_input_buffer_ = NULL;
540
541 delete context_switcher_;
542 context_switcher_ = NULL;
543 delete thread_manager_;
544 thread_manager_ = NULL;
545
546 delete string_tracker_;
547 string_tracker_ = NULL;
548
549 delete memory_allocator_;
550 memory_allocator_ = NULL;
551 delete code_range_;
552 code_range_ = NULL;
553 delete global_handles_;
554 global_handles_ = NULL;
555
556#ifdef ENABLE_DEBUGGER_SUPPORT
557 delete debugger_;
558 debugger_ = NULL;
559 delete debug_;
560 debug_ = NULL;
561#endif
562}
563
564
565bool Isolate::PreInit() {
566 if (state_ != UNINITIALIZED) return true;
567
568 TRACE_ISOLATE(preinit);
569
570 ASSERT(Isolate::Current() == this);
571
572#ifdef ENABLE_DEBUGGER_SUPPORT
573 debug_ = new Debug(this);
574 debugger_ = new Debugger();
575 debugger_->isolate_ = this;
576#endif
577
578 memory_allocator_ = new MemoryAllocator();
579 memory_allocator_->isolate_ = this;
580 code_range_ = new CodeRange();
581 code_range_->isolate_ = this;
582
583 // Safe after setting Heap::isolate_, initializing StackGuard and
584 // ensuring that Isolate::Current() == this.
585 heap_.SetStackLimits();
586
587#ifdef DEBUG
588 DisallowAllocationFailure disallow_allocation_failure;
589#endif
590
591#define C(name) isolate_addresses_[Isolate::k_##name] = \
592 reinterpret_cast<Address>(name());
593 ISOLATE_ADDRESS_LIST(C)
594 ISOLATE_ADDRESS_LIST_PROF(C)
595#undef C
596
597 string_tracker_ = new StringTracker();
598 string_tracker_->isolate_ = this;
599 thread_manager_ = new ThreadManager();
600 thread_manager_->isolate_ = this;
601 compilation_cache_ = new CompilationCache(this);
602 transcendental_cache_ = new TranscendentalCache();
603 keyed_lookup_cache_ = new KeyedLookupCache();
604 context_slot_cache_ = new ContextSlotCache();
605 descriptor_lookup_cache_ = new DescriptorLookupCache();
606 scanner_constants_ = new ScannerConstants();
607 pc_to_code_cache_ = new PcToCodeCache(this);
608 write_input_buffer_ = new StringInputBuffer();
609 global_handles_ = new GlobalHandles(this);
610 bootstrapper_ = new Bootstrapper();
611 cpu_features_ = new CpuFeatures();
612 handle_scope_implementer_ = new HandleScopeImplementer();
613 stub_cache_ = new StubCache(this);
614 ast_sentinels_ = new AstSentinels();
615 regexp_stack_ = new RegExpStack();
616 regexp_stack_->isolate_ = this;
617
618#ifdef ENABLE_LOGGING_AND_PROFILING
619 producer_heap_profile_ = new ProducerHeapProfile();
620 producer_heap_profile_->isolate_ = this;
621#endif
622
623 state_ = PREINITIALIZED;
624 return true;
625}
626
627
628void Isolate::InitializeThreadLocal() {
629 thread_local_top_.Initialize();
630 clear_pending_exception();
631 clear_pending_message();
632 clear_scheduled_exception();
633}
634
635
636bool Isolate::Init(Deserializer* des) {
637 ASSERT(state_ != INITIALIZED);
638
639 TRACE_ISOLATE(init);
640
641 bool create_heap_objects = des == NULL;
642
643#ifdef DEBUG
644 // The initialization process does not handle memory exhaustion.
645 DisallowAllocationFailure disallow_allocation_failure;
646#endif
647
648 if (state_ == UNINITIALIZED && !PreInit()) return false;
649
650 // Enable logging before setting up the heap
651 logger_->Setup();
652
653 CpuProfiler::Setup();
654 HeapProfiler::Setup();
655
656 // Setup the platform OS support.
657 OS::Setup();
658
659 // Initialize other runtime facilities
660#if defined(USE_SIMULATOR)
661#if defined(V8_TARGET_ARCH_ARM)
662 Simulator::Initialize();
663#elif defined(V8_TARGET_ARCH_MIPS)
664 ::assembler::mips::Simulator::Initialize();
665#endif
666#endif
667
668 { // NOLINT
669 // Ensure that the thread has a valid stack guard. The v8::Locker object
670 // will ensure this too, but we don't have to use lockers if we are only
671 // using one thread.
672 ExecutionAccess lock(this);
673 stack_guard_.InitThread(lock);
674 }
675
676 // Setup the object heap
677 ASSERT(!heap_.HasBeenSetup());
678 if (!heap_.Setup(create_heap_objects)) {
679 V8::SetFatalError();
680 return false;
681 }
682
683 bootstrapper_->Initialize(create_heap_objects);
684 builtins_.Setup(create_heap_objects);
685
686 InitializeThreadLocal();
687
688 // Only preallocate on the first initialization.
689 if (FLAG_preallocate_message_memory && preallocated_message_space_ == NULL) {
690 // Start the thread which will set aside some memory.
691 PreallocatedMemoryThreadStart();
692 preallocated_message_space_ =
693 new NoAllocationStringAllocator(
694 preallocated_memory_thread_->data(),
695 preallocated_memory_thread_->length());
696 PreallocatedStorageInit(preallocated_memory_thread_->length() / 4);
697 }
698
699 if (FLAG_preemption) {
700 v8::Locker locker;
701 v8::Locker::StartPreemption(100);
702 }
703
704#ifdef ENABLE_DEBUGGER_SUPPORT
705 debug_->Setup(create_heap_objects);
706#endif
707 stub_cache_->Initialize(create_heap_objects);
708
709 // If we are deserializing, read the state into the now-empty heap.
710 if (des != NULL) {
711 des->Deserialize();
712 stub_cache_->Clear();
713 }
714
715 // Deserializing may put strange things in the root array's copy of the
716 // stack guard.
717 heap_.SetStackLimits();
718
719 // Setup the CPU support. Must be done after heap setup and after
720 // any deserialization because we have to have the initial heap
721 // objects in place for creating the code object used for probing.
722 CPU::Setup();
723
724 deoptimizer_data_ = new DeoptimizerData;
725 runtime_profiler_ = new RuntimeProfiler(this);
726 runtime_profiler_->Setup();
727
728 // If we are deserializing, log non-function code objects and compiled
729 // functions found in the snapshot.
730 if (des != NULL && FLAG_log_code) {
731 HandleScope scope;
732 LOG(this, LogCodeObjects());
733 LOG(this, LogCompiledFunctions());
734 }
735
736 state_ = INITIALIZED;
737 return true;
738}
739
740
741void Isolate::Enter() {
742 Isolate* current_isolate = NULL;
743 PerIsolateThreadData* current_data = CurrentPerIsolateThreadData();
744 if (current_data != NULL) {
745 current_isolate = current_data->isolate_;
746 ASSERT(current_isolate != NULL);
747 if (current_isolate == this) {
748 ASSERT(Current() == this);
749 ASSERT(entry_stack_ != NULL);
750 ASSERT(entry_stack_->previous_thread_data == NULL ||
751 entry_stack_->previous_thread_data->thread_id() ==
752 Thread::GetThreadLocalInt(thread_id_key_));
753 // Same thread re-enters the isolate, no need to re-init anything.
754 entry_stack_->entry_count++;
755 return;
756 }
757 }
758
759 // Threads can have default isolate set into TLS as Current but not yet have
760 // PerIsolateThreadData for it, as it requires more advanced phase of the
761 // initialization. For example, a thread might be the one that system used for
762 // static initializers - in this case the default isolate is set in TLS but
763 // the thread did not yet Enter the isolate. If PerisolateThreadData is not
764 // there, use the isolate set in TLS.
765 if (current_isolate == NULL) {
766 current_isolate = Isolate::UncheckedCurrent();
767 }
768
769 PerIsolateThreadData* data = FindOrAllocatePerThreadDataForThisThread();
770 ASSERT(data != NULL);
771 ASSERT(data->isolate_ == this);
772
773 EntryStackItem* item = new EntryStackItem(current_data,
774 current_isolate,
775 entry_stack_);
776 entry_stack_ = item;
777
778 SetIsolateThreadLocals(this, data);
779
780 CHECK(PreInit());
781
782 // In case it's the first time some thread enters the isolate.
783 set_thread_id(data->thread_id());
784}
785
786
787void Isolate::Exit() {
788 ASSERT(entry_stack_ != NULL);
789 ASSERT(entry_stack_->previous_thread_data == NULL ||
790 entry_stack_->previous_thread_data->thread_id() ==
791 Thread::GetThreadLocalInt(thread_id_key_));
792
793 if (--entry_stack_->entry_count > 0) return;
794
795 ASSERT(CurrentPerIsolateThreadData() != NULL);
796 ASSERT(CurrentPerIsolateThreadData()->isolate_ == this);
797
798 // Pop the stack.
799 EntryStackItem* item = entry_stack_;
800 entry_stack_ = item->previous_item;
801
802 PerIsolateThreadData* previous_thread_data = item->previous_thread_data;
803 Isolate* previous_isolate = item->previous_isolate;
804
805 delete item;
806
807 // Reinit the current thread for the isolate it was running before this one.
808 SetIsolateThreadLocals(previous_isolate, previous_thread_data);
809}
810
811
812void Isolate::ResetEagerOptimizingData() {
813 compilation_cache_->ResetEagerOptimizingData();
814}
815
816
817#ifdef DEBUG
818#define ISOLATE_FIELD_OFFSET(type, name, ignored) \
819const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_);
820ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
821ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
822#undef ISOLATE_FIELD_OFFSET
823#endif
824
825} } // namespace v8::internal