Bug 1364821 - Introduce AbstractThread::AutoEnter and remove main thread's AbstractThread::GetCurrent() (r=jwwang)

MozReview-Commit-ID: FSLVn7xe3sq
This commit is contained in:
Bill McCloskey 2017-07-31 14:29:07 -07:00
Родитель a38177ad5a
Коммит 2273c53b75
6 изменённых файлов: 72 добавлений и 1 удалений

Просмотреть файл

@ -508,6 +508,8 @@ HTMLMediaElement::MediaLoadListener::OnStartRequest(nsIRequest* aRequest,
RefPtr<HTMLMediaElement> element;
element.swap(mElement);
AbstractThread::AutoEnter context(element->AbstractMainThread());
if (mLoadID != element->GetCurrentLoadID()) {
// The channel has been cancelled before we had a chance to create
// a decoder. Abort, don't dispatch an "error" event, as the new load
@ -2471,6 +2473,8 @@ void HTMLMediaElement::UpdatePreloadAction()
nsresult HTMLMediaElement::LoadResource()
{
AbstractThread::AutoEnter context(AbstractMainThread());
NS_ASSERTION(mDelayingLoadEvent,
"Should delay load event (if in document) during load");

Просмотреть файл

@ -291,6 +291,7 @@ MediaDecoder::NotifyOwnerActivityChanged(bool aIsDocumentVisible,
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
AbstractThread::AutoEnter context(AbstractMainThread());
SetElementVisibility(aIsDocumentVisible, aElementVisibility, aIsElementInTree);
NotifyCompositor();
@ -301,6 +302,7 @@ MediaDecoder::Pause()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
AbstractThread::AutoEnter context(AbstractMainThread());
if (mPlayState == PLAY_STATE_LOADING || IsEnded()) {
mNextState = PLAY_STATE_PAUSED;
return;
@ -312,6 +314,7 @@ void
MediaDecoder::SetVolume(double aVolume)
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
mVolume = aVolume;
}
@ -321,6 +324,7 @@ MediaDecoder::AddOutputStream(ProcessedMediaStream* aStream,
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mDecoderStateMachine, "Must be called after Load().");
AbstractThread::AutoEnter context(AbstractMainThread());
mDecoderStateMachine->AddOutputStream(aStream, aFinishWhenEnded);
}
@ -329,6 +333,7 @@ MediaDecoder::RemoveOutputStream(MediaStream* aStream)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mDecoderStateMachine, "Must be called after Load().");
AbstractThread::AutoEnter context(AbstractMainThread());
mDecoderStateMachine->RemoveOutputStream(aStream);
}
@ -336,6 +341,7 @@ double
MediaDecoder::GetDuration()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
return mDuration;
}
@ -344,6 +350,7 @@ MediaDecoder::SetInfinite(bool aInfinite)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
AbstractThread::AutoEnter context(AbstractMainThread());
mInfiniteStream = aInfinite;
DurationChanged();
}
@ -352,6 +359,7 @@ bool
MediaDecoder::IsInfinite() const
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
return mInfiniteStream;
}
@ -444,6 +452,7 @@ MediaDecoder::Shutdown()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
AbstractThread::AutoEnter context(AbstractMainThread());
// Unwatch all watch targets to prevent further notifications.
mWatchManager.Shutdown();
@ -597,6 +606,7 @@ MediaDecoder::InitializeStateMachine()
{
MOZ_ASSERT(NS_IsMainThread());
NS_ASSERTION(mDecoderStateMachine, "Cannot initialize null state machine!");
AbstractThread::AutoEnter context(AbstractMainThread());
nsresult rv = mDecoderStateMachine->Init(this);
NS_ENSURE_SUCCESS(rv, rv);
@ -645,6 +655,7 @@ MediaDecoder::Play()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
NS_ASSERTION(mDecoderStateMachine != nullptr, "Should have state machine.");
if (mPlaybackRate == 0) {
return NS_OK;
@ -667,6 +678,7 @@ MediaDecoder::Seek(double aTime, SeekTarget::Type aSeekType)
MOZ_ASSERT(NS_IsMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
AbstractThread::AutoEnter context(AbstractMainThread());
MOZ_ASSERT(aTime >= 0.0, "Cannot seek to a negative value.");
int64_t timeUsecs = TimeUnit::FromSeconds(aTime).ToMicroseconds();
@ -688,6 +700,7 @@ void
MediaDecoder::DiscardOngoingSeekIfExists()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
mSeekRequest.DisconnectIfExists();
GetOwner()->AsyncRejectSeekDOMPromiseIfExists();
}
@ -696,6 +709,7 @@ void
MediaDecoder::CallSeek(const SeekTarget& aTarget)
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
DiscardOngoingSeekIfExists();
mDecoderStateMachine->InvokeSeek(aTarget)
@ -708,6 +722,7 @@ double
MediaDecoder::GetCurrentTime()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
return mLogicalPosition;
}
@ -715,6 +730,7 @@ already_AddRefed<nsIPrincipal>
MediaDecoder::GetCurrentPrincipal()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
return mResource ? mResource->GetCurrentPrincipal() : nullptr;
}
@ -722,6 +738,7 @@ void
MediaDecoder::OnMetadataUpdate(TimedMetadata&& aMetadata)
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
GetOwner()->RemoveMediaTracks();
MetadataLoaded(MakeUnique<MediaInfo>(*aMetadata.mInfo),
UniquePtr<MetadataTags>(aMetadata.mTags.forget()),
@ -736,6 +753,7 @@ MediaDecoder::MetadataLoaded(UniquePtr<MediaInfo> aInfo,
MediaDecoderEventVisibility aEventVisibility)
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
LOG("MetadataLoaded, channels=%u rate=%u hasAudio=%d hasVideo=%d",
@ -773,6 +791,7 @@ void
MediaDecoder::EnsureTelemetryReported()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
if (mTelemetryReported || !mInfo) {
// Note: sometimes we get multiple MetadataLoaded calls (for example
@ -806,6 +825,7 @@ const char*
MediaDecoder::PlayStateStr()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
return ToPlayStateStr(mPlayState);
}
@ -815,6 +835,7 @@ MediaDecoder::FirstFrameLoaded(nsAutoPtr<MediaInfo> aInfo,
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
AbstractThread::AutoEnter context(AbstractMainThread());
LOG("FirstFrameLoaded, channels=%u rate=%u hasAudio=%d hasVideo=%d "
"mPlayState=%s",
@ -867,6 +888,7 @@ void
MediaDecoder::UpdateSameOriginStatus(bool aSameOrigin)
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
mSameOriginMedia = aSameOrigin;
}
@ -990,6 +1012,7 @@ MediaDecoder::NotifySuspendedStatusChanged()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
AbstractThread::AutoEnter context(AbstractMainThread());
if (mResource) {
bool suspended = mResource->IsSuspendedByCache();
GetOwner()->NotifySuspendedByCache(suspended);
@ -1043,6 +1066,7 @@ MediaDecoder::NotifyDownloadEnded(nsresult aStatus)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
AbstractThread::AutoEnter context(AbstractMainThread());
LOG("NotifyDownloadEnded, status=%" PRIx32, static_cast<uint32_t>(aStatus));
@ -1069,6 +1093,7 @@ MediaDecoder::NotifyPrincipalChanged()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
AbstractThread::AutoEnter context(AbstractMainThread());
nsCOMPtr<nsIPrincipal> newPrincipal = GetCurrentPrincipal();
mMediaPrincipalHandle = MakePrincipalHandle(newPrincipal);
GetOwner()->NotifyDecoderPrincipalChanged();
@ -1079,6 +1104,7 @@ MediaDecoder::NotifyBytesConsumed(int64_t aBytes, int64_t aOffset)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
AbstractThread::AutoEnter context(AbstractMainThread());
if (mIgnoreProgressData) {
return;
@ -1096,6 +1122,7 @@ MediaDecoder::OnSeekResolved()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
AbstractThread::AutoEnter context(AbstractMainThread());
mSeekRequest.Complete();
{
@ -1134,6 +1161,7 @@ MediaDecoder::ChangeState(PlayState aState)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!IsShutdown(), "SHUTDOWN is the final state.");
AbstractThread::AutoEnter context(AbstractMainThread());
if (mNextState == aState) {
mNextState = PLAY_STATE_PAUSED;
@ -1178,6 +1206,7 @@ MediaDecoder::DurationChanged()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
AbstractThread::AutoEnter context(AbstractMainThread());
double oldDuration = mDuration;
if (IsInfinite()) {
@ -1269,6 +1298,9 @@ MediaDecoder::SetSuspendTaint(bool aTainted)
void
MediaDecoder::UpdateVideoDecodeMode()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(mAbstractMainThread);
// The MDSM may yet be set.
if (!mDecoderStateMachine) {
LOG("UpdateVideoDecodeMode(), early return because we don't have MDSM.");
@ -1414,6 +1446,7 @@ void
MediaDecoder::SetPlaybackRate(double aPlaybackRate)
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
double oldRate = mPlaybackRate;
mPlaybackRate = aPlaybackRate;
@ -1438,6 +1471,7 @@ void
MediaDecoder::SetPreservesPitch(bool aPreservesPitch)
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
mPreservesPitch = aPreservesPitch;
}
@ -1445,6 +1479,7 @@ void
MediaDecoder::SetLooping(bool aLooping)
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
mLooping = aLooping;
}

Просмотреть файл

@ -3152,6 +3152,7 @@ void MediaDecoderStateMachine::PlayStateChanged()
void MediaDecoderStateMachine::SetVideoDecodeMode(VideoDecodeMode aMode)
{
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIRunnable> r = NewRunnableMethod<VideoDecodeMode>(
"MediaDecoderStateMachine::SetVideoDecodeModeInternal",
this,

Просмотреть файл

@ -53,6 +53,7 @@ MediaSourceDecoder::Load(nsIPrincipal* aPrincipal)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!GetStateMachine());
AbstractThread::AutoEnter context(AbstractMainThread());
mResource = new MediaSourceResource(aPrincipal);
@ -78,6 +79,7 @@ media::TimeIntervals
MediaSourceDecoder::GetSeekable()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
if (!mMediaSource) {
NS_WARNING("MediaSource element isn't attached");
return media::TimeIntervals::Invalid();
@ -119,6 +121,7 @@ media::TimeIntervals
MediaSourceDecoder::GetBuffered()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
if (!mMediaSource) {
NS_WARNING("MediaSource element isn't attached");
@ -164,6 +167,7 @@ void
MediaSourceDecoder::Shutdown()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
MSE_DEBUG("Shutdown");
// Detach first so that TrackBuffers are unused on the main thread when
// shut down on the decode task queue.
@ -193,6 +197,7 @@ void
MediaSourceDecoder::Ended(bool aEnded)
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
static_cast<MediaSourceResource*>(mResource.get())->SetEnded(aEnded);
if (aEnded) {
// We want the MediaSourceReader to refresh its buffered range as it may
@ -206,6 +211,7 @@ void
MediaSourceDecoder::AddSizeOfResources(ResourceSizes* aSizes)
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
if (GetDemuxer()) {
GetDemuxer()->AddSizeOfResources(aSizes);
}
@ -215,6 +221,7 @@ void
MediaSourceDecoder::SetInitialDuration(int64_t aDuration)
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
// Only use the decoded duration if one wasn't already
// set.
if (!mMediaSource || !IsNaN(ExplicitDuration())) {
@ -232,6 +239,7 @@ void
MediaSourceDecoder::SetMediaSourceDuration(double aDuration)
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
MOZ_ASSERT(!IsShutdown());
if (aDuration >= 0) {
int64_t checkedDuration;
@ -259,6 +267,7 @@ double
MediaSourceDecoder::GetDuration()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
return ExplicitDuration();
}
@ -266,6 +275,7 @@ MediaDecoderOwner::NextFrameStatus
MediaSourceDecoder::NextFrameBufferedStatus()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
if (!mMediaSource
|| mMediaSource->ReadyState() == dom::MediaSourceReadyState::Closed) {
@ -289,6 +299,7 @@ bool
MediaSourceDecoder::CanPlayThrough()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
if (NextFrameBufferedStatus() == MediaDecoderOwner::NEXT_FRAME_UNAVAILABLE) {
return false;
@ -321,6 +332,7 @@ TimeInterval
MediaSourceDecoder::ClampIntervalToEnd(const TimeInterval& aInterval)
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
if (!mEnded) {
return aInterval;
@ -338,6 +350,7 @@ void
MediaSourceDecoder::NotifyInitDataArrived()
{
MOZ_ASSERT(NS_IsMainThread());
AbstractThread::AutoEnter context(AbstractMainThread());
if (mDemuxer) {
mDemuxer->NotifyInitDataArrived();

Просмотреть файл

@ -90,6 +90,8 @@ public:
void FireTailDispatcher()
{
AutoEnter context(this);
MOZ_DIAGNOSTIC_ASSERT(mTailDispatcher.isSome());
mTailDispatcher.ref().DrainDirectTasks();
mTailDispatcher.reset();
@ -315,7 +317,6 @@ AbstractThread::InitMainThread()
if (!sCurrentThreadTLS.init()) {
MOZ_CRASH();
}
sCurrentThreadTLS.set(sMainThread);
}
void

Просмотреть файл

@ -124,6 +124,23 @@ public:
MOZ_CRASH("Not support!");
}
struct AutoEnter
{
AutoEnter(AbstractThread* aThread)
{
mLastCurrentThread = sCurrentThreadTLS.get();
sCurrentThreadTLS.set(aThread);
}
~AutoEnter()
{
sCurrentThreadTLS.set(mLastCurrentThread);
}
private:
AbstractThread* mLastCurrentThread = nullptr;
};
protected:
virtual ~AbstractThread() {}
static MOZ_THREAD_LOCAL(AbstractThread*) sCurrentThreadTLS;