@@ -1815,23 +1815,20 @@ BOOL ObjHeader::GetThreadOwningMonitorLock(DWORD *pThreadId, DWORD *pAcquisition
18151815 SyncBlock* psb = g_pSyncTable[(int )index].m_SyncBlock ;
18161816
18171817 _ASSERTE (psb->GetMonitor () != NULL );
1818- Thread* pThread = psb->GetMonitor ()->GetHoldingThread ();
1819- // If the lock is orphaned during sync block creation, pThread would be assigned -1.
1820- // Otherwise pThread would point to the owning thread if there was one or NULL if there wasn 't.
1821- if (pThread == NULL || pThread == (Thread*) -1 )
1818+ DWORD holdingThreadId = psb->GetMonitor ()->GetHoldingThreadId ();
1819+ // If the lock is orphaned during sync block creation, holdingThreadId would be assigned -1.
1820+ // Otherwise it would be the id of the holding thread if there is one or 0 if there isn 't.
1821+ if (holdingThreadId == 0 || holdingThreadId == (DWORD) -1 )
18221822 {
18231823 *pThreadId = 0 ;
18241824 *pAcquisitionCount = 0 ;
18251825 return FALSE ;
18261826 }
18271827 else
18281828 {
1829- // However, the lock might get orphaned after the sync block is created.
1830- // Therefore accessing pThread is not safe and pThread->GetThreadId() shouldn't be used.
1831- // The thread id can be obtained from the monitor, which would be the id of the thread that owned the lock.
1832- // Notice this id now could have been reused for a different thread,
1833- // but this way we avoid crashing the process and orphaned locks shouldn't be expected to work correctly anyway.
1834- *pThreadId = psb->GetMonitor ()->GetHoldingThreadId ();
1829+ // Notice this id now could have been reused for a different thread (in case the lock was orphaned),
1830+ // but orphaned locks shouldn't be expected to work correctly anyway.
1831+ *pThreadId = holdingThreadId;
18351832 *pAcquisitionCount = psb->GetMonitor ()->GetRecursionLevel ();
18361833 return TRUE ;
18371834 }
@@ -2203,7 +2200,6 @@ SyncBlock *ObjHeader::GetSyncBlock()
22032200 if (pThread == NULL )
22042201 {
22052202 // The lock is orphaned.
2206- pThread = (Thread*) -1 ;
22072203 threadId = -1 ;
22082204 osThreadId = (SIZE_T)-1 ;
22092205 }
@@ -2213,7 +2209,7 @@ SyncBlock *ObjHeader::GetSyncBlock()
22132209 osThreadId = pThread->GetOSThreadId64 ();
22142210 }
22152211
2216- syncBlock->InitState (recursionLevel + 1 , pThread, threadId, osThreadId);
2212+ syncBlock->InitState (recursionLevel + 1 , threadId, osThreadId);
22172213 }
22182214 }
22192215 else if ((bits & BIT_SBLK_IS_HASHCODE) != 0 )
@@ -2334,6 +2330,17 @@ void ObjHeader::PulseAll()
23342330//
23352331// ***************************************************************************
23362332
2333+ #endif // !DACCESS_COMPILE
2334+
2335+ // the DAC needs to have this method available
2336+ PTR_Thread AwareLock::GetHoldingThread ()
2337+ {
2338+ LIMITED_METHOD_CONTRACT;
2339+ return g_pThinLockThreadIdDispenser->IdToThreadWithValidation (m_HoldingThreadId);
2340+ }
2341+
2342+ #ifndef DACCESS_COMPILE
2343+
23372344void AwareLock::AllocLockSemEvent ()
23382345{
23392346 CONTRACTL
@@ -2371,12 +2378,11 @@ void AwareLock::Enter()
23712378
23722379 Thread *pCurThread = GetThread ();
23732380 LockState state = m_lockState.VolatileLoadWithoutBarrier ();
2374- if (!state.IsLocked () || m_HoldingThread != pCurThread)
2381+ if (!state.IsLocked () || m_HoldingThreadId != pCurThread-> GetThreadId () )
23752382 {
23762383 if (m_lockState.InterlockedTryLock_Or_RegisterWaiter (this , state))
23772384 {
23782385 // We get here if we successfully acquired the mutex.
2379- m_HoldingThread = pCurThread;
23802386 m_HoldingThreadId = pCurThread->GetThreadId ();
23812387 m_HoldingOSThreadId = pCurThread->GetOSThreadId64 ();
23822388 m_Recursion = 1 ;
@@ -2433,14 +2439,13 @@ BOOL AwareLock::TryEnter(INT32 timeOut)
24332439 }
24342440
24352441 LockState state = m_lockState.VolatileLoadWithoutBarrier ();
2436- if (!state.IsLocked () || m_HoldingThread != pCurThread)
2442+ if (!state.IsLocked () || m_HoldingThreadId != pCurThread-> GetThreadId () )
24372443 {
24382444 if (timeOut == 0
24392445 ? m_lockState.InterlockedTryLock (state)
24402446 : m_lockState.InterlockedTryLock_Or_RegisterWaiter (this , state))
24412447 {
24422448 // We get here if we successfully acquired the mutex.
2443- m_HoldingThread = pCurThread;
24442449 m_HoldingThreadId = pCurThread->GetThreadId ();
24452450 m_HoldingOSThreadId = pCurThread->GetOSThreadId64 ();
24462451 m_Recursion = 1 ;
@@ -2720,7 +2725,6 @@ BOOL AwareLock::EnterEpilogHelper(Thread* pCurThread, INT32 timeOut)
27202725 return false ;
27212726 }
27222727
2723- m_HoldingThread = pCurThread;
27242728 m_HoldingThreadId = pCurThread->GetThreadId ();
27252729 m_HoldingOSThreadId = pCurThread->GetOSThreadId64 ();
27262730 m_Recursion = 1 ;
@@ -2783,7 +2787,7 @@ LONG AwareLock::LeaveCompletely()
27832787BOOL AwareLock::OwnedByCurrentThread ()
27842788{
27852789 WRAPPER_NO_CONTRACT;
2786- return (GetThread () == m_HoldingThread );
2790+ return (GetThread ()-> GetThreadId () == m_HoldingThreadId );
27872791}
27882792
27892793// ***************************************************************************
0 commit comments