java_lang_Thread.cc revision 57aba86f29d7e795bf7e68c65cc464d2291b6af1
18daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes/* 28daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Copyright (C) 2008 The Android Open Source Project 38daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * 48daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 58daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * you may not use this file except in compliance with the License. 68daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * You may obtain a copy of the License at 78daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * 88daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 98daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * 108daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Unless required by applicable law or agreed to in writing, software 118daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 128daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * See the License for the specific language governing permissions and 148daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * limitations under the License. 158daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes */ 168daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 178218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes#include "debugger.h" 188daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "jni_internal.h" 198daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "object.h" 2088c5c355fc3d881f905564911d746b2313d5fc89Elliott Hughes#include "scoped_thread_list_lock.h" 2154643083afd5b99f4d52a32b4030aec0db8d0e2fElliott Hughes#include "ScopedUtfChars.h" 228daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "thread.h" 238daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "thread_list.h" 248daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 258daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesnamespace art { 268daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 270512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic jobject Thread_currentThread(JNIEnv* env, jclass) { 28d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes return AddLocalReference<jobject>(env, Thread::Current()->GetPeer()); 298daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} 308daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 311bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughesstatic jboolean Thread_interrupted(JNIEnv*, jclass) { 328daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes return Thread::Current()->Interrupted(); 338daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} 348daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 3557aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughesstatic jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) { 36bbd9d830fe0eb8ce44405d7504dcf9a6fe91ffa1Elliott Hughes ScopedThreadListLock thread_list_lock; 3757aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes Thread* thread = Thread::FromManagedThread(env, java_thread); 38d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE; 398daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} 408daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 4157aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughesstatic void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size) { 4257aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes Object* managedThread = Decode<Object*>(env, java_thread); 4357aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes Thread::CreateNativeThread(managedThread, stack_size); 448daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} 458daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 4657aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughesstatic jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) { 47cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes // Ordinals from Java's Thread.State. 48cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes const jint kJavaNew = 0; 49cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes const jint kJavaRunnable = 1; 50cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes const jint kJavaBlocked = 2; 51cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes const jint kJavaWaiting = 3; 52cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes const jint kJavaTimedWaiting = 4; 53cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes const jint kJavaTerminated = 5; 54cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes 5557aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting); 56bbd9d830fe0eb8ce44405d7504dcf9a6fe91ffa1Elliott Hughes ScopedThreadListLock thread_list_lock; 5757aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes Thread* thread = Thread::FromManagedThread(env, java_thread); 58cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes if (thread != NULL) { 59cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes internal_thread_state = thread->GetState(); 60cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes } 61cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes switch (internal_thread_state) { 6234e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes case kTerminated: return kJavaTerminated; 6334e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes case kRunnable: return kJavaRunnable; 6434e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes case kTimedWaiting: return kJavaTimedWaiting; 6534e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes case kBlocked: return kJavaBlocked; 6634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes case kWaiting: return kJavaWaiting; 6734e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes case kStarting: return kJavaNew; 6834e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes case kNative: return kJavaRunnable; 6934e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes case kVmWait: return kJavaWaiting; 7034e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes case kSuspended: return kJavaRunnable; 71cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes // Don't add a 'default' here so the compiler can spot incompatible enum changes. 728e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 73cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes return -1; // Unreachable. 748daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} 758daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 7657aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughesstatic jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) { 7757aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes Object* object = Decode<Object*>(env, java_object); 785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (object == NULL) { 7934e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ScopedThreadStateChange tsc(Thread::Current(), kRunnable); 805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null"); 815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return JNI_FALSE; 825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 83bbd9d830fe0eb8ce44405d7504dcf9a6fe91ffa1Elliott Hughes ScopedThreadListLock thread_list_lock; 8457aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes Thread* thread = Thread::FromManagedThread(env, java_thread); 855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return thread->HoldsLock(object); 868daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} 878daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 8857aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughesstatic void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) { 89bbd9d830fe0eb8ce44405d7504dcf9a6fe91ffa1Elliott Hughes ScopedThreadListLock thread_list_lock; 9057aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes Thread* thread = Thread::FromManagedThread(env, java_thread); 915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (thread != NULL) { 925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->Interrupt(); 935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 948daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} 958daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 9657aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughesstatic void Thread_nativeSetName(JNIEnv* env, jobject java_thread, jstring java_name) { 97bbd9d830fe0eb8ce44405d7504dcf9a6fe91ffa1Elliott Hughes ScopedThreadListLock thread_list_lock; 9857aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes Thread* thread = Thread::FromManagedThread(env, java_thread); 998218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes if (thread == NULL) { 1008218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes return; 10154643083afd5b99f4d52a32b4030aec0db8d0e2fElliott Hughes } 10257aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes ScopedUtfChars name(env, java_name); 103899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes if (name.c_str() == NULL) { 104899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes return; 105899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes } 106899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes thread->SetThreadName(name.c_str()); 1078daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} 1088daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 1098daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes/* 11057aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes * Alter the priority of the specified thread. "new_priority" will range 1118daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal" 1128daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * threads at Thread.NORM_PRIORITY (5). 1138daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes */ 11457aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughesstatic void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) { 115bbd9d830fe0eb8ce44405d7504dcf9a6fe91ffa1Elliott Hughes ScopedThreadListLock thread_list_lock; 11657aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes Thread* thread = Thread::FromManagedThread(env, java_thread); 117d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (thread != NULL) { 11857aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes thread->SetNativePriority(new_priority); 119d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 1208daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} 1218daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 1228daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes/* 1238daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * Causes the thread to temporarily pause and allow other threads to execute. 1248daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * 1258daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * The exact behavior is poorly defined. Some discussion here: 1268daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html 1278daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes */ 1280512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesstatic void Thread_yield(JNIEnv*, jobject) { 1298daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes sched_yield(); 1308daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} 1318daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 1328daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesstatic JNINativeMethod gMethods[] = { 1338daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"), 1348daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes NATIVE_METHOD(Thread, interrupted, "()Z"), 1358daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes NATIVE_METHOD(Thread, isInterrupted, "()Z"), 1368daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;J)V"), 137cf2b2d44070de8a3baa2c19ebf3d8cef2ad7fd5bElliott Hughes NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"), 1388daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"), 1398daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes NATIVE_METHOD(Thread, nativeInterrupt, "()V"), 1408daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"), 1418daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"), 1428daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes NATIVE_METHOD(Thread, yield, "()V"), 1438daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes}; 1448daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 1458daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesvoid register_java_lang_Thread(JNIEnv* env) { 146eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes REGISTER_NATIVE_METHODS("java/lang/Thread"); 1478daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} 1488daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes 1498daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 150