Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 17 | #include "debugger.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 18 | #include "jni_internal.h" |
| 19 | #include "object.h" |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 20 | #include "scoped_thread_list_lock.h" |
Elliott Hughes | 5464308 | 2011-09-25 17:48:00 -0700 | [diff] [blame] | 21 | #include "ScopedUtfChars.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 22 | #include "thread.h" |
| 23 | #include "thread_list.h" |
| 24 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 27 | static jobject Thread_currentThread(JNIEnv* env, jclass) { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 28 | return AddLocalReference<jobject>(env, Thread::Current()->GetPeer()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 31 | static jboolean Thread_interrupted(JNIEnv*, jclass) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 32 | return Thread::Current()->Interrupted(); |
| 33 | } |
| 34 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 35 | static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 36 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 37 | Thread* thread = Thread::FromManagedThread(env, java_thread); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 38 | return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 41 | static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size) { |
| 42 | Object* managedThread = Decode<Object*>(env, java_thread); |
| 43 | Thread::CreateNativeThread(managedThread, stack_size); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 46 | static jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) { |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 47 | // Ordinals from Java's Thread.State. |
| 48 | const jint kJavaNew = 0; |
| 49 | const jint kJavaRunnable = 1; |
| 50 | const jint kJavaBlocked = 2; |
| 51 | const jint kJavaWaiting = 3; |
| 52 | const jint kJavaTimedWaiting = 4; |
| 53 | const jint kJavaTerminated = 5; |
| 54 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 55 | ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting); |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 56 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 57 | Thread* thread = Thread::FromManagedThread(env, java_thread); |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 58 | if (thread != NULL) { |
| 59 | internal_thread_state = thread->GetState(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 60 | } |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 61 | switch (internal_thread_state) { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 62 | case kTerminated: return kJavaTerminated; |
| 63 | case kRunnable: return kJavaRunnable; |
| 64 | case kTimedWaiting: return kJavaTimedWaiting; |
| 65 | case kBlocked: return kJavaBlocked; |
| 66 | case kWaiting: return kJavaWaiting; |
| 67 | case kStarting: return kJavaNew; |
| 68 | case kNative: return kJavaRunnable; |
| 69 | case kVmWait: return kJavaWaiting; |
| 70 | case kSuspended: return kJavaRunnable; |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 71 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 72 | } |
| 73 | return -1; // Unreachable. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 76 | static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) { |
| 77 | Object* object = Decode<Object*>(env, java_object); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 78 | if (object == NULL) { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 79 | ScopedThreadStateChange tsc(Thread::Current(), kRunnable); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 80 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null"); |
| 81 | return JNI_FALSE; |
| 82 | } |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 83 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 84 | Thread* thread = Thread::FromManagedThread(env, java_thread); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 85 | return thread->HoldsLock(object); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 88 | static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 89 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 90 | Thread* thread = Thread::FromManagedThread(env, java_thread); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 91 | if (thread != NULL) { |
| 92 | thread->Interrupt(); |
| 93 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 96 | static void Thread_nativeSetName(JNIEnv* env, jobject java_thread, jstring java_name) { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 97 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 98 | Thread* thread = Thread::FromManagedThread(env, java_thread); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 99 | if (thread == NULL) { |
| 100 | return; |
Elliott Hughes | 5464308 | 2011-09-25 17:48:00 -0700 | [diff] [blame] | 101 | } |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 102 | ScopedUtfChars name(env, java_name); |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 103 | if (name.c_str() == NULL) { |
| 104 | return; |
| 105 | } |
| 106 | thread->SetThreadName(name.c_str()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 110 | * Alter the priority of the specified thread. "new_priority" will range |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 111 | * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal" |
| 112 | * threads at Thread.NORM_PRIORITY (5). |
| 113 | */ |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 114 | static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 115 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 116 | Thread* thread = Thread::FromManagedThread(env, java_thread); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 117 | if (thread != NULL) { |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame^] | 118 | thread->SetNativePriority(new_priority); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 119 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Causes the thread to temporarily pause and allow other threads to execute. |
| 124 | * |
| 125 | * The exact behavior is poorly defined. Some discussion here: |
| 126 | * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html |
| 127 | */ |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 128 | static void Thread_yield(JNIEnv*, jobject) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 129 | sched_yield(); |
| 130 | } |
| 131 | |
| 132 | static JNINativeMethod gMethods[] = { |
| 133 | NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"), |
| 134 | NATIVE_METHOD(Thread, interrupted, "()Z"), |
| 135 | NATIVE_METHOD(Thread, isInterrupted, "()Z"), |
| 136 | NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;J)V"), |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 137 | NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 138 | NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"), |
| 139 | NATIVE_METHOD(Thread, nativeInterrupt, "()V"), |
| 140 | NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"), |
| 141 | NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"), |
| 142 | NATIVE_METHOD(Thread, yield, "()V"), |
| 143 | }; |
| 144 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 145 | void register_java_lang_Thread(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 146 | REGISTER_NATIVE_METHODS("java/lang/Thread"); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | } // namespace art |