Main Page | Namespace List | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

thread.c File Reference


Detailed Description

Create and manage real machine Java thread structures.

Control

$URL: https://svn.apache.org/path/name/thread.c $ $Id: thread.c 0 09/28/2005 dlydick $

Copyright 2005 The Apache Software Foundation or its licensors, as applicable.

Licensed under the Apache License, Version 2.0 ("the License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and limitations under the License.

Version:
$LastChangedRevision: 0 $
Date:
$LastChangedDate: 09/28/2005 $
Author:
$LastChangedBy: dlydick $ Original code contributed by Daniel Lydick on 09/28/2005.

Reference

Definition in file thread.c.

#include "arch.h"
#include <strings.h>
#include "jvmcfg.h"
#include "cfmacros.h"
#include "classfile.h"
#include "exit.h"
#include "gc.h"
#include "jvm.h"
#include "jvmclass.h"
#include "linkage.h"
#include "method.h"
#include "util.h"
#include "utf.h"

Go to the source code of this file.

Thread allocation and loading.

Allocate a new thread from the thread area and load a class to run on it.

Activity on the system thread JVMCFG_SYSTEM_THREAD will not attempt to load a java.lang.Thread to represent a thread as an object. This is because the system thread is for internal use only outside the normal JVM runtime environment.

As a variation, allocate the system thread (JVMCFG_SYSTEM_THREAD) for an internal task. This method-- thread_new_common()-- is the common routine, where thread_new() is the general-purpose function and thread_new_system() is for loading a class onto the system thread.

Warning:
Do not attempt to call this function with codeatridx set to jvm_attribute_index_native because this code does not apply to native methods. Call that native method directly from the code instead!
Parameters:
thridx Thread index of unused thread-- for thread_new_common() only-- the others generate this value and pass it in.
clsidx Class index of code to run
mthidx Method index of code to run
codeatridx Method's attribute index of code to run
excpatridx Method's attribute index of exception index tbl
priority THREAD_PRIORITY_xxx value for thread priority
isdaemon Daemon thread, rtrue or rfalse
Returns:
index to thread slot containing this thread.
Exceptions:
JVMCLASS_JAVA_LANG_STACKOVERFLOWERROR if loading the class on this thread would overfill the JVM stack for this thread.. for this thread.
JVMCLASS_JAVA_LANG_OUTOFMEMORYERROR if no class slots or thread slots are available..


static jvm_thread_index thread_new (jvm_class_index clsidx, jvm_method_index mthidx, jvm_attribute_index codeatridx, jvm_attribute_index excpatridx, rint priority, rboolean isdaemon)
 Allocate a new thread from the thread area and load a class to run on it.
static jvm_thread_index thread_new_common (jvm_thread_index thridx, jvm_class_index clsidx, jvm_method_index mthidx, jvm_attribute_index codeatridx, jvm_attribute_index excpatridx, rint priority, rboolean isdaemon)
 Common function to load a class on a thread to be run.
static jvm_thread_index thread_new_system (jvm_class_index clsidx, jvm_method_index mthidx, jvm_attribute_index codeatridx, jvm_attribute_index excpatridx, rint priority, rboolean isdaemon)
 Reserve the system thread and load a class to run on it.

Functions

static jvm_thread_index thread_allocate_slot (rvoid)
 Locate an unused thread table slot for a new thread.
static void thread_c_dummy (void)
jvm_thread_index thread_class_load (rchar *clsname, rchar *mthname, rchar *mthdesc, rint priority, rboolean isdaemon, rboolean usesystemthread, rboolean find_registerNatives)
 Load a class onto a thread during JVM initialization.
rboolean thread_die (jvm_thread_index thridx)
 Terminate and deallocate a thread that is currently in the DEAD state.
int thread_exception_setup (jvm_thread_index thridx)
 Set up JVM thread-relative exception handler-- implements setjmp(3)/longjmp(3).
rvoid thread_init ()
 Initialize the thread area of the JVM model.
rvoid thread_shutdown ()
 Shut down the thread area of the JVM model at the end of JVM execution.
const rcharthread_state_get_name (rushort state)
 Map state numbers to state names.
rvoid thread_throw_exception (jvm_thread_index thridx, rushort thread_status_bits, rchar *exception_name)
 Global handler setup for JVM thread errors and exceptions-- implements setjmp(3).

Variables

static char * thread_c_copyright = "\0" "$URL: https://svn.apache.org/path/name/thread.c $ $Id: thread.c 0 09/28/2005 dlydick $" " " "Copyright 2005 The Apache Software Foundation or its licensors, as applicable."
const rcharthread_state_names [THREAD_STATE_BADLOGIC+1+1]
 Look up table for names of thread states.


Function Documentation

static void thread_c_dummy void   )  [static]
 

