Index: Source/platform/heap/Heap.h |
diff --git a/Source/platform/heap/Heap.h b/Source/platform/heap/Heap.h |
index d28cac06c23c38209c4bfe9e030d5e477a8aaa0f..af1854436b719d11aacffe815069f3b414ec2caf 100644 |
--- a/Source/platform/heap/Heap.h |
+++ b/Source/platform/heap/Heap.h |
@@ -39,6 +39,7 @@ |
#include "wtf/Assertions.h" |
#include "wtf/OwnPtr.h" |
#include "wtf/PassRefPtr.h" |
+#include "wtf/ThreadSafeRefCounted.h" |
#include <stdint.h> |
@@ -67,6 +68,7 @@ class HeapStats; |
class PageMemory; |
template<ThreadAffinity affinity> class ThreadLocalPersistents; |
template<typename T, typename RootsAccessor = ThreadLocalPersistents<ThreadingTrait<T>::Affinity > > class Persistent; |
+template<typename T> class CrossThreadPersistent; |
PLATFORM_EXPORT size_t osPageSize(); |
@@ -535,6 +537,68 @@ private: |
friend class ThreadState; |
}; |
+// FIXME(oilpan): The current implementation requires the deallocating |
haraken
2014/04/08 06:02:08
FIXME: oilpan:
keishi
2014/04/21 17:10:11
Done.
|
+// thread to be the same as the thread on which this instance was first |
+// allocated. Only the refs/derefs that are not 0 -> 1 or 1 -> 0 are |
+// thread safe. |
haraken
2014/04/08 06:02:08
Do we still have this restriction? I guess that th
keishi
2014/04/21 17:10:11
Done.
|
+// This is currently used by the WebAudio code. |
+// We should attempt to restructure the WebAudio code so that the main thread |
+// alone determines life-time and receives messages about life-time from the |
+// audio thread. |
+template<typename T> |
+class ThreadSafeRefCountedGarbageCollected : public GarbageCollectedFinalized<T>, public WTF::ThreadSafeRefCountedBase { |
+ WTF_MAKE_NONCOPYABLE(ThreadSafeRefCountedGarbageCollected); |
+ |
+public: |
+ ThreadSafeRefCountedGarbageCollected() |
+ { |
+#ifndef NDEBUG |
+ m_threadState = ThreadState::current(); |
+#endif |
+ m_keepAlive = adoptPtr(new CrossThreadPersistent<T>(static_cast<T*>(this))); |
+ } |
+ |
+ // Override ref to deal with a case where a reference count goes up |
+ // from 0 to 1. This can happen in the following scenario: |
+ // (1) The reference count becomes 0, but on-stack pointers keep references to the object. |
+ // (2) The on-stack pointer is assigned to a RefPtr. The reference count becomes 1. |
+ // In this case, we have to resurrect m_keepAlive. |
+ void ref() |
+ { |
+ MutexLocker lock(m_mutex); |
+ if (UNLIKELY(!refCount())) { |
+ ASSERT(!m_keepAlive); |
+ ASSERT(m_threadState->contains(reinterpret_cast<Address>(this))); |
+ // ASSERT(m_threadState == ThreadState::current()); |
haraken
2014/04/08 06:02:08
Do we need this ASSERT?
keishi
2014/04/21 17:10:11
This was in the branch but I don't think we should
|
+ m_keepAlive = adoptPtr(new CrossThreadPersistent<T>(static_cast<T*>(this))); |
+ } |
+ WTF::ThreadSafeRefCountedBase::ref(); |
+ } |
+ |
+ // Override deref to deal with our own deallocation based on ref counting. |
+ void deref() |
+ { |
+ MutexLocker lock(m_mutex); |
+ if (derefBase()) { |
+ ASSERT(m_threadState == ThreadState::current()); |
haraken
2014/04/08 06:02:08
Do we need this ASSERT?
haraken
2014/04/08 06:02:08
Add ASSERT(m_keepAlive).
keishi
2014/04/21 17:10:11
Done.
keishi
2014/04/21 17:10:11
Removed.
|
+ m_keepAlive.clear(); |
+ } |
+ } |
+ |
+ using GarbageCollectedFinalized<T>::operator new; |
+ using GarbageCollectedFinalized<T>::operator delete; |
+ |
+protected: |
+ ~ThreadSafeRefCountedGarbageCollected() { } |
+ |
+private: |
+ OwnPtr<CrossThreadPersistent<T> > m_keepAlive; |
+ mutable Mutex m_mutex; |
+#ifndef NDEBUG |
+ ThreadState* m_threadState; |
+#endif |
+}; |
+ |
// The CallbackStack contains all the visitor callbacks used to trace and mark |
// objects. A specific CallbackStack instance contains at most bufferSize elements. |
// If more space is needed a new CallbackStack instance is created and chained |