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 | |
| 17 | #include "jni_internal.h" |
| 18 | #include "object.h" |
Elliott Hughes | 5464308 | 2011-09-25 17:48:00 -0700 | [diff] [blame] | 19 | #include "ScopedUtfChars.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 20 | #include "thread.h" |
| 21 | #include "thread_list.h" |
| 22 | |
| 23 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | 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 | |
| 33 | jboolean Thread_interrupted(JNIEnv* env, jclass) { |
| 34 | return Thread::Current()->Interrupted(); |
| 35 | } |
| 36 | |
| 37 | jboolean Thread_isInterrupted(JNIEnv* env, jobject javaThread) { |
| 38 | ThreadListLock lock; |
| 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 | |
| 43 | 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 | |
| 48 | jint Thread_nativeGetStatus(JNIEnv* env, jobject javaThread) { |
| 49 | ThreadListLock lock; |
| 50 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 51 | if (thread == NULL) { |
| 52 | return -1; |
| 53 | } |
| 54 | return static_cast<jint>(thread->GetState()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject javaThread, jobject javaObject) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 58 | Object* object = Decode<Object*>(env, javaObject); |
| 59 | if (object == NULL) { |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 60 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 61 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null"); |
| 62 | return JNI_FALSE; |
| 63 | } |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 64 | ThreadListLock lock; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 65 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
| 66 | return thread->HoldsLock(object); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void Thread_nativeInterrupt(JNIEnv* env, jobject javaThread) { |
| 70 | ThreadListLock lock; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 71 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
| 72 | if (thread != NULL) { |
| 73 | thread->Interrupt(); |
| 74 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void Thread_nativeSetName(JNIEnv* env, jobject javaThread, jstring javaName) { |
| 78 | ThreadListLock lock; |
Elliott Hughes | 5464308 | 2011-09-25 17:48:00 -0700 | [diff] [blame] | 79 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
| 80 | if (thread != NULL) { |
| 81 | ScopedUtfChars name(env, javaName); |
| 82 | if (name.c_str() == NULL) { |
| 83 | return; |
| 84 | } |
| 85 | LOG(INFO) << "Thread " << *thread << " changing name to '" << name.c_str() << "'"; |
| 86 | // TODO: needed for debugging (DDMS) support. |
| 87 | //StringObject* nameStr = (StringObject*) dvmDecodeIndirectRef(env, javaName); |
| 88 | //int threadId = (thread != NULL) ? thread->threadId : -1; |
| 89 | //dvmDdmSendThreadNameChange(threadId, nameStr); |
| 90 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Alter the priority of the specified thread. "newPriority" will range |
| 95 | * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal" |
| 96 | * threads at Thread.NORM_PRIORITY (5). |
| 97 | */ |
| 98 | void Thread_nativeSetPriority(JNIEnv* env, jobject javaThread, jint newPriority) { |
| 99 | ThreadListLock lock; |
| 100 | Thread* thread = Thread::FromManagedThread(env, javaThread); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 101 | if (thread != NULL) { |
| 102 | thread->SetNativePriority(newPriority); |
| 103 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Causes the thread to temporarily pause and allow other threads to execute. |
| 108 | * |
| 109 | * The exact behavior is poorly defined. Some discussion here: |
| 110 | * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html |
| 111 | */ |
| 112 | void Thread_yield(JNIEnv*, jobject) { |
| 113 | sched_yield(); |
| 114 | } |
| 115 | |
| 116 | static JNINativeMethod gMethods[] = { |
| 117 | NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"), |
| 118 | NATIVE_METHOD(Thread, interrupted, "()Z"), |
| 119 | NATIVE_METHOD(Thread, isInterrupted, "()Z"), |
| 120 | NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;J)V"), |
| 121 | NATIVE_METHOD(Thread, nativeGetStatus, "()I"), |
| 122 | NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"), |
| 123 | NATIVE_METHOD(Thread, nativeInterrupt, "()V"), |
| 124 | NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"), |
| 125 | NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"), |
| 126 | NATIVE_METHOD(Thread, yield, "()V"), |
| 127 | }; |
| 128 | |
| 129 | } // namespace |
| 130 | |
| 131 | void register_java_lang_Thread(JNIEnv* env) { |
| 132 | jniRegisterNativeMethods(env, "java/lang/Thread", gMethods, NELEM(gMethods)); |
| 133 | } |
| 134 | |
| 135 | } // namespace art |