jamvm

changeset 407:f5ea446164a8 challenge

2008-08-04 Andrew John Hughes <gnu_andrew@member.fsf.org>

* src/symbol.h,
* src/thread.c:
Support initialising java.lang.Thread on
OpenJDK.
author andrew
date Tue Aug 05 06:57:40 2008 +0100 (2008-08-05)
parents 2f855beaaf73
children 730da428aa2c
files ChangeLog src/symbol.h src/thread.c
line diff
     1.1 --- a/ChangeLog	Tue Aug 05 06:28:05 2008 +0100
     1.2 +++ b/ChangeLog	Tue Aug 05 06:57:40 2008 +0100
     1.3 @@ -5,6 +5,13 @@
     1.4  ==============
     1.5  2008-08-04  Andrew John Hughes  <gnu_andrew@member.fsf.org>
     1.6  
     1.7 +	* src/symbol.h,
     1.8 +	* src/thread.c:
     1.9 +	Support initialising java.lang.Thread on
    1.10 +	OpenJDK.
    1.11 +	
    1.12 +2008-08-04  Andrew John Hughes  <gnu_andrew@member.fsf.org>
    1.13 +
    1.14  	* src/natives.c:
    1.15  	Document VM differences.
    1.16  	
     2.1 --- a/src/symbol.h	Tue Aug 05 06:28:05 2008 +0100
     2.2 +++ b/src/symbol.h	Tue Aug 05 06:57:40 2008 +0100
     2.3 @@ -63,8 +63,13 @@
     2.4      action(backtrace, "backtrace"), \
     2.5      action(initCause, "initCause"), \
     2.6      action(loadClass, "loadClass"), \
     2.7 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
     2.8      action(addThread, "addThread"), \
     2.9      action(removeThread, "removeThread"), \
    2.10 +#elif WITH_JAVA_RUNTIME_LIBRARY_OPENJDK
    2.11 +    action(add, "add"), \
    2.12 +    action(remove, "remove"), \
    2.13 +#endif
    2.14      action(declaringClass, "declaringClass"), \
    2.15      action(printStackTrace, "printStackTrace"), \
    2.16      action(exceptionHandler, "exceptionHandler"), \
    2.17 @@ -207,15 +212,17 @@
    2.18      action(_J__V, "(J)V"), \
    2.19      action(_java_lang_Thread_java_lang_Throwable__V, \
    2.20             "(Ljava/lang/Thread;Ljava/lang/Throwable;)V"), \
    2.21 -    action(_java_lang_VMThread_java_lang_String_I_Z__V, \
    2.22 -           "(Ljava/lang/VMThread;Ljava/lang/String;IZ)V"), \
    2.23      action(_java_lang_Throwable__java_lang_Throwable, \
    2.24             "(Ljava/lang/Throwable;)Ljava/lang/Throwable;"), \
    2.25  #ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
    2.26 +    action(_java_lang_VMThread_java_lang_String_I_Z__V, \
    2.27 +           "(Ljava/lang/VMThread;Ljava/lang/String;IZ)V"), \
    2.28      action(_java_lang_Object_gnu_classpath_Pointer_III__V, \
    2.29             "(Ljava/lang/Object;Lgnu/classpath/Pointer;III)V"), \
    2.30  #elif WITH_JAVA_RUNTIME_LIBRARY_OPENJDK
    2.31      action(_LI__V, "(LI)V"), \
    2.32 +    action(_java_lang_ThreadGroup_java_lang_String__V, \
    2.33 +           "(Ljava/lang/ThreadGroup;Ljava/lang/String;)V"), \
    2.34  #endif
    2.35      action(_java_lang_String__V, "(Ljava/lang/String;)V"), \
    2.36      action(_array_java_lang_String__V, "([Ljava/lang/String;)V"), \
     3.1 --- a/src/thread.c	Tue Aug 05 06:28:05 2008 +0100
     3.2 +++ b/src/thread.c	Tue Aug 05 06:57:40 2008 +0100
     3.3 @@ -90,7 +90,13 @@
     3.4  
     3.5  /* Cached java.lang.Thread class */
     3.6  static Class *thread_class;
     3.7 +/* VMDIFF: For OpenJDK, we need ThreadGroup.  For
     3.8 +   Classpath, VMThread. */
     3.9 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
    3.10  static Class *vmthread_class;
    3.11 +#elif WITH_JAVA_RUNTIME_LIBRARY_OPENJDK
    3.12 +static Class *thrdGrp_class;
    3.13 +#endif
    3.14  
    3.15  /* Count of non-daemon threads still running in VM */
    3.16  static int non_daemon_thrds = 0;
    3.17 @@ -339,17 +345,28 @@
    3.18      deleteHashEntry(thread_id_map, thread, TRUE);
    3.19  }
    3.20  
    3.21 +/* VMDIFF: OpenJDK does not need a VMThread but
    3.22 + does need a thread group. */
    3.23 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
    3.24  Object *initJavaThread(Thread *thread, char is_daemon, char *name) {
    3.25      Object *vmthread, *jlthread, *thread_name = NULL;
    3.26 -
    3.27      /* Create the java.lang.Thread object and the VMThread */
    3.28      if((vmthread = allocObject(vmthread_class)) == NULL ||
    3.29         (jlthread = allocObject(thread_class)) == NULL)
    3.30          return NULL;
    3.31  
    3.32 -    thread->ee->thread = jlthread;
    3.33      INST_DATA(vmthread)[vmData_offset] = (uintptr_t)thread;
    3.34      INST_DATA(vmthread)[thread_offset] = (uintptr_t)jlthread;
    3.35 +#elif WITH_JAVA_RUNTIME_LIBRARY_OPENJDK
    3.36 +Object *initJavaThread(Thread *thread, char is_daemon, Object *thrdgrp, char *name) {
    3.37 +    Object *jlthread, *thread_name = NULL;
    3.38 +
    3.39 +    /* Create the java.lang.Thread object */
    3.40 +    if((jlthread = allocObject(thread_class)) == NULL)
    3.41 +        return NULL;
    3.42 +#endif
    3.43 +
    3.44 +    thread->ee->thread = jlthread;
    3.45  
    3.46      /* Create the string for the thread name.  If null is specified
    3.47         the initialiser method will generate a name of the form Thread-X */
    3.48 @@ -358,7 +375,15 @@
    3.49  
    3.50      /* Call the initialiser method -- this is for use by threads
    3.51         created or attached by the VM "outside" of Java */
    3.52 +    /* VMDIFF: The daemon status and priority are not arguments
    3.53 +       for OpenJDK's java.lang.Thread, so we poke them into place instead. */
    3.54 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
    3.55      executeMethod(jlthread, init_mb, vmthread, thread_name, NORM_PRIORITY, is_daemon);
    3.56 +#elif WITH_JAVA_RUNTIME_LIBRARY_OPENJDK
    3.57 +    executeMethod(jlthread, init_mb, thrdgrp, thread_name);
    3.58 +    INST_DATA(jlthread)[daemon_offset] = is_daemon;
    3.59 +    INST_DATA(jlthread)[priority_offset] = NORM_PRIORITY;
    3.60 +#endif
    3.61  
    3.62      /* Add thread to thread ID map hash table. */
    3.63      addThreadToHash(thread);
    3.64 @@ -431,11 +456,18 @@
    3.65      initThread(thread, is_daemon, stack_base);
    3.66  
    3.67      /* Initialise the Java-level thread objects representing this thread */
    3.68 +    /* VMDIFF: The thread group needs to be poked into place for Classpath */
    3.69 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
    3.70      if((java_thread = initJavaThread(thread, is_daemon, name)) == NULL)
    3.71 +#elif WITH_JAVA_RUNTIME_LIBRARY_OPENJDK
    3.72 +    if((java_thread = initJavaThread(thread, is_daemon, group, name)) == NULL)
    3.73 +#endif
    3.74          return NULL;
    3.75  
    3.76 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
    3.77      /* Initialiser doesn't handle the thread group */
    3.78      INST_DATA(java_thread)[group_offset] = (uintptr_t)group;
    3.79 +#endif
    3.80      executeMethod(group, addThread_mb, java_thread);
    3.81  
    3.82      /* We're now attached to the VM...*/
    3.83 @@ -562,10 +594,13 @@
    3.84      ExecEnv *ee;
    3.85      Thread *thread;
    3.86      Thread *self = threadSelf();
    3.87 +    /* VMDIFF: No VMThread in OpenJDK */
    3.88 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
    3.89      Object *vmthread = allocObject(vmthread_class);
    3.90  
    3.91      if(vmthread == NULL)
    3.92          return;
    3.93 +#endif
    3.94  
    3.95      disableSuspend(self);
    3.96  
    3.97 @@ -585,10 +620,13 @@
    3.98      thread->ee = ee;
    3.99      ee->thread = jThread;
   3.100      ee->stack_size = stack_size;
   3.101 -
   3.102 +    
   3.103 +    /* VMDIFF: No VMThread in OpenJDK */
   3.104 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
   3.105      INST_DATA(vmthread)[vmData_offset] = (uintptr_t)thread;
   3.106      INST_DATA(vmthread)[thread_offset] = (uintptr_t)jThread;
   3.107      INST_DATA(jThread)[vmthread_offset] = (uintptr_t)vmthread;
   3.108 +#endif
   3.109  
   3.110      pthread_mutex_unlock(&lock);
   3.111  
   3.112 @@ -1048,63 +1086,114 @@
   3.113      setThreadSelf(&main_thread);
   3.114  }
   3.115  
   3.116 +/* VMDIFF: GNU Classpath needs a VMThread which connects to
   3.117 +   java.lang.Thread.  OpenJDK needs two thread groups, "system"
   3.118 +   and "main", the latter being sent to the Thread constructor. 
   3.119 +   GNU Classpath only has a "main" thread group, which is stored
   3.120 +   in a variable called "root" and created by the class
   3.121 +   initialiser. */
   3.122  void initialiseThreadStage2(InitArgs *args) {
   3.123      Object *java_thread;
   3.124 -    Class *thrdGrp_class;
   3.125      MethodBlock *run, *remove_thread;
   3.126      FieldBlock *group, *priority, *root, *threadId;
   3.127      FieldBlock *vmThread = NULL, *thread = NULL;
   3.128 -    FieldBlock *vmData, *daemon, *name;
   3.129 +    FieldBlock *vmData, *daemon, *name
   3.130 +      /* VMDIFF: OpenJDK needs to create two thread groups */
   3.131 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
   3.132 +    Class *thrdGrp_class;
   3.133 +#elif WITH_JAVA_RUNTIME_LIBRARY_OPENJDK
   3.134 +    Object *main_group;
   3.135 +    MethodBlock *tginit_mb, *tginit_mb2;
   3.136 +#endif
   3.137  
   3.138      /* Load thread class and register reference for compaction threading */
   3.139      thread_class = findSystemClass0(SYMBOL(java_lang_Thread));
   3.140      registerStaticClassRef(&thread_class);
   3.141  
   3.142      if(thread_class != NULL) {
   3.143 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
   3.144          vmThread = findField(thread_class, SYMBOL(vmThread), SYMBOL(sig_java_lang_VMThread));
   3.145 +        threadId = findField(thread_class, SYMBOL(threadId), SYMBOL(J));
   3.146 +        init_mb = findMethod(thread_class, SYMBOL(object_init),
   3.147 +                             SYMBOL(_java_lang_VMThread_java_lang_String_I_Z__V));
   3.148 +#elif WITH_JAVA_RUNTIME_LIBRARY_OPENJDK
   3.149 +        init_mb = findMethod(thread_class, SYMBOL(object_init),
   3.150 +                             SYMBOL(_java_lang_ThreadGroup_java_lang_String__V));
   3.151 +#endif
   3.152          daemon = findField(thread_class, SYMBOL(daemon), SYMBOL(Z));
   3.153          name = findField(thread_class, SYMBOL(name), SYMBOL(sig_java_lang_String));
   3.154          group = findField(thread_class, SYMBOL(group), SYMBOL(sig_java_lang_ThreadGroup));
   3.155          priority = findField(thread_class, SYMBOL(priority), SYMBOL(I));
   3.156 -        threadId = findField(thread_class, SYMBOL(threadId), SYMBOL(J));
   3.157 -
   3.158 -        init_mb = findMethod(thread_class, SYMBOL(object_init),
   3.159 -                             SYMBOL(_java_lang_VMThread_java_lang_String_I_Z__V));
   3.160          run = findMethod(thread_class, SYMBOL(run), SYMBOL(___V));
   3.161  
   3.162 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
   3.163          vmthread_class = findSystemClass0(SYMBOL(java_lang_VMThread));
   3.164          CLASS_CB(vmthread_class)->flags |= VMTHREAD;
   3.165  
   3.166          /* Register class reference for compaction threading */
   3.167          registerStaticClassRef(&vmthread_class);
   3.168 +#elif WITH_JAVA_RUNTIME_LIBRARY_OPENJDK
   3.169 +	thrdGrp_class = findSystemClass0(SYMBOL(java_lang_ThreadGroup));
   3.170 +        registerStaticClassRef(&thrdGrp_class);
   3.171 +	tginit_mb = findMethod(thrdGrp_class, SYMBOL(object_init),
   3.172 +			       SYMBOL(___V));
   3.173 +	tginit_mb2 = findMethod(thrdGrp_class, SYMBOL(object_init),
   3.174 +				SYMBOL(_java_lang_ThreadGroup_java_lang_String__V));
   3.175 +#endif
   3.176  
   3.177 +
   3.178 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
   3.179          if(vmthread_class != NULL) {
   3.180              thread = findField(vmthread_class, SYMBOL(thread), SYMBOL(sig_java_lang_Thread));
   3.181              vmData = findField(vmthread_class, SYMBOL(vmData), SYMBOL(I));
   3.182          }
   3.183 +#elif WITH_JAVA_RUNTIME_LIBRARY_OPENJDK
   3.184 +	if (thrdGrp_class != NULL) {
   3.185 +	  Object* system_group = allocObject(thrdGrp_class);
   3.186 +	  executeMethod(system_group, tginit_mb);
   3.187 +	  main_group = allocObject(thrdGrp_class);
   3.188 +	  executeMethod(main_group, tginit_mb2, system_group,
   3.189 +			Cstr2String("main"));
   3.190 +	}
   3.191 +#endif
   3.192      }
   3.193  
   3.194      /* findField and findMethod do not throw an exception... */
   3.195 -    if((init_mb == NULL) || (vmData == NULL) || (run == NULL) || (daemon == NULL) ||
   3.196 -       (name == NULL) || (group == NULL) || (priority == NULL) || (vmThread == NULL) ||
   3.197 +    if((init_mb == NULL) || (run == NULL) || (daemon == NULL) ||
   3.198 +       (name == NULL) || (group == NULL) || (priority == NULL))
   3.199 +        goto error;
   3.200 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
   3.201 +    if((vmData == NULL) || (vmThread == NULL) ||
   3.202         (thread == NULL) || (threadId == NULL))
   3.203          goto error;
   3.204 +#elif WITH_JAVA_RUNTIME_LIBRARY_OPENJDK
   3.205 +    if((thrdGrp_class == NULL) || (tginit_mb == NULL) || (tginit_mb2 == NULL) ||
   3.206 +       (system_group == NULL) || (main_group == NULL))
   3.207 +	goto error;
   3.208 +#endif
   3.209  
   3.210 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
   3.211      vmthread_offset = vmThread->offset;
   3.212 +    threadId_offset = threadId->offset;
   3.213 +#endif
   3.214      thread_offset = thread->offset;
   3.215      vmData_offset = vmData->offset;
   3.216      daemon_offset = daemon->offset;
   3.217      group_offset = group->offset;
   3.218      priority_offset = priority->offset;
   3.219 -    threadId_offset = threadId->offset;
   3.220      name_offset = name->offset;
   3.221      run_mtbl_idx = run->method_table_index;
   3.222  
   3.223      /* Initialise the Java-level thread objects for the main thread */
   3.224 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
   3.225      java_thread = initJavaThread(&main_thread, FALSE, "main");
   3.226 +#elif WITH_JAVA_RUNTIME_LIBRARY_OPENJDK
   3.227 +    java_thread = initJavaThread(&main_thread, FALSE, main_group, "main");
   3.228 +#endif
   3.229  
   3.230      /* Main thread is now sufficiently setup to be able to run the thread group
   3.231         initialiser.  This is essential to create the root thread group */
   3.232 +#ifdef WITH_JAVA_RUNTIME_LIBRARY_CLASSPATH
   3.233      thrdGrp_class = findSystemClass(SYMBOL(java_lang_ThreadGroup));
   3.234  
   3.235      root = findField(thrdGrp_class, SYMBOL(root), SYMBOL(sig_java_lang_ThreadGroup));
   3.236 @@ -1119,11 +1208,20 @@
   3.237      if((root == NULL) || (addThread_mb == NULL) || (remove_thread == NULL))
   3.238          goto error;
   3.239  
   3.240 -    rmveThrd_mtbl_idx = remove_thread->method_table_index;
   3.241 -
   3.242      /* Add the main thread to the root thread group */
   3.243      INST_DATA(java_thread)[group_offset] = root->static_value;
   3.244      executeMethod(((Object*)root->static_value), addThread_mb, java_thread);
   3.245 +#elif WITH_JAVA_RUNTIME_LIBRARY_OPENJDK
   3.246 +    addThread_mb = findMethod(thrdGrp_class, SYMBOL(add),
   3.247 +                                             SYMBOL(_java_lang_Thread_args__void));
   3.248 +    remove_thread = findMethod(thrdGrp_class, SYMBOL(remove),
   3.249 +                                              SYMBOL(_java_lang_Thread_args__void));
   3.250 +    if((addThread_mb == NULL) || (remove_thread == NULL))
   3.251 +         goto error;
   3.252 +    executeMethod(main_group, addThread_mb, java_thread);
   3.253 +#endif
   3.254 +
   3.255 +    rmveThrd_mtbl_idx = remove_thread->method_table_index;
   3.256  
   3.257      /* Setup signal handling.  This will be inherited by all
   3.258         threads created within Java */