blob: d2f1349500a6cbd0f72ef9309ed96a767d55e29c [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"
19#include "thread.h"
20#include "thread_list.h"
21
22#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
23
24namespace art {
25
26namespace {
27
28jobject Thread_currentThread(JNIEnv* env, jclass) {
Elliott Hughesd369bb72011-09-12 14:41:14 -070029 return AddLocalReference<jobject>(env, Thread::Current()->GetPeer());
Elliott Hughes8daa0922011-09-11 13:46:25 -070030}
31
32jboolean Thread_interrupted(JNIEnv* env, jclass) {
33 return Thread::Current()->Interrupted();
34}
35
36jboolean Thread_isInterrupted(JNIEnv* env, jobject javaThread) {
37 ThreadListLock lock;
38 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070039 return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070040}
41
42void Thread_nativeCreate(JNIEnv* env, jclass, jobject javaThread, jlong stackSize) {
Elliott Hughesd369bb72011-09-12 14:41:14 -070043 Object* managedThread = Decode<Object*>(env, javaThread);
44 Thread::Create(managedThread, stackSize);
Elliott Hughes8daa0922011-09-11 13:46:25 -070045}
46
47jint Thread_nativeGetStatus(JNIEnv* env, jobject javaThread) {
48 ThreadListLock lock;
49 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070050 Thread::State state = (thread != NULL) ? thread->GetState() : Thread::kUnknown;
51 return static_cast<jint>(state);
Elliott Hughes8daa0922011-09-11 13:46:25 -070052}
53
54jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject javaThread, jobject javaObject) {
55 ThreadListLock lock;
Elliott Hughes5f791332011-09-15 17:45:30 -070056 Object* object = Decode<Object*>(env, javaObject);
57 if (object == NULL) {
58 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null");
59 return JNI_FALSE;
60 }
61 Thread* thread = Thread::FromManagedThread(env, javaThread);
62 return thread->HoldsLock(object);
Elliott Hughes8daa0922011-09-11 13:46:25 -070063}
64
65void Thread_nativeInterrupt(JNIEnv* env, jobject javaThread) {
66 ThreadListLock lock;
Elliott Hughes5f791332011-09-15 17:45:30 -070067 Thread* thread = Thread::FromManagedThread(env, javaThread);
68 if (thread != NULL) {
69 thread->Interrupt();
70 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070071}
72
73void Thread_nativeSetName(JNIEnv* env, jobject javaThread, jstring javaName) {
74 ThreadListLock lock;
Elliott Hughes5f791332011-09-15 17:45:30 -070075 // TODO: needed for debugging (DDMS) support.
Elliott Hughes8daa0922011-09-11 13:46:25 -070076 //Thread* thread = Thread::FromManagedThread(env, javaThread);
77 //StringObject* nameStr = (StringObject*) dvmDecodeIndirectRef(env, javaName);
78 //int threadId = (thread != NULL) ? thread->threadId : -1;
79 //dvmDdmSendThreadNameChange(threadId, nameStr);
80}
81
82/*
83 * Alter the priority of the specified thread. "newPriority" will range
84 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
85 * threads at Thread.NORM_PRIORITY (5).
86 */
87void Thread_nativeSetPriority(JNIEnv* env, jobject javaThread, jint newPriority) {
88 ThreadListLock lock;
89 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070090 if (thread != NULL) {
91 thread->SetNativePriority(newPriority);
92 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070093}
94
95/*
96 * Causes the thread to temporarily pause and allow other threads to execute.
97 *
98 * The exact behavior is poorly defined. Some discussion here:
99 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
100 */
101void Thread_yield(JNIEnv*, jobject) {
102 sched_yield();
103}
104
105static JNINativeMethod gMethods[] = {
106 NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"),
107 NATIVE_METHOD(Thread, interrupted, "()Z"),
108 NATIVE_METHOD(Thread, isInterrupted, "()Z"),
109 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;J)V"),
110 NATIVE_METHOD(Thread, nativeGetStatus, "()I"),
111 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
112 NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
113 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
114 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
115 NATIVE_METHOD(Thread, yield, "()V"),
116};
117
118} // namespace
119
120void register_java_lang_Thread(JNIEnv* env) {
121 jniRegisterNativeMethods(env, "java/lang/Thread", gMethods, NELEM(gMethods));
122}
123
124} // namespace art