blob: 3881d66fb017a2c9bba435a4aa82a74a6b8812ed [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 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 "v8.h"
29
30#include "api.h"
31#include "bootstrapper.h"
32#include "debug.h"
33#include "execution.h"
34#include "v8threads.h"
35#include "regexp-stack.h"
36
37namespace v8 {
38
Steve Blocka7e24c12009-10-30 11:49:00 +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
45// Constructor for the Locker object. Once the Locker is constructed the
Ben Murdoch257744e2011-11-30 15:57:28 +000046// current thread will be guaranteed to have the lock for a given isolate.
47Locker::Locker(v8::Isolate* isolate)
48 : has_lock_(false),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000049 top_level_(true),
Ben Murdoch257744e2011-11-30 15:57:28 +000050 isolate_(reinterpret_cast<i::Isolate*>(isolate)) {
51 if (isolate_ == NULL) {
52 isolate_ = i::Isolate::GetDefaultIsolateForLocking();
53 }
Steve Blocka7e24c12009-10-30 11:49:00 +000054 // Record that the Locker has been used at least once.
55 active_ = true;
56 // Get the big lock if necessary.
Ben Murdoch257744e2011-11-30 15:57:28 +000057 if (!isolate_->thread_manager()->IsLockedByCurrentThread()) {
58 isolate_->thread_manager()->Lock();
Steve Blocka7e24c12009-10-30 11:49:00 +000059 has_lock_ = true;
Steve Block44f0eee2011-05-26 01:26:41 +010060
Steve Blocka7e24c12009-10-30 11:49:00 +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().
Ben Murdoch257744e2011-11-30 15:57:28 +000064 if (!isolate_->IsInitialized()) {
65 isolate_->Enter();
Steve Blocka7e24c12009-10-30 11:49:00 +000066 V8::Initialize();
Ben Murdoch257744e2011-11-30 15:57:28 +000067 isolate_->Exit();
Steve Blocka7e24c12009-10-30 11:49:00 +000068 }
Ben Murdoch257744e2011-11-30 15:57:28 +000069
Steve Blocka7e24c12009-10-30 11:49:00 +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.
Ben Murdoch257744e2011-11-30 15:57:28 +000072 if (isolate_->thread_manager()->RestoreThread()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000073 top_level_ = false;
74 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +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();
Steve Blocka7e24c12009-10-30 11:49:00 +000082 }
83 }
Ben Murdoch257744e2011-11-30 15:57:28 +000084 ASSERT(isolate_->thread_manager()->IsLockedByCurrentThread());
Steve Blocka7e24c12009-10-30 11:49:00 +000085}
86
87
Ben Murdoch257744e2011-11-30 15:57:28 +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();
Steve Blocka7e24c12009-10-30 11:49:00 +000094}
95
96
Ben Murdoch69a99ed2011-11-30 16:03:39 +000097bool Locker::IsActive() {
98 return active_;
99}
100
101
Steve Blocka7e24c12009-10-30 11:49:00 +0000102Locker::~Locker() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000103 ASSERT(isolate_->thread_manager()->IsLockedByCurrentThread());
Steve Blocka7e24c12009-10-30 11:49:00 +0000104 if (has_lock_) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000105 if (isolate_->IsDefaultIsolate()) {
106 isolate_->Exit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000107 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000108 if (top_level_) {
109 isolate_->thread_manager()->FreeThreadResources();
110 } else {
111 isolate_->thread_manager()->ArchiveThread();
112 }
113 isolate_->thread_manager()->Unlock();
Steve Blocka7e24c12009-10-30 11:49:00 +0000114 }
115}
116
117
Ben Murdoch257744e2011-11-30 15:57:28 +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();
Steve Blocka7e24c12009-10-30 11:49:00 +0000129}
130
131
132Unlocker::~Unlocker() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000133 ASSERT(!isolate_->thread_manager()->IsLockedByCurrentThread());
134 isolate_->thread_manager()->Lock();
135 isolate_->thread_manager()->RestoreThread();
136 if (isolate_->IsDefaultIsolate()) {
137 isolate_->Enter();
138 }
Steve Blocka7e24c12009-10-30 11:49:00 +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() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000156 ASSERT(IsLockedByCurrentThread());
Steve Blocka7e24c12009-10-30 11:49:00 +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.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100160 if (lazily_archived_thread_.Equals(ThreadId::Current())) {
161 lazily_archived_thread_ = ThreadId::Invalid();
Ben Murdoch257744e2011-11-30 15:57:28 +0000162 Isolate::PerIsolateThreadData* per_thread =
163 isolate_->FindPerThreadDataForThisThread();
164 ASSERT(per_thread != NULL);
165 ASSERT(per_thread->thread_state() == lazily_archived_thread_state_);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100166 lazily_archived_thread_state_->set_id(ThreadId::Invalid());
Steve Blocka7e24c12009-10-30 11:49:00 +0000167 lazily_archived_thread_state_->LinkInto(ThreadState::FREE_LIST);
168 lazily_archived_thread_state_ = NULL;
Ben Murdoch257744e2011-11-30 15:57:28 +0000169 per_thread->set_thread_state(NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000170 return true;
171 }
172
173 // Make sure that the preemption thread cannot modify the thread state while
174 // it is being archived or restored.
Steve Block44f0eee2011-05-26 01:26:41 +0100175 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000176
177 // 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 }
Steve Block44f0eee2011-05-26 01:26:41 +0100182 Isolate::PerIsolateThreadData* per_thread =
Ben Murdoch257744e2011-11-30 15:57:28 +0000183 isolate_->FindPerThreadDataForThisThread();
Steve Block44f0eee2011-05-26 01:26:41 +0100184 if (per_thread == NULL || per_thread->thread_state() == NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000185 // This is a new thread.
Steve Block44f0eee2011-05-26 01:26:41 +0100186 isolate_->stack_guard()->InitThread(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 return false;
188 }
Steve Block44f0eee2011-05-26 01:26:41 +0100189 ThreadState* state = per_thread->thread_state();
Steve Blocka7e24c12009-10-30 11:49:00 +0000190 char* from = state->data();
Steve Block44f0eee2011-05-26 01:26:41 +0100191 from = isolate_->handle_scope_implementer()->RestoreThread(from);
192 from = isolate_->RestoreThread(from);
Ben Murdoch257744e2011-11-30 15:57:28 +0000193 from = Relocatable::RestoreState(isolate_, from);
Steve Blocka7e24c12009-10-30 11:49:00 +0000194#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100195 from = isolate_->debug()->RestoreDebug(from);
Steve Blocka7e24c12009-10-30 11:49:00 +0000196#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100197 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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000201 if (state->terminate_on_restore()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100202 isolate_->stack_guard()->TerminateExecution();
Steve Blocka7e24c12009-10-30 11:49:00 +0000203 state->set_terminate_on_restore(false);
204 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100205 state->set_id(ThreadId::Invalid());
Steve Blocka7e24c12009-10-30 11:49:00 +0000206 state->Unlink();
207 state->LinkInto(ThreadState::FREE_LIST);
208 return true;
209}
210
211
212void ThreadManager::Lock() {
213 mutex_->Lock();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100214 mutex_owner_ = ThreadId::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +0000215 ASSERT(IsLockedByCurrentThread());
216}
217
218
219void ThreadManager::Unlock() {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100220 mutex_owner_ = ThreadId::Invalid();
Steve Blocka7e24c12009-10-30 11:49:00 +0000221 mutex_->Unlock();
222}
223
224
225static int ArchiveSpacePerThread() {
226 return HandleScopeImplementer::ArchiveSpacePerThread() +
Steve Block44f0eee2011-05-26 01:26:41 +0100227 Isolate::ArchiveSpacePerThread() +
Steve Blocka7e24c12009-10-30 11:49:00 +0000228#ifdef ENABLE_DEBUGGER_SUPPORT
229 Debug::ArchiveSpacePerThread() +
230#endif
231 StackGuard::ArchiveSpacePerThread() +
232 RegExpStack::ArchiveSpacePerThread() +
233 Bootstrapper::ArchiveSpacePerThread() +
234 Relocatable::ArchiveSpacePerThread();
235}
236
237
Steve Block44f0eee2011-05-26 01:26:41 +0100238ThreadState::ThreadState(ThreadManager* thread_manager)
Ben Murdoch8b112d22011-06-08 16:22:53 +0100239 : id_(ThreadId::Invalid()),
Steve Block44f0eee2011-05-26 01:26:41 +0100240 terminate_on_restore_(false),
241 next_(this),
242 previous_(this),
243 thread_manager_(thread_manager) {
Steve Blocka7e24c12009-10-30 11:49:00 +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 =
Steve Block44f0eee2011-05-26 01:26:41 +0100260 list == FREE_LIST ? thread_manager_->free_anchor_
261 : thread_manager_->in_use_anchor_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000262 next_ = flying_anchor->next_;
263 previous_ = flying_anchor;
264 flying_anchor->next_ = this;
265 next_->previous_ = this;
266}
267
268
Steve Block44f0eee2011-05-26 01:26:41 +0100269ThreadState* ThreadManager::GetFreeThreadState() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 ThreadState* gotten = free_anchor_->next_;
271 if (gotten == free_anchor_) {
Steve Block44f0eee2011-05-26 01:26:41 +0100272 ThreadState* new_thread_state = new ThreadState(this);
Steve Blocka7e24c12009-10-30 11:49:00 +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.
Steve Block44f0eee2011-05-26 01:26:41 +0100281ThreadState* ThreadManager::FirstThreadStateInUse() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000282 return in_use_anchor_->Next();
283}
284
285
286ThreadState* ThreadState::Next() {
Steve Block44f0eee2011-05-26 01:26:41 +0100287 if (next_ == thread_manager_->in_use_anchor_) return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000288 return next_;
289}
290
291
292// 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.)
Steve Block44f0eee2011-05-26 01:26:41 +0100295ThreadManager::ThreadManager()
296 : mutex_(OS::CreateMutex()),
Ben Murdoch8b112d22011-06-08 16:22:53 +0100297 mutex_owner_(ThreadId::Invalid()),
298 lazily_archived_thread_(ThreadId::Invalid()),
Steve Block44f0eee2011-05-26 01:26:41 +0100299 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() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000308 delete mutex_;
309 delete free_anchor_;
310 delete in_use_anchor_;
Steve Block44f0eee2011-05-26 01:26:41 +0100311}
Steve Blocka7e24c12009-10-30 11:49:00 +0000312
313
314void ThreadManager::ArchiveThread() {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100315 ASSERT(lazily_archived_thread_.Equals(ThreadId::Invalid()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000316 ASSERT(!IsArchived());
Ben Murdoch257744e2011-11-30 15:57:28 +0000317 ASSERT(IsLockedByCurrentThread());
Steve Block44f0eee2011-05-26 01:26:41 +0100318 ThreadState* state = GetFreeThreadState();
Steve Blocka7e24c12009-10-30 11:49:00 +0000319 state->Unlink();
Ben Murdoch257744e2011-11-30 15:57:28 +0000320 Isolate::PerIsolateThreadData* per_thread =
321 isolate_->FindOrAllocatePerThreadDataForThisThread();
322 per_thread->set_thread_state(state);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100323 lazily_archived_thread_ = ThreadId::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +0000324 lazily_archived_thread_state_ = state;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100325 ASSERT(state->id().Equals(ThreadId::Invalid()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000326 state->set_id(CurrentId());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100327 ASSERT(!state->id().Equals(ThreadId::Invalid()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000328}
329
330
331void ThreadManager::EagerlyArchiveThread() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000332 ASSERT(IsLockedByCurrentThread());
Steve Blocka7e24c12009-10-30 11:49:00 +0000333 ThreadState* state = lazily_archived_thread_state_;
334 state->LinkInto(ThreadState::IN_USE_LIST);
335 char* to = state->data();
336 // Ensure that data containing GC roots are archived first, and handle them
337 // in ThreadManager::Iterate(ObjectVisitor*).
Steve Block44f0eee2011-05-26 01:26:41 +0100338 to = isolate_->handle_scope_implementer()->ArchiveThread(to);
339 to = isolate_->ArchiveThread(to);
Ben Murdoch257744e2011-11-30 15:57:28 +0000340 to = Relocatable::ArchiveState(isolate_, to);
Steve Blocka7e24c12009-10-30 11:49:00 +0000341#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100342 to = isolate_->debug()->ArchiveDebug(to);
Steve Blocka7e24c12009-10-30 11:49:00 +0000343#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100344 to = isolate_->stack_guard()->ArchiveStackGuard(to);
345 to = isolate_->regexp_stack()->ArchiveStack(to);
346 to = isolate_->bootstrapper()->ArchiveState(to);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100347 lazily_archived_thread_ = ThreadId::Invalid();
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 lazily_archived_thread_state_ = NULL;
349}
350
351
352void ThreadManager::FreeThreadResources() {
Steve Block44f0eee2011-05-26 01:26:41 +0100353 isolate_->handle_scope_implementer()->FreeThreadResources();
354 isolate_->FreeThreadResources();
Steve Blocka7e24c12009-10-30 11:49:00 +0000355#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100356 isolate_->debug()->FreeThreadResources();
Steve Blocka7e24c12009-10-30 11:49:00 +0000357#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100358 isolate_->stack_guard()->FreeThreadResources();
359 isolate_->regexp_stack()->FreeThreadResources();
360 isolate_->bootstrapper()->FreeThreadResources();
Steve Blocka7e24c12009-10-30 11:49:00 +0000361}
362
363
364bool ThreadManager::IsArchived() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000365 Isolate::PerIsolateThreadData* data =
366 isolate_->FindPerThreadDataForThisThread();
Steve Block44f0eee2011-05-26 01:26:41 +0100367 return data != NULL && data->thread_state() != NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000368}
369
Steve Blocka7e24c12009-10-30 11:49:00 +0000370void ThreadManager::Iterate(ObjectVisitor* v) {
371 // Expecting no threads during serialization/deserialization
Steve Block44f0eee2011-05-26 01:26:41 +0100372 for (ThreadState* state = FirstThreadStateInUse();
Steve Blocka7e24c12009-10-30 11:49:00 +0000373 state != NULL;
374 state = state->Next()) {
375 char* data = state->data();
376 data = HandleScopeImplementer::Iterate(v, data);
Steve Block44f0eee2011-05-26 01:26:41 +0100377 data = isolate_->Iterate(v, data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 data = Relocatable::Iterate(v, data);
379 }
380}
381
382
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100383void ThreadManager::IterateArchivedThreads(ThreadVisitor* v) {
Steve Block44f0eee2011-05-26 01:26:41 +0100384 for (ThreadState* state = FirstThreadStateInUse();
Steve Block6ded16b2010-05-10 14:33:55 +0100385 state != NULL;
386 state = state->Next()) {
387 char* data = state->data();
388 data += HandleScopeImplementer::ArchiveSpacePerThread();
Steve Block44f0eee2011-05-26 01:26:41 +0100389 isolate_->IterateThread(v, data);
Steve Block6ded16b2010-05-10 14:33:55 +0100390 }
391}
392
393
Ben Murdoch8b112d22011-06-08 16:22:53 +0100394ThreadId ThreadManager::CurrentId() {
395 return ThreadId::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +0000396}
397
398
Ben Murdoch8b112d22011-06-08 16:22:53 +0100399void ThreadManager::TerminateExecution(ThreadId thread_id) {
Steve Block44f0eee2011-05-26 01:26:41 +0100400 for (ThreadState* state = FirstThreadStateInUse();
Steve Blocka7e24c12009-10-30 11:49:00 +0000401 state != NULL;
402 state = state->Next()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100403 if (thread_id.Equals(state->id())) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 state->set_terminate_on_restore(true);
405 }
406 }
407}
408
409
Steve Block44f0eee2011-05-26 01:26:41 +0100410ContextSwitcher::ContextSwitcher(Isolate* isolate, int every_n_ms)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000411 : Thread("v8:CtxtSwitcher"),
Steve Block9fac8402011-05-12 15:51:54 +0100412 keep_going_(true),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000413 sleep_ms_(every_n_ms),
414 isolate_(isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000415}
416
417
418// Set the scheduling interval of V8 threads. This function starts the
419// ContextSwitcher thread if needed.
420void ContextSwitcher::StartPreemption(int every_n_ms) {
Steve Block44f0eee2011-05-26 01:26:41 +0100421 Isolate* isolate = Isolate::Current();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000422 ASSERT(Locker::IsLocked(reinterpret_cast<v8::Isolate*>(isolate)));
Steve Block44f0eee2011-05-26 01:26:41 +0100423 if (isolate->context_switcher() == NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000424 // If the ContextSwitcher thread is not running at the moment start it now.
Steve Block44f0eee2011-05-26 01:26:41 +0100425 isolate->set_context_switcher(new ContextSwitcher(isolate, every_n_ms));
426 isolate->context_switcher()->Start();
Steve Blocka7e24c12009-10-30 11:49:00 +0000427 } else {
428 // ContextSwitcher thread is already running, so we just change the
429 // scheduling interval.
Steve Block44f0eee2011-05-26 01:26:41 +0100430 isolate->context_switcher()->sleep_ms_ = every_n_ms;
Steve Blocka7e24c12009-10-30 11:49:00 +0000431 }
432}
433
434
435// Disable preemption of V8 threads. If multiple threads want to use V8 they
436// must cooperatively schedule amongst them from this point on.
437void ContextSwitcher::StopPreemption() {
Steve Block44f0eee2011-05-26 01:26:41 +0100438 Isolate* isolate = Isolate::Current();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000439 ASSERT(Locker::IsLocked(reinterpret_cast<v8::Isolate*>(isolate)));
Steve Block44f0eee2011-05-26 01:26:41 +0100440 if (isolate->context_switcher() != NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000441 // The ContextSwitcher thread is running. We need to stop it and release
442 // its resources.
Steve Block44f0eee2011-05-26 01:26:41 +0100443 isolate->context_switcher()->keep_going_ = false;
444 // Wait for the ContextSwitcher thread to exit.
445 isolate->context_switcher()->Join();
Steve Blocka7e24c12009-10-30 11:49:00 +0000446 // Thread has exited, now we can delete it.
Steve Block44f0eee2011-05-26 01:26:41 +0100447 delete(isolate->context_switcher());
448 isolate->set_context_switcher(NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000449 }
450}
451
452
453// Main loop of the ContextSwitcher thread: Preempt the currently running V8
454// thread at regular intervals.
455void ContextSwitcher::Run() {
456 while (keep_going_) {
457 OS::Sleep(sleep_ms_);
Steve Block44f0eee2011-05-26 01:26:41 +0100458 isolate()->stack_guard()->Preempt();
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 }
460}
461
462
463// Acknowledge the preemption by the receiving thread.
464void ContextSwitcher::PreemptionReceived() {
465 ASSERT(Locker::IsLocked());
466 // There is currently no accounting being done for this. But could be in the
467 // future, which is why we leave this in.
468}
469
470
471} // namespace internal
472} // namespace v8