blob: 86b3a204f568e93a65e31f27a289cffbb3cf6f89 [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"
Ian Rogers365c1022012-06-22 15:05:28 -070020#include "scoped_jni_thread_state.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070021#include "scoped_thread_list_lock.h"
Elliott Hughes54643082011-09-25 17:48:00 -070022#include "ScopedUtfChars.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070023#include "thread.h"
24#include "thread_list.h"
25
Elliott Hughes8daa0922011-09-11 13:46:25 -070026namespace art {
27
Elliott Hughes0512f022012-03-15 22:10:52 -070028static jobject Thread_currentThread(JNIEnv* env, jclass) {
Ian Rogers365c1022012-06-22 15:05:28 -070029 ScopedJniThreadState ts(env);
30 return ts.AddLocalReference<jobject>(ts.Self()->GetPeer());
Elliott Hughes8daa0922011-09-11 13:46:25 -070031}
32
Ian Rogers365c1022012-06-22 15:05:28 -070033static jboolean Thread_interrupted(JNIEnv* env, jclass) {
34 ScopedJniThreadState ts(env, kNative); // Doesn't touch objects, so keep in native state.
35 return ts.Self()->Interrupted();
Elliott Hughes8daa0922011-09-11 13:46:25 -070036}
37
Elliott Hughes57aba862012-06-20 14:00:47 -070038static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) {
Ian Rogers365c1022012-06-22 15:05:28 -070039 ScopedJniThreadState ts(env);
Elliott Hughesbbd9d832011-11-07 14:40:00 -080040 ScopedThreadListLock thread_list_lock;
Ian Rogers365c1022012-06-22 15:05:28 -070041 Thread* thread = Thread::FromManagedThread(ts, java_thread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070042 return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070043}
44
Elliott Hughes57aba862012-06-20 14:00:47 -070045static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size) {
Ian Rogers365c1022012-06-22 15:05:28 -070046 Thread::CreateNativeThread(env, java_thread, stack_size);
Elliott Hughes8daa0922011-09-11 13:46:25 -070047}
48
Elliott Hughes57aba862012-06-20 14:00:47 -070049static jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) {
Elliott Hughescf2b2d42012-03-27 17:11:42 -070050 // Ordinals from Java's Thread.State.
51 const jint kJavaNew = 0;
52 const jint kJavaRunnable = 1;
53 const jint kJavaBlocked = 2;
54 const jint kJavaWaiting = 3;
55 const jint kJavaTimedWaiting = 4;
56 const jint kJavaTerminated = 5;
57
Ian Rogers365c1022012-06-22 15:05:28 -070058 ScopedJniThreadState ts(env);
Elliott Hughes57aba862012-06-20 14:00:47 -070059 ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting);
Elliott Hughesbbd9d832011-11-07 14:40:00 -080060 ScopedThreadListLock thread_list_lock;
Ian Rogers365c1022012-06-22 15:05:28 -070061 Thread* thread = Thread::FromManagedThread(ts, java_thread);
Elliott Hughescf2b2d42012-03-27 17:11:42 -070062 if (thread != NULL) {
63 internal_thread_state = thread->GetState();
Elliott Hughes8e4aac52011-09-26 17:03:36 -070064 }
Elliott Hughescf2b2d42012-03-27 17:11:42 -070065 switch (internal_thread_state) {
Elliott Hughes34e06962012-04-09 13:55:55 -070066 case kTerminated: return kJavaTerminated;
67 case kRunnable: return kJavaRunnable;
68 case kTimedWaiting: return kJavaTimedWaiting;
69 case kBlocked: return kJavaBlocked;
70 case kWaiting: return kJavaWaiting;
71 case kStarting: return kJavaNew;
72 case kNative: return kJavaRunnable;
73 case kVmWait: return kJavaWaiting;
74 case kSuspended: return kJavaRunnable;
Elliott Hughescf2b2d42012-03-27 17:11:42 -070075 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
76 }
77 return -1; // Unreachable.
Elliott Hughes8daa0922011-09-11 13:46:25 -070078}
79
Elliott Hughes57aba862012-06-20 14:00:47 -070080static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) {
Ian Rogers365c1022012-06-22 15:05:28 -070081 ScopedJniThreadState ts(env);
82 Object* object = ts.Decode<Object*>(java_object);
Elliott Hughes5f791332011-09-15 17:45:30 -070083 if (object == NULL) {
84 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null");
85 return JNI_FALSE;
86 }
Elliott Hughesbbd9d832011-11-07 14:40:00 -080087 ScopedThreadListLock thread_list_lock;
Ian Rogers365c1022012-06-22 15:05:28 -070088 Thread* thread = Thread::FromManagedThread(ts, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -070089 return thread->HoldsLock(object);
Elliott Hughes8daa0922011-09-11 13:46:25 -070090}
91
Elliott Hughes57aba862012-06-20 14:00:47 -070092static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) {
Ian Rogers365c1022012-06-22 15:05:28 -070093 ScopedJniThreadState ts(env);
Elliott Hughesbbd9d832011-11-07 14:40:00 -080094 ScopedThreadListLock thread_list_lock;
Ian Rogers365c1022012-06-22 15:05:28 -070095 Thread* thread = Thread::FromManagedThread(ts, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -070096 if (thread != NULL) {
97 thread->Interrupt();
98 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070099}
100
Elliott Hughes57aba862012-06-20 14:00:47 -0700101static void Thread_nativeSetName(JNIEnv* env, jobject java_thread, jstring java_name) {
Ian Rogers365c1022012-06-22 15:05:28 -0700102 ScopedJniThreadState ts(env);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800103 ScopedThreadListLock thread_list_lock;
Ian Rogers365c1022012-06-22 15:05:28 -0700104 Thread* thread = Thread::FromManagedThread(ts, java_thread);
Elliott Hughes82188472011-11-07 18:11:48 -0800105 if (thread == NULL) {
106 return;
Elliott Hughes54643082011-09-25 17:48:00 -0700107 }
Elliott Hughes57aba862012-06-20 14:00:47 -0700108 ScopedUtfChars name(env, java_name);
Elliott Hughes899e7892012-01-24 14:57:32 -0800109 if (name.c_str() == NULL) {
110 return;
111 }
112 thread->SetThreadName(name.c_str());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700113}
114
115/*
Elliott Hughes57aba862012-06-20 14:00:47 -0700116 * Alter the priority of the specified thread. "new_priority" will range
Elliott Hughes8daa0922011-09-11 13:46:25 -0700117 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
118 * threads at Thread.NORM_PRIORITY (5).
119 */
Elliott Hughes57aba862012-06-20 14:00:47 -0700120static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) {
Ian Rogers365c1022012-06-22 15:05:28 -0700121 ScopedJniThreadState ts(env);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800122 ScopedThreadListLock thread_list_lock;
Ian Rogers365c1022012-06-22 15:05:28 -0700123 Thread* thread = Thread::FromManagedThread(ts, java_thread);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700124 if (thread != NULL) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700125 thread->SetNativePriority(new_priority);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700126 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700127}
128
129/*
130 * Causes the thread to temporarily pause and allow other threads to execute.
131 *
132 * The exact behavior is poorly defined. Some discussion here:
133 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
134 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700135static void Thread_yield(JNIEnv*, jobject) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700136 sched_yield();
137}
138
139static JNINativeMethod gMethods[] = {
140 NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"),
141 NATIVE_METHOD(Thread, interrupted, "()Z"),
142 NATIVE_METHOD(Thread, isInterrupted, "()Z"),
143 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;J)V"),
Elliott Hughescf2b2d42012-03-27 17:11:42 -0700144 NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700145 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
146 NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
147 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
148 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
149 NATIVE_METHOD(Thread, yield, "()V"),
150};
151
Elliott Hughes8daa0922011-09-11 13:46:25 -0700152void register_java_lang_Thread(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700153 REGISTER_NATIVE_METHODS("java/lang/Thread");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700154}
155
156} // namespace art