Bug 1698697 - Fix BlockingResourceBase code with callstack tracking enabled. r=nika

Since GetAcquisitionState is never used independently of
ClearAcquisitionState, we change it to TakeAcquisitionState, such that
we don't require an expensive array copy in this case.

Differential Revision: https://phabricator.services.mozilla.com/D108554
This commit is contained in:
Mike Hommey 2021-03-16 05:18:10 +00:00
parent 2cc3b39bc9
commit 85aac3e34e
2 changed files with 20 additions and 12 deletions

View File

@ -269,7 +269,8 @@ void BlockingResourceBase::Acquire() {
MOZ_ASSERT(IsAcquired());
if (!mFirstSeen) {
mFirstSeen = mAcquired;
mFirstSeen = mAcquired.map(
[](AcquisitionState::ValueType& state) { return state.Clone(); });
}
# endif
}
@ -423,10 +424,9 @@ nsresult ReentrantMonitor::Wait(PRIntervalTime aInterval) {
// save monitor state and reset it to empty
int32_t savedEntryCount = mEntryCount;
AcquisitionState savedAcquisitionState = GetAcquisitionState();
AcquisitionState savedAcquisitionState = TakeAcquisitionState();
BlockingResourceBase* savedChainPrev = mChainPrev;
mEntryCount = 0;
ClearAcquisitionState();
mChainPrev = 0;
nsresult rv;
@ -441,7 +441,7 @@ nsresult ReentrantMonitor::Wait(PRIntervalTime aInterval) {
// restore saved state
mEntryCount = savedEntryCount;
SetAcquisitionState(savedAcquisitionState);
SetAcquisitionState(std::move(savedAcquisitionState));
mChainPrev = savedChainPrev;
return rv;
@ -513,10 +513,9 @@ CVStatus OffTheBooksCondVar::Wait(TimeDuration aDuration) {
AssertCurrentThreadOwnsMutex();
// save mutex state and reset to empty
AcquisitionState savedAcquisitionState = mLock->GetAcquisitionState();
AcquisitionState savedAcquisitionState = mLock->TakeAcquisitionState();
BlockingResourceBase* savedChainPrev = mLock->mChainPrev;
PRThread* savedOwningThread = mLock->mOwningThread;
mLock->ClearAcquisitionState();
mLock->mChainPrev = 0;
mLock->mOwningThread = nullptr;
@ -530,7 +529,7 @@ CVStatus OffTheBooksCondVar::Wait(TimeDuration aDuration) {
}
// restore saved state
mLock->SetAcquisitionState(savedAcquisitionState);
mLock->SetAcquisitionState(std::move(savedAcquisitionState));
mLock->mChainPrev = savedChainPrev;
mLock->mOwningThread = savedOwningThread;

View File

@ -193,12 +193,21 @@ class BlockingResourceBase {
} // NS_NEEDS_RESOURCE(this)
/**
* GetAcquisitionState
* Return whether or not this resource was acquired.
* TakeAcquisitionState
* Return whether or not this resource was acquired and mark the resource
* as not acquired for subsequent uses.
*
* *NOT* thread safe. Requires ownership of underlying resource.
*/
AcquisitionState GetAcquisitionState() { return mAcquired; }
AcquisitionState TakeAcquisitionState() {
# ifdef MOZ_CALLSTACK_DISABLED
bool acquired = mAcquired;
ClearAcquisitionState();
return acquired;
# else
return mAcquired.take();
# endif
}
/**
* SetAcquisitionState
@ -206,8 +215,8 @@ class BlockingResourceBase {
*
* *NOT* thread safe. Requires ownership of underlying resource.
*/
void SetAcquisitionState(const AcquisitionState& aAcquisitionState) {
mAcquired = aAcquisitionState;
void SetAcquisitionState(AcquisitionState&& aAcquisitionState) {
mAcquired = std::move(aAcquisitionState);
}
/**