Changeset 163804 in webkit
- Timestamp:
- Feb 10, 2014, 11:39:30 AM (11 years ago)
- Location:
- trunk/Source/JavaScriptCore
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/Source/JavaScriptCore/ChangeLog ¶
r163803 r163804 1 2014-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 1 81 2014-02-10 Filip Pizlo <fpizlo@apple.com> 2 82 -
TabularUnified trunk/Source/JavaScriptCore/runtime/JSLock.cpp ¶
r163730 r163804 28 28 #include "Operations.h" 29 29 30 #if USE(PTHREADS)31 #include <pthread.h>32 #endif33 34 30 namespace JSC { 35 31 … … 91 87 , m_vm(vm) 92 88 { 93 m_spinLock.Init();94 89 } 95 90 … … 107 102 { 108 103 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; 115 107 } 116 108 117 109 m_lock.lock(); 118 110 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()); 134 124 } 135 125 136 126 void JSLock::unlock() 137 127 { 138 SpinLockHolder holder(&m_spinLock); 139 ASSERT(currentThreadIsHoldingLock()); 128 RELEASE_ASSERT(currentThreadIsHoldingLock()); 140 129 141 130 m_lockCount--; … … 146 135 m_vm->updateStackLimitWithReservedZoneSize(wtfThreadData().savedReservedZoneSize()); 147 136 } 137 setOwnerThread(0); 148 138 m_lock.unlock(); 149 139 } … … 162 152 bool JSLock::currentThreadIsHoldingLock() 163 153 { 164 return m_ lockCount && m_ownerThread == WTF::currentThread();154 return m_ownerThread == WTF::currentThread(); 165 155 } 166 156 … … 207 197 208 198 // This function returns the number of locks that were dropped. 209 unsigned JSLock::dropAllLocks(SpinLock& spinLock) 210 { 211 ASSERT_UNUSED(spinLock, spinLock.IsHeld()); 199 unsigned JSLock::dropAllLocks() 200 { 212 201 // Check if this thread is currently holding the lock. 213 202 // 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()) 216 204 return 0; 217 205 … … 229 217 // m_lockDropDepth is only incremented if any locks were dropped. 230 218 ++m_lockDropDepth; 219 220 unsigned droppedLockCount = m_lockCount; 231 221 m_lockCount = 0; 232 222 if (m_vm) { … … 234 224 m_vm->updateStackLimitWithReservedZoneSize(wtfThreadData().savedReservedZoneSize()); 235 225 } 226 setOwnerThread(0); 236 227 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 231 unsigned JSLock::dropAllLocksUnconditionally() 232 { 243 233 // Check if this thread is currently holding the lock. 244 234 // 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()) 247 236 return 0; 248 237 … … 254 243 // m_lockDropDepth is only incremented if any locks were dropped. 255 244 ++m_lockDropDepth; 245 246 unsigned droppedLockCount = m_lockCount; 256 247 m_lockCount = 0; 257 248 if (m_vm) { … … 259 250 m_vm->updateStackLimitWithReservedZoneSize(wtfThreadData().savedReservedZoneSize()); 260 251 } 252 setOwnerThread(0); 261 253 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 257 void JSLock::grabAllLocks(unsigned droppedLockCount) 258 { 268 259 // If no locks were dropped, nothing to do! 269 260 if (!droppedLockCount) 270 261 return; 271 262 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 282 265 m_lock.lock(); 283 spinLock.Lock(); 284 285 m_ownerThread = currentThread; 266 267 setOwnerThread(WTF::currentThread()); 286 268 ASSERT(!m_lockCount); 287 269 m_lockCount = droppedLockCount; … … 300 282 if (!m_vm) 301 283 return; 302 SpinLock& spinLock = m_vm->apiLock().m_spinLock;303 SpinLockHolder holder(&spinLock);304 284 305 285 if (alwaysDropLocks) 306 m_droppedLockCount = m_vm->apiLock().dropAllLocksUnconditionally( spinLock);286 m_droppedLockCount = m_vm->apiLock().dropAllLocksUnconditionally(); 307 287 else 308 m_droppedLockCount = m_vm->apiLock().dropAllLocks( spinLock);288 m_droppedLockCount = m_vm->apiLock().dropAllLocks(); 309 289 } 310 290 … … 315 295 if (!m_vm) 316 296 return; 317 SpinLock& spinLock = m_vm->apiLock().m_spinLock;318 SpinLockHolder holder(&spinLock);319 297 320 298 if (alwaysDropLocks) 321 m_droppedLockCount = m_vm->apiLock().dropAllLocksUnconditionally( spinLock);299 m_droppedLockCount = m_vm->apiLock().dropAllLocksUnconditionally(); 322 300 else 323 m_droppedLockCount = m_vm->apiLock().dropAllLocks( spinLock);301 m_droppedLockCount = m_vm->apiLock().dropAllLocks(); 324 302 } 325 303 … … 328 306 if (!m_vm) 329 307 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); 333 309 } 334 310 -
TabularUnified trunk/Source/JavaScriptCore/runtime/JSLock.h ¶
r163730 r163804 25 25 #include <wtf/Noncopyable.h> 26 26 #include <wtf/RefPtr.h> 27 #include <wtf/TCSpinLock.h>28 27 #include <wtf/Threading.h> 29 28 … … 114 113 115 114 private: 116 unsigned dropAllLocks(SpinLock&); 117 unsigned dropAllLocksUnconditionally(SpinLock&); 118 void grabAllLocks(unsigned lockCount, SpinLock&); 115 void setOwnerThread(ThreadIdentifier owner) { m_ownerThread = owner; } 119 116 120 SpinLock m_spinLock; 117 unsigned dropAllLocks(); 118 unsigned dropAllLocksUnconditionally(); 119 void grabAllLocks(unsigned lockCount); 120 121 121 Mutex m_lock; 122 122 ThreadIdentifier m_ownerThread;
Note:
See TracChangeset
for help on using the changeset viewer.