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