mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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" |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 31 | #include "bootstrapper.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "debug.h" |
| 33 | #include "execution.h" |
| 34 | #include "v8threads.h" |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 35 | #include "regexp-stack.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
| 37 | namespace v8 { |
| 38 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 39 | |
| 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. |
| 42 | bool Locker::active_ = false; |
| 43 | |
| 44 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | // Constructor for the Locker object. Once the Locker is constructed the |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 46 | // current thread will be guaranteed to have the lock for a given isolate. |
| 47 | Locker::Locker(v8::Isolate* isolate) |
| 48 | : has_lock_(false), |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 49 | top_level_(true), |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 50 | isolate_(reinterpret_cast<i::Isolate*>(isolate)) { |
| 51 | if (isolate_ == NULL) { |
| 52 | isolate_ = i::Isolate::GetDefaultIsolateForLocking(); |
| 53 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 54 | // Record that the Locker has been used at least once. |
| 55 | active_ = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | // Get the big lock if necessary. |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 57 | if (!isolate_->thread_manager()->IsLockedByCurrentThread()) { |
| 58 | isolate_->thread_manager()->Lock(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | has_lock_ = true; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 60 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 61 | // 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.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 64 | if (!isolate_->IsInitialized()) { |
| 65 | isolate_->Enter(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 66 | V8::Initialize(); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 67 | isolate_->Exit(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 68 | } |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 69 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 70 | // 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.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 72 | if (isolate_->thread_manager()->RestoreThread()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 73 | top_level_ = false; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 74 | } else { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 75 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 84 | ASSERT(isolate_->thread_manager()->IsLockedByCurrentThread()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 88 | bool 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 94 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | |
| 96 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 97 | bool Locker::IsActive() { |
| 98 | return active_; |
| 99 | } |
| 100 | |
| 101 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 102 | Locker::~Locker() { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 103 | ASSERT(isolate_->thread_manager()->IsLockedByCurrentThread()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | if (has_lock_) { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 105 | if (isolate_->IsDefaultIsolate()) { |
| 106 | isolate_->Exit(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | } |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 108 | if (top_level_) { |
| 109 | isolate_->thread_manager()->FreeThreadResources(); |
| 110 | } else { |
| 111 | isolate_->thread_manager()->ArchiveThread(); |
| 112 | } |
| 113 | isolate_->thread_manager()->Unlock(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 118 | Unlocker::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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| 132 | Unlocker::~Unlocker() { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 133 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
| 142 | void Locker::StartPreemption(int every_n_ms) { |
| 143 | v8::internal::ContextSwitcher::StartPreemption(every_n_ms); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void Locker::StopPreemption() { |
| 148 | v8::internal::ContextSwitcher::StopPreemption(); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | namespace internal { |
| 153 | |
| 154 | |
| 155 | bool ThreadManager::RestoreThread() { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 156 | ASSERT(IsLockedByCurrentThread()); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 157 | // First check whether the current thread has been 'lazily archived', i.e. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 160 | if (lazily_archived_thread_.Equals(ThreadId::Current())) { |
| 161 | lazily_archived_thread_ = ThreadId::Invalid(); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 162 | Isolate::PerIsolateThreadData* per_thread = |
| 163 | isolate_->FindPerThreadDataForThisThread(); |
| 164 | ASSERT(per_thread != NULL); |
| 165 | ASSERT(per_thread->thread_state() == lazily_archived_thread_state_); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 166 | lazily_archived_thread_state_->set_id(ThreadId::Invalid()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | lazily_archived_thread_state_->LinkInto(ThreadState::FREE_LIST); |
| 168 | lazily_archived_thread_state_ = NULL; |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 169 | per_thread->set_thread_state(NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 170 | return true; |
| 171 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 172 | |
| 173 | // Make sure that the preemption thread cannot modify the thread state while |
| 174 | // it is being archived or restored. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 175 | ExecutionAccess access(isolate_); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 176 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 182 | Isolate::PerIsolateThreadData* per_thread = |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 183 | isolate_->FindPerThreadDataForThisThread(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 184 | if (per_thread == NULL || per_thread->thread_state() == NULL) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 185 | // This is a new thread. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 186 | isolate_->stack_guard()->InitThread(access); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 187 | return false; |
| 188 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 189 | ThreadState* state = per_thread->thread_state(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 190 | char* from = state->data(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 191 | from = isolate_->handle_scope_implementer()->RestoreThread(from); |
| 192 | from = isolate_->RestoreThread(from); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 193 | from = Relocatable::RestoreState(isolate_, from); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 194 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 195 | from = isolate_->debug()->RestoreDebug(from); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 196 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 197 | 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.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 201 | if (state->terminate_on_restore()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 202 | isolate_->stack_guard()->TerminateExecution(); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 203 | state->set_terminate_on_restore(false); |
| 204 | } |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 205 | state->set_id(ThreadId::Invalid()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 206 | state->Unlink(); |
| 207 | state->LinkInto(ThreadState::FREE_LIST); |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | void ThreadManager::Lock() { |
| 213 | mutex_->Lock(); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 214 | mutex_owner_ = ThreadId::Current(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 215 | ASSERT(IsLockedByCurrentThread()); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | void ThreadManager::Unlock() { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 220 | mutex_owner_ = ThreadId::Invalid(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 221 | mutex_->Unlock(); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | static int ArchiveSpacePerThread() { |
| 226 | return HandleScopeImplementer::ArchiveSpacePerThread() + |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 227 | Isolate::ArchiveSpacePerThread() + |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 228 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 229 | Debug::ArchiveSpacePerThread() + |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 230 | #endif |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 231 | StackGuard::ArchiveSpacePerThread() + |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 232 | RegExpStack::ArchiveSpacePerThread() + |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 233 | Bootstrapper::ArchiveSpacePerThread() + |
| 234 | Relocatable::ArchiveSpacePerThread(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 238 | ThreadState::ThreadState(ThreadManager* thread_manager) |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 239 | : id_(ThreadId::Invalid()), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 240 | terminate_on_restore_(false), |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 241 | data_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 242 | next_(this), |
| 243 | previous_(this), |
| 244 | thread_manager_(thread_manager) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 248 | ThreadState::~ThreadState() { |
| 249 | DeleteArray<char>(data_); |
| 250 | } |
| 251 | |
| 252 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | void ThreadState::AllocateSpace() { |
| 254 | data_ = NewArray<char>(ArchiveSpacePerThread()); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | void ThreadState::Unlink() { |
| 259 | next_->previous_ = previous_; |
| 260 | previous_->next_ = next_; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | void ThreadState::LinkInto(List list) { |
| 265 | ThreadState* flying_anchor = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 266 | list == FREE_LIST ? thread_manager_->free_anchor_ |
| 267 | : thread_manager_->in_use_anchor_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 268 | next_ = flying_anchor->next_; |
| 269 | previous_ = flying_anchor; |
| 270 | flying_anchor->next_ = this; |
| 271 | next_->previous_ = this; |
| 272 | } |
| 273 | |
| 274 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 275 | ThreadState* ThreadManager::GetFreeThreadState() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 276 | ThreadState* gotten = free_anchor_->next_; |
| 277 | if (gotten == free_anchor_) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 278 | ThreadState* new_thread_state = new ThreadState(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | 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.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 287 | ThreadState* ThreadManager::FirstThreadStateInUse() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 288 | return in_use_anchor_->Next(); |
| 289 | } |
| 290 | |
| 291 | |
| 292 | ThreadState* ThreadState::Next() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 293 | if (next_ == thread_manager_->in_use_anchor_) return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 294 | return next_; |
| 295 | } |
| 296 | |
| 297 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 298 | // 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.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 301 | ThreadManager::ThreadManager() |
| 302 | : mutex_(OS::CreateMutex()), |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 303 | mutex_owner_(ThreadId::Invalid()), |
| 304 | lazily_archived_thread_(ThreadId::Invalid()), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 305 | 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 | |
| 313 | ThreadManager::~ThreadManager() { |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 314 | delete mutex_; |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 315 | DeleteThreadStateList(free_anchor_); |
| 316 | DeleteThreadStateList(in_use_anchor_); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | void 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.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 328 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 329 | |
| 330 | |
| 331 | void ThreadManager::ArchiveThread() { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 332 | ASSERT(lazily_archived_thread_.Equals(ThreadId::Invalid())); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 333 | ASSERT(!IsArchived()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 334 | ASSERT(IsLockedByCurrentThread()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 335 | ThreadState* state = GetFreeThreadState(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 336 | state->Unlink(); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 337 | Isolate::PerIsolateThreadData* per_thread = |
| 338 | isolate_->FindOrAllocatePerThreadDataForThisThread(); |
| 339 | per_thread->set_thread_state(state); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 340 | lazily_archived_thread_ = ThreadId::Current(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 341 | lazily_archived_thread_state_ = state; |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 342 | ASSERT(state->id().Equals(ThreadId::Invalid())); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 343 | state->set_id(CurrentId()); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 344 | ASSERT(!state->id().Equals(ThreadId::Invalid())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | |
| 348 | void ThreadManager::EagerlyArchiveThread() { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 349 | ASSERT(IsLockedByCurrentThread()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 350 | ThreadState* state = lazily_archived_thread_state_; |
| 351 | state->LinkInto(ThreadState::IN_USE_LIST); |
| 352 | char* to = state->data(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 353 | // Ensure that data containing GC roots are archived first, and handle them |
| 354 | // in ThreadManager::Iterate(ObjectVisitor*). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 355 | to = isolate_->handle_scope_implementer()->ArchiveThread(to); |
| 356 | to = isolate_->ArchiveThread(to); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 357 | to = Relocatable::ArchiveState(isolate_, to); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 358 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 359 | to = isolate_->debug()->ArchiveDebug(to); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 360 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 361 | to = isolate_->stack_guard()->ArchiveStackGuard(to); |
| 362 | to = isolate_->regexp_stack()->ArchiveStack(to); |
| 363 | to = isolate_->bootstrapper()->ArchiveState(to); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 364 | lazily_archived_thread_ = ThreadId::Invalid(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 365 | lazily_archived_thread_state_ = NULL; |
| 366 | } |
| 367 | |
| 368 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 369 | void ThreadManager::FreeThreadResources() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 370 | isolate_->handle_scope_implementer()->FreeThreadResources(); |
| 371 | isolate_->FreeThreadResources(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 372 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 373 | isolate_->debug()->FreeThreadResources(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 374 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 375 | isolate_->stack_guard()->FreeThreadResources(); |
| 376 | isolate_->regexp_stack()->FreeThreadResources(); |
| 377 | isolate_->bootstrapper()->FreeThreadResources(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 381 | bool ThreadManager::IsArchived() { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 382 | Isolate::PerIsolateThreadData* data = |
| 383 | isolate_->FindPerThreadDataForThisThread(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 384 | return data != NULL && data->thread_state() != NULL; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 385 | } |
| 386 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 387 | void ThreadManager::Iterate(ObjectVisitor* v) { |
| 388 | // Expecting no threads during serialization/deserialization |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 389 | for (ThreadState* state = FirstThreadStateInUse(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 390 | state != NULL; |
| 391 | state = state->Next()) { |
| 392 | char* data = state->data(); |
| 393 | data = HandleScopeImplementer::Iterate(v, data); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 394 | data = isolate_->Iterate(v, data); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 395 | data = Relocatable::Iterate(v, data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
| 399 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 400 | void ThreadManager::IterateArchivedThreads(ThreadVisitor* v) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 401 | for (ThreadState* state = FirstThreadStateInUse(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 402 | state != NULL; |
| 403 | state = state->Next()) { |
| 404 | char* data = state->data(); |
| 405 | data += HandleScopeImplementer::ArchiveSpacePerThread(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 406 | isolate_->IterateThread(v, data); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
| 410 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 411 | ThreadId ThreadManager::CurrentId() { |
| 412 | return ThreadId::Current(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 416 | void ThreadManager::TerminateExecution(ThreadId thread_id) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 417 | for (ThreadState* state = FirstThreadStateInUse(); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 418 | state != NULL; |
| 419 | state = state->Next()) { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 420 | if (thread_id.Equals(state->id())) { |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 421 | state->set_terminate_on_restore(true); |
| 422 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 427 | ContextSwitcher::ContextSwitcher(Isolate* isolate, int every_n_ms) |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 428 | : Thread("v8:CtxtSwitcher"), |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 429 | keep_going_(true), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 430 | sleep_ms_(every_n_ms), |
| 431 | isolate_(isolate) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 435 | // Set the scheduling interval of V8 threads. This function starts the |
| 436 | // ContextSwitcher thread if needed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 437 | void ContextSwitcher::StartPreemption(int every_n_ms) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 438 | Isolate* isolate = Isolate::Current(); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 439 | ASSERT(Locker::IsLocked(reinterpret_cast<v8::Isolate*>(isolate))); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 440 | if (isolate->context_switcher() == NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 441 | // If the ContextSwitcher thread is not running at the moment start it now. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 442 | isolate->set_context_switcher(new ContextSwitcher(isolate, every_n_ms)); |
| 443 | isolate->context_switcher()->Start(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 444 | } else { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 445 | // ContextSwitcher thread is already running, so we just change the |
| 446 | // scheduling interval. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 447 | isolate->context_switcher()->sleep_ms_ = every_n_ms; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 452 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 454 | void ContextSwitcher::StopPreemption() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 455 | Isolate* isolate = Isolate::Current(); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 456 | ASSERT(Locker::IsLocked(reinterpret_cast<v8::Isolate*>(isolate))); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 457 | if (isolate->context_switcher() != NULL) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 458 | // The ContextSwitcher thread is running. We need to stop it and release |
| 459 | // its resources. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 460 | isolate->context_switcher()->keep_going_ = false; |
| 461 | // Wait for the ContextSwitcher thread to exit. |
| 462 | isolate->context_switcher()->Join(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 463 | // Thread has exited, now we can delete it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 464 | delete(isolate->context_switcher()); |
| 465 | isolate->set_context_switcher(NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 470 | // Main loop of the ContextSwitcher thread: Preempt the currently running V8 |
| 471 | // thread at regular intervals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 472 | void ContextSwitcher::Run() { |
| 473 | while (keep_going_) { |
| 474 | OS::Sleep(sleep_ms_); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 475 | isolate()->stack_guard()->Preempt(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
| 479 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 480 | // Acknowledge the preemption by the receiving thread. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 481 | void ContextSwitcher::PreemptionReceived() { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 482 | ASSERT(Locker::IsLocked()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 483 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | |
| 488 | } // namespace internal |
| 489 | } // namespace v8 |