blob: 32ea5e197c483ff0b018e3b9a68a223f5b1432ce [file] [log] [blame]
mstarzinger@chromium.orgc6d9cee2012-07-03 10:03:19 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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 "v8.h"
29
30#include "api.h"
ager@chromium.orgddb913d2009-01-27 10:01:48 +000031#include "bootstrapper.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "debug.h"
33#include "execution.h"
34#include "v8threads.h"
ager@chromium.org32912102009-01-16 10:38:43 +000035#include "regexp-stack.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
37namespace v8 {
38
ager@chromium.orgddb913d2009-01-27 10:01:48 +000039
40// Track whether this V8 instance has ever called v8::Locker. This allows the
41// API code to verify that the lock is always held when V8 is being entered.
42bool Locker::active_ = false;
43
44
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045// Constructor for the Locker object. Once the Locker is constructed the
lrn@chromium.org1c092762011-05-09 09:42:16 +000046// current thread will be guaranteed to have the lock for a given isolate.
47Locker::Locker(v8::Isolate* isolate)
48 : has_lock_(false),
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +000049 top_level_(true),
lrn@chromium.org1c092762011-05-09 09:42:16 +000050 isolate_(reinterpret_cast<i::Isolate*>(isolate)) {
51 if (isolate_ == NULL) {
52 isolate_ = i::Isolate::GetDefaultIsolateForLocking();
53 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +000054 // Record that the Locker has been used at least once.
55 active_ = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056 // Get the big lock if necessary.
lrn@chromium.org1c092762011-05-09 09:42:16 +000057 if (!isolate_->thread_manager()->IsLockedByCurrentThread()) {
58 isolate_->thread_manager()->Lock();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000059 has_lock_ = true;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000060
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000061 // Make sure that V8 is initialized. Archiving of threads interferes
62 // with deserialization by adding additional root pointers, so we must
63 // initialize here, before anyone can call ~Locker() or Unlocker().
lrn@chromium.org1c092762011-05-09 09:42:16 +000064 if (!isolate_->IsInitialized()) {
65 isolate_->Enter();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000066 V8::Initialize();
lrn@chromium.org1c092762011-05-09 09:42:16 +000067 isolate_->Exit();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000068 }
lrn@chromium.org1c092762011-05-09 09:42:16 +000069
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070 // This may be a locker within an unlocker in which case we have to
71 // get the saved state for this thread and restore it.
lrn@chromium.org1c092762011-05-09 09:42:16 +000072 if (isolate_->thread_manager()->RestoreThread()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073 top_level_ = false;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000074 } else {
lrn@chromium.org1c092762011-05-09 09:42:16 +000075 internal::ExecutionAccess access(isolate_);
76 isolate_->stack_guard()->ClearThread(access);
77 isolate_->stack_guard()->InitThread(access);
78 }
79 if (isolate_->IsDefaultIsolate()) {
80 // This only enters if not yet entered.
81 internal::Isolate::EnterDefaultIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082 }
83 }
lrn@chromium.org1c092762011-05-09 09:42:16 +000084 ASSERT(isolate_->thread_manager()->IsLockedByCurrentThread());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000085}
86
87
lrn@chromium.org1c092762011-05-09 09:42:16 +000088bool Locker::IsLocked(v8::Isolate* isolate) {
89 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
90 if (internal_isolate == NULL) {
91 internal_isolate = i::Isolate::GetDefaultIsolateForLocking();
92 }
93 return internal_isolate->thread_manager()->IsLockedByCurrentThread();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095
96
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000097bool Locker::IsActive() {
98 return active_;
99}
100
101
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102Locker::~Locker() {
lrn@chromium.org1c092762011-05-09 09:42:16 +0000103 ASSERT(isolate_->thread_manager()->IsLockedByCurrentThread());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104 if (has_lock_) {
lrn@chromium.org1c092762011-05-09 09:42:16 +0000105 if (isolate_->IsDefaultIsolate()) {
106 isolate_->Exit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107 }
lrn@chromium.org1c092762011-05-09 09:42:16 +0000108 if (top_level_) {
109 isolate_->thread_manager()->FreeThreadResources();
110 } else {
111 isolate_->thread_manager()->ArchiveThread();
112 }
113 isolate_->thread_manager()->Unlock();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114 }
115}
116
117
lrn@chromium.org1c092762011-05-09 09:42:16 +0000118Unlocker::Unlocker(v8::Isolate* isolate)
119 : isolate_(reinterpret_cast<i::Isolate*>(isolate)) {
120 if (isolate_ == NULL) {
121 isolate_ = i::Isolate::GetDefaultIsolateForLocking();
122 }
123 ASSERT(isolate_->thread_manager()->IsLockedByCurrentThread());
124 if (isolate_->IsDefaultIsolate()) {
125 isolate_->Exit();
126 }
127 isolate_->thread_manager()->ArchiveThread();
128 isolate_->thread_manager()->Unlock();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129}
130
131
132Unlocker::~Unlocker() {
lrn@chromium.org1c092762011-05-09 09:42:16 +0000133 ASSERT(!isolate_->thread_manager()->IsLockedByCurrentThread());
134 isolate_->thread_manager()->Lock();
135 isolate_->thread_manager()->RestoreThread();
136 if (isolate_->IsDefaultIsolate()) {
137 isolate_->Enter();
138 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139}
140
141
142void Locker::StartPreemption(int every_n_ms) {
143 v8::internal::ContextSwitcher::StartPreemption(every_n_ms);
144}
145
146
147void Locker::StopPreemption() {
148 v8::internal::ContextSwitcher::StopPreemption();
149}
150
151
152namespace internal {
153
154
155bool ThreadManager::RestoreThread() {
lrn@chromium.org1c092762011-05-09 09:42:16 +0000156 ASSERT(IsLockedByCurrentThread());
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000157 // First check whether the current thread has been 'lazily archived', i.e.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 // not archived at all. If that is the case we put the state storage we
159 // had prepared back in the free list, since we didn't need it after all.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000160 if (lazily_archived_thread_.Equals(ThreadId::Current())) {
161 lazily_archived_thread_ = ThreadId::Invalid();
lrn@chromium.org1c092762011-05-09 09:42:16 +0000162 Isolate::PerIsolateThreadData* per_thread =
163 isolate_->FindPerThreadDataForThisThread();
164 ASSERT(per_thread != NULL);
165 ASSERT(per_thread->thread_state() == lazily_archived_thread_state_);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000166 lazily_archived_thread_state_->set_id(ThreadId::Invalid());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 lazily_archived_thread_state_->LinkInto(ThreadState::FREE_LIST);
168 lazily_archived_thread_state_ = NULL;
lrn@chromium.org1c092762011-05-09 09:42:16 +0000169 per_thread->set_thread_state(NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 return true;
171 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000172
173 // Make sure that the preemption thread cannot modify the thread state while
174 // it is being archived or restored.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000175 ExecutionAccess access(isolate_);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000176
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177 // If there is another thread that was lazily archived then we have to really
178 // archive it now.
179 if (lazily_archived_thread_.IsValid()) {
180 EagerlyArchiveThread();
181 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000182 Isolate::PerIsolateThreadData* per_thread =
lrn@chromium.org1c092762011-05-09 09:42:16 +0000183 isolate_->FindPerThreadDataForThisThread();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000184 if (per_thread == NULL || per_thread->thread_state() == NULL) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000185 // This is a new thread.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000186 isolate_->stack_guard()->InitThread(access);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187 return false;
188 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000189 ThreadState* state = per_thread->thread_state();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 char* from = state->data();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000191 from = isolate_->handle_scope_implementer()->RestoreThread(from);
192 from = isolate_->RestoreThread(from);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000193 from = Relocatable::RestoreState(isolate_, from);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000194#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000195 from = isolate_->debug()->RestoreDebug(from);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000196#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000197 from = isolate_->stack_guard()->RestoreStackGuard(from);
198 from = isolate_->regexp_stack()->RestoreStack(from);
199 from = isolate_->bootstrapper()->RestoreState(from);
200 per_thread->set_thread_state(NULL);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000201 if (state->terminate_on_restore()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000202 isolate_->stack_guard()->TerminateExecution();
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000203 state->set_terminate_on_restore(false);
204 }
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000205 state->set_id(ThreadId::Invalid());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 state->Unlink();
207 state->LinkInto(ThreadState::FREE_LIST);
208 return true;
209}
210
211
212void ThreadManager::Lock() {
213 mutex_->Lock();
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000214 mutex_owner_ = ThreadId::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215 ASSERT(IsLockedByCurrentThread());
216}
217
218
219void ThreadManager::Unlock() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000220 mutex_owner_ = ThreadId::Invalid();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221 mutex_->Unlock();
222}
223
224
225static int ArchiveSpacePerThread() {
226 return HandleScopeImplementer::ArchiveSpacePerThread() +
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000227 Isolate::ArchiveSpacePerThread() +
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000228#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229 Debug::ArchiveSpacePerThread() +
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000230#endif
ager@chromium.org32912102009-01-16 10:38:43 +0000231 StackGuard::ArchiveSpacePerThread() +
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000232 RegExpStack::ArchiveSpacePerThread() +
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000233 Bootstrapper::ArchiveSpacePerThread() +
234 Relocatable::ArchiveSpacePerThread();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235}
236
237
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000238ThreadState::ThreadState(ThreadManager* thread_manager)
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000239 : id_(ThreadId::Invalid()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000240 terminate_on_restore_(false),
mstarzinger@chromium.orgc6d9cee2012-07-03 10:03:19 +0000241 data_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000242 next_(this),
243 previous_(this),
244 thread_manager_(thread_manager) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245}
246
247
mstarzinger@chromium.orgc6d9cee2012-07-03 10:03:19 +0000248ThreadState::~ThreadState() {
249 DeleteArray<char>(data_);
250}
251
252
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253void ThreadState::AllocateSpace() {
254 data_ = NewArray<char>(ArchiveSpacePerThread());
255}
256
257
258void ThreadState::Unlink() {
259 next_->previous_ = previous_;
260 previous_->next_ = next_;
261}
262
263
264void ThreadState::LinkInto(List list) {
265 ThreadState* flying_anchor =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000266 list == FREE_LIST ? thread_manager_->free_anchor_
267 : thread_manager_->in_use_anchor_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 next_ = flying_anchor->next_;
269 previous_ = flying_anchor;
270 flying_anchor->next_ = this;
271 next_->previous_ = this;
272}
273
274
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000275ThreadState* ThreadManager::GetFreeThreadState() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 ThreadState* gotten = free_anchor_->next_;
277 if (gotten == free_anchor_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000278 ThreadState* new_thread_state = new ThreadState(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 new_thread_state->AllocateSpace();
280 return new_thread_state;
281 }
282 return gotten;
283}
284
285
286// Gets the first in the list of archived threads.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000287ThreadState* ThreadManager::FirstThreadStateInUse() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 return in_use_anchor_->Next();
289}
290
291
292ThreadState* ThreadState::Next() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000293 if (next_ == thread_manager_->in_use_anchor_) return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294 return next_;
295}
296
297
ager@chromium.orga1645e22009-09-09 19:27:10 +0000298// Thread ids must start with 1, because in TLS having thread id 0 can't
299// be distinguished from not having a thread id at all (since NULL is
300// defined as 0.)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000301ThreadManager::ThreadManager()
302 : mutex_(OS::CreateMutex()),
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000303 mutex_owner_(ThreadId::Invalid()),
304 lazily_archived_thread_(ThreadId::Invalid()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000305 lazily_archived_thread_state_(NULL),
306 free_anchor_(NULL),
307 in_use_anchor_(NULL) {
308 free_anchor_ = new ThreadState(this);
309 in_use_anchor_ = new ThreadState(this);
310}
311
312
313ThreadManager::~ThreadManager() {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000314 delete mutex_;
mstarzinger@chromium.orgc6d9cee2012-07-03 10:03:19 +0000315 DeleteThreadStateList(free_anchor_);
316 DeleteThreadStateList(in_use_anchor_);
317}
318
319
320void ThreadManager::DeleteThreadStateList(ThreadState* anchor) {
321 // The list starts and ends with the anchor.
322 for (ThreadState* current = anchor->next_; current != anchor;) {
323 ThreadState* next = current->next_;
324 delete current;
325 current = next;
326 }
327 delete anchor;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000328}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329
330
331void ThreadManager::ArchiveThread() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000332 ASSERT(lazily_archived_thread_.Equals(ThreadId::Invalid()));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000333 ASSERT(!IsArchived());
lrn@chromium.org1c092762011-05-09 09:42:16 +0000334 ASSERT(IsLockedByCurrentThread());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000335 ThreadState* state = GetFreeThreadState();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 state->Unlink();
lrn@chromium.org1c092762011-05-09 09:42:16 +0000337 Isolate::PerIsolateThreadData* per_thread =
338 isolate_->FindOrAllocatePerThreadDataForThisThread();
339 per_thread->set_thread_state(state);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000340 lazily_archived_thread_ = ThreadId::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 lazily_archived_thread_state_ = state;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000342 ASSERT(state->id().Equals(ThreadId::Invalid()));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000343 state->set_id(CurrentId());
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000344 ASSERT(!state->id().Equals(ThreadId::Invalid()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345}
346
347
348void ThreadManager::EagerlyArchiveThread() {
lrn@chromium.org1c092762011-05-09 09:42:16 +0000349 ASSERT(IsLockedByCurrentThread());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 ThreadState* state = lazily_archived_thread_state_;
351 state->LinkInto(ThreadState::IN_USE_LIST);
352 char* to = state->data();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000353 // Ensure that data containing GC roots are archived first, and handle them
354 // in ThreadManager::Iterate(ObjectVisitor*).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000355 to = isolate_->handle_scope_implementer()->ArchiveThread(to);
356 to = isolate_->ArchiveThread(to);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000357 to = Relocatable::ArchiveState(isolate_, to);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000358#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000359 to = isolate_->debug()->ArchiveDebug(to);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000360#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000361 to = isolate_->stack_guard()->ArchiveStackGuard(to);
362 to = isolate_->regexp_stack()->ArchiveStack(to);
363 to = isolate_->bootstrapper()->ArchiveState(to);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000364 lazily_archived_thread_ = ThreadId::Invalid();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 lazily_archived_thread_state_ = NULL;
366}
367
368
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000369void ThreadManager::FreeThreadResources() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000370 isolate_->handle_scope_implementer()->FreeThreadResources();
371 isolate_->FreeThreadResources();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000372#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000373 isolate_->debug()->FreeThreadResources();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000374#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000375 isolate_->stack_guard()->FreeThreadResources();
376 isolate_->regexp_stack()->FreeThreadResources();
377 isolate_->bootstrapper()->FreeThreadResources();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000378}
379
380
ager@chromium.orga1645e22009-09-09 19:27:10 +0000381bool ThreadManager::IsArchived() {
lrn@chromium.org1c092762011-05-09 09:42:16 +0000382 Isolate::PerIsolateThreadData* data =
383 isolate_->FindPerThreadDataForThisThread();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000384 return data != NULL && data->thread_state() != NULL;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000385}
386
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387void ThreadManager::Iterate(ObjectVisitor* v) {
388 // Expecting no threads during serialization/deserialization
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000389 for (ThreadState* state = FirstThreadStateInUse();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 state != NULL;
391 state = state->Next()) {
392 char* data = state->data();
393 data = HandleScopeImplementer::Iterate(v, data);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000394 data = isolate_->Iterate(v, data);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000395 data = Relocatable::Iterate(v, data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396 }
397}
398
399
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000400void ThreadManager::IterateArchivedThreads(ThreadVisitor* v) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000401 for (ThreadState* state = FirstThreadStateInUse();
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000402 state != NULL;
403 state = state->Next()) {
404 char* data = state->data();
405 data += HandleScopeImplementer::ArchiveSpacePerThread();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000406 isolate_->IterateThread(v, data);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000407 }
408}
409
410
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000411ThreadId ThreadManager::CurrentId() {
412 return ThreadId::Current();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000413}
414
415
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000416void ThreadManager::TerminateExecution(ThreadId thread_id) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000417 for (ThreadState* state = FirstThreadStateInUse();
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000418 state != NULL;
419 state = state->Next()) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000420 if (thread_id.Equals(state->id())) {
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000421 state->set_terminate_on_restore(true);
422 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000423 }
424}
425
426
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000427ContextSwitcher::ContextSwitcher(Isolate* isolate, int every_n_ms)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000428 : Thread("v8:CtxtSwitcher"),
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000429 keep_going_(true),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000430 sleep_ms_(every_n_ms),
431 isolate_(isolate) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432}
433
434
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000435// Set the scheduling interval of V8 threads. This function starts the
436// ContextSwitcher thread if needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437void ContextSwitcher::StartPreemption(int every_n_ms) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000438 Isolate* isolate = Isolate::Current();
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000439 ASSERT(Locker::IsLocked(reinterpret_cast<v8::Isolate*>(isolate)));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000440 if (isolate->context_switcher() == NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000441 // If the ContextSwitcher thread is not running at the moment start it now.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000442 isolate->set_context_switcher(new ContextSwitcher(isolate, every_n_ms));
443 isolate->context_switcher()->Start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444 } else {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000445 // ContextSwitcher thread is already running, so we just change the
446 // scheduling interval.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000447 isolate->context_switcher()->sleep_ms_ = every_n_ms;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448 }
449}
450
451
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000452// Disable preemption of V8 threads. If multiple threads want to use V8 they
453// must cooperatively schedule amongst them from this point on.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454void ContextSwitcher::StopPreemption() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000455 Isolate* isolate = Isolate::Current();
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000456 ASSERT(Locker::IsLocked(reinterpret_cast<v8::Isolate*>(isolate)));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000457 if (isolate->context_switcher() != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000458 // The ContextSwitcher thread is running. We need to stop it and release
459 // its resources.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000460 isolate->context_switcher()->keep_going_ = false;
461 // Wait for the ContextSwitcher thread to exit.
462 isolate->context_switcher()->Join();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000463 // Thread has exited, now we can delete it.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000464 delete(isolate->context_switcher());
465 isolate->set_context_switcher(NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466 }
467}
468
469
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000470// Main loop of the ContextSwitcher thread: Preempt the currently running V8
471// thread at regular intervals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472void ContextSwitcher::Run() {
473 while (keep_going_) {
474 OS::Sleep(sleep_ms_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000475 isolate()->stack_guard()->Preempt();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 }
477}
478
479
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000480// Acknowledge the preemption by the receiving thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481void ContextSwitcher::PreemptionReceived() {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000482 ASSERT(Locker::IsLocked());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000483 // There is currently no accounting being done for this. But could be in the
484 // future, which is why we leave this in.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485}
486
487
488} // namespace internal
489} // namespace v8