blob: 0338454cf47d0e71bdbdae641293bd21b55d1809 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
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 Hughes82188472011-11-07 18:11:48 -080017#include "debugger.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070018#include "jni_internal.h"
19#include "object.h"
Elliott Hughes54643082011-09-25 17:48:00 -070020#include "ScopedUtfChars.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070021#include "thread.h"
22#include "thread_list.h"
23
24#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
25
26namespace art {
27
28namespace {
29
30jobject Thread_currentThread(JNIEnv* env, jclass) {
Elliott Hughesd369bb72011-09-12 14:41:14 -070031 return AddLocalReference<jobject>(env, Thread::Current()->GetPeer());
Elliott Hughes8daa0922011-09-11 13:46:25 -070032}
33
34jboolean Thread_interrupted(JNIEnv* env, jclass) {
35 return Thread::Current()->Interrupted();
36}
37
38jboolean Thread_isInterrupted(JNIEnv* env, jobject javaThread) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080039 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -070040 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070041 return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070042}
43
44void Thread_nativeCreate(JNIEnv* env, jclass, jobject javaThread, jlong stackSize) {
Elliott Hughesd369bb72011-09-12 14:41:14 -070045 Object* managedThread = Decode<Object*>(env, javaThread);
46 Thread::Create(managedThread, stackSize);
Elliott Hughes8daa0922011-09-11 13:46:25 -070047}
48
49jint Thread_nativeGetStatus(JNIEnv* env, jobject javaThread) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080050 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -070051 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughes8e4aac52011-09-26 17:03:36 -070052 if (thread == NULL) {
53 return -1;
54 }
55 return static_cast<jint>(thread->GetState());
Elliott Hughes8daa0922011-09-11 13:46:25 -070056}
57
58jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject javaThread, jobject javaObject) {
Elliott Hughes5f791332011-09-15 17:45:30 -070059 Object* object = Decode<Object*>(env, javaObject);
60 if (object == NULL) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070061 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes5f791332011-09-15 17:45:30 -070062 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null");
63 return JNI_FALSE;
64 }
Elliott Hughesbbd9d832011-11-07 14:40:00 -080065 ScopedThreadListLock thread_list_lock;
Elliott Hughes5f791332011-09-15 17:45:30 -070066 Thread* thread = Thread::FromManagedThread(env, javaThread);
67 return thread->HoldsLock(object);
Elliott Hughes8daa0922011-09-11 13:46:25 -070068}
69
70void Thread_nativeInterrupt(JNIEnv* env, jobject javaThread) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080071 ScopedThreadListLock thread_list_lock;
Elliott Hughes5f791332011-09-15 17:45:30 -070072 Thread* thread = Thread::FromManagedThread(env, javaThread);
73 if (thread != NULL) {
74 thread->Interrupt();
75 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070076}
77
Elliott Hughes899e7892012-01-24 14:57:32 -080078void Thread_nativeSetName(JNIEnv* env, jobject javaThread, jstring javaName) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080079 ScopedThreadListLock thread_list_lock;
Elliott Hughes54643082011-09-25 17:48:00 -070080 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughes82188472011-11-07 18:11:48 -080081 if (thread == NULL) {
82 return;
Elliott Hughes54643082011-09-25 17:48:00 -070083 }
Elliott Hughes899e7892012-01-24 14:57:32 -080084 ScopedUtfChars name(env, javaName);
85 if (name.c_str() == NULL) {
86 return;
87 }
88 thread->SetThreadName(name.c_str());
Elliott Hughes8daa0922011-09-11 13:46:25 -070089}
90
91/*
92 * Alter the priority of the specified thread. "newPriority" will range
93 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
94 * threads at Thread.NORM_PRIORITY (5).
95 */
96void Thread_nativeSetPriority(JNIEnv* env, jobject javaThread, jint newPriority) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080097 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -070098 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070099 if (thread != NULL) {
100 thread->SetNativePriority(newPriority);
101 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700102}
103
104/*
105 * Causes the thread to temporarily pause and allow other threads to execute.
106 *
107 * The exact behavior is poorly defined. Some discussion here:
108 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
109 */
110void Thread_yield(JNIEnv*, jobject) {
111 sched_yield();
112}
113
114static JNINativeMethod gMethods[] = {
115 NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"),
116 NATIVE_METHOD(Thread, interrupted, "()Z"),
117 NATIVE_METHOD(Thread, isInterrupted, "()Z"),
118 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;J)V"),
119 NATIVE_METHOD(Thread, nativeGetStatus, "()I"),
120 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
121 NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
122 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
123 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
124 NATIVE_METHOD(Thread, yield, "()V"),
125};
126
127} // namespace
128
129void register_java_lang_Thread(JNIEnv* env) {
130 jniRegisterNativeMethods(env, "java/lang/Thread", gMethods, NELEM(gMethods));
131}
132
133} // namespace art