hythread_ext.h File Reference

Detailed Description

Overview

The Thread Manager (TM) component provides various threading functionality for VM and class libraries code. provides support for threading inside the virtual machine and for class libraries. The implementation of thread management uses the black-box approach exposing two interfaces: Java and native. The whole component is divided into two layers: the middle layer of native threading with the layer of Java threading on top. The implementation of Thread manager is based solely on the Apache Portable Runtime (APR) layer.

The top layer provides the following functionality (see jthread, thread_ti.h):

Interaction

The following components are primary customers of the Thread Manager:

Kernel classes

Garbage collector

java.util.concurrent classes

java.lang.management classes

JVMTI

These components interact with different parts of thread management: some work with Java threads and objects, while others interact with the middle layer of native threading. Whenever the component operates with Java objects and threads, it would call the top (Java) interface functions. Otherwise, it would call the middle (native) interface functions.

Safe suspension

One of the key features of thread management in the VM is the safe suspension functionality. The purpose of the safe suspension mechanism is mostly to ensure that:

Suspended thread can be safely explored by the Garbage Collector while enumerating live references;

Suspended thread is guaranteed to do not hold any system-critical locks which can be requested by the other parts of VM, such as the locks associated with the native memory heap.

At every time moment, thread can be in one of three possible states:

Safe region. A period of time during thread execution when the thread can be safely suspended. This would typically be a region of a native C code where java objects and stack are not changed. An example is native file I/O call which reads a big amount of data from disk.

Unsafe region. A period of time during thread execution when the code is in process of changing java objects or impacts java stack, such as parts of the JITT-ed java code or JNI function calls. Suspending a thread in that region would typically be unsafe.

Safe point. A single point during the thread execution time when the thread can safely suspended. For example, JIT may want to injec t the safe points in the Java code between the selected chunks of the assembly code, those size is determined in terms of performance balanc e between safe point function call overhead and suspension time overhead.

Each thread managed by TM has the following fields:

safe_region 226 boolean flag which reports whether suspend is safe at this moment or not.

request 226 integer indicating if any request for suspension were made for this thread

suspend_count 226 integer indicating how many requests for suspension were made for this thread

The suspension algorithm typically invokes two threads 226 223suspender224 thread and 223suspendee224 thread. A typical example is than "suspender" is GC thread, and the "suspendee" is Java thread. The safe thread suspension algorithm works as follows:

1. GC thread invokes thread_supend() method of a thread manager which does the following:
a. If Java thread was already requested to suspend, increase the suspend_count count and return;
b. Increase suspend_count for Java thread;
c. If Java thread is currently in unsafe region, wait while it reaches the safe region.
2. GC thread, after completing the enumeration-related activities, calls the resume_thread() method which does the following:
a. If suspend_count was previously set, decrease the number of suspend requests;
b. If the number of suspend requests reaches zero, notifies the Java thread that it can wake up now.
3. A Java thread may reach safe point, which is denoted by calling the safe_point() method in the Java thread.
The safe_point() method does the following:
a. If there was request for suspension then:
i. notify GC thread that it can proceed with enumeration activities
ii. wait for the resume notification to come from the GC thread
4. A Java thread may enter the safe region, which is denoted by calling the suspend_enable() method in the Java thread.
The suspend_enable() method does the following:
a. Sets the flag safe_region to true;
b. If there was a request for suspension, notifies the GC thread that Java thread has reached safe region so GC may
proceed with enumeration activities
5. A Java thread may leave the safe region, which is denoted by calling the suspend_disable() method in the Java thread.
The suspend_disable() method does the following:
a. Sets the flag safe_region to false;
b. Calls the safe_point() method.

For more detailes see thread manager component documentation located at vm/thread/doc/ThreadManager.htm

Denotes the end of the code region where safe suspension was possible.

The method increases the disable_count field. The disable_count could be recursive, so safe suspension region is enabled on value 0. If there was a suspension request set for this thread, the method invokes hythread_safe_point().