blob: 3881d66fb017a2c9bba435a4aa82a74a6b8812ed [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2008 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());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 // First check whether the current thread has been 'lazily archived', ie
158 // 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),
241 next_(this),
242 previous_(this),
243 thread_manager_(thread_manager) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244}
245
246
247void ThreadState::AllocateSpace() {
248 data_ = NewArray<char>(ArchiveSpacePerThread());
249}
250
251
252void ThreadState::Unlink() {
253 next_->previous_ = previous_;
254 previous_->next_ = next_;
255}
256
257
258void ThreadState::LinkInto(List list) {
259 ThreadState* flying_anchor =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000260 list == FREE_LIST ? thread_manager_->free_anchor_
261 : thread_manager_->in_use_anchor_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 next_ = flying_anchor->next_;
263 previous_ = flying_anchor;
264 flying_anchor->next_ = this;
265 next_->previous_ = this;
266}
267
268
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000269ThreadState* ThreadManager::GetFreeThreadState() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 ThreadState* gotten = free_anchor_->next_;
271 if (gotten == free_anchor_) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000272 ThreadState* new_thread_state = new ThreadState(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273 new_thread_state->AllocateSpace();
274 return new_thread_state;
275 }
276 return gotten;
277}
278
279
280// Gets the first in the list of archived threads.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000281ThreadState* ThreadManager::FirstThreadStateInUse() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282 return in_use_anchor_->Next();
283}
284
285
286ThreadState* ThreadState::Next() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000287 if (next_ == thread_manager_->in_use_anchor_) return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 return next_;
289}
290
291
ager@chromium.orga1645e22009-09-09 19:27:10 +0000292// Thread ids must start with 1, because in TLS having thread id 0 can't
293// be distinguished from not having a thread id at all (since NULL is
294// defined as 0.)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000295ThreadManager::ThreadManager()
296 : mutex_(OS::CreateMutex()),
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000297 mutex_owner_(ThreadId::Invalid()),
298 lazily_archived_thread_(ThreadId::Invalid()),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000299 lazily_archived_thread_state_(NULL),
300 free_anchor_(NULL),
301 in_use_anchor_(NULL) {
302 free_anchor_ = new ThreadState(this);
303 in_use_anchor_ = new ThreadState(this);
304}
305
306
307ThreadManager::~ThreadManager() {
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000308 delete mutex_;
309 delete free_anchor_;
310 delete in_use_anchor_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000311}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312
313
314void ThreadManager::ArchiveThread() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000315 ASSERT(lazily_archived_thread_.Equals(ThreadId::Invalid()));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000316 ASSERT(!IsArchived());
lrn@chromium.org1c092762011-05-09 09:42:16 +0000317 ASSERT(IsLockedByCurrentThread());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000318 ThreadState* state = GetFreeThreadState();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319 state->Unlink();
lrn@chromium.org1c092762011-05-09 09:42:16 +0000320 Isolate::PerIsolateThreadData* per_thread =
321 isolate_->FindOrAllocatePerThreadDataForThisThread();
322 per_thread->set_thread_state(state);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000323 lazily_archived_thread_ = ThreadId::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 lazily_archived_thread_state_ = state;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000325 ASSERT(state->id().Equals(ThreadId::Invalid()));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000326 state->set_id(CurrentId());
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000327 ASSERT(!state->id().Equals(ThreadId::Invalid()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328}
329
330
331void ThreadManager::EagerlyArchiveThread() {
lrn@chromium.org1c092762011-05-09 09:42:16 +0000332 ASSERT(IsLockedByCurrentThread());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333 ThreadState* state = lazily_archived_thread_state_;
334 state->LinkInto(ThreadState::IN_USE_LIST);
335 char* to = state->data();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000336 // Ensure that data containing GC roots are archived first, and handle them
337 // in ThreadManager::Iterate(ObjectVisitor*).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000338 to = isolate_->handle_scope_implementer()->ArchiveThread(to);
339 to = isolate_->ArchiveThread(to);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000340 to = Relocatable::ArchiveState(isolate_, to);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000341#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000342 to = isolate_->debug()->ArchiveDebug(to);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000343#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000344 to = isolate_->stack_guard()->ArchiveStackGuard(to);
345 to = isolate_->regexp_stack()->ArchiveStack(to);
346 to = isolate_->bootstrapper()->ArchiveState(to);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000347 lazily_archived_thread_ = ThreadId::Invalid();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348 lazily_archived_thread_state_ = NULL;
349}
350
351
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000352void ThreadManager::FreeThreadResources() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000353 isolate_->handle_scope_implementer()->FreeThreadResources();
354 isolate_->FreeThreadResources();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000355#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000356 isolate_->debug()->FreeThreadResources();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000357#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000358 isolate_->stack_guard()->FreeThreadResources();
359 isolate_->regexp_stack()->FreeThreadResources();
360 isolate_->bootstrapper()->FreeThreadResources();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000361}
362
363
ager@chromium.orga1645e22009-09-09 19:27:10 +0000364bool ThreadManager::IsArchived() {
lrn@chromium.org1c092762011-05-09 09:42:16 +0000365 Isolate::PerIsolateThreadData* data =
366 isolate_->FindPerThreadDataForThisThread();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000367 return data != NULL && data->thread_state() != NULL;
ager@chromium.orga1645e22009-09-09 19:27:10 +0000368}
369
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370void ThreadManager::Iterate(ObjectVisitor* v) {
371 // Expecting no threads during serialization/deserialization
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000372 for (ThreadState* state = FirstThreadStateInUse();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373 state != NULL;
374 state = state->Next()) {
375 char* data = state->data();
376 data = HandleScopeImplementer::Iterate(v, data);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000377 data = isolate_->Iterate(v, data);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000378 data = Relocatable::Iterate(v, data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379 }
380}
381
382
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000383void ThreadManager::IterateArchivedThreads(ThreadVisitor* v) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000384 for (ThreadState* state = FirstThreadStateInUse();
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000385 state != NULL;
386 state = state->Next()) {
387 char* data = state->data();
388 data += HandleScopeImplementer::ArchiveSpacePerThread();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000389 isolate_->IterateThread(v, data);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000390 }
391}
392
393
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000394ThreadId ThreadManager::CurrentId() {
395 return ThreadId::Current();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000396}
397
398
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000399void ThreadManager::TerminateExecution(ThreadId thread_id) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000400 for (ThreadState* state = FirstThreadStateInUse();
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000401 state != NULL;
402 state = state->Next()) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000403 if (thread_id.Equals(state->id())) {
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000404 state->set_terminate_on_restore(true);
405 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000406 }
407}
408
409
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000410ContextSwitcher::ContextSwitcher(Isolate* isolate, int every_n_ms)
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000411 : Thread("v8:CtxtSwitcher"),
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000412 keep_going_(true),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000413 sleep_ms_(every_n_ms),
414 isolate_(isolate) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000415}
416
417
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000418// Set the scheduling interval of V8 threads. This function starts the
419// ContextSwitcher thread if needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420void ContextSwitcher::StartPreemption(int every_n_ms) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000421 Isolate* isolate = Isolate::Current();
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000422 ASSERT(Locker::IsLocked(reinterpret_cast<v8::Isolate*>(isolate)));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000423 if (isolate->context_switcher() == NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000424 // If the ContextSwitcher thread is not running at the moment start it now.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000425 isolate->set_context_switcher(new ContextSwitcher(isolate, every_n_ms));
426 isolate->context_switcher()->Start();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427 } else {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000428 // ContextSwitcher thread is already running, so we just change the
429 // scheduling interval.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000430 isolate->context_switcher()->sleep_ms_ = every_n_ms;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431 }
432}
433
434
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000435// Disable preemption of V8 threads. If multiple threads want to use V8 they
436// must cooperatively schedule amongst them from this point on.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437void ContextSwitcher::StopPreemption() {
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 // The ContextSwitcher thread is running. We need to stop it and release
442 // its resources.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000443 isolate->context_switcher()->keep_going_ = false;
444 // Wait for the ContextSwitcher thread to exit.
445 isolate->context_switcher()->Join();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000446 // Thread has exited, now we can delete it.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000447 delete(isolate->context_switcher());
448 isolate->set_context_switcher(NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449 }
450}
451
452
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000453// Main loop of the ContextSwitcher thread: Preempt the currently running V8
454// thread at regular intervals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455void ContextSwitcher::Run() {
456 while (keep_going_) {
457 OS::Sleep(sleep_ms_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000458 isolate()->stack_guard()->Preempt();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459 }
460}
461
462
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000463// Acknowledge the preemption by the receiving thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464void ContextSwitcher::PreemptionReceived() {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000465 ASSERT(Locker::IsLocked());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000466 // There is currently no accounting being done for this. But could be in the
467 // future, which is why we leave this in.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468}
469
470
471} // namespace internal
472} // namespace v8