J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004-2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | #include "jni.h" |
| 27 | #include "jni_util.h" |
| 28 | #include "jlong.h" |
| 29 | #include "jvm.h" |
| 30 | #include "jdk_util.h" |
| 31 | |
| 32 | #include "sun_misc_VM.h" |
| 33 | |
| 34 | typedef jintArray (JNICALL *GET_THREAD_STATE_VALUES_FN)(JNIEnv *, jint); |
| 35 | typedef jobjectArray (JNICALL *GET_THREAD_STATE_NAMES_FN)(JNIEnv *, jint, jintArray); |
| 36 | |
| 37 | static GET_THREAD_STATE_VALUES_FN GetThreadStateValues_fp = NULL; |
| 38 | static GET_THREAD_STATE_NAMES_FN GetThreadStateNames_fp = NULL; |
| 39 | |
| 40 | static void get_thread_state_info(JNIEnv *env, jint state, |
| 41 | jobjectArray stateValues, |
| 42 | jobjectArray stateNames) { |
| 43 | char errmsg[128]; |
| 44 | jintArray values; |
| 45 | jobjectArray names; |
| 46 | |
| 47 | values = (*GetThreadStateValues_fp)(env, state); |
| 48 | if (values == NULL) { |
| 49 | sprintf(errmsg, "Mismatched VM version: Thread state (%d) " |
| 50 | "not supported", state); |
| 51 | JNU_ThrowInternalError(env, errmsg); |
| 52 | return; |
| 53 | } |
| 54 | /* state is also used as the index in the array */ |
| 55 | (*env)->SetObjectArrayElement(env, stateValues, state, values); |
| 56 | |
| 57 | names = (*GetThreadStateNames_fp)(env, state, values); |
| 58 | if (names == NULL) { |
| 59 | sprintf(errmsg, "Mismatched VM version: Thread state (%d) " |
| 60 | "not supported", state); |
| 61 | JNU_ThrowInternalError(env, errmsg); |
| 62 | return; |
| 63 | } |
| 64 | (*env)->SetObjectArrayElement(env, stateNames, state, names); |
| 65 | } |
| 66 | |
| 67 | JNIEXPORT void JNICALL |
| 68 | Java_sun_misc_VM_getThreadStateValues(JNIEnv *env, jclass cls, |
| 69 | jobjectArray values, |
| 70 | jobjectArray names) |
| 71 | { |
| 72 | char errmsg[128]; |
| 73 | |
| 74 | // check if the number of Thread.State enum constants |
| 75 | // matches the number of states defined in jvm.h |
| 76 | jsize len1 = (*env)->GetArrayLength(env, values); |
| 77 | jsize len2 = (*env)->GetArrayLength(env, names); |
| 78 | if (len1 != JAVA_THREAD_STATE_COUNT || len2 != JAVA_THREAD_STATE_COUNT) { |
| 79 | sprintf(errmsg, "Mismatched VM version: JAVA_THREAD_STATE_COUNT = %d " |
| 80 | " but JDK expects %d / %d", |
| 81 | JAVA_THREAD_STATE_COUNT, len1, len2); |
| 82 | JNU_ThrowInternalError(env, errmsg); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | if (GetThreadStateValues_fp == NULL) { |
| 87 | GetThreadStateValues_fp = (GET_THREAD_STATE_VALUES_FN) |
| 88 | JDK_FindJvmEntry("JVM_GetThreadStateValues"); |
| 89 | if (GetThreadStateValues_fp == NULL) { |
| 90 | JNU_ThrowInternalError(env, |
| 91 | "Mismatched VM version: JVM_GetThreadStateValues not found"); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | GetThreadStateNames_fp = (GET_THREAD_STATE_NAMES_FN) |
| 96 | JDK_FindJvmEntry("JVM_GetThreadStateNames"); |
| 97 | if (GetThreadStateNames_fp == NULL) { |
| 98 | JNU_ThrowInternalError(env, |
| 99 | "Mismatched VM version: JVM_GetThreadStateNames not found"); |
| 100 | return ; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | get_thread_state_info(env, JAVA_THREAD_STATE_NEW, values, names); |
| 105 | get_thread_state_info(env, JAVA_THREAD_STATE_RUNNABLE, values, names); |
| 106 | get_thread_state_info(env, JAVA_THREAD_STATE_BLOCKED, values, names); |
| 107 | get_thread_state_info(env, JAVA_THREAD_STATE_WAITING, values, names); |
| 108 | get_thread_state_info(env, JAVA_THREAD_STATE_TIMED_WAITING, values, names); |
| 109 | get_thread_state_info(env, JAVA_THREAD_STATE_TERMINATED, values, names); |
| 110 | } |
| 111 | |
| 112 | JNIEXPORT void JNICALL |
| 113 | Java_sun_misc_VM_initialize(JNIEnv *env, jclass cls) { |
| 114 | char errmsg[128]; |
| 115 | |
| 116 | if (!JDK_InitJvmHandle()) { |
| 117 | JNU_ThrowInternalError(env, "Handle for JVM not found for symbol lookup"); |
| 118 | } |
| 119 | } |