Definition at line 41 of file thread.c.

rvoid thread_init  ) 
 

Initialize the thread area of the JVM model.

Parameters: rvoid

Returns:
rvoid
< Access structures of the thread now running in the JVM.

< Access structures of the thread now running in the JVM.

< Access structures of the thread now running in the JVM.

< Access structures of the thread now running in the JVM.

< Arbitrary max thread name length

< This slot is available for use. DO NOT CHANGE since this is also the normal setjmp(3) return code from thread_exception_setup()

Todo:
What should the priority be on these threads?
< This slot contains a thread

< Null thread (only 1 exists in normal use, any else besides the JVMCFG_NULL_THREAD is a thread slot that is being initialized.)

< This slot is available for use. DO NOT CHANGE since this is also the normal setjmp(3) return code from thread_exception_setup()

< This slot contains a thread

< Access structures of the thread now running in the JVM.

Definition at line 70 of file thread.c.

References CURRENT_THREAD, HEAP_GET_DATA, jvm_attribute_index_bad, jvm_class_index_null, jvm_method_index_bad, jvm_object_hash_null, jvm_pc_offset_bad, jvm_thread_index_null, jvm_thread_initialized, JVMCFG_GC_THREAD, JVMCFG_MAX_THREADS, JVMCFG_NULL_SP, JVMCFG_SYSTEM_THREAD, pjvm, PUT_PC_IMMEDIATE, rfalse, rnull, rtrue, sprintfLocal(), THREAD, THREAD_NAME_MAX_LEN, rjvm::thread_new_last, THREAD_PRIORITY_MAX, THREAD_PRIORITY_MIN, THREAD_STATE_DEAD, THREAD_STATE_NEW, THREAD_STATUS_EMPTY, THREAD_STATUS_INUSE, and THREAD_STATUS_NULL.

const rchar* thread_state_get_name rushort  state  ) 
 

Map state numbers to state names.

Parameters:
state state number per THREAD_STATE_xxx definitions
Returns:
string name of that state
< Highest possible state number

< Highest possible state number

Definition at line 224 of file thread.c.

References EXIT_JVM_THREAD, exit_throw_exception(), JVMCFG_FIRST_THREAD, JVMCFG_MAX_THREADS, JVMCLASS_JAVA_LANG_OUTOFMEMORYERROR, pjvm, rtrue, THREAD, rjvm::thread_new_last, and THREAD_STATUS_INUSE.

Referenced by opcode_run().

static jvm_thread_index thread_allocate_slot rvoid   )  [static]
 

Locate an unused thread table slot for a new thread.

Parameters: rvoid

Returns:
Thread table index of an empty slot. Throw error if no slots.
Exceptions:
JVMCLASS_JAVA_LANG_OUTOFMEMORYERROR if no thread slots are available.
< This slot contains a thread

< This slot contains a thread

< Null thread (only 1 exists in normal use, any else besides the JVMCFG_NULL_THREAD is a thread slot that is being initialized.)

Definition at line 266 of file thread.c.

static jvm_thread_index thread_new_common jvm_thread_index  thridx,
jvm_class_index  clsidx,
jvm_method_index  mthidx,
jvm_attribute_index  codeatridx,
jvm_attribute_index  excpatridx,
rint  priority,
rboolean  isdaemon
[static]
 

Common function to load a class on a thread to be run.

Given a thread index, load a method onto it to be run. Do not use this function to load a method onto a thread that has existing JVM execution active because the stack is initialized and the method entry point loaded into the PC. See opcode_run() for examples of how to accomplish this in other ways.

< This slot contains a thread

< Null thread (only 1 exists in normal use, any else besides the JVMCFG_NULL_THREAD is a thread slot that is being initialized.)

If no stack overflow would occur during execution, continue to load up a new thread. Unless this class will run on the system thread, start out by generating a new java.lang.Thread object to represent this thread in the object table.

