implement Thread.getStackTrace, activeCount, and enumerate

This commit is contained in:
Joel Dice 2008-04-11 16:48:39 -06:00
parent 04c3abc967
commit d9ec8e20bf
5 changed files with 62 additions and 1 deletions

View File

@ -108,4 +108,14 @@ public class Thread implements Runnable {
t.sleepLock.wait(milliseconds);
}
}
public StackTraceElement[] getStackTrace() {
return Throwable.resolveTrace(getStackTrace(peer));
}
private static native Object getStackTrace(long peer);
public static native int activeCount();
public static native int enumerate(Thread[] array);
}

View File

@ -65,7 +65,7 @@ public class Throwable {
private static native Object trace(int skipCount);
private static native StackTraceElement[] resolveTrace(Object trace);
static native StackTraceElement[] resolveTrace(Object trace);
private StackTraceElement[] resolveTrace() {
if (! (trace instanceof StackTraceElement[])) {

View File

@ -47,6 +47,19 @@ search(Thread* t, jstring name, object (*op)(Thread*, object),
}
}
void
enumerateThreads(Thread* t, Thread* x, object array, unsigned* index,
unsigned limit)
{
if (*index < limit) {
set(t, array, ArrayBody + (*index * BytesPerWord), x->javaThread);
if (x->peer) enumerateThreads(t, x->peer, array, index, limit);
if (x->child) enumerateThreads(t, x->child, array, index, limit);
}
}
} // namespace
extern "C" JNIEXPORT jstring JNICALL
@ -697,6 +710,36 @@ Java_java_lang_Thread_interrupt(Thread* t, jclass, jlong peer)
interrupt(t, reinterpret_cast<Thread*>(peer));
}
extern "C" JNIEXPORT jobject JNICALL
Java_java_lang_Thread_getTrace(Thread* t, jclass, jlong peer)
{
if (reinterpret_cast<Thread*>(peer) == t) {
return makeLocalReference(t, makeTrace(t));
} else {
return makeLocalReference
(t, t->m->processor->getStackTrace(t, reinterpret_cast<Thread*>(peer)));
}
}
extern "C" JNIEXPORT jint JNICALL
Java_java_lang_Thread_activeCount(Thread* t, jclass)
{
return t->m->liveCount;
}
extern "C" JNIEXPORT jint JNICALL
Java_java_lang_Thread_enumerate(Thread* t, jclass, jobjectArray array)
{
ACQUIRE_RAW(t, t->m->stateLock);
ENTER(t, Thread::ActiveState);
unsigned count = min(t->m->liveCount, objectArrayLength(t, *array));
unsigned index = 0;
enumerateThreads(t, t->m->rootThread, *array, &index, count);
return count;
}
extern "C" JNIEXPORT jlong JNICALL
Java_java_net_URL_00024ResourceInputStream_open
(Thread* t, jclass, jstring path)

View File

@ -3051,6 +3051,11 @@ class MyProcessor: public Processor {
}
}
virtual object getStackTrace(Thread*, Thread*) {
// not implemented
return 0;
}
virtual void dispose(vm::Thread* t) {
t->m->heap->free(t, sizeof(Thread), false);
}

View File

@ -115,6 +115,9 @@ class Processor {
virtual void
dispose() = 0;
virtual object
getStackTrace(Thread* t, Thread* target) = 0;
object
invoke(Thread* t, object method, object this_, ...)
{