| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 class BasePage; | 55 class BasePage; |
| 56 class CallbackStack; | 56 class CallbackStack; |
| 57 struct GCInfo; | 57 struct GCInfo; |
| 58 class GarbageCollectedMixinConstructorMarker; | 58 class GarbageCollectedMixinConstructorMarker; |
| 59 class HeapObjectHeader; | 59 class HeapObjectHeader; |
| 60 class PersistentNode; | 60 class PersistentNode; |
| 61 class PersistentRegion; | 61 class PersistentRegion; |
| 62 class BaseArena; | 62 class BaseArena; |
| 63 class SafePointAwareMutexLocker; | 63 class SafePointAwareMutexLocker; |
| 64 class SafePointBarrier; | 64 class SafePointBarrier; |
| 65 class ThreadHeap; | |
| 66 class ThreadState; | 65 class ThreadState; |
| 67 class Visitor; | 66 class Visitor; |
| 68 | 67 |
| 69 // Declare that a class has a pre-finalizer. The pre-finalizer is called | 68 // Declare that a class has a pre-finalizer. The pre-finalizer is called |
| 70 // before any object gets swept, so it is safe to touch on-heap objects | 69 // before any object gets swept, so it is safe to touch on-heap objects |
| 71 // that may be collected in the same GC cycle. If you cannot avoid touching | 70 // that may be collected in the same GC cycle. If you cannot avoid touching |
| 72 // on-heap objects in a destructor (which is not allowed), you can consider | 71 // on-heap objects in a destructor (which is not allowed), you can consider |
| 73 // using the pre-finalizer. The only restriction is that the pre-finalizer | 72 // using the pre-finalizer. The only restriction is that the pre-finalizer |
| 74 // must not resurrect dead objects (e.g., store unmarked objects into | 73 // must not resurrect dead objects (e.g., store unmarked objects into |
| 75 // Members etc). The pre-finalizer is called on the thread that registered | 74 // Members etc). The pre-finalizer is called on the thread that registered |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 } | 162 } |
| 164 ~SweepForbiddenScope() | 163 ~SweepForbiddenScope() |
| 165 { | 164 { |
| 166 ASSERT(m_state->m_sweepForbidden); | 165 ASSERT(m_state->m_sweepForbidden); |
| 167 m_state->m_sweepForbidden = false; | 166 m_state->m_sweepForbidden = false; |
| 168 } | 167 } |
| 169 private: | 168 private: |
| 170 ThreadState* m_state; | 169 ThreadState* m_state; |
| 171 }; | 170 }; |
| 172 | 171 |
| 173 void lockThreadAttachMutex(); | 172 // The set of ThreadStates for all threads attached to the Blink |
| 174 void unlockThreadAttachMutex(); | 173 // garbage collector. |
| 174 using AttachedThreadStateSet = HashSet<ThreadState*>; |
| 175 static AttachedThreadStateSet& attachedThreads(); |
| 176 static RecursiveMutex& threadAttachMutex(); |
| 177 static void lockThreadAttachMutex(); |
| 178 static void unlockThreadAttachMutex(); |
| 175 | 179 |
| 180 // Initialize threading infrastructure. Should be called from the main |
| 181 // thread. |
| 182 static void init(); |
| 183 static void shutdown(); |
| 176 bool isTerminating() { return m_isTerminating; } | 184 bool isTerminating() { return m_isTerminating; } |
| 177 | 185 |
| 178 static void attachMainThread(); | 186 static void attachMainThread(); |
| 179 static void detachMainThread(); | 187 static void detachMainThread(); |
| 180 void cleanupMainThread(); | 188 void cleanupMainThread(); |
| 181 | 189 |
| 190 // Trace all persistent roots, called when marking the managed heap objects. |
| 191 static void visitPersistentRoots(Visitor*); |
| 192 |
| 193 // Trace all objects found on the stack, used when doing conservative GCs. |
| 194 static void visitStackRoots(Visitor*); |
| 195 |
| 182 // Associate ThreadState object with the current thread. After this | 196 // Associate ThreadState object with the current thread. After this |
| 183 // call thread can start using the garbage collected heap infrastructure. | 197 // call thread can start using the garbage collected heap infrastructure. |
| 184 // It also has to periodically check for safepoints. | 198 // It also has to periodically check for safepoints. |
| 185 static void attachCurrentThread(); | 199 static void attach(); |
| 186 | 200 |
| 187 // Disassociate attached ThreadState from the current thread. The thread | 201 // Disassociate attached ThreadState from the current thread. The thread |
| 188 // can no longer use the garbage collected heap after this call. | 202 // can no longer use the garbage collected heap after this call. |
| 189 static void detachCurrentThread(); | 203 static void detach(); |
| 190 | 204 |
| 191 static ThreadState* current() | 205 static ThreadState* current() |
| 192 { | 206 { |
| 193 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) | 207 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) |
| 194 // TLS lookup is fast in these platforms. | 208 // TLS lookup is fast in these platforms. |
| 195 return **s_threadSpecific; | 209 return **s_threadSpecific; |
| 196 #else | 210 #else |
| 197 uintptr_t dummy; | 211 uintptr_t dummy; |
| 198 uintptr_t addressDiff = s_mainThreadStackStart - reinterpret_cast<uintpt
r_t>(&dummy); | 212 uintptr_t addressDiff = s_mainThreadStackStart - reinterpret_cast<uintpt
r_t>(&dummy); |
| 199 // This is a fast way to judge if we are in the main thread. | 213 // This is a fast way to judge if we are in the main thread. |
| 200 // If |&dummy| is within |s_mainThreadUnderestimatedStackSize| byte from | 214 // If |&dummy| is within |s_mainThreadUnderestimatedStackSize| byte from |
| 201 // the stack start of the main thread, we judge that we are in | 215 // the stack start of the main thread, we judge that we are in |
| 202 // the main thread. | 216 // the main thread. |
| 203 if (LIKELY(addressDiff < s_mainThreadUnderestimatedStackSize)) { | 217 if (LIKELY(addressDiff < s_mainThreadUnderestimatedStackSize)) { |
| 204 ASSERT(**s_threadSpecific == mainThreadState()); | 218 ASSERT(**s_threadSpecific == mainThreadState()); |
| 205 return mainThreadState(); | 219 return mainThreadState(); |
| 206 } | 220 } |
| 207 // TLS lookup is slow. | 221 // TLS lookup is slow. |
| 208 return **s_threadSpecific; | 222 return **s_threadSpecific; |
| 209 #endif | 223 #endif |
| 210 } | 224 } |
| 211 | 225 |
| 212 static ThreadState* mainThreadState() | 226 static ThreadState* mainThreadState() |
| 213 { | 227 { |
| 214 return reinterpret_cast<ThreadState*>(s_mainThreadStateStorage); | 228 return reinterpret_cast<ThreadState*>(s_mainThreadStateStorage); |
| 215 } | 229 } |
| 216 | 230 |
| 217 static ThreadState* fromObject(const void*); | |
| 218 | |
| 219 bool isMainThread() const { return this == mainThreadState(); } | 231 bool isMainThread() const { return this == mainThreadState(); } |
| 220 #if ENABLE(ASSERT) | 232 #if ENABLE(ASSERT) |
| 221 bool checkThread() const { return m_thread == currentThread(); } | 233 bool checkThread() const { return m_thread == currentThread(); } |
| 222 #endif | 234 #endif |
| 223 | 235 |
| 224 ThreadHeap& heap() { return *m_heap; } | |
| 225 | |
| 226 // When ThreadState is detaching from non-main thread its | |
| 227 // heap is expected to be empty (because it is going away). | |
| 228 // Perform registered cleanup tasks and garbage collection | |
| 229 // to sweep away any objects that are left on this heap. | |
| 230 // We assert that nothing must remain after this cleanup. | |
| 231 // If assertion does not hold we crash as we are potentially | |
| 232 // in the dangling pointer situation. | |
| 233 void runTerminationGC(); | |
| 234 | |
| 235 void performIdleGC(double deadlineSeconds); | 236 void performIdleGC(double deadlineSeconds); |
| 236 void performIdleLazySweep(double deadlineSeconds); | 237 void performIdleLazySweep(double deadlineSeconds); |
| 237 | 238 |
| 238 void scheduleIdleGC(); | 239 void scheduleIdleGC(); |
| 239 void scheduleIdleLazySweep(); | 240 void scheduleIdleLazySweep(); |
| 240 void schedulePreciseGC(); | 241 void schedulePreciseGC(); |
| 241 void scheduleV8FollowupGCIfNeeded(BlinkGC::V8GCType); | 242 void scheduleV8FollowupGCIfNeeded(BlinkGC::V8GCType); |
| 242 void schedulePageNavigationGCIfNeeded(float estimatedRemovalRatio); | 243 void schedulePageNavigationGCIfNeeded(float estimatedRemovalRatio); |
| 243 void schedulePageNavigationGC(); | 244 void schedulePageNavigationGC(); |
| 244 void scheduleGCIfNeeded(); | 245 void scheduleGCIfNeeded(); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 // there is a GC in progress. | 317 // there is a GC in progress. |
| 317 // | 318 // |
| 318 // Each thread that has ThreadState attached must: | 319 // Each thread that has ThreadState attached must: |
| 319 // - periodically check if GC is requested from another thread by calling
a safePoint() method; | 320 // - periodically check if GC is requested from another thread by calling
a safePoint() method; |
| 320 // - use SafePointScope around long running loops that have no safePoint()
invocation inside, | 321 // - use SafePointScope around long running loops that have no safePoint()
invocation inside, |
| 321 // such loops must not touch any heap object; | 322 // such loops must not touch any heap object; |
| 322 // - register an BlinkGCInterruptor that can interrupt long running loops
that have no calls to safePoint and | 323 // - register an BlinkGCInterruptor that can interrupt long running loops
that have no calls to safePoint and |
| 323 // are not wrapped in a SafePointScope (e.g. BlinkGCInterruptor for Java
Script code) | 324 // are not wrapped in a SafePointScope (e.g. BlinkGCInterruptor for Java
Script code) |
| 324 // | 325 // |
| 325 | 326 |
| 327 // Request all other threads to stop. Must only be called if the current thr
ead is at safepoint. |
| 328 static bool stopThreads(); |
| 329 static void resumeThreads(); |
| 330 |
| 326 // Check if GC is requested by another thread and pause this thread if this
is the case. | 331 // Check if GC is requested by another thread and pause this thread if this
is the case. |
| 327 // Can only be called when current thread is in a consistent state. | 332 // Can only be called when current thread is in a consistent state. |
| 328 void safePoint(BlinkGC::StackState); | 333 void safePoint(BlinkGC::StackState); |
| 329 | 334 |
| 330 // Mark current thread as running inside safepoint. | 335 // Mark current thread as running inside safepoint. |
| 331 void enterSafePoint(BlinkGC::StackState, void*); | 336 void enterSafePoint(BlinkGC::StackState, void*); |
| 332 void leaveSafePoint(SafePointAwareMutexLocker* = nullptr); | 337 void leaveSafePoint(SafePointAwareMutexLocker* = nullptr); |
| 333 bool isAtSafePoint() const { return m_atSafePoint; } | 338 bool isAtSafePoint() const { return m_atSafePoint; } |
| 334 | 339 |
| 335 void addInterruptor(PassOwnPtr<BlinkGCInterruptor>); | 340 void addInterruptor(PassOwnPtr<BlinkGCInterruptor>); |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 #if defined(LEAK_SANITIZER) | 516 #if defined(LEAK_SANITIZER) |
| 512 void enterStaticReferenceRegistrationDisabledScope(); | 517 void enterStaticReferenceRegistrationDisabledScope(); |
| 513 void leaveStaticReferenceRegistrationDisabledScope(); | 518 void leaveStaticReferenceRegistrationDisabledScope(); |
| 514 #endif | 519 #endif |
| 515 | 520 |
| 516 void resetHeapCounters(); | 521 void resetHeapCounters(); |
| 517 void increaseAllocatedObjectSize(size_t); | 522 void increaseAllocatedObjectSize(size_t); |
| 518 void decreaseAllocatedObjectSize(size_t); | 523 void decreaseAllocatedObjectSize(size_t); |
| 519 void increaseMarkedObjectSize(size_t); | 524 void increaseMarkedObjectSize(size_t); |
| 520 | 525 |
| 521 void callThreadShutdownHooks(); | |
| 522 | |
| 523 private: | 526 private: |
| 524 enum SnapshotType { | 527 enum SnapshotType { |
| 525 HeapSnapshot, | 528 HeapSnapshot, |
| 526 FreelistSnapshot | 529 FreelistSnapshot |
| 527 }; | 530 }; |
| 528 | 531 |
| 529 ThreadState(); | 532 ThreadState(); |
| 530 ~ThreadState(); | 533 ~ThreadState(); |
| 531 | 534 |
| 532 NO_SANITIZE_ADDRESS void copyStackUntilSafePointScope(); | 535 NO_SANITIZE_ADDRESS void copyStackUntilSafePointScope(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 | 576 |
| 574 void runScheduledGC(BlinkGC::StackState); | 577 void runScheduledGC(BlinkGC::StackState); |
| 575 | 578 |
| 576 void eagerSweep(); | 579 void eagerSweep(); |
| 577 | 580 |
| 578 #if defined(ADDRESS_SANITIZER) | 581 #if defined(ADDRESS_SANITIZER) |
| 579 void poisonEagerArena(); | 582 void poisonEagerArena(); |
| 580 void poisonAllHeaps(); | 583 void poisonAllHeaps(); |
| 581 #endif | 584 #endif |
| 582 | 585 |
| 586 // When ThreadState is detaching from non-main thread its |
| 587 // heap is expected to be empty (because it is going away). |
| 588 // Perform registered cleanup tasks and garbage collection |
| 589 // to sweep away any objects that are left on this heap. |
| 590 // We assert that nothing must remain after this cleanup. |
| 591 // If assertion does not hold we crash as we are potentially |
| 592 // in the dangling pointer situation. |
| 593 void cleanup(); |
| 583 void cleanupPages(); | 594 void cleanupPages(); |
| 584 | 595 |
| 585 void prepareForThreadStateTermination(); | 596 void prepareForThreadStateTermination(); |
| 586 | 597 |
| 587 void invokePreFinalizers(); | 598 void invokePreFinalizers(); |
| 588 | 599 |
| 589 void takeSnapshot(SnapshotType); | 600 void takeSnapshot(SnapshotType); |
| 590 void clearArenaAges(); | 601 void clearArenaAges(); |
| 591 int arenaIndexOfVectorArenaLeastRecentlyExpanded(int beginArenaIndex, int en
dArenaIndex); | 602 int arenaIndexOfVectorArenaLeastRecentlyExpanded(int beginArenaIndex, int en
dArenaIndex); |
| 592 | 603 |
| 593 void reportMemoryToV8(); | 604 void reportMemoryToV8(); |
| 594 | 605 |
| 595 // Should only be called under protection of threadAttachMutex(). | 606 // Should only be called under protection of threadAttachMutex(). |
| 596 const Vector<OwnPtr<BlinkGCInterruptor>>& interruptors() const { return m_in
terruptors; } | 607 const Vector<OwnPtr<BlinkGCInterruptor>>& interruptors() const { return m_in
terruptors; } |
| 597 | 608 |
| 598 friend class SafePointAwareMutexLocker; | 609 friend class SafePointAwareMutexLocker; |
| 599 friend class SafePointBarrier; | 610 friend class SafePointBarrier; |
| 600 friend class SafePointScope; | 611 friend class SafePointScope; |
| 601 | 612 |
| 602 static WTF::ThreadSpecific<ThreadState*>* s_threadSpecific; | 613 static WTF::ThreadSpecific<ThreadState*>* s_threadSpecific; |
| 603 static uintptr_t s_mainThreadStackStart; | 614 static uintptr_t s_mainThreadStackStart; |
| 604 static uintptr_t s_mainThreadUnderestimatedStackSize; | 615 static uintptr_t s_mainThreadUnderestimatedStackSize; |
| 616 static SafePointBarrier* s_safePointBarrier; |
| 605 | 617 |
| 606 // We can't create a static member of type ThreadState here | 618 // We can't create a static member of type ThreadState here |
| 607 // because it will introduce global constructor and destructor. | 619 // because it will introduce global constructor and destructor. |
| 608 // We would like to manage lifetime of the ThreadState attached | 620 // We would like to manage lifetime of the ThreadState attached |
| 609 // to the main thread explicitly instead and still use normal | 621 // to the main thread explicitly instead and still use normal |
| 610 // constructor and destructor for the ThreadState class. | 622 // constructor and destructor for the ThreadState class. |
| 611 // For this we reserve static storage for the main ThreadState | 623 // For this we reserve static storage for the main ThreadState |
| 612 // and lazily construct ThreadState in it using placement new. | 624 // and lazily construct ThreadState in it using placement new. |
| 613 static uint8_t s_mainThreadStateStorage[]; | 625 static uint8_t s_mainThreadStateStorage[]; |
| 614 | 626 |
| 615 ThreadHeap* m_heap; | |
| 616 ThreadIdentifier m_thread; | 627 ThreadIdentifier m_thread; |
| 617 OwnPtr<PersistentRegion> m_persistentRegion; | 628 OwnPtr<PersistentRegion> m_persistentRegion; |
| 618 BlinkGC::StackState m_stackState; | 629 BlinkGC::StackState m_stackState; |
| 619 #if OS(WIN) && COMPILER(MSVC) | 630 #if OS(WIN) && COMPILER(MSVC) |
| 620 size_t m_threadStackSize; | 631 size_t m_threadStackSize; |
| 621 #endif | 632 #endif |
| 622 intptr_t* m_startOfStack; | 633 intptr_t* m_startOfStack; |
| 623 intptr_t* m_endOfStack; | 634 intptr_t* m_endOfStack; |
| 624 | 635 |
| 625 void* m_safePointScopeMarker; | 636 void* m_safePointScopeMarker; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 696 | 707 |
| 697 template<> class ThreadStateFor<AnyThread> { | 708 template<> class ThreadStateFor<AnyThread> { |
| 698 STATIC_ONLY(ThreadStateFor); | 709 STATIC_ONLY(ThreadStateFor); |
| 699 public: | 710 public: |
| 700 static ThreadState* state() { return ThreadState::current(); } | 711 static ThreadState* state() { return ThreadState::current(); } |
| 701 }; | 712 }; |
| 702 | 713 |
| 703 } // namespace blink | 714 } // namespace blink |
| 704 | 715 |
| 705 #endif // ThreadState_h | 716 #endif // ThreadState_h |
| OLD | NEW |