Todo:
Need to also implement java.lang.ThreadGroup as a preparatory step to instantiating java.lang.Thread
< This slot is available for use. DO NOT CHANGE since this is also the normal setjmp(3) return code from thread_exception_setup()

< This slot is available for use

< Not stated in spec, but implied

< Magic number for class files

alias

Todo:
Any time the isdaemon field is modified, MAKE SURE that the value in the java.lang.Thread instance variable is set to the same value. This should never be an issue except when instantiating a new java.lang.Thread object since the API docs say that it can only be set once.
< Daemon thread state, vs user thread, per Thread.isdaemon()

< Null thread (only 1 exists in normal use, any else besides the JVMCFG_NULL_THREAD is a thread slot that is being initialized.)

< Start PC location

Definition at line 406 of file thread.c.

References jvm_object_hash_null, and THREAD.

static jvm_thread_index thread_new_system jvm_class_index  clsidx,
jvm_method_index  mthidx,
jvm_attribute_index  codeatridx,
jvm_attribute_index  excpatridx,
rint  priority,
rboolean  isdaemon
[static]
 

Reserve the system thread and load a class to run on it.

Reserve the system thread specifically-- see description above for thread_new_common()

< This slot contains a thread

Definition at line 613 of file thread.c.

static jvm_thread_index thread_new jvm_class_index  clsidx,
jvm_method_index  mthidx,
jvm_attribute_index  codeatridx,
jvm_attribute_index  excpatridx,
rint  priority,
rboolean  isdaemon
[static]
 

Allocate a new thread from the thread area and load a class to run on it.

General-purpuse version-- see description above for thread_new_common()

Definition at line 649 of file thread.c.

References ACC_INTERFACE, ClassFile::access_flags, class_find_by_prchar(), class_load_from_prchar(), CLASS_OBJECT_LINKAGE, CONSTANT_CP_DEFAULT_INDEX, EXIT_JVM_CLASS, exit_throw_exception(), JVMCLASS_JAVA_LANG_INCOMPATIBLECLASSCHANGEERROR, PTR_CP1_CLASS_NAME_STRNAME, rnull, and ClassFile::super_class.

jvm_thread_index thread_class_load rchar clsname,
rchar mthname,
rchar mthdesc,
rint  priority,
rboolean  isdaemon,
rboolean  usesystemthread,
rboolean  find_registerNatives
 

Load a class onto a thread during JVM initialization.

Allocate a new thread and load a class (array or non-array), prepare to invoke static methods, and prepare to start thread.

Classes loaded through this function will not be marked as referenced, but will also not be marked for garbage collection, either.

Parameters:
clsname Name of class to load.
mthname Name of method in class.
mthdesc Description of method parameters and return type.
priority THREAD_PRIORITY_xxx value for thread priority.
isdaemon Daemon thread, rtrue or rfalse
usesystemthread Allocate the system thread with thread_new_system() instead of a user thread via thread_new_system(), rtrue or rfalse.
find_registerNatives When rtrue, will return the ordinal for JVMCFG_JLOBJECT_NMO_REGISTER and JVMCFG_JLOBJECT_NMO_UNREGISTER as well as the other ordinals. Once JVM initialization is complete, this should always be rfalse because all future classes should never have local ordinals.
Returns:
Thread index of NEW thread, ready to move to START state, or throw error if no slots.
Exceptions:
JVMCLASS_JAVA_LANG_OUTOFMEMORYERROR if no class slots or thread slots are available..
JVMCLASS_JAVA_LANG_CLASSNOTFOUNDEXCEPTION if class cannot be located..
JVMCLASS_JAVA_LANG_NOSUCHMETHODERROR if the requested method is not found in the class or has no code area..
JVMCLASS_JAVA_LANG_STACKOVERFLOWERROR if loading the class on this thread would overfill the JVM stack for this thread..
< References java.lang.Object < Is an interface, not a class.

< References java.lang.Object

Definition at line 740 of file thread.c.

rboolean thread_die jvm_thread_index  thridx  ) 
 

Terminate and deallocate a thread that is currently in the DEAD state.

Parameters:
thridx thread index of thread to be deallocated.
Returns:
rtrue if successful, else rfalse.
Mark ONLY fields requiring it, leave rest alone for post-mortem use by developer.

Todo:
Could leave stack in place for post-mortem if it is assumed that there would be a reasonable amount of contents still valid in memory.
< This slot contains a thread

