OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/api.h" | 7 #include "src/api.h" |
8 #include "src/bootstrapper.h" | 8 #include "src/bootstrapper.h" |
9 #include "src/debug.h" | 9 #include "src/debug.h" |
10 #include "src/execution.h" | 10 #include "src/execution.h" |
11 #include "src/regexp-stack.h" | 11 #include "src/regexp-stack.h" |
12 #include "src/v8threads.h" | 12 #include "src/v8threads.h" |
13 | 13 |
14 namespace v8 { | 14 namespace v8 { |
15 | 15 |
16 | 16 |
17 // Track whether this V8 instance has ever called v8::Locker. This allows the | 17 // Track whether this V8 instance has ever called v8::Locker. This allows the |
18 // API code to verify that the lock is always held when V8 is being entered. | 18 // API code to verify that the lock is always held when V8 is being entered. |
19 bool Locker::active_ = false; | 19 bool Locker::active_ = false; |
20 | 20 |
21 | 21 |
22 // Once the Locker is initialized, the current thread will be guaranteed to have | 22 // Once the Locker is initialized, the current thread will be guaranteed to have |
23 // the lock for a given isolate. | 23 // the lock for a given isolate. |
24 void Locker::Initialize(v8::Isolate* isolate) { | 24 void Locker::Initialize(v8::Isolate* isolate) { |
25 ASSERT(isolate != NULL); | 25 DCHECK(isolate != NULL); |
26 has_lock_= false; | 26 has_lock_= false; |
27 top_level_ = true; | 27 top_level_ = true; |
28 isolate_ = reinterpret_cast<i::Isolate*>(isolate); | 28 isolate_ = reinterpret_cast<i::Isolate*>(isolate); |
29 // Record that the Locker has been used at least once. | 29 // Record that the Locker has been used at least once. |
30 active_ = true; | 30 active_ = true; |
31 // Get the big lock if necessary. | 31 // Get the big lock if necessary. |
32 if (!isolate_->thread_manager()->IsLockedByCurrentThread()) { | 32 if (!isolate_->thread_manager()->IsLockedByCurrentThread()) { |
33 isolate_->thread_manager()->Lock(); | 33 isolate_->thread_manager()->Lock(); |
34 has_lock_ = true; | 34 has_lock_ = true; |
35 | 35 |
36 // Make sure that V8 is initialized. Archiving of threads interferes | 36 // Make sure that V8 is initialized. Archiving of threads interferes |
37 // with deserialization by adding additional root pointers, so we must | 37 // with deserialization by adding additional root pointers, so we must |
38 // initialize here, before anyone can call ~Locker() or Unlocker(). | 38 // initialize here, before anyone can call ~Locker() or Unlocker(). |
39 if (!isolate_->IsInitialized()) { | 39 if (!isolate_->IsInitialized()) { |
40 isolate_->Enter(); | 40 isolate_->Enter(); |
41 V8::Initialize(); | 41 V8::Initialize(); |
42 isolate_->Exit(); | 42 isolate_->Exit(); |
43 } | 43 } |
44 | 44 |
45 // This may be a locker within an unlocker in which case we have to | 45 // This may be a locker within an unlocker in which case we have to |
46 // get the saved state for this thread and restore it. | 46 // get the saved state for this thread and restore it. |
47 if (isolate_->thread_manager()->RestoreThread()) { | 47 if (isolate_->thread_manager()->RestoreThread()) { |
48 top_level_ = false; | 48 top_level_ = false; |
49 } else { | 49 } else { |
50 internal::ExecutionAccess access(isolate_); | 50 internal::ExecutionAccess access(isolate_); |
51 isolate_->stack_guard()->ClearThread(access); | 51 isolate_->stack_guard()->ClearThread(access); |
52 isolate_->stack_guard()->InitThread(access); | 52 isolate_->stack_guard()->InitThread(access); |
53 } | 53 } |
54 } | 54 } |
55 ASSERT(isolate_->thread_manager()->IsLockedByCurrentThread()); | 55 DCHECK(isolate_->thread_manager()->IsLockedByCurrentThread()); |
56 } | 56 } |
57 | 57 |
58 | 58 |
59 bool Locker::IsLocked(v8::Isolate* isolate) { | 59 bool Locker::IsLocked(v8::Isolate* isolate) { |
60 ASSERT(isolate != NULL); | 60 DCHECK(isolate != NULL); |
61 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); | 61 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
62 return internal_isolate->thread_manager()->IsLockedByCurrentThread(); | 62 return internal_isolate->thread_manager()->IsLockedByCurrentThread(); |
63 } | 63 } |
64 | 64 |
65 | 65 |
66 bool Locker::IsActive() { | 66 bool Locker::IsActive() { |
67 return active_; | 67 return active_; |
68 } | 68 } |
69 | 69 |
70 | 70 |
71 Locker::~Locker() { | 71 Locker::~Locker() { |
72 ASSERT(isolate_->thread_manager()->IsLockedByCurrentThread()); | 72 DCHECK(isolate_->thread_manager()->IsLockedByCurrentThread()); |
73 if (has_lock_) { | 73 if (has_lock_) { |
74 if (top_level_) { | 74 if (top_level_) { |
75 isolate_->thread_manager()->FreeThreadResources(); | 75 isolate_->thread_manager()->FreeThreadResources(); |
76 } else { | 76 } else { |
77 isolate_->thread_manager()->ArchiveThread(); | 77 isolate_->thread_manager()->ArchiveThread(); |
78 } | 78 } |
79 isolate_->thread_manager()->Unlock(); | 79 isolate_->thread_manager()->Unlock(); |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 | 83 |
84 void Unlocker::Initialize(v8::Isolate* isolate) { | 84 void Unlocker::Initialize(v8::Isolate* isolate) { |
85 ASSERT(isolate != NULL); | 85 DCHECK(isolate != NULL); |
86 isolate_ = reinterpret_cast<i::Isolate*>(isolate); | 86 isolate_ = reinterpret_cast<i::Isolate*>(isolate); |
87 ASSERT(isolate_->thread_manager()->IsLockedByCurrentThread()); | 87 DCHECK(isolate_->thread_manager()->IsLockedByCurrentThread()); |
88 isolate_->thread_manager()->ArchiveThread(); | 88 isolate_->thread_manager()->ArchiveThread(); |
89 isolate_->thread_manager()->Unlock(); | 89 isolate_->thread_manager()->Unlock(); |
90 } | 90 } |
91 | 91 |
92 | 92 |
93 Unlocker::~Unlocker() { | 93 Unlocker::~Unlocker() { |
94 ASSERT(!isolate_->thread_manager()->IsLockedByCurrentThread()); | 94 DCHECK(!isolate_->thread_manager()->IsLockedByCurrentThread()); |
95 isolate_->thread_manager()->Lock(); | 95 isolate_->thread_manager()->Lock(); |
96 isolate_->thread_manager()->RestoreThread(); | 96 isolate_->thread_manager()->RestoreThread(); |
97 } | 97 } |
98 | 98 |
99 | 99 |
100 namespace internal { | 100 namespace internal { |
101 | 101 |
102 | 102 |
103 bool ThreadManager::RestoreThread() { | 103 bool ThreadManager::RestoreThread() { |
104 ASSERT(IsLockedByCurrentThread()); | 104 DCHECK(IsLockedByCurrentThread()); |
105 // First check whether the current thread has been 'lazily archived', i.e. | 105 // First check whether the current thread has been 'lazily archived', i.e. |
106 // not archived at all. If that is the case we put the state storage we | 106 // not archived at all. If that is the case we put the state storage we |
107 // had prepared back in the free list, since we didn't need it after all. | 107 // had prepared back in the free list, since we didn't need it after all. |
108 if (lazily_archived_thread_.Equals(ThreadId::Current())) { | 108 if (lazily_archived_thread_.Equals(ThreadId::Current())) { |
109 lazily_archived_thread_ = ThreadId::Invalid(); | 109 lazily_archived_thread_ = ThreadId::Invalid(); |
110 Isolate::PerIsolateThreadData* per_thread = | 110 Isolate::PerIsolateThreadData* per_thread = |
111 isolate_->FindPerThreadDataForThisThread(); | 111 isolate_->FindPerThreadDataForThisThread(); |
112 ASSERT(per_thread != NULL); | 112 DCHECK(per_thread != NULL); |
113 ASSERT(per_thread->thread_state() == lazily_archived_thread_state_); | 113 DCHECK(per_thread->thread_state() == lazily_archived_thread_state_); |
114 lazily_archived_thread_state_->set_id(ThreadId::Invalid()); | 114 lazily_archived_thread_state_->set_id(ThreadId::Invalid()); |
115 lazily_archived_thread_state_->LinkInto(ThreadState::FREE_LIST); | 115 lazily_archived_thread_state_->LinkInto(ThreadState::FREE_LIST); |
116 lazily_archived_thread_state_ = NULL; | 116 lazily_archived_thread_state_ = NULL; |
117 per_thread->set_thread_state(NULL); | 117 per_thread->set_thread_state(NULL); |
118 return true; | 118 return true; |
119 } | 119 } |
120 | 120 |
121 // Make sure that the preemption thread cannot modify the thread state while | 121 // Make sure that the preemption thread cannot modify the thread state while |
122 // it is being archived or restored. | 122 // it is being archived or restored. |
123 ExecutionAccess access(isolate_); | 123 ExecutionAccess access(isolate_); |
(...skipping 27 matching lines...) Expand all Loading... |
151 state->set_id(ThreadId::Invalid()); | 151 state->set_id(ThreadId::Invalid()); |
152 state->Unlink(); | 152 state->Unlink(); |
153 state->LinkInto(ThreadState::FREE_LIST); | 153 state->LinkInto(ThreadState::FREE_LIST); |
154 return true; | 154 return true; |
155 } | 155 } |
156 | 156 |
157 | 157 |
158 void ThreadManager::Lock() { | 158 void ThreadManager::Lock() { |
159 mutex_.Lock(); | 159 mutex_.Lock(); |
160 mutex_owner_ = ThreadId::Current(); | 160 mutex_owner_ = ThreadId::Current(); |
161 ASSERT(IsLockedByCurrentThread()); | 161 DCHECK(IsLockedByCurrentThread()); |
162 } | 162 } |
163 | 163 |
164 | 164 |
165 void ThreadManager::Unlock() { | 165 void ThreadManager::Unlock() { |
166 mutex_owner_ = ThreadId::Invalid(); | 166 mutex_owner_ = ThreadId::Invalid(); |
167 mutex_.Unlock(); | 167 mutex_.Unlock(); |
168 } | 168 } |
169 | 169 |
170 | 170 |
171 static int ArchiveSpacePerThread() { | 171 static int ArchiveSpacePerThread() { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 for (ThreadState* current = anchor->next_; current != anchor;) { | 264 for (ThreadState* current = anchor->next_; current != anchor;) { |
265 ThreadState* next = current->next_; | 265 ThreadState* next = current->next_; |
266 delete current; | 266 delete current; |
267 current = next; | 267 current = next; |
268 } | 268 } |
269 delete anchor; | 269 delete anchor; |
270 } | 270 } |
271 | 271 |
272 | 272 |
273 void ThreadManager::ArchiveThread() { | 273 void ThreadManager::ArchiveThread() { |
274 ASSERT(lazily_archived_thread_.Equals(ThreadId::Invalid())); | 274 DCHECK(lazily_archived_thread_.Equals(ThreadId::Invalid())); |
275 ASSERT(!IsArchived()); | 275 DCHECK(!IsArchived()); |
276 ASSERT(IsLockedByCurrentThread()); | 276 DCHECK(IsLockedByCurrentThread()); |
277 ThreadState* state = GetFreeThreadState(); | 277 ThreadState* state = GetFreeThreadState(); |
278 state->Unlink(); | 278 state->Unlink(); |
279 Isolate::PerIsolateThreadData* per_thread = | 279 Isolate::PerIsolateThreadData* per_thread = |
280 isolate_->FindOrAllocatePerThreadDataForThisThread(); | 280 isolate_->FindOrAllocatePerThreadDataForThisThread(); |
281 per_thread->set_thread_state(state); | 281 per_thread->set_thread_state(state); |
282 lazily_archived_thread_ = ThreadId::Current(); | 282 lazily_archived_thread_ = ThreadId::Current(); |
283 lazily_archived_thread_state_ = state; | 283 lazily_archived_thread_state_ = state; |
284 ASSERT(state->id().Equals(ThreadId::Invalid())); | 284 DCHECK(state->id().Equals(ThreadId::Invalid())); |
285 state->set_id(CurrentId()); | 285 state->set_id(CurrentId()); |
286 ASSERT(!state->id().Equals(ThreadId::Invalid())); | 286 DCHECK(!state->id().Equals(ThreadId::Invalid())); |
287 } | 287 } |
288 | 288 |
289 | 289 |
290 void ThreadManager::EagerlyArchiveThread() { | 290 void ThreadManager::EagerlyArchiveThread() { |
291 ASSERT(IsLockedByCurrentThread()); | 291 DCHECK(IsLockedByCurrentThread()); |
292 ThreadState* state = lazily_archived_thread_state_; | 292 ThreadState* state = lazily_archived_thread_state_; |
293 state->LinkInto(ThreadState::IN_USE_LIST); | 293 state->LinkInto(ThreadState::IN_USE_LIST); |
294 char* to = state->data(); | 294 char* to = state->data(); |
295 // Ensure that data containing GC roots are archived first, and handle them | 295 // Ensure that data containing GC roots are archived first, and handle them |
296 // in ThreadManager::Iterate(ObjectVisitor*). | 296 // in ThreadManager::Iterate(ObjectVisitor*). |
297 to = isolate_->handle_scope_implementer()->ArchiveThread(to); | 297 to = isolate_->handle_scope_implementer()->ArchiveThread(to); |
298 to = isolate_->ArchiveThread(to); | 298 to = isolate_->ArchiveThread(to); |
299 to = Relocatable::ArchiveState(isolate_, to); | 299 to = Relocatable::ArchiveState(isolate_, to); |
300 to = isolate_->debug()->ArchiveDebug(to); | 300 to = isolate_->debug()->ArchiveDebug(to); |
301 to = isolate_->stack_guard()->ArchiveStackGuard(to); | 301 to = isolate_->stack_guard()->ArchiveStackGuard(to); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 state = state->Next()) { | 358 state = state->Next()) { |
359 if (thread_id.Equals(state->id())) { | 359 if (thread_id.Equals(state->id())) { |
360 state->set_terminate_on_restore(true); | 360 state->set_terminate_on_restore(true); |
361 } | 361 } |
362 } | 362 } |
363 } | 363 } |
364 | 364 |
365 | 365 |
366 } // namespace internal | 366 } // namespace internal |
367 } // namespace v8 | 367 } // namespace v8 |
OLD | NEW |