v8threads.cc 10.5 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/execution/v8threads.h"
6

7
#include "src/api/api.h"
8
#include "src/debug/debug.h"
9 10
#include "src/execution/execution.h"
#include "src/execution/isolate-inl.h"
11
#include "src/init/bootstrapper.h"
12
#include "src/objects/visitors.h"
13
#include "src/regexp/regexp-stack.h"
14 15 16

namespace v8 {

17 18
namespace {

19 20
// Track whether this V8 instance has ever called v8::Locker. This allows the
// API code to verify that the lock is always held when V8 is being entered.
21 22 23
base::Atomic32 g_locker_was_ever_used_ = 0;

}  // namespace
24

25 26 27
// Once the Locker is initialized, the current thread will be guaranteed to have
// the lock for a given isolate.
void Locker::Initialize(v8::Isolate* isolate) {
28
  DCHECK_NOT_NULL(isolate);
Benedikt Meurer's avatar
Benedikt Meurer committed
29
  has_lock_ = false;
30 31
  top_level_ = true;
  isolate_ = reinterpret_cast<i::Isolate*>(isolate);
32
  // Record that the Locker has been used at least once.
33
  base::Relaxed_Store(&g_locker_was_ever_used_, 1);
34
  // Get the big lock if necessary.
35 36
  if (!isolate_->thread_manager()->IsLockedByCurrentThread()) {
    isolate_->thread_manager()->Lock();
37
    has_lock_ = true;
38

39 40
    // This may be a locker within an unlocker in which case we have to
    // get the saved state for this thread and restore it.
41
    if (isolate_->thread_manager()->RestoreThread()) {
42
      top_level_ = false;
43
    } else {
44 45
      internal::ExecutionAccess access(isolate_);
      isolate_->stack_guard()->ClearThread(access);
46
      isolate_->thread_manager()->InitThread(access);
47
    }
48
  }
49
  DCHECK(isolate_->thread_manager()->IsLockedByCurrentThread());
50 51
}

52
bool Locker::IsLocked(v8::Isolate* isolate) {
53
  DCHECK_NOT_NULL(isolate);
54 55
  i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
  return internal_isolate->thread_manager()->IsLockedByCurrentThread();
56 57
}

58
bool Locker::IsActive() {
59
  return !!base::Relaxed_Load(&g_locker_was_ever_used_);
60 61
}

62
Locker::~Locker() {
63
  DCHECK(isolate_->thread_manager()->IsLockedByCurrentThread());
64
  if (has_lock_) {
65
    if (top_level_) {
66
      isolate_->thread_manager()->FreeThreadResources();
67
    } else {
68
      isolate_->thread_manager()->ArchiveThread();
69
    }
70
    isolate_->thread_manager()->Unlock();
71 72 73
  }
}

74
void Unlocker::Initialize(v8::Isolate* isolate) {
75
  DCHECK_NOT_NULL(isolate);
76
  isolate_ = reinterpret_cast<i::Isolate*>(isolate);
77
  DCHECK(isolate_->thread_manager()->IsLockedByCurrentThread());
78 79
  isolate_->thread_manager()->ArchiveThread();
  isolate_->thread_manager()->Unlock();
80 81 82
}

Unlocker::~Unlocker() {
83
  DCHECK(!isolate_->thread_manager()->IsLockedByCurrentThread());
84 85
  isolate_->thread_manager()->Lock();
  isolate_->thread_manager()->RestoreThread();
86 87 88 89
}

namespace internal {

90 91 92 93
void ThreadManager::InitThread(const ExecutionAccess& lock) {
  isolate_->stack_guard()->InitThread(lock);
  isolate_->debug()->InitThread(lock);
}
94 95

bool ThreadManager::RestoreThread() {
96
  DCHECK(IsLockedByCurrentThread());
97
  // First check whether the current thread has been 'lazily archived', i.e.
98 99
  // not archived at all.  If that is the case we put the state storage we
  // had prepared back in the free list, since we didn't need it after all.
Clemens Hammacher's avatar
Clemens Hammacher committed
100
  if (lazily_archived_thread_ == ThreadId::Current()) {
101
    lazily_archived_thread_ = ThreadId::Invalid();
102 103
    Isolate::PerIsolateThreadData* per_thread =
        isolate_->FindPerThreadDataForThisThread();
104
    DCHECK_NOT_NULL(per_thread);
105
    DCHECK(per_thread->thread_state() == lazily_archived_thread_state_);
106
    lazily_archived_thread_state_->set_id(ThreadId::Invalid());
107
    lazily_archived_thread_state_->LinkInto(ThreadState::FREE_LIST);
108 109
    lazily_archived_thread_state_ = nullptr;
    per_thread->set_thread_state(nullptr);
110 111
    return true;
  }
112

113 114
  // Make sure that the preemption thread cannot modify the thread state while
  // it is being archived or restored.
115
  ExecutionAccess access(isolate_);
116

117 118 119 120 121
  // If there is another thread that was lazily archived then we have to really
  // archive it now.
  if (lazily_archived_thread_.IsValid()) {
    EagerlyArchiveThread();
  }
122
  Isolate::PerIsolateThreadData* per_thread =
123
      isolate_->FindPerThreadDataForThisThread();
124
  if (per_thread == nullptr || per_thread->thread_state() == nullptr) {
125
    // This is a new thread.
126
    InitThread(access);
127 128
    return false;
  }
129
  ThreadState* state = per_thread->thread_state();
130
  char* from = state->data();
131 132
  from = isolate_->handle_scope_implementer()->RestoreThread(from);
  from = isolate_->RestoreThread(from);
133
  from = Relocatable::RestoreState(isolate_, from);
134 135 136 137
  from = isolate_->debug()->RestoreDebug(from);
  from = isolate_->stack_guard()->RestoreStackGuard(from);
  from = isolate_->regexp_stack()->RestoreStack(from);
  from = isolate_->bootstrapper()->RestoreState(from);
138
  per_thread->set_thread_state(nullptr);
139
  state->set_id(ThreadId::Invalid());
140 141 142 143 144 145
  state->Unlink();
  state->LinkInto(ThreadState::FREE_LIST);
  return true;
}

void ThreadManager::Lock() {
146
  mutex_.Lock();
Clemens Hammacher's avatar
Clemens Hammacher committed
147
  mutex_owner_.store(ThreadId::Current(), std::memory_order_relaxed);
148
  DCHECK(IsLockedByCurrentThread());
149 150 151
}

void ThreadManager::Unlock() {
Clemens Hammacher's avatar
Clemens Hammacher committed
152
  mutex_owner_.store(ThreadId::Invalid(), std::memory_order_relaxed);
153
  mutex_.Unlock();
154 155
}

156
static int ArchiveSpacePerThread() {
157
  return HandleScopeImplementer::ArchiveSpacePerThread() +
158 159 160 161 162
         Isolate::ArchiveSpacePerThread() + Debug::ArchiveSpacePerThread() +
         StackGuard::ArchiveSpacePerThread() +
         RegExpStack::ArchiveSpacePerThread() +
         Bootstrapper::ArchiveSpacePerThread() +
         Relocatable::ArchiveSpacePerThread();
163 164
}

165
ThreadState::ThreadState(ThreadManager* thread_manager)
166
    : id_(ThreadId::Invalid()),
167
      data_(nullptr),
168 169
      next_(this),
      previous_(this),
170
      thread_manager_(thread_manager) {}
171

172
ThreadState::~ThreadState() { DeleteArray<char>(data_); }
173

174
void ThreadState::AllocateSpace() {
175
  data_ = NewArray<char>(ArchiveSpacePerThread());
176 177 178 179 180 181 182 183
}

void ThreadState::Unlink() {
  next_->previous_ = previous_;
  previous_->next_ = next_;
}

void ThreadState::LinkInto(List list) {
184 185 186
  ThreadState* flying_anchor = list == FREE_LIST
                                   ? thread_manager_->free_anchor_
                                   : thread_manager_->in_use_anchor_;
187 188 189 190 191 192
  next_ = flying_anchor->next_;
  previous_ = flying_anchor;
  flying_anchor->next_ = this;
  next_->previous_ = this;
}

193
ThreadState* ThreadManager::GetFreeThreadState() {
194 195
  ThreadState* gotten = free_anchor_->next_;
  if (gotten == free_anchor_) {
196
    ThreadState* new_thread_state = new ThreadState(this);
197 198 199 200 201 202 203
    new_thread_state->AllocateSpace();
    return new_thread_state;
  }
  return gotten;
}

// Gets the first in the list of archived threads.
204
ThreadState* ThreadManager::FirstThreadStateInUse() {
205 206 207 208
  return in_use_anchor_->Next();
}

ThreadState* ThreadState::Next() {
209
  if (next_ == thread_manager_->in_use_anchor_) return nullptr;
210 211 212
  return next_;
}

213 214 215
// Thread ids must start with 1, because in TLS having thread id 0 can't
// be distinguished from not having a thread id at all (since NULL is
// defined as 0.)
216
ThreadManager::ThreadManager(Isolate* isolate)
217
    : mutex_owner_(ThreadId::Invalid()),
218
      lazily_archived_thread_(ThreadId::Invalid()),
219 220
      lazily_archived_thread_state_(nullptr),
      free_anchor_(nullptr),
221 222
      in_use_anchor_(nullptr),
      isolate_(isolate) {
223 224 225 226 227
  free_anchor_ = new ThreadState(this);
  in_use_anchor_ = new ThreadState(this);
}

ThreadManager::~ThreadManager() {
228 229 230 231 232 233 234 235 236 237 238 239
  DeleteThreadStateList(free_anchor_);
  DeleteThreadStateList(in_use_anchor_);
}

void ThreadManager::DeleteThreadStateList(ThreadState* anchor) {
  // The list starts and ends with the anchor.
  for (ThreadState* current = anchor->next_; current != anchor;) {
    ThreadState* next = current->next_;
    delete current;
    current = next;
  }
  delete anchor;
240
}
241 242

void ThreadManager::ArchiveThread() {
Clemens Hammacher's avatar
Clemens Hammacher committed
243
  DCHECK_EQ(lazily_archived_thread_, ThreadId::Invalid());
244 245
  DCHECK(!IsArchived());
  DCHECK(IsLockedByCurrentThread());
246
  ThreadState* state = GetFreeThreadState();
247
  state->Unlink();
248 249 250
  Isolate::PerIsolateThreadData* per_thread =
      isolate_->FindOrAllocatePerThreadDataForThisThread();
  per_thread->set_thread_state(state);
251
  lazily_archived_thread_ = ThreadId::Current();
252
  lazily_archived_thread_state_ = state;
Clemens Hammacher's avatar
Clemens Hammacher committed
253
  DCHECK_EQ(state->id(), ThreadId::Invalid());
254
  state->set_id(CurrentId());
Clemens Hammacher's avatar
Clemens Hammacher committed
255
  DCHECK_NE(state->id(), ThreadId::Invalid());
256 257 258
}

void ThreadManager::EagerlyArchiveThread() {
259
  DCHECK(IsLockedByCurrentThread());
260 261 262
  ThreadState* state = lazily_archived_thread_state_;
  state->LinkInto(ThreadState::IN_USE_LIST);
  char* to = state->data();
263
  // Ensure that data containing GC roots are archived first, and handle them
264
  // in ThreadManager::Iterate(RootVisitor*).
265 266
  to = isolate_->handle_scope_implementer()->ArchiveThread(to);
  to = isolate_->ArchiveThread(to);
267
  to = Relocatable::ArchiveState(isolate_, to);
268 269 270 271
  to = isolate_->debug()->ArchiveDebug(to);
  to = isolate_->stack_guard()->ArchiveStackGuard(to);
  to = isolate_->regexp_stack()->ArchiveStack(to);
  to = isolate_->bootstrapper()->ArchiveState(to);
272
  lazily_archived_thread_ = ThreadId::Invalid();
273
  lazily_archived_thread_state_ = nullptr;
274 275
}

276
void ThreadManager::FreeThreadResources() {
277 278
  DCHECK(!isolate_->has_pending_exception());
  DCHECK(!isolate_->external_caught_exception());
279
  DCHECK_NULL(isolate_->try_catch_handler());
280 281 282 283 284 285
  isolate_->handle_scope_implementer()->FreeThreadResources();
  isolate_->FreeThreadResources();
  isolate_->debug()->FreeThreadResources();
  isolate_->stack_guard()->FreeThreadResources();
  isolate_->regexp_stack()->FreeThreadResources();
  isolate_->bootstrapper()->FreeThreadResources();
286 287
}

288
bool ThreadManager::IsArchived() {
289 290
  Isolate::PerIsolateThreadData* data =
      isolate_->FindPerThreadDataForThisThread();
291
  return data != nullptr && data->thread_state() != nullptr;
292 293
}

294
void ThreadManager::Iterate(RootVisitor* v) {
295
  // Expecting no threads during serialization/deserialization
296
  for (ThreadState* state = FirstThreadStateInUse(); state != nullptr;
297 298 299
       state = state->Next()) {
    char* data = state->data();
    data = HandleScopeImplementer::Iterate(v, data);
300
    data = isolate_->Iterate(v, data);
301
    data = Relocatable::Iterate(v, data);
302 303 304
  }
}

305
void ThreadManager::IterateArchivedThreads(ThreadVisitor* v) {
306
  for (ThreadState* state = FirstThreadStateInUse(); state != nullptr;
307 308 309
       state = state->Next()) {
    char* data = state->data();
    data += HandleScopeImplementer::ArchiveSpacePerThread();
310
    isolate_->IterateThread(v, data);
311 312 313
  }
}

314
ThreadId ThreadManager::CurrentId() { return ThreadId::Current(); }
315

316 317
}  // namespace internal
}  // namespace v8