blob: 6ce260921449cde384353d9f904f430828d7992a [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 Hughes88c5c352012-03-15 18:49:48 -070020#include "scoped_thread_list_lock.h"
Elliott Hughes54643082011-09-25 17:48:00 -070021#include "ScopedUtfChars.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070022#include "thread.h"
23#include "thread_list.h"
24
25#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
26
27namespace art {
28
Elliott Hughes0512f022012-03-15 22:10:52 -070029static jobject 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
Elliott Hughes1bac54f2012-03-16 12:48:31 -070033static jboolean Thread_interrupted(JNIEnv*, jclass) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070034 return Thread::Current()->Interrupted();
35}
36
Elliott Hughes0512f022012-03-15 22:10:52 -070037static jboolean Thread_isInterrupted(JNIEnv* env, jobject javaThread) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080038 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -070039 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
Elliott Hughes0512f022012-03-15 22:10:52 -070043static void 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
Elliott Hughescf2b2d42012-03-27 17:11:42 -070048static jint Thread_nativeGetStatus(JNIEnv* env, jobject javaThread, jboolean hasBeenStarted) {
49 // Ordinals from Java's Thread.State.
50 const jint kJavaNew = 0;
51 const jint kJavaRunnable = 1;
52 const jint kJavaBlocked = 2;
53 const jint kJavaWaiting = 3;
54 const jint kJavaTimedWaiting = 4;
55 const jint kJavaTerminated = 5;
56
57 Thread::State internal_thread_state = (hasBeenStarted ? Thread::kTerminated : Thread::kStarting);
Elliott Hughesbbd9d832011-11-07 14:40:00 -080058 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -070059 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughescf2b2d42012-03-27 17:11:42 -070060 if (thread != NULL) {
61 internal_thread_state = thread->GetState();
Elliott Hughes8e4aac52011-09-26 17:03:36 -070062 }
Elliott Hughescf2b2d42012-03-27 17:11:42 -070063 switch (internal_thread_state) {
64 case Thread::kTerminated: return kJavaTerminated;
65 case Thread::kRunnable: return kJavaRunnable;
66 case Thread::kTimedWaiting: return kJavaTimedWaiting;
67 case Thread::kBlocked: return kJavaBlocked;
68 case Thread::kWaiting: return kJavaWaiting;
69 case Thread::kStarting: return kJavaNew;
70 case Thread::kNative: return kJavaRunnable;
71 case Thread::kVmWait: return kJavaWaiting;
72 case Thread::kSuspended: return kJavaRunnable;
73 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
74 }
75 return -1; // Unreachable.
Elliott Hughes8daa0922011-09-11 13:46:25 -070076}
77
Elliott Hughes0512f022012-03-15 22:10:52 -070078static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject javaThread, jobject javaObject) {
Elliott Hughes5f791332011-09-15 17:45:30 -070079 Object* object = Decode<Object*>(env, javaObject);
80 if (object == NULL) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070081 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes5f791332011-09-15 17:45:30 -070082 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null");
83 return JNI_FALSE;
84 }
Elliott Hughesbbd9d832011-11-07 14:40:00 -080085 ScopedThreadListLock thread_list_lock;
Elliott Hughes5f791332011-09-15 17:45:30 -070086 Thread* thread = Thread::FromManagedThread(env, javaThread);
87 return thread->HoldsLock(object);
Elliott Hughes8daa0922011-09-11 13:46:25 -070088}
89
Elliott Hughes0512f022012-03-15 22:10:52 -070090static void Thread_nativeInterrupt(JNIEnv* env, jobject javaThread) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080091 ScopedThreadListLock thread_list_lock;
Elliott Hughes5f791332011-09-15 17:45:30 -070092 Thread* thread = Thread::FromManagedThread(env, javaThread);
93 if (thread != NULL) {
94 thread->Interrupt();
95 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070096}
97
Elliott Hughes0512f022012-03-15 22:10:52 -070098static void Thread_nativeSetName(JNIEnv* env, jobject javaThread, jstring javaName) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080099 ScopedThreadListLock thread_list_lock;
Elliott Hughes54643082011-09-25 17:48:00 -0700100 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughes82188472011-11-07 18:11:48 -0800101 if (thread == NULL) {
102 return;
Elliott Hughes54643082011-09-25 17:48:00 -0700103 }
Elliott Hughes899e7892012-01-24 14:57:32 -0800104 ScopedUtfChars name(env, javaName);
105 if (name.c_str() == NULL) {
106 return;
107 }
108 thread->SetThreadName(name.c_str());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700109}
110
111/*
112 * Alter the priority of the specified thread. "newPriority" will range
113 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
114 * threads at Thread.NORM_PRIORITY (5).
115 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700116static void Thread_nativeSetPriority(JNIEnv* env, jobject javaThread, jint newPriority) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800117 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700118 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700119 if (thread != NULL) {
120 thread->SetNativePriority(newPriority);
121 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700122}
123
124/*
125 * Causes the thread to temporarily pause and allow other threads to execute.
126 *
127 * The exact behavior is poorly defined. Some discussion here:
128 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
129 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700130static void Thread_yield(JNIEnv*, jobject) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700131 sched_yield();
132}
133
134static JNINativeMethod gMethods[] = {
135 NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"),
136 NATIVE_METHOD(Thread, interrupted, "()Z"),
137 NATIVE_METHOD(Thread, isInterrupted, "()Z"),
138 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;J)V"),
Elliott Hughescf2b2d42012-03-27 17:11:42 -0700139 NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700140 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
141 NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
142 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
143 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
144 NATIVE_METHOD(Thread, yield, "()V"),
145};
146
Elliott Hughes8daa0922011-09-11 13:46:25 -0700147void register_java_lang_Thread(JNIEnv* env) {
148 jniRegisterNativeMethods(env, "java/lang/Thread", gMethods, NELEM(gMethods));
149}
150
151} // namespace art