Chromium Code Reviews| Index: third_party/WebKit/Source/platform/heap/ThreadState.cpp |
| diff --git a/third_party/WebKit/Source/platform/heap/ThreadState.cpp b/third_party/WebKit/Source/platform/heap/ThreadState.cpp |
| index 8446223a5880b38e074f72b38ad995b6393ba25a..a472683256f7c8ed07d8d142e935a2fc46084d2e 100644 |
| --- a/third_party/WebKit/Source/platform/heap/ThreadState.cpp |
| +++ b/third_party/WebKit/Source/platform/heap/ThreadState.cpp |
| @@ -209,6 +209,22 @@ void ThreadState::attachMainThread() |
| attachedThreads().add(state); |
| } |
| + |
| +void ThreadState::stopMainThread() |
| +{ |
| + ASSERT(isMainThread()); |
| + |
| + // Finish sweeping before shutting down V8. Otherwise, some destructor |
| + // may access V8 and cause crashes. |
| + completeSweep(); |
| + |
| + // It is unsafe to trigger GCs after this point because some |
| + // destructor may access already-detached V8 and cause crashes. |
| + // Also there is no benefit in triggering a GC on a detaching thread. |
| + // So we forbid GCs. |
| + enterGCForbiddenScope(); |
| +} |
| + |
| void ThreadState::detachMainThread() |
| { |
| // Enter a safe point before trying to acquire threadAttachMutex |
| @@ -218,23 +234,21 @@ void ThreadState::detachMainThread() |
| ThreadState* state = mainThreadState(); |
| ASSERT(state == ThreadState::current()); |
| ASSERT(state->checkThread()); |
| - // You must call unregisterTraceDOMWrappers before detaching |
| - // the main thread. |
| - ASSERT(!state->m_isolate); |
| + ASSERT(!state->isSweepingInProgress()); |
|
sof
2016/03/10 07:52:16
nit: add ASSERT(state->isGCForbidden());
|
| - // 1. Finish sweeping. |
| - state->completeSweep(); |
| - { |
| - SafePointAwareMutexLocker locker(threadAttachMutex(), BlinkGC::NoHeapPointersOnStack); |
| + // The main thread must be the last thread that gets detached. |
| + RELEASE_ASSERT(ThreadState::attachedThreads().size() == 1); |
| - // 2. Add the main thread's heap pages to the orphaned pool. |
| - state->cleanupPages(); |
| + // Add the main thread's heap pages to the orphaned pool. |
| + state->cleanupPages(); |
| - // 3. Detach the main thread. |
| - ASSERT(attachedThreads().contains(state)); |
| - attachedThreads().remove(state); |
| - state->~ThreadState(); |
| - } |
| + state->leaveGCForbiddenScope(); |
| + |
| + // Detach the main thread. We don't need to grab a lock because |
| + // the main thread should be the last thread that gets detached. |
| + ASSERT(attachedThreads().contains(state)); |
| + attachedThreads().remove(state); |
| + state->~ThreadState(); |
| } |
| void ThreadState::attach() |