diff --git a/layout/style/Loader.cpp b/layout/style/Loader.cpp index a58fecbf6e19..bbcdbda1c705 100644 --- a/layout/style/Loader.cpp +++ b/layout/style/Loader.cpp @@ -472,7 +472,7 @@ SheetLoadData::ScheduleLoadEventIfNeeded(nsresult aStatus) mStatus = aStatus; - nsCOMPtr thread = do_GetMainThread(); + nsCOMPtr thread = do_GetCurrentThread(); nsCOMPtr internalThread = do_QueryInterface(thread); if (NS_SUCCEEDED(internalThread->AddObserver(this))) { // Make sure to block onload here diff --git a/xpcom/threads/SynchronizedEventQueue.cpp b/xpcom/threads/SynchronizedEventQueue.cpp new file mode 100644 index 000000000000..f91546d407e0 --- /dev/null +++ b/xpcom/threads/SynchronizedEventQueue.cpp @@ -0,0 +1,31 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "SynchronizedEventQueue.h" + +using namespace mozilla; + +void +SynchronizedEventQueue::AddObserver(nsIThreadObserver* aObserver) +{ + MOZ_ASSERT(aObserver); + MOZ_ASSERT(!mEventObservers.Contains(aObserver)); + mEventObservers.AppendElement(aObserver); +} + +void +SynchronizedEventQueue::RemoveObserver(nsIThreadObserver* aObserver) +{ + MOZ_ASSERT(aObserver); + MOZ_ALWAYS_TRUE(mEventObservers.RemoveElement(aObserver)); +} + +const nsTObserverArray>& +SynchronizedEventQueue::EventObservers() +{ + return mEventObservers; +} + diff --git a/xpcom/threads/SynchronizedEventQueue.h b/xpcom/threads/SynchronizedEventQueue.h index f54f5131c4f7..d37c4d33b379 100644 --- a/xpcom/threads/SynchronizedEventQueue.h +++ b/xpcom/threads/SynchronizedEventQueue.h @@ -10,6 +10,7 @@ #include "mozilla/AlreadyAddRefed.h" #include "mozilla/AbstractEventQueue.h" #include "mozilla/Mutex.h" +#include "nsTObserverArray.h" class nsIThreadObserver; @@ -65,6 +66,10 @@ public: virtual already_AddRefed GetObserverOnThread() = 0; virtual void SetObserver(nsIThreadObserver* aObserver) = 0; + void AddObserver(nsIThreadObserver* aObserver); + void RemoveObserver(nsIThreadObserver* aObserver); + const nsTObserverArray>& EventObservers(); + virtual void EnableInputEventPrioritization() = 0; virtual void FlushInputEventPrioritization() = 0; virtual void SuspendInputEventPrioritization() = 0; @@ -72,6 +77,9 @@ public: protected: virtual ~SynchronizedEventQueue() {} + +private: + nsTObserverArray> mEventObservers; }; } // namespace mozilla diff --git a/xpcom/threads/moz.build b/xpcom/threads/moz.build index ba6d50ee4f10..2c5025e225c8 100644 --- a/xpcom/threads/moz.build +++ b/xpcom/threads/moz.build @@ -94,6 +94,7 @@ UNIFIED_SOURCES += [ 'RWLock.cpp', 'SchedulerGroup.cpp', 'SharedThreadPool.cpp', + 'SynchronizedEventQueue.cpp', 'SystemGroup.cpp', 'TaskQueue.cpp', 'ThreadEventQueue.cpp', diff --git a/xpcom/threads/nsThread.cpp b/xpcom/threads/nsThread.cpp index da90f7c0fcdc..8d71493a8992 100755 --- a/xpcom/threads/nsThread.cpp +++ b/xpcom/threads/nsThread.cpp @@ -874,11 +874,11 @@ void canary_alarm_handler(int signum) #endif -#define NOTIFY_EVENT_OBSERVERS(func_, params_) \ +#define NOTIFY_EVENT_OBSERVERS(observers_, func_, params_) \ do { \ - if (!mEventObservers.IsEmpty()) { \ - nsAutoTObserverArray>, 2>::ForwardIterator \ - iter_(mEventObservers); \ + if (!observers_.IsEmpty()) { \ + nsTObserverArray>::ForwardIterator \ + iter_(observers_); \ nsCOMPtr obs_; \ while (iter_.HasMore()) { \ obs_ = iter_.GetNext(); \ @@ -951,7 +951,7 @@ nsThread::ProcessNextEvent(bool aMayWait, bool* aResult) obs->OnProcessNextEvent(this, reallyWait); } - NOTIFY_EVENT_OBSERVERS(OnProcessNextEvent, (this, reallyWait)); + NOTIFY_EVENT_OBSERVERS(EventQueue()->EventObservers(), OnProcessNextEvent, (this, reallyWait)); #ifdef MOZ_CANARY Canary canary; @@ -1039,7 +1039,7 @@ nsThread::ProcessNextEvent(bool aMayWait, bool* aResult) } } - NOTIFY_EVENT_OBSERVERS(AfterProcessNextEvent, (this, *aResult)); + NOTIFY_EVENT_OBSERVERS(EventQueue()->EventObservers(), AfterProcessNextEvent, (this, *aResult)); if (obs) { obs->AfterProcessNextEvent(this, *aResult); @@ -1146,13 +1146,7 @@ nsThread::AddObserver(nsIThreadObserver* aObserver) return NS_ERROR_NOT_SAME_THREAD; } - NS_WARNING_ASSERTION(!mEventObservers.Contains(aObserver), - "Adding an observer twice!"); - - if (!mEventObservers.AppendElement(WrapNotNull(aObserver))) { - NS_WARNING("Out of memory!"); - return NS_ERROR_OUT_OF_MEMORY; - } + EventQueue()->AddObserver(aObserver); return NS_OK; } @@ -1164,9 +1158,7 @@ nsThread::RemoveObserver(nsIThreadObserver* aObserver) return NS_ERROR_NOT_SAME_THREAD; } - if (aObserver && !mEventObservers.RemoveElement(aObserver)) { - NS_WARNING("Removing an observer that was never added!"); - } + EventQueue()->RemoveObserver(aObserver); return NS_OK; }