harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Pavel Rebriy" <pavel.reb...@gmail.com>
Subject [drlvm][vmmagic] Thread.currentThread() helper creation ploblems
Date Tue, 05 Feb 2008 12:46:17 GMT
Hello,

Working on HARMONY-4555 [1] I've tried to create VMMagic helper for
Thread.currentThread() function but faced with some difficulties related to
VMMagic mechanism.
Missing detailed patch (you can see it in JIRA [1]) I'm going to show
problematic function. Here they are:

1. org.apache.harmony.drlvm.thread.ThreadHelper.getCurrentThreadAddr()

@Inline
public static Address getCurrentThreadAddr() {
        Address tlsThread = VMHelper.getTlsBaseAddress();
        Address javaObjectPtr = tlsThread.plus
(THREAD_JAVA_OBJECT_OFFSET).loadAddress();
        return javaObjectPtr.isZero() ? null : javaObjectPtr.loadAddress();
}

2. java.lang.VMThreadManager.currentThread()

static Thread currentThread() {
    if (VMHelper.isVMMagicPackageSupported()) {
         Address currentThreadAddr = ThreadHelper.getCurrentThreadAddr();
         if (currentThreadAddr.isZero()) {
             return null;
         } else {
             return
(Thread)currentThreadAddr.toObjectReference().toObject();
         }
     }
     return currentThreadNative();
}

As you can see the 1st function is inline and the 2nd calls the 1st. The 1st
function can return null in case if thread don't attached yet to VM (for
instance if you call Thread.currentThread() during execution of constructor
of current Java thread).

Tested test is the following:

public class CurrentThreadTest {
    public static void main(String[] args) {
        long st = System.currentTimeMillis();
        for(int i=0; i< 100000000; i++) {
            Thread.currentThread();
        }
        long res = System.currentTimeMillis()-st;
        System.out.println("res="+res);
    }
}

Helper implemented in such way causes to crash in JIT on it (see [1] to
detailed stack).

Question is: Is it incorrect implementation of helper function? Where is the
problem?

I've tried to another version of helper which is not so good as the first
try:

1. org.apache.harmony.drlvm.thread.ThreadHelper.getCurrentThreadAddr()

@Inline
public static Address getCurrentThreadAddr() {
public static Address getCurrentThreadAddr() {
     Address tlsThread = VMHelper.getTlsBaseAddress();
     Address javaObjectPtr = tlsThread.plus
(THREAD_JAVA_OBJECT_OFFSET).loadAddress();
     return javaObjectPtr.loadAddress();
}

2. java.lang.VMThreadManager.currentThread()

static Thread currentThread() {
     if (VMHelper.isVMMagicPackageSupported()) {
         try {
             return
(Thread)ThreadHelper.getCurrentThreadAddr().toObjectReference().toObject();
         } catch (NullPointerException e) {
             return null;
         }
     }
     return currentThreadNative();
}

Second helper doesn't lead to crash but throws NullPointerException in

<<<< return
(Thread)ThreadHelper.getCurrentThreadAddr().toObjectReference().toObject();
>>>>

line.

Question is: Why trap doesn't catch NullPointerException? Helper problems?

I've modified getCurrentThreadAddr() function to force throwing exception
case:

1. org.apache.harmony.drlvm.thread.ThreadHelper.getCurrentThreadAddr()

@Inline
public static Address getCurrentThreadAddr() {
     Address tlsThread = VMHelper.getTlsBaseAddress();
     Address javaObjectPtr = tlsThread.plus
(THREAD_JAVA_OBJECT_OFFSET).loadAddress();
     if (javaObjectPtr != null) {
         return javaObjectPtr.loadAddress();
     } else {
         throw new NullPointerException();
     }
}

After it the test passes.

Could any JIT experts help me to understand the root of the problems?

[1] http://issues.apache.org/jira/browse/HARMONY-4555

-- 
Best regards,
Pavel Rebriy

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message