Skip to content

Commit 1b077c2

Browse files
author
bsmedberg%covad.net
committed
Bug 316414 part 1, try mozilla#2 - Add xpcom-shutdown-threads and xpcom-shutdown-loaders notifications, r=darin
1 parent 5a4c72c commit 1b077c2

File tree

6 files changed

+126
-174
lines changed

6 files changed

+126
-174
lines changed

xpcom/build/nsXPComInit.cpp

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272

7373
#include "nsEventQueueService.h"
7474
#include "nsEventQueue.h"
75+
#include "nsEventQueueUtils.h"
7576

7677
#include "nsIProxyObjectManager.h"
7778
#include "nsProxyEventPrivate.h" // access to the impl of nsProxyObjectManager for the generic factory registration.
@@ -142,7 +143,6 @@ extern void _FreeAutoLockStatics();
142143

143144
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
144145
static NS_DEFINE_CID(kMemoryCID, NS_MEMORY_CID);
145-
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
146146
static NS_DEFINE_CID(kINIParserFactoryCID, NS_INIPARSERFACTORY_CID);
147147

148148
NS_GENERIC_FACTORY_CONSTRUCTOR(nsProcess)
@@ -773,11 +773,15 @@ NS_UnregisterXPCOMExitRoutine(XPCOMExitRoutine exitRoutine)
773773
//
774774
// The shutdown sequence for xpcom would be
775775
//
776+
// - Notify "xpcom-shutdown" for modules to release primary (root) references
777+
// - Notify "xpcom-shutdown-threads" for thread joins
778+
// - Shutdown the main event queue (TODO)
776779
// - Release the Global Service Manager
777780
// - Release all service instances held by the global service manager
778781
// - Release the Global Service Manager itself
779782
// - Release the Component Manager
780783
// - Release all factories cached by the Component Manager
784+
// - Notify module loaders to shut down
781785
// - Unload Libraries
782786
// - Release Contractid Cache held by Component Manager
783787
// - Release dll abstraction held by Component Manager
@@ -787,37 +791,52 @@ NS_UnregisterXPCOMExitRoutine(XPCOMExitRoutine exitRoutine)
787791
EXPORT_XPCOM_API(nsresult)
788792
NS_ShutdownXPCOM(nsIServiceManager* servMgr)
789793
{
794+
nsresult rv;
795+
796+
// grab the event queue so that we can process events before exiting.
797+
nsCOMPtr <nsIEventQueue> currentQ;
798+
NS_GetCurrentEventQ(getter_AddRefs(currentQ));
799+
800+
nsCOMPtr<nsISimpleEnumerator> moduleLoaders;
790801

791802
// Notify observers of xpcom shutting down
792-
nsresult rv = NS_OK;
793803
{
794804
// Block it so that the COMPtr will get deleted before we hit
795805
// servicemanager shutdown
796806
nsCOMPtr<nsIObserverService> observerService =
797-
do_GetService("@mozilla.org/observer-service;1", &rv);
798-
if (NS_SUCCEEDED(rv))
807+
do_GetService("@mozilla.org/observer-service;1");
808+
809+
if (observerService)
799810
{
800811
nsCOMPtr<nsIServiceManager> mgr;
801812
rv = NS_GetServiceManager(getter_AddRefs(mgr));
802813
if (NS_SUCCEEDED(rv))
803814
{
804-
(void) observerService->NotifyObservers(mgr,
805-
NS_XPCOM_SHUTDOWN_OBSERVER_ID,
806-
nsnull);
815+
(void) observerService->
816+
NotifyObservers(mgr, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
817+
nsnull);
807818
}
808819
}
809-
}
810820

811-
// grab the event queue so that we can process events one last time before exiting
812-
nsCOMPtr <nsIEventQueue> currentQ;
813-
{
814-
nsCOMPtr<nsIEventQueueService> eventQService =
815-
do_GetService(kEventQueueServiceCID, &rv);
821+
if (currentQ)
822+
currentQ->ProcessPendingEvents();
816823

817-
if (eventQService) {
818-
eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(currentQ));
819-
}
824+
if (observerService)
825+
(void) observerService->
826+
NotifyObservers(nsnull, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID,
827+
nsnull);
828+
829+
if (currentQ)
830+
currentQ->ProcessPendingEvents();
831+
832+
// We save the "xpcom-shutdown-loaders" observers to notify after
833+
// the observerservice is gone.
834+
if (observerService)
835+
observerService->
836+
EnumerateObservers(NS_XPCOM_SHUTDOWN_LOADERS_OBSERVER_ID,
837+
getter_AddRefs(moduleLoaders));
820838
}
839+
821840
// XPCOM is officially in shutdown mode NOW
822841
// Set this only after the observers have been notified as this
823842
// will cause servicemanager to become inaccessible.
@@ -845,6 +864,27 @@ NS_ShutdownXPCOM(nsIServiceManager* servMgr)
845864
// Release the directory service
846865
NS_IF_RELEASE(nsDirectoryService::gService);
847866

867+
if (moduleLoaders) {
868+
PRBool more;
869+
nsCOMPtr<nsISupports> el;
870+
while (NS_SUCCEEDED(moduleLoaders->HasMoreElements(&more)) &&
871+
more) {
872+
moduleLoaders->GetNext(getter_AddRefs(el));
873+
874+
// Don't worry about weak-reference observers here: there is
875+
// no reason for weak-ref observers to register for
876+
// xpcom-shutdown-loaders
877+
878+
nsCOMPtr<nsIObserver> obs(do_QueryInterface(el));
879+
if (obs)
880+
(void) obs->Observe(nsnull,
881+
NS_XPCOM_SHUTDOWN_LOADERS_OBSERVER_ID,
882+
nsnull);
883+
}
884+
885+
moduleLoaders = nsnull;
886+
}
887+
848888
// Shutdown nsLocalFile string conversion
849889
NS_ShutdownLocalFile();
850890
#ifdef XP_UNIX

xpcom/ds/nsArrayEnumerator.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ class nsCOMArrayEnumerator : public nsISimpleEnumerator
112112
}
113113

114114
// specialized operator to make sure we make room for mValues
115-
void* operator new (size_t size, const nsCOMArray_base& aArray) CPP_THROW_NEW;
115+
void* operator new (size_t size, const nsCOMArray_base& aArray,
116+
PRBool aReverse) CPP_THROW_NEW;
116117
void operator delete(void* ptr) {
117118
::operator delete(ptr);
118119
}
@@ -172,7 +173,8 @@ nsCOMArrayEnumerator::GetNext(nsISupports** aResult)
172173
}
173174

174175
void*
175-
nsCOMArrayEnumerator::operator new (size_t size, const nsCOMArray_base& aArray)
176+
nsCOMArrayEnumerator::operator new (size_t size, const nsCOMArray_base& aArray,
177+
PRBool aReverse)
176178
CPP_THROW_NEW
177179
{
178180
// create enough space such that mValueArray points to a large
@@ -184,25 +186,32 @@ nsCOMArrayEnumerator::operator new (size_t size, const nsCOMArray_base& aArray)
184186
nsCOMArrayEnumerator * result =
185187
NS_STATIC_CAST(nsCOMArrayEnumerator*, ::operator new(size));
186188

189+
if (!result)
190+
return result;
191+
187192
// now need to copy over the values, and addref each one
188193
// now this might seem like a lot of work, but we're actually just
189194
// doing all our AddRef's ahead of time since GetNext() doesn't
190195
// need to AddRef() on the way out
191-
PRUint32 i;
192196
PRUint32 max = result->mArraySize = aArray.Count();
193-
for (i = 0; i<max; i++) {
194-
result->mValueArray[i] = aArray[i];
195-
NS_IF_ADDREF(result->mValueArray[i]);
197+
PRUint32 cur = aReverse ? max - 1 : 0;
198+
PRUint32 incr = aReverse ? -1 : 1;
199+
200+
for (PRUint32 i = 0; i < max; ++i, cur += incr) {
201+
result->mValueArray[cur] = aArray[i];
202+
NS_IF_ADDREF(result->mValueArray[cur]);
196203
}
197204

198205
return result;
199206
}
200207

201208
extern NS_COM nsresult
202209
NS_NewArrayEnumerator(nsISimpleEnumerator* *aResult,
203-
const nsCOMArray_base& aArray)
210+
const nsCOMArray_base& aArray,
211+
PRBool aReverse)
204212
{
205-
nsCOMArrayEnumerator *enumerator = new (aArray) nsCOMArrayEnumerator();
213+
nsCOMArrayEnumerator *enumerator =
214+
new (aArray, aReverse) nsCOMArrayEnumerator();
206215
if (!enumerator) return NS_ERROR_OUT_OF_MEMORY;
207216

208217
NS_ADDREF(*aResult = enumerator);

xpcom/ds/nsArrayEnumerator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ NS_NewArrayEnumerator(nsISimpleEnumerator* *result,
8282
// without its objects going away.
8383
extern NS_COM nsresult
8484
NS_NewArrayEnumerator(nsISimpleEnumerator* *aResult,
85-
const nsCOMArray_base& aArray);
85+
const nsCOMArray_base& aArray,
86+
PRBool aReverse = PR_FALSE);
8687

8788
#endif

0 commit comments

Comments
 (0)