Definition at line 923 of file thread.c.

rvoid thread_shutdown  ) 
 

Shut down the thread area of the JVM model at the end of JVM execution.

Parameters: rvoid

Returns:
rvoid
< This slot contains a thread

alias

Definition at line 959 of file thread.c.

References HEAP_FREE_STACK, and THREAD.

int thread_exception_setup jvm_thread_index  thridx  ) 
 

Set up JVM thread-relative exception handler-- implements setjmp(3)/longjmp(3).

Parameters:
thridx Thread table index of thread for which to set up this exception handler.
Returns:
From normal setup, integer THREAD_STATUS_EMPTY. Otherwise, return error code passed in to thread_throw_exception() as shown below.

For internal use only.

setjmp(3) and struct[idx].member do _not_ get along at all:

       int nonlocal_rc =
                    setjmp(*THREAD(thridx).nonlocal_ThrowableEvent);
      
   

To get this working, the nonlocal_ThrowableEvent had to be changed to a pointer and a buffer allocated from heap. This probably is due to the typedef syntax of jmp_buf and a long array, which pointers like. However, instead of playing games with addressing &element[0], a pointer works better because not all jmp_buf implementations wil be a long array.

Definition at line 1077 of file thread.c.

References EXIT_JVM_INTERNAL, exit_throw_exception(), JVMCLASS_JAVA_LANG_INTERNALERROR, THREAD_STATUS_THREW_ERROR, THREAD_STATUS_THREW_EXCEPTION, THREAD_STATUS_THREW_THROWABLE, and THREAD_STATUS_THREW_UNCAUGHT.

Referenced by opcode_run().

rvoid thread_throw_exception jvm_thread_index  thridx,
rushort  thread_status_bits,
rchar exception_name
 

Global handler setup for JVM thread errors and exceptions-- implements setjmp(3).

Parameters:
thridx Thread index of thread where exception occurred.
thread_status_bits Type of exception being generated, typically using one of the following exit codes from the status bits in thread.h per the definition there of each code:

Parameters:
exception_name Null-terminated string name of error or exception class to be invoked as a result of this situation.
Returns:
non-local state restoration from setup via setjmp(3) as stored in exit_LinkageError buffer by exit_init() in jvm_init() before any of these errors could occur. All code invoking this function should use the standard lint(1) comment for "code not reached" as shown after the longjmp(3) function call in the source code of this function: /*NOTREACHED*/
< thread threw a java.lang.Exception (but NOT a java.lang.Error). The object type is found in pThrowableEvent and is not rnull.

< thread threw a java.lang.Error (but NOT a java.lang.Exception). The object type is found in pThrowableEvent and is not rnull.

< thread threw a java.lang.Throwable of unknowable type. The object type is found in pThrowableEvent and is not rnull.

< A java.lang.Throwable of some type was thrown, but not handled by the Java code itself. Instead, it will be handled by the default java.lang.ThreadGroup.uncaughtException method. The value of pThrowableEvent will be rnull.

Definition at line 1130 of file thread.c.

Referenced by jlObject_wait4ever(), jlThread_getPriority(), jlThread_interrupted(), jlThread_isAlive(), jlThread_isDaemon(), jlThread_setDaemon(), jlThread_setPriority(), jlThread_start(), objectutil_synchronize(), and opcode_run().


Variable Documentation

char* thread_c_copyright = "\0" "$URL: https://svn.apache.org/path/name/thread.c $ $Id: thread.c 0 09/28/2005 dlydick $" " " "Copyright 2005 The Apache Software Foundation or its licensors, as applicable." [static]
 

Definition at line 41 of file thread.c.

const rchar* thread_state_names[THREAD_STATE_BADLOGIC+1+1]
 

Initial value:

{
     "new" ,
     "started" ,
     "runnable" ,
     "running" ,
     "complete" ,
     "blockingevent" ,
     "blocked" ,
     "unblocked" ,
     "synchronized" ,
     "release" ,
     "wait" ,
     "notify" ,
     "lock" ,
     "acquire" ,
     "dead" ,
     "bad_logic" ,

    
     "illegal" 
}
Look up table for names of thread states.

Definition at line 192 of file thread.c.


Generated on Fri Sep 30 19:00:49 2005 by  doxygen 1.4.4