Changeset 163804 in webkit


Ignore:
Timestamp:
Feb 10, 2014 11:39:30 AM (10 years ago)
Author:
mark.lam@apple.com
Message:

Remove unnecessary spinLock in JSLock.
<https://webkit.org/b/128450>

Reviewed by Filip Pizlo.

The JSLock's mutex already provides protection for write access to
JSLock's internal state. The only JSLock state that needs to be read
from any thread including threads that don't own the JSLock is
m_ownerThread, which is used in currentThreadIsHoldingLock() to do an
ownership test on the lock.

It is safe for other threads to read from m_ownerThread because they
only need to know whether its value matches their own thread id
(provided by WTF::currentThread()).

Here are the scenarios for how the ownership test can go:

  1. The JSLock has just been initialized and is not owned by any thread.

In this case, m_ownerThread will be 0 and will not match any thread's
thread id. The checking thread will know that it needs to lock the
JSLock before using the VM.

  1. The JSLock was previously locked, but now is unlocked.

When we unlock it in JSLock::unlock(), the owner thread clears
m_ownerThread to 0. Hence, this case is the same as (1) above.

  1. The JSLock is locked by Thread A. Thread B is checking ownership.

In this case, m_ownerThread will contains the Thread A's thread id.
Thread B will see that the thread id does not match its own and will
proceed to block on the JSLock's mutex to wait for its turn to use
the VM.

With Weak Memory Ordering architectures, Thread A's thread id may
not get written out to memory before Thread B inspects m_ownerThread.
However, though Thread B may not see Thread A's thread id in
m_ownerThread, it will see 0 which is the last value written to it
before the JSLock mutex was unlocked. The mutex unlock would have
executed a memory fence which would have flushed the 0 to
m_ownerThread in memory. Hence, Thread B will know that it does not
own the lock.

Apart from removing the unneeded spin lock code, I also changed the
JSLock code to use currentThreadIsHoldingLock() and setOwnerThread()
instead of accessing m_ownerThread directly.

  • runtime/JSLock.cpp:

(JSC::JSLock::JSLock):

(JSC::JSLock::lock):

  • Removed spinLock but left the indentation as is to keep the diff to a minimum for better readability. Will unindent in a subsequent patch.

(JSC::JSLock::unlock):

  • Before unlocking the mutex, clear m_ownerThread to indicate that the lock is no longer owned.

(JSC::JSLock::currentThreadIsHoldingLock):

  • Removed the check of m_lockCount for determining ownership. Checking m_ownerThread is sufficient.

(JSC::JSLock::dropAllLocks):
(JSC::JSLock::dropAllLocksUnconditionally):

  • Renamed local locksToDrop to the better name droppedLockCount.
  • Clear m_ownerThread since we're unlocking the JSLock.

(JSC::JSLock::grabAllLocks):

  • Removed unneeded lock ownership test for lock re-entry case because grabAllLocks() is never used to re-enter a locked JSLock.

(JSC::JSLock::DropAllLocks::DropAllLocks):
(JSC::JSLock::DropAllLocks::~DropAllLocks):

  • runtime/JSLock.h:

(JSC::JSLock::setOwnerThread):

Location:
trunk/Source/JavaScriptCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r163803 r163804  
     12014-02-09  Mark Lam  <mark.lam@apple.com>
     2
     3        Remove unnecessary spinLock in JSLock.
     4        <https://webkit.org/b/128450>
     5
     6        Reviewed by Filip Pizlo.
     7
     8        The JSLock's mutex already provides protection for write access to
     9        JSLock's internal state. The only JSLock state that needs to be read
     10        from any thread including threads that don't own the JSLock is
     11        m_ownerThread, which is used in currentThreadIsHoldingLock() to do an
     12        ownership test on the lock.
     13
     14        It is safe for other threads to read from m_ownerThread because they
     15        only need to know whether its value matches their own thread id
     16        (provided by WTF::currentThread()).
     17
     18        Here are the scenarios for how the ownership test can go:
     19
     20        1. The JSLock has just been initialized and is not owned by any thread.
     21
     22           In this case, m_ownerThread will be 0 and will not match any thread's
     23           thread id. The checking thread will know that it needs to lock the
     24           JSLock before using the VM.
     25
     26        2. The JSLock was previously locked, but now is unlocked.
     27
     28           When we unlock it in JSLock::unlock(), the owner thread clears
     29           m_ownerThread to 0. Hence, this case is the same as (1) above.
     30
     31        3. The JSLock is locked by Thread A. Thread B is checking ownership.
     32
     33           In this case, m_ownerThread will contains the Thread A's thread id.
     34           Thread B will see that the thread id does not match its own and will
     35           proceed to block on the JSLock's mutex to wait for its turn to use
     36           the VM.
     37
     38           With Weak Memory Ordering architectures, Thread A's thread id may
     39           not get written out to memory before Thread B inspects m_ownerThread.
     40           However, though Thread B may not see Thread A's thread id in
     41           m_ownerThread, it will see 0 which is the last value written to it
     42           before the JSLock mutex was unlocked. The mutex unlock would have
     43           executed a memory fence which would have flushed the 0 to
     44           m_ownerThread in memory. Hence, Thread B will know that it does not
     45           own the lock.
     46
     47        Apart from removing the unneeded spin lock code, I also changed the
     48        JSLock code to use currentThreadIsHoldingLock() and setOwnerThread()
     49        instead of accessing m_ownerThread directly.
     50
     51        * runtime/JSLock.cpp:
     52        (JSC::JSLock::JSLock):
     53
     54        (JSC::JSLock::lock):
     55        - Removed spinLock but left the indentation as is to keep the diff to a
     56          minimum for better readability. Will unindent in a subsequent patch.
     57
     58        (JSC::JSLock::unlock):
     59        - Before unlocking the mutex, clear m_ownerThread to indicate that the
     60          lock is no longer owned.
     61
     62        (JSC::JSLock::currentThreadIsHoldingLock):
     63        - Removed the check of m_lockCount for determining ownership. Checking
     64          m_ownerThread is sufficient.
     65
     66        (JSC::JSLock::dropAllLocks):
     67        (JSC::JSLock::dropAllLocksUnconditionally):
     68        - Renamed local locksToDrop to the better name droppedLockCount.
     69        - Clear m_ownerThread since we're unlocking the JSLock.
     70
     71        (JSC::JSLock::grabAllLocks):
     72        - Removed unneeded lock ownership test for lock re-entry case because
     73          grabAllLocks() is never used to re-enter a locked JSLock.
     74
     75        (JSC::JSLock::DropAllLocks::DropAllLocks):
     76        (JSC::JSLock::DropAllLocks::~DropAllLocks):
     77
     78        * runtime/JSLock.h:
     79        (JSC::JSLock::setOwnerThread):
     80
    1812014-02-10  Filip Pizlo  <fpizlo@apple.com>
    282
  • trunk/Source/JavaScriptCore/runtime/JSLock.cpp

    r163730 r163804  
    2828#include "Operations.h"
    2929
    30 #if USE(PTHREADS)
    31 #include <pthread.h>
    32 #endif
    33 
    3430namespace JSC {
    3531
     
    9187    , m_vm(vm)
    9288{
    93     m_spinLock.Init();
    9489}
    9590
     
    107102{
    108103    ThreadIdentifier currentThread = WTF::currentThread();
    109     {
    110         SpinLockHolder holder(&m_spinLock);
    111         if (m_ownerThread == currentThread && m_lockCount) {
    112             m_lockCount++;
    113             return;
    114         }
     104    if (currentThreadIsHoldingLock()) {
     105        m_lockCount++;
     106        return;
    115107    }
    116108
    117109    m_lock.lock();
    118110
    119     {
    120         SpinLockHolder holder(&m_spinLock);
    121         m_ownerThread = currentThread;
    122         ASSERT(!m_lockCount);
    123         m_lockCount = 1;
    124 
    125         WTFThreadData& threadData = wtfThreadData();
    126 
    127         if (!m_vm->stackPointerAtVMEntry) {
    128             m_vm->stackPointerAtVMEntry = &holder; // A proxy for the current stack pointer.
    129             threadData.setSavedReservedZoneSize(m_vm->updateStackLimitWithReservedZoneSize(Options::reservedZoneSize()));
    130         }
    131 
    132         m_vm->setLastStackTop(threadData.savedLastStackTop());
    133     }
     111    setOwnerThread(currentThread);
     112    ASSERT(!m_lockCount);
     113    m_lockCount = 1;
     114
     115    WTFThreadData& threadData = wtfThreadData();
     116
     117    if (!m_vm->stackPointerAtVMEntry) {
     118        void* p = &p;
     119        m_vm->stackPointerAtVMEntry = p; // A proxy for the current stack pointer.
     120        threadData.setSavedReservedZoneSize(m_vm->updateStackLimitWithReservedZoneSize(Options::reservedZoneSize()));
     121    }
     122
     123    m_vm->setLastStackTop(threadData.savedLastStackTop());
    134124}
    135125
    136126void JSLock::unlock()
    137127{
    138     SpinLockHolder holder(&m_spinLock);
    139     ASSERT(currentThreadIsHoldingLock());
     128    RELEASE_ASSERT(currentThreadIsHoldingLock());
    140129
    141130    m_lockCount--;
     
    146135            m_vm->updateStackLimitWithReservedZoneSize(wtfThreadData().savedReservedZoneSize());
    147136        }
     137        setOwnerThread(0);
    148138        m_lock.unlock();
    149139    }
     
    162152bool JSLock::currentThreadIsHoldingLock()
    163153{
    164     return m_lockCount && m_ownerThread == WTF::currentThread();
     154    return m_ownerThread == WTF::currentThread();
    165155}
    166156
     
    207197
    208198// This function returns the number of locks that were dropped.
    209 unsigned JSLock::dropAllLocks(SpinLock& spinLock)
    210 {
    211     ASSERT_UNUSED(spinLock, spinLock.IsHeld());
     199unsigned JSLock::dropAllLocks()
     200{
    212201    // Check if this thread is currently holding the lock.
    213202    // FIXME: Maybe we want to require this, guard with an ASSERT?
    214     unsigned locksToDrop = m_lockCount;
    215     if (!locksToDrop || m_ownerThread != WTF::currentThread())
     203    if (!currentThreadIsHoldingLock())
    216204        return 0;
    217205
     
    229217    // m_lockDropDepth is only incremented if any locks were dropped.
    230218    ++m_lockDropDepth;
     219
     220    unsigned droppedLockCount = m_lockCount;
    231221    m_lockCount = 0;
    232222    if (m_vm) {
     
    234224        m_vm->updateStackLimitWithReservedZoneSize(wtfThreadData().savedReservedZoneSize());
    235225    }
     226    setOwnerThread(0);
    236227    m_lock.unlock();
    237     return locksToDrop;
    238 }
    239 
    240 unsigned JSLock::dropAllLocksUnconditionally(SpinLock& spinLock)
    241 {
    242     ASSERT_UNUSED(spinLock, spinLock.IsHeld());
     228    return droppedLockCount;
     229}
     230
     231unsigned JSLock::dropAllLocksUnconditionally()
     232{
    243233    // Check if this thread is currently holding the lock.
    244234    // FIXME: Maybe we want to require this, guard with an ASSERT?
    245     unsigned locksToDrop = m_lockCount;
    246     if (!locksToDrop || m_ownerThread != WTF::currentThread())
     235    if (!currentThreadIsHoldingLock())
    247236        return 0;
    248237
     
    254243    // m_lockDropDepth is only incremented if any locks were dropped.
    255244    ++m_lockDropDepth;
     245
     246    unsigned droppedLockCount = m_lockCount;
    256247    m_lockCount = 0;
    257248    if (m_vm) {
     
    259250        m_vm->updateStackLimitWithReservedZoneSize(wtfThreadData().savedReservedZoneSize());
    260251    }
     252    setOwnerThread(0);
    261253    m_lock.unlock();
    262     return locksToDrop;
    263 }
    264 
    265 void JSLock::grabAllLocks(unsigned droppedLockCount, SpinLock& spinLock)
    266 {
    267     ASSERT(spinLock.IsHeld());
     254    return droppedLockCount;
     255}
     256
     257void JSLock::grabAllLocks(unsigned droppedLockCount)
     258{
    268259    // If no locks were dropped, nothing to do!
    269260    if (!droppedLockCount)
    270261        return;
    271262
    272     ThreadIdentifier currentThread = WTF::currentThread();
    273     // Check if this thread is currently holding the lock.
    274     // FIXME: Maybe we want to prohibit this, guard against with an ASSERT?
    275     if (m_ownerThread == currentThread && m_lockCount) {
    276         m_lockCount += droppedLockCount;
    277         --m_lockDropDepth;
    278         return;
    279     }
    280 
    281     spinLock.Unlock();
     263    ASSERT(!currentThreadIsHoldingLock());
     264
    282265    m_lock.lock();
    283     spinLock.Lock();
    284 
    285     m_ownerThread = currentThread;
     266
     267    setOwnerThread(WTF::currentThread());
    286268    ASSERT(!m_lockCount);
    287269    m_lockCount = droppedLockCount;
     
    300282    if (!m_vm)
    301283        return;
    302     SpinLock& spinLock = m_vm->apiLock().m_spinLock;
    303     SpinLockHolder holder(&spinLock);
    304284
    305285    if (alwaysDropLocks)
    306         m_droppedLockCount = m_vm->apiLock().dropAllLocksUnconditionally(spinLock);
     286        m_droppedLockCount = m_vm->apiLock().dropAllLocksUnconditionally();
    307287    else
    308         m_droppedLockCount = m_vm->apiLock().dropAllLocks(spinLock);
     288        m_droppedLockCount = m_vm->apiLock().dropAllLocks();
    309289}
    310290
     
    315295    if (!m_vm)
    316296        return;
    317     SpinLock& spinLock = m_vm->apiLock().m_spinLock;
    318     SpinLockHolder holder(&spinLock);
    319297
    320298    if (alwaysDropLocks)
    321         m_droppedLockCount = m_vm->apiLock().dropAllLocksUnconditionally(spinLock);
     299        m_droppedLockCount = m_vm->apiLock().dropAllLocksUnconditionally();
    322300    else
    323         m_droppedLockCount = m_vm->apiLock().dropAllLocks(spinLock);
     301        m_droppedLockCount = m_vm->apiLock().dropAllLocks();
    324302}
    325303
     
    328306    if (!m_vm)
    329307        return;
    330     SpinLock& spinLock = m_vm->apiLock().m_spinLock;
    331     SpinLockHolder holder(&spinLock);
    332     m_vm->apiLock().grabAllLocks(m_droppedLockCount, spinLock);
     308    m_vm->apiLock().grabAllLocks(m_droppedLockCount);
    333309}
    334310
  • trunk/Source/JavaScriptCore/runtime/JSLock.h

    r163730 r163804  
    2525#include <wtf/Noncopyable.h>
    2626#include <wtf/RefPtr.h>
    27 #include <wtf/TCSpinLock.h>
    2827#include <wtf/Threading.h>
    2928
     
    114113
    115114    private:
    116         unsigned dropAllLocks(SpinLock&);
    117         unsigned dropAllLocksUnconditionally(SpinLock&);
    118         void grabAllLocks(unsigned lockCount, SpinLock&);
     115        void setOwnerThread(ThreadIdentifier owner) { m_ownerThread = owner; }
    119116
    120         SpinLock m_spinLock;
     117        unsigned dropAllLocks();
     118        unsigned dropAllLocksUnconditionally();
     119        void grabAllLocks(unsigned lockCount);
     120
    121121        Mutex m_lock;
    122122        ThreadIdentifier m_ownerThread;
Note: See TracChangeset for help on using the changeset viewer.