Ignore:
Timestamp:
Aug 15, 2017, 5:03:45 PM (8 years ago)
Author:
[email protected]
Message:

Make VM::scratchBufferForSize thread safe
https://bugs.webkit.org/show_bug.cgi?id=175604

Reviewed by Geoffrey Garen and Mark Lam.

I want to use the VM::scratchBufferForSize in another patch I'm writing.
The use case for my other patch is to call it from the compiler thread.
When reading the code, I saw that this API was not thread safe. This patch
makes it thread safe. It actually turns out we were calling this API from
the compiler thread already when we created FTL::State for an FTL OSR entry
compilation, and from FTLLowerDFGToB3. That code was racy and wrong, but
is now correct with this patch.

  • runtime/VM.cpp:

(JSC::VM::VM):
(JSC::VM::~VM):
(JSC::VM::gatherConservativeRoots):
(JSC::VM::scratchBufferForSize):

  • runtime/VM.h:

(JSC::VM::scratchBufferForSize): Deleted.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/runtime/VM.cpp

    r220606 r220777  
    195195    , prototypeMap(*this)
    196196    , interpreter(0)
    197     , sizeOfLastScratchBuffer(0)
    198197    , entryScope(0)
    199198    , m_regExpCache(new RegExpCache(this))
     
    424423
    425424#if ENABLE(DFG_JIT)
    426     for (unsigned i = 0; i < scratchBuffers.size(); ++i)
    427         fastFree(scratchBuffers[i]);
     425    for (unsigned i = 0; i < m_scratchBuffers.size(); ++i)
     426        fastFree(m_scratchBuffers[i]);
    428427#endif
    429428}
     
    747746void VM::gatherConservativeRoots(ConservativeRoots& conservativeRoots)
    748747{
    749     for (auto* scratchBuffer : scratchBuffers) {
     748    auto lock = holdLock(m_scratchBufferLock);
     749    for (auto* scratchBuffer : m_scratchBuffers) {
    750750        if (scratchBuffer->activeLength()) {
    751751            void* bufferStart = scratchBuffer->dataBuffer();
     
    10091009#endif // USE(CF)
    10101010
     1011ScratchBuffer* VM::scratchBufferForSize(size_t size)
     1012{
     1013    if (!size)
     1014        return nullptr;
     1015
     1016    auto locker = holdLock(m_scratchBufferLock);
     1017
     1018    if (size > m_sizeOfLastScratchBuffer) {
     1019        // Protect against a N^2 memory usage pathology by ensuring
     1020        // that at worst, we get a geometric series, meaning that the
     1021        // total memory usage is somewhere around
     1022        // max(scratch buffer size) * 4.
     1023        m_sizeOfLastScratchBuffer = size * 2;
     1024
     1025        ScratchBuffer* newBuffer = ScratchBuffer::create(m_sizeOfLastScratchBuffer);
     1026        RELEASE_ASSERT(newBuffer);
     1027        m_scratchBuffers.append(newBuffer);
     1028    }
     1029
     1030    ScratchBuffer* result = m_scratchBuffers.last();
     1031    return result;
     1032}
     1033
    10111034} // namespace JSC
Note: See TracChangeset for help on using the changeset viewer.