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 | |
| 25 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 26 | |
| 27 | namespace art { |
| 28 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 29 | static jobject Thread_currentThread(JNIEnv* env, jclass) { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 30 | return AddLocalReference<jobject>(env, Thread::Current()->GetPeer()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 33 | static jboolean Thread_interrupted(JNIEnv*, jclass) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 34 | return Thread::Current()->Interrupted(); |
| 35 | } |
| 36 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 37 | static jboolean Thread_isInterrupted(JNIEnv* env, jobject javaThread) { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 38 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 39 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 40 | return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 43 | static void Thread_nativeCreate(JNIEnv* env, jclass, jobject javaThread, jlong stackSize) { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 44 | Object* managedThread = Decode<Object*>(env, javaThread); |
| 45 | Thread::Create(managedThread, stackSize); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 48 | static jint Thread_nativeGetStatus(JNIEnv* env, jobject javaThread, jboolean hasBeenStarted) { |
| 49 | // Ordinals from Java's Thread.State. |
| 50 | const jint kJavaNew = 0; |
| 51 | const jint kJavaRunnable = 1; |
| 52 | const jint kJavaBlocked = 2; |
| 53 | const jint kJavaWaiting = 3; |
| 54 | const jint kJavaTimedWaiting = 4; |
| 55 | const jint kJavaTerminated = 5; |
| 56 | |
| 57 | Thread::State internal_thread_state = (hasBeenStarted ? Thread::kTerminated : Thread::kStarting); |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 58 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 59 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 60 | if (thread != NULL) { |
| 61 | internal_thread_state = thread->GetState(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 62 | } |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 63 | switch (internal_thread_state) { |
| 64 | case Thread::kTerminated: return kJavaTerminated; |
| 65 | case Thread::kRunnable: return kJavaRunnable; |
| 66 | case Thread::kTimedWaiting: return kJavaTimedWaiting; |
| 67 | case Thread::kBlocked: return kJavaBlocked; |
| 68 | case Thread::kWaiting: return kJavaWaiting; |
| 69 | case Thread::kStarting: return kJavaNew; |
| 70 | case Thread::kNative: return kJavaRunnable; |
| 71 | case Thread::kVmWait: return kJavaWaiting; |
| 72 | case Thread::kSuspended: return kJavaRunnable; |
| 73 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 74 | } |
| 75 | return -1; // Unreachable. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 78 | static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject javaThread, jobject javaObject) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 79 | Object* object = Decode<Object*>(env, javaObject); |
| 80 | if (object == NULL) { |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 81 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 82 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null"); |
| 83 | return JNI_FALSE; |
| 84 | } |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 85 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 86 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
| 87 | return thread->HoldsLock(object); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 90 | static void Thread_nativeInterrupt(JNIEnv* env, jobject javaThread) { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 91 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 92 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
| 93 | if (thread != NULL) { |
| 94 | thread->Interrupt(); |
| 95 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 98 | static void Thread_nativeSetName(JNIEnv* env, jobject javaThread, jstring javaName) { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 99 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 5464308 | 2011-09-25 17:48:00 -0700 | [diff] [blame] | 100 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 101 | if (thread == NULL) { |
| 102 | return; |
Elliott Hughes | 5464308 | 2011-09-25 17:48:00 -0700 | [diff] [blame] | 103 | } |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 104 | ScopedUtfChars name(env, javaName); |
| 105 | if (name.c_str() == NULL) { |
| 106 | return; |
| 107 | } |
| 108 | thread->SetThreadName(name.c_str()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Alter the priority of the specified thread. "newPriority" will range |
| 113 | * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal" |
| 114 | * threads at Thread.NORM_PRIORITY (5). |
| 115 | */ |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 116 | static void Thread_nativeSetPriority(JNIEnv* env, jobject javaThread, jint newPriority) { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 117 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 118 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 119 | if (thread != NULL) { |
| 120 | thread->SetNativePriority(newPriority); |
| 121 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Causes the thread to temporarily pause and allow other threads to execute. |
| 126 | * |
| 127 | * The exact behavior is poorly defined. Some discussion here: |
| 128 | * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html |
| 129 | */ |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 130 | static void Thread_yield(JNIEnv*, jobject) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 131 | sched_yield(); |
| 132 | } |
| 133 | |
| 134 | static JNINativeMethod gMethods[] = { |
| 135 | NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"), |
| 136 | NATIVE_METHOD(Thread, interrupted, "()Z"), |
| 137 | NATIVE_METHOD(Thread, isInterrupted, "()Z"), |
| 138 | NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;J)V"), |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 139 | NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 140 | NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"), |
| 141 | NATIVE_METHOD(Thread, nativeInterrupt, "()V"), |
| 142 | NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"), |
| 143 | NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"), |
| 144 | NATIVE_METHOD(Thread, yield, "()V"), |
| 145 | }; |
| 146 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 147 | void register_java_lang_Thread(JNIEnv* env) { |
| 148 | jniRegisterNativeMethods(env, "java/lang/Thread", gMethods, NELEM(gMethods)); |
| 149 | } |
| 150 | |
| 151 | } // namespace art |