blob: b1da60d8a0c9841d4fb2e3faec837893eb397fbe [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
17#include "jni_internal.h"
18#include "object.h"
Elliott Hughes54643082011-09-25 17:48:00 -070019#include "ScopedUtfChars.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070020#include "thread.h"
21#include "thread_list.h"
22
23#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
24
25namespace art {
26
27namespace {
28
29jobject Thread_currentThread(JNIEnv* env, jclass) {
Elliott Hughesd369bb72011-09-12 14:41:14 -070030 return AddLocalReference<jobject>(env, Thread::Current()->GetPeer());
Elliott Hughes8daa0922011-09-11 13:46:25 -070031}
32
33jboolean Thread_interrupted(JNIEnv* env, jclass) {
34 return Thread::Current()->Interrupted();
35}
36
37jboolean Thread_isInterrupted(JNIEnv* env, jobject javaThread) {
38 ThreadListLock lock;
39 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070040 return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070041}
42
43void Thread_nativeCreate(JNIEnv* env, jclass, jobject javaThread, jlong stackSize) {
Elliott Hughesd369bb72011-09-12 14:41:14 -070044 Object* managedThread = Decode<Object*>(env, javaThread);
45 Thread::Create(managedThread, stackSize);
Elliott Hughes8daa0922011-09-11 13:46:25 -070046}
47
48jint Thread_nativeGetStatus(JNIEnv* env, jobject javaThread) {
49 ThreadListLock lock;
50 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070051 Thread::State state = (thread != NULL) ? thread->GetState() : Thread::kUnknown;
52 return static_cast<jint>(state);
Elliott Hughes8daa0922011-09-11 13:46:25 -070053}
54
55jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject javaThread, jobject javaObject) {
56 ThreadListLock lock;
Elliott Hughes5f791332011-09-15 17:45:30 -070057 Object* object = Decode<Object*>(env, javaObject);
58 if (object == NULL) {
59 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null");
60 return JNI_FALSE;
61 }
62 Thread* thread = Thread::FromManagedThread(env, javaThread);
63 return thread->HoldsLock(object);
Elliott Hughes8daa0922011-09-11 13:46:25 -070064}
65
66void Thread_nativeInterrupt(JNIEnv* env, jobject javaThread) {
67 ThreadListLock lock;
Elliott Hughes5f791332011-09-15 17:45:30 -070068 Thread* thread = Thread::FromManagedThread(env, javaThread);
69 if (thread != NULL) {
70 thread->Interrupt();
71 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070072}
73
74void Thread_nativeSetName(JNIEnv* env, jobject javaThread, jstring javaName) {
75 ThreadListLock lock;
Elliott Hughes54643082011-09-25 17:48:00 -070076 Thread* thread = Thread::FromManagedThread(env, javaThread);
77 if (thread != NULL) {
78 ScopedUtfChars name(env, javaName);
79 if (name.c_str() == NULL) {
80 return;
81 }
82 LOG(INFO) << "Thread " << *thread << " changing name to '" << name.c_str() << "'";
83 // TODO: needed for debugging (DDMS) support.
84 //StringObject* nameStr = (StringObject*) dvmDecodeIndirectRef(env, javaName);
85 //int threadId = (thread != NULL) ? thread->threadId : -1;
86 //dvmDdmSendThreadNameChange(threadId, nameStr);
87 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070088}
89
90/*
91 * Alter the priority of the specified thread. "newPriority" will range
92 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
93 * threads at Thread.NORM_PRIORITY (5).
94 */
95void Thread_nativeSetPriority(JNIEnv* env, jobject javaThread, jint newPriority) {
96 ThreadListLock lock;
97 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070098 if (thread != NULL) {
99 thread->SetNativePriority(newPriority);
100 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700101}
102
103/*
104 * Causes the thread to temporarily pause and allow other threads to execute.
105 *
106 * The exact behavior is poorly defined. Some discussion here:
107 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
108 */
109void Thread_yield(JNIEnv*, jobject) {
110 sched_yield();
111}
112
113static JNINativeMethod gMethods[] = {
114 NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"),
115 NATIVE_METHOD(Thread, interrupted, "()Z"),
116 NATIVE_METHOD(Thread, isInterrupted, "()Z"),
117 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;J)V"),
118 NATIVE_METHOD(Thread, nativeGetStatus, "()I"),
119 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
120 NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
121 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
122 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
123 NATIVE_METHOD(Thread, yield, "()V"),
124};
125
126} // namespace
127
128void register_java_lang_Thread(JNIEnv* env) {
129 jniRegisterNativeMethods(env, "java/lang/Thread", gMethods, NELEM(gMethods));
130}
131
132} // namespace art