root-running-threads
author Benjamin Smedberg <benjamin@smedbergs.us>
Sat, 26 Jul 2008 22:49:39 -0400
changeset 167 a4da40849f5436e629c5732f4368c6c48189637f
parent 71 70ec1aa8d4ad52472b356e5cbccd2a8da0605f0b
permissions -rw-r--r--
State as of now

Root threads that are running. I'm not sure this is the correct solution or necessary, because I think the thread stack has a pointer to the thread on it already. Hrm.

diff --git a/xpcom/threads/nsThread.cpp b/xpcom/threads/nsThread.cpp
--- a/xpcom/threads/nsThread.cpp
+++ b/xpcom/threads/nsThread.cpp
@@ -313,6 +313,7 @@ nsThread::nsThread()
   , mShutdownContext(nsnull)
   , mShutdownRequired(PR_FALSE)
   , mEventsAreDoomed(PR_FALSE)
+  , mRoot(nsnull)
 {
 }
 
@@ -354,6 +355,9 @@ nsThread::Init()
   // Wait for thread to call ThreadManager::SetupCurrentThread, which completes
   // initialization of ThreadFunc.
   startup->Wait();
+
+  mRoot = new Root(this);
+
   return NS_OK;
 }
 
@@ -489,6 +493,7 @@ nsThread::Shutdown()
     PR_JoinThread(mThread);
   }
   mThread = nsnull;
+  delete mRoot;
   return NS_OK;
 }
 
diff --git a/xpcom/threads/nsThread.h b/xpcom/threads/nsThread.h
--- a/xpcom/threads/nsThread.h
+++ b/xpcom/threads/nsThread.h
@@ -146,6 +146,20 @@ private:
   PRPackedBool mShutdownPending;
   // Set to true when events posted to this thread will never run.
   PRPackedBool mEventsAreDoomed;
+  
+  class Root : public MMgc::GCRoot
+  {
+  public:
+    Root(nsThread *thread)
+      : MMgc::GCRoot(NS_GetGC())
+      , mThreadRooted(thread)
+    { }
+
+    nsThread *mThreadRooted;
+  };
+
+  // A thread roots itself while it is running
+  Root *mRoot;
 };
 
 //-----------------------------------------------------------------------------