Index: Source/platform/heap/Visitor.cpp |
diff --git a/Source/platform/heap/Visitor.cpp b/Source/platform/heap/Visitor.cpp |
index 7183b329e54e5a745bf63448032ab8ad8bafd27d..57c2edbd311d3702ca4b2e4da070bcebcc39db4d 100644 |
--- a/Source/platform/heap/Visitor.cpp |
+++ b/Source/platform/heap/Visitor.cpp |
@@ -43,32 +43,32 @@ int GCInfoTable::s_gcInfoIndex = 0; |
size_t GCInfoTable::s_gcInfoTableSize = 0; |
GCInfo const** s_gcInfoTable = nullptr; |
-size_t GCInfoTable::allocateGCInfoSlot() |
+void GCInfoTable::ensureGCInfoIndex(const GCInfo* gcInfo, size_t* gcInfoIndexSlot) |
{ |
- // FIXME: if multiple threads attempt to allocate the initial object |
- // for a given type, there'll be a write race on updating the 'static' |
- // holding its index. Duplicate GCInfo slots might be written for the |
- // object, which is benign, but verify that the index update race is |
- // acceptable. |
- int index = atomicIncrement(&s_gcInfoIndex); |
+ ASSERT(gcInfo); |
+ ASSERT(gcInfoIndexSlot); |
+ // Keep a global GCInfoTable lock while allocating a new slot. |
+ AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex); |
+ MutexLocker locker(mutex); |
+ |
+ // If more than one thread ends up allocating a slot for |
+ // the same GCInfo, have later threads reuse the slot |
+ // allocated by the first. |
+ if (*gcInfoIndexSlot) |
+ return; |
+ |
+ int index = ++s_gcInfoIndex; |
size_t gcInfoIndex = static_cast<size_t>(index); |
ASSERT(gcInfoIndex < GCInfoTable::maxIndex); |
if (gcInfoIndex >= s_gcInfoTableSize) |
- resize(gcInfoIndex); |
+ resize(); |
- return gcInfoIndex; |
+ s_gcInfoTable[gcInfoIndex] = gcInfo; |
+ *gcInfoIndexSlot = gcInfoIndex; |
} |
-void GCInfoTable::resize(size_t index) |
+void GCInfoTable::resize() |
{ |
- AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex); |
- MutexLocker locker(mutex); |
- |
- // Keep a lock while expanding the shared table; check |
- // if another thread have already resized. |
- if (index < s_gcInfoTableSize) |
- return; |
- |
// (Light) experimentation suggests that Blink doesn't need |
// more than this while handling content on popular web properties. |
const size_t initialSize = 512; |
@@ -84,7 +84,7 @@ void GCInfoTable::resize(size_t index) |
void GCInfoTable::init() |
{ |
RELEASE_ASSERT(!s_gcInfoTable); |
- resize(0); |
+ resize(); |
} |
void GCInfoTable::shutdown() |