OSDN Git Service

Moved the context class loader init down a bit to ensure proper init.
authorAndy McFadden <fadden@android.com>
Mon, 4 May 2009 20:29:30 +0000 (13:29 -0700)
committerAndy McFadden <fadden@android.com>
Mon, 4 May 2009 20:29:30 +0000 (13:29 -0700)
Since we're executing interpreted code it's best if we set the context
class loader as late as possible in this function.  I also updated a few
comments while I was in there.

vm/Thread.c

index b2753f0..acfc7a9 100644 (file)
@@ -627,6 +627,7 @@ bool dvmPrepMainForJni(JNIEnv* pEnv)
 /*
  * Finish preparing the main thread, allocating some objects to represent
  * it.  As part of doing so, we finish initializing Thread and ThreadGroup.
+ * This will execute some interpreted code (e.g. class initializers).
  */
 bool dvmPrepMainThread(void)
 {
@@ -691,22 +692,6 @@ bool dvmPrepMainThread(void)
     }
 
     /*
-     * Set the context class loader.
-     */
-    Object* systemLoader = dvmGetSystemClassLoader();
-    if (systemLoader == NULL) {
-        LOGW("WARNING: system class loader is NULL (setting main ctxt)\n");
-        /* keep going */
-    }
-    int ctxtClassLoaderOffset = dvmFindFieldOffset(gDvm.classJavaLangThread,
-        "contextClassLoader", "Ljava/lang/ClassLoader;");
-    if (ctxtClassLoaderOffset < 0) {
-        LOGE("Unable to find contextClassLoader field in Thread\n");
-        return false;
-    }
-    dvmSetFieldObject(threadObj, ctxtClassLoaderOffset, systemLoader);
-
-    /*
      * Allocate and construct a VMThread.
      */
     vmThreadObj = dvmAllocObject(gDvm.classJavaLangVMThread, ALLOC_DEFAULT);
@@ -730,7 +715,8 @@ bool dvmPrepMainThread(void)
 
     /*
      * Stuff the VMThread back into the Thread.  From this point on, other
-     * Threads will see that this Thread is running.
+     * Threads will see that this Thread is running (at least, they would,
+     * if there were any).
      */
     dvmSetFieldObject(threadObj, gDvm.offJavaLangThread_vmThread,
         vmThreadObj);
@@ -738,6 +724,24 @@ bool dvmPrepMainThread(void)
     thread->threadObj = threadObj;
 
     /*
+     * Set the context class loader.  This invokes a ClassLoader method,
+     * which could conceivably call Thread.currentThread(), so we want the
+     * Thread to be fully configured before we do this.
+     */
+    Object* systemLoader = dvmGetSystemClassLoader();
+    if (systemLoader == NULL) {
+        LOGW("WARNING: system class loader is NULL (setting main ctxt)\n");
+        /* keep going */
+    }
+    int ctxtClassLoaderOffset = dvmFindFieldOffset(gDvm.classJavaLangThread,
+        "contextClassLoader", "Ljava/lang/ClassLoader;");
+    if (ctxtClassLoaderOffset < 0) {
+        LOGE("Unable to find contextClassLoader field in Thread\n");
+        return false;
+    }
+    dvmSetFieldObject(threadObj, ctxtClassLoaderOffset, systemLoader);
+
+    /*
      * Finish our thread prep.
      */