threadmanager-shutdown-norecreate
author Benjamin Smedberg <benjamin@smedbergs.us>
Sat, 26 Jul 2008 22:49:39 -0400
changeset 167 a4da40849f5436e629c5732f4368c6c48189637f
parent 95 b35cbfa6ad4f8bac97b5a0ae2a3bfc18ed88de35
permissions -rw-r--r--
State as of now

* * *

diff --git a/xpcom/build/nsXPComInit.cpp b/xpcom/build/nsXPComInit.cpp
--- a/xpcom/build/nsXPComInit.cpp
+++ b/xpcom/build/nsXPComInit.cpp
@@ -516,7 +516,7 @@ NS_InitXPCOM3(nsIServiceManager* *result
                                 &dummy);
 
     // Establish the main thread here.
-    rv = nsThreadManager::get()->Init();
+    rv = nsThreadManager::create()->Init();
     if (NS_FAILED(rv)) return rv;
 
     // Set up the timer globals/timer thread
diff --git a/xpcom/glue/nsThreadUtils.cpp b/xpcom/glue/nsThreadUtils.cpp
--- a/xpcom/glue/nsThreadUtils.cpp
+++ b/xpcom/glue/nsThreadUtils.cpp
@@ -60,17 +60,17 @@ NS_METHOD
 NS_METHOD
 NS_NewThread(nsIThread **result, nsIRunnable *event)
 {
-  nsCOMPtr<nsIThread> thread;
+  nsIThread* thread = nsnull;
 #ifdef MOZILLA_INTERNAL_API
   nsresult rv = nsThreadManager::get()->
-      nsThreadManager::NewThread(0, getter_AddRefs(thread));
+      nsThreadManager::NewThread(0, &thread);
 #else
   nsresult rv;
-  nsCOMPtr<nsIThreadManager> mgr =
+  nsIThreadManager* mgr =
       do_GetService(NS_THREADMANAGER_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = mgr->NewThread(0, getter_AddRefs(thread));
+  rv = mgr->NewThread(0, &thread);
 #endif
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -80,7 +80,7 @@ NS_NewThread(nsIThread **result, nsIRunn
   }
 
   *result = nsnull;
-  thread.swap(*result);
+  swap(thread, *result);
   return NS_OK;
 }
 
@@ -91,7 +91,7 @@ NS_GetCurrentThread(nsIThread **result)
   return nsThreadManager::get()->nsThreadManager::GetCurrentThread(result);
 #else
   nsresult rv;
-  nsCOMPtr<nsIThreadManager> mgr =
+  nsIThreadManager* mgr =
       do_GetService(NS_THREADMANAGER_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
   return mgr->GetCurrentThread(result);
@@ -105,7 +105,7 @@ NS_GetMainThread(nsIThread **result)
   return nsThreadManager::get()->nsThreadManager::GetMainThread(result);
 #else
   nsresult rv;
-  nsCOMPtr<nsIThreadManager> mgr =
+  nsIThreadManager* mgr =
       do_GetService(NS_THREADMANAGER_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
   return mgr->GetMainThread(result);
@@ -117,9 +117,11 @@ NS_IsMainThread()
 {
   PRBool result = PR_FALSE;
 #ifdef MOZILLA_INTERNAL_API
-  nsThreadManager::get()->nsThreadManager::GetIsMainThread(&result);
+  nsThreadManager* mgr = nsThreadManager::get();
+  if (mgr)
+    mgr->nsThreadManager::GetIsMainThread(&result);
 #else
-  nsCOMPtr<nsIThreadManager> mgr =
+  nsIThreadManager* mgr =
       do_GetService(NS_THREADMANAGER_CONTRACTID);
   if (mgr)
     mgr->GetIsMainThread(&result);
@@ -134,8 +136,8 @@ NS_DispatchToCurrentThread(nsIRunnable *
   nsIThread *thread = NS_GetCurrentThread();
   if (!thread) { return NS_ERROR_UNEXPECTED; }
 #else
-  nsCOMPtr<nsIThread> thread;
-  nsresult rv = NS_GetCurrentThread(getter_AddRefs(thread));
+  nsIThread* thread = nsnull;
+  nsresult rv = NS_GetCurrentThread(&thread);
   NS_ENSURE_SUCCESS(rv, rv);
 #endif
   return thread->Dispatch(event, NS_DISPATCH_NORMAL);
@@ -144,8 +146,8 @@ NS_METHOD
 NS_METHOD
 NS_DispatchToMainThread(nsIRunnable *event, PRUint32 dispatchFlags)
 {
-  nsCOMPtr<nsIThread> thread;
-  nsresult rv = NS_GetMainThread(getter_AddRefs(thread));
+  nsIThread* thread = nsnull;
+  nsresult rv = NS_GetMainThread(&thread);
   NS_ENSURE_SUCCESS(rv, rv);
   return thread->Dispatch(event, dispatchFlags);
 }
@@ -161,11 +163,11 @@ NS_ProcessPendingEvents(nsIThread *threa
     NS_ENSURE_STATE(thread);
   }
 #else
-  nsCOMPtr<nsIThread> current;
+  nsIThread* current = nsnull;
   if (!thread) {
-    rv = NS_GetCurrentThread(getter_AddRefs(current));
+    rv = NS_GetCurrentThread(&current);
     NS_ENSURE_SUCCESS(rv, rv);
-    thread = current.get();
+    thread = current;
   }
 #endif
 
@@ -190,11 +192,11 @@ NS_HasPendingEvents(nsIThread *thread)
     NS_ENSURE_TRUE(thread, PR_FALSE);
   }
 #else
-  nsCOMPtr<nsIThread> current;
+  nsIThread* current = nsnull;
   if (!thread) {
-    NS_GetCurrentThread(getter_AddRefs(current));
+    NS_GetCurrentThread(&current);
     NS_ENSURE_TRUE(current, PR_FALSE);
-    thread = current.get();
+    thread = current;
   }
 #endif
   PRBool val;
@@ -210,11 +212,11 @@ NS_ProcessNextEvent(nsIThread *thread, P
     NS_ENSURE_TRUE(thread, PR_FALSE);
   }
 #else
-  nsCOMPtr<nsIThread> current;
+  nsIThread* current = nsnull;
   if (!thread) {
-    NS_GetCurrentThread(getter_AddRefs(current));
+    NS_GetCurrentThread(&current);
     NS_ENSURE_TRUE(current, PR_FALSE);
-    thread = current.get();
+    thread = current;
   }
 #endif
   PRBool val;
diff --git a/xpcom/threads/nsThreadManager.h b/xpcom/threads/nsThreadManager.h
--- a/xpcom/threads/nsThreadManager.h
+++ b/xpcom/threads/nsThreadManager.h
@@ -51,9 +51,16 @@ public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSITHREADMANAGER
 
+  static nsThreadManager *create() {
+    NS_ASSERTION(!gInstance, "Created threadmanager twice.");
+    gInstance = new nsThreadManager::Root();
+    return gInstance->instance;
+  }
+
   static nsThreadManager *get() {
     if (!gInstance) {
-      gInstance = new nsThreadManager::Root();
+      NS_WARNING("No threadmanager... before startup or after shutdown?");
+      return nsnull;
     }
     return gInstance->instance;
   }