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 16 matching lines...) Expand all Loading... | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #ifndef ThreadState_h | 31 #ifndef ThreadState_h |
32 #define ThreadState_h | 32 #define ThreadState_h |
33 | 33 |
34 #include "platform/PlatformExport.h" | 34 #include "platform/PlatformExport.h" |
35 #include "platform/heap/AddressSanitizer.h" | 35 #include "platform/heap/AddressSanitizer.h" |
36 #include "public/platform/WebThread.h" | 36 #include "public/platform/WebThread.h" |
37 #include "wtf/HashMap.h" | |
37 #include "wtf/HashSet.h" | 38 #include "wtf/HashSet.h" |
38 #include "wtf/OwnPtr.h" | 39 #include "wtf/OwnPtr.h" |
39 #include "wtf/PassOwnPtr.h" | 40 #include "wtf/PassOwnPtr.h" |
40 #include "wtf/ThreadSpecific.h" | 41 #include "wtf/ThreadSpecific.h" |
41 #include "wtf/Threading.h" | 42 #include "wtf/Threading.h" |
42 #include "wtf/ThreadingPrimitives.h" | 43 #include "wtf/ThreadingPrimitives.h" |
43 #include "wtf/Vector.h" | 44 #include "wtf/Vector.h" |
44 | 45 |
45 #if ENABLE(GC_PROFILE_HEAP) | |
46 #include "wtf/HashMap.h" | |
47 #endif | |
48 | |
49 namespace blink { | 46 namespace blink { |
50 | 47 |
51 class BaseHeap; | 48 class BaseHeap; |
52 class BaseHeapPage; | 49 class BaseHeapPage; |
53 class FinalizedHeapObjectHeader; | 50 class FinalizedHeapObjectHeader; |
54 struct GCInfo; | 51 struct GCInfo; |
55 class HeapContainsCache; | 52 class HeapContainsCache; |
56 class HeapObjectHeader; | 53 class HeapObjectHeader; |
57 class PageMemory; | 54 class PageMemory; |
58 class PersistentNode; | 55 class PersistentNode; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 class Class; \ | 121 class Class; \ |
125 } \ | 122 } \ |
126 namespace blink { \ | 123 namespace blink { \ |
127 template<> struct ThreadingTrait<Namespace::Class> { \ | 124 template<> struct ThreadingTrait<Namespace::Class> { \ |
128 static const ThreadAffinity Affinity = AnyThread; \ | 125 static const ThreadAffinity Affinity = AnyThread; \ |
129 }; \ | 126 }; \ |
130 } | 127 } |
131 | 128 |
132 template<typename U> class ThreadingTrait<const U> : public ThreadingTrait<U> { }; | 129 template<typename U> class ThreadingTrait<const U> : public ThreadingTrait<U> { }; |
133 | 130 |
131 // Declare that a class has a pre-finalizer function. The function is called in | |
132 // the object's owner thread, and can access Member<>s to other | |
133 // garbarge-collected objects allocated in the thread. However we must not | |
134 // allocate new garbage-collected objects, nor must update Member<> and | |
135 // Persistent<> pointers. | |
haraken
2014/10/06 05:26:42
nor must update => nor update
(I'm not familiar w
| |
136 // | |
137 // This feature is similar to the HeapHashMap<WeakMember<Foo>, OwnPtr<Disposer>> | |
138 // idiom. The difference between this and the idiom is that pre-finalizer | |
139 // function is called whenever an object is destructed with this feature. With | |
140 // the HeapHashMap<WeakMember...> idiom, a Disposer object is not destructed | |
141 // before the corresponding ~Foo if the Foo object and the onwer of the | |
142 // HeapHashMap are collected at same time. | |
haraken
2014/10/06 05:26:42
I'm a bit confused with this description.
class B
tkent
2014/10/06 06:02:24
It's not correct. ~Disposer is called when a back
haraken
2014/10/06 06:28:09
Thanks, understood.
Then is the only difference w
tkent
2014/10/06 07:00:52
Right. Both of them have O(n) cost, and the pre-f
haraken
2014/10/06 07:04:09
Sounds great. Then let's replace this comment with
tkent
2014/10/06 07:17:59
Done. Added a FIXME.
| |
143 // | |
144 // See ThreadState::registerPreFinalizer. | |
145 // | |
146 // Usage: | |
147 // | |
148 // class Foo : GarbageCollected<Foo> { | |
149 // USING_PRE_FINALIZER(Foo, dispose); | |
150 // public: | |
151 // Foo() | |
152 // { | |
153 // registerPreFinalizer(); | |
haraken
2014/10/06 05:26:42
registerPreFinalizer(*this);
| |
154 // } | |
155 // private: | |
156 // void dispose(); | |
157 // Member<Bar> m_bar; | |
158 // }; | |
159 // | |
160 // void Foo::dispose() | |
161 // { | |
162 // m_bar->... | |
163 // } | |
164 #define USING_PRE_FINALIZER(Class, method) \ | |
165 public: \ | |
166 static bool invokePreFinalizer(void* object, Visitor& visitor) \ | |
167 { \ | |
168 Class* self = reinterpret_cast<Class*>(object); \ | |
169 if (visitor.isAlive(self)) \ | |
170 return false; \ | |
171 self->method(); \ | |
172 return true; \ | |
173 } \ | |
174 typedef char UsingPreFinazlizerMacroNeedsTrailingSemiColon | |
175 | |
134 // List of typed heaps. The list is used to generate the implementation | 176 // List of typed heaps. The list is used to generate the implementation |
135 // of typed heap related methods. | 177 // of typed heap related methods. |
136 // | 178 // |
137 // To create a new typed heap add a H(<ClassName>) to the | 179 // To create a new typed heap add a H(<ClassName>) to the |
138 // FOR_EACH_TYPED_HEAP macro below. | 180 // FOR_EACH_TYPED_HEAP macro below. |
139 #define FOR_EACH_TYPED_HEAP(H) \ | 181 #define FOR_EACH_TYPED_HEAP(H) \ |
140 H(Node) | 182 H(Node) |
141 | 183 |
142 #define TypedHeapEnumName(Type) Type##Heap, | 184 #define TypedHeapEnumName(Type) Type##Heap, |
143 #define TypedHeapEnumNameNonFinalized(Type) Type##HeapNonFinalized, | 185 #define TypedHeapEnumNameNonFinalized(Type) Type##HeapNonFinalized, |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
622 | 664 |
623 void getStats(HeapStats&); | 665 void getStats(HeapStats&); |
624 HeapStats& stats() { return m_stats; } | 666 HeapStats& stats() { return m_stats; } |
625 HeapStats& statsAfterLastGC() { return m_statsAfterLastGC; } | 667 HeapStats& statsAfterLastGC() { return m_statsAfterLastGC; } |
626 | 668 |
627 void setupHeapsForTermination(); | 669 void setupHeapsForTermination(); |
628 | 670 |
629 void registerSweepingTask(); | 671 void registerSweepingTask(); |
630 void unregisterSweepingTask(); | 672 void unregisterSweepingTask(); |
631 | 673 |
674 // Request to call a pref-finalizer of the target object before the object | |
675 // is destructed. The class T must have USING_PRE_FINALIZER(). The | |
676 // argument should be |*this|. Registering a lot of objects affects GC | |
677 // performance. We should register an object only if the object really | |
678 // requires pre-finalizer, and we should unregister the object if | |
679 // pre-finalizer is unnecessary. | |
680 template<typename T> | |
681 void registerPreFinalizer(T& target) | |
682 { | |
683 ASSERT(!m_preFinalizers.contains(&target)); | |
684 ASSERT(!isSweepInProgress()); | |
685 m_preFinalizers.add(&target, &T::invokePreFinalizer); | |
686 } | |
687 | |
688 // Cancel above requests. The argument should be |*this|. This function is | |
689 // ignored if it is called in pre-finalizer functions. | |
690 template<typename T> | |
691 void unregisterPreFinalizer(T& target) | |
692 { | |
693 ASSERT(&T::invokePreFinalizer); | |
694 unregisterPreFinalizerInternal(&target); | |
695 } | |
696 | |
632 Mutex& sweepMutex() { return m_sweepMutex; } | 697 Mutex& sweepMutex() { return m_sweepMutex; } |
633 | 698 |
634 private: | 699 private: |
635 explicit ThreadState(); | 700 explicit ThreadState(); |
636 ~ThreadState(); | 701 ~ThreadState(); |
637 | 702 |
638 friend class SafePointBarrier; | 703 friend class SafePointBarrier; |
639 friend class SafePointAwareMutexLocker; | 704 friend class SafePointAwareMutexLocker; |
640 | 705 |
641 void enterSafePoint(StackState, void*); | 706 void enterSafePoint(StackState, void*); |
(...skipping 18 matching lines...) Expand all Loading... | |
660 // to sweep away any objects that are left on this heap. | 725 // to sweep away any objects that are left on this heap. |
661 // We assert that nothing must remain after this cleanup. | 726 // We assert that nothing must remain after this cleanup. |
662 // If assertion does not hold we crash as we are potentially | 727 // If assertion does not hold we crash as we are potentially |
663 // in the dangling pointer situation. | 728 // in the dangling pointer situation. |
664 void cleanup(); | 729 void cleanup(); |
665 void cleanupPages(); | 730 void cleanupPages(); |
666 | 731 |
667 void setLowCollectionRate(bool value) { m_lowCollectionRate = value; } | 732 void setLowCollectionRate(bool value) { m_lowCollectionRate = value; } |
668 | 733 |
669 void waitUntilSweepersDone(); | 734 void waitUntilSweepersDone(); |
735 void unregisterPreFinalizerInternal(void*); | |
736 void invokePreFinalizers(Visitor&); | |
670 | 737 |
671 static WTF::ThreadSpecific<ThreadState*>* s_threadSpecific; | 738 static WTF::ThreadSpecific<ThreadState*>* s_threadSpecific; |
672 static SafePointBarrier* s_safePointBarrier; | 739 static SafePointBarrier* s_safePointBarrier; |
673 | 740 |
674 // This variable is flipped to true after all threads are stoped | 741 // This variable is flipped to true after all threads are stoped |
675 // and outermost GC has started. | 742 // and outermost GC has started. |
676 static bool s_inGC; | 743 static bool s_inGC; |
677 | 744 |
678 // We can't create a static member of type ThreadState here | 745 // We can't create a static member of type ThreadState here |
679 // because it will introduce global constructor and destructor. | 746 // because it will introduce global constructor and destructor. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
711 bool m_isTerminating; | 778 bool m_isTerminating; |
712 | 779 |
713 bool m_lowCollectionRate; | 780 bool m_lowCollectionRate; |
714 | 781 |
715 OwnPtr<blink::WebThread> m_sweeperThread; | 782 OwnPtr<blink::WebThread> m_sweeperThread; |
716 int m_numberOfSweeperTasks; | 783 int m_numberOfSweeperTasks; |
717 Mutex m_sweepMutex; | 784 Mutex m_sweepMutex; |
718 ThreadCondition m_sweepThreadCondition; | 785 ThreadCondition m_sweepThreadCondition; |
719 | 786 |
720 CallbackStack* m_weakCallbackStack; | 787 CallbackStack* m_weakCallbackStack; |
788 HashMap<void*, bool (*)(void*, Visitor&)> m_preFinalizers; | |
721 | 789 |
722 #if defined(ADDRESS_SANITIZER) | 790 #if defined(ADDRESS_SANITIZER) |
723 void* m_asanFakeStack; | 791 void* m_asanFakeStack; |
724 #endif | 792 #endif |
725 }; | 793 }; |
726 | 794 |
727 template<ThreadAffinity affinity> class ThreadStateFor; | 795 template<ThreadAffinity affinity> class ThreadStateFor; |
728 | 796 |
729 template<> class ThreadStateFor<MainThreadOnly> { | 797 template<> class ThreadStateFor<MainThreadOnly> { |
730 public: | 798 public: |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
847 // whether the page is part of a terminting thread or | 915 // whether the page is part of a terminting thread or |
848 // if the page is traced after being terminated (orphaned). | 916 // if the page is traced after being terminated (orphaned). |
849 uintptr_t m_terminating : 1; | 917 uintptr_t m_terminating : 1; |
850 uintptr_t m_tracedAfterOrphaned : 1; | 918 uintptr_t m_tracedAfterOrphaned : 1; |
851 uintptr_t m_promptlyFreedSize : 17; // == blinkPageSizeLog2 | 919 uintptr_t m_promptlyFreedSize : 17; // == blinkPageSizeLog2 |
852 }; | 920 }; |
853 | 921 |
854 } | 922 } |
855 | 923 |
856 #endif // ThreadState_h | 924 #endif // ThreadState_h |
OLD | NEW |