blob: 7ccfaaa350367ea23cea3c5f2bd0afc1721edafc [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
Ian Rogers62d6c772013-02-27 08:32:07 -080017#include "common_throws.h"
Elliott Hughes82188472011-11-07 18:11:48 -080018#include "debugger.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070019#include "jni_internal.h"
Elliott Hughes4cd121e2013-01-07 17:35:41 -080020#include "monitor.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/object.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070022#include "scoped_thread_state_change.h"
Elliott Hughes54643082011-09-25 17:48:00 -070023#include "ScopedUtfChars.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070024#include "thread.h"
25#include "thread_list.h"
26
Elliott Hughes8daa0922011-09-11 13:46:25 -070027namespace art {
28
Elliott Hughes0512f022012-03-15 22:10:52 -070029static jobject Thread_currentThread(JNIEnv* env, jclass) {
Ian Rogerscfaa4552012-11-26 21:00:08 -080030 ScopedObjectAccess soa(env);
31 return soa.AddLocalReference<jobject>(soa.Self()->GetPeer());
Elliott Hughes8daa0922011-09-11 13:46:25 -070032}
33
Ian Rogers365c1022012-06-22 15:05:28 -070034static jboolean Thread_interrupted(JNIEnv* env, jclass) {
Ian Rogerscfaa4552012-11-26 21:00:08 -080035 return static_cast<JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE;
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 Rogers00f7d0e2012-07-19 15:28:27 -070039 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -070040 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041 Thread* thread = Thread::FromManagedThread(soa, 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
Ian Rogers52673ff2012-06-27 23:25:34 -070045static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size,
46 jboolean daemon) {
47 Thread::CreateNativeThread(env, java_thread, stack_size, daemon == JNI_TRUE);
Elliott Hughes8daa0922011-09-11 13:46:25 -070048}
49
Elliott Hughes57aba862012-06-20 14:00:47 -070050static jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) {
Elliott Hughescf2b2d42012-03-27 17:11:42 -070051 // Ordinals from Java's Thread.State.
52 const jint kJavaNew = 0;
53 const jint kJavaRunnable = 1;
54 const jint kJavaBlocked = 2;
55 const jint kJavaWaiting = 3;
56 const jint kJavaTimedWaiting = 4;
57 const jint kJavaTerminated = 5;
58
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059 ScopedObjectAccess soa(env);
Elliott Hughes57aba862012-06-20 14:00:47 -070060 ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting);
Ian Rogers50b35e22012-10-04 10:09:15 -070061 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughescf2b2d42012-03-27 17:11:42 -070063 if (thread != NULL) {
64 internal_thread_state = thread->GetState();
Elliott Hughes8e4aac52011-09-26 17:03:36 -070065 }
Elliott Hughescf2b2d42012-03-27 17:11:42 -070066 switch (internal_thread_state) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070067 case kTerminated: return kJavaTerminated;
68 case kRunnable: return kJavaRunnable;
69 case kTimedWaiting: return kJavaTimedWaiting;
Elliott Hughes4cd121e2013-01-07 17:35:41 -080070 case kSleeping: return kJavaTimedWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071 case kBlocked: return kJavaBlocked;
72 case kWaiting: return kJavaWaiting;
73 case kStarting: return kJavaNew;
74 case kNative: return kJavaRunnable;
75 case kWaitingForGcToComplete: return kJavaWaiting;
76 case kWaitingPerformingGc: return kJavaWaiting;
77 case kWaitingForDebuggerSend: return kJavaWaiting;
78 case kWaitingForDebuggerToAttach: return kJavaWaiting;
79 case kWaitingInMainDebuggerLoop: return kJavaWaiting;
80 case kWaitingForDebuggerSuspension: return kJavaWaiting;
81 case kWaitingForJniOnLoad: return kJavaWaiting;
82 case kWaitingForSignalCatcherOutput: return kJavaWaiting;
83 case kWaitingInMainSignalCatcherLoop: return kJavaWaiting;
84 case kSuspended: return kJavaRunnable;
Elliott Hughescf2b2d42012-03-27 17:11:42 -070085 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
86 }
87 return -1; // Unreachable.
Elliott Hughes8daa0922011-09-11 13:46:25 -070088}
89
Elliott Hughes57aba862012-06-20 14:00:47 -070090static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 mirror::Object* object = soa.Decode<mirror::Object*>(java_object);
Elliott Hughes5f791332011-09-15 17:45:30 -070093 if (object == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -080094 ThrowNullPointerException(NULL, "object == null");
Elliott Hughes5f791332011-09-15 17:45:30 -070095 return JNI_FALSE;
96 }
Ian Rogers50b35e22012-10-04 10:09:15 -070097 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070098 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -070099 return thread->HoldsLock(object);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700100}
101
Elliott Hughes57aba862012-06-20 14:00:47 -0700102static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700103 ScopedObjectAccess soa(env);
Ian Rogers81d425b2012-09-27 16:03:43 -0700104 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -0700106 if (thread != NULL) {
107 thread->Interrupt();
108 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700109}
110
Ian Rogers15bf2d32012-08-28 17:33:04 -0700111static void Thread_nativeSetName(JNIEnv* env, jobject peer, jstring java_name) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700112 ScopedUtfChars name(env, java_name);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700113 {
114 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 if (soa.Decode<mirror::Object*>(peer) == soa.Self()->GetPeer()) {
Mathieu Chartierdbe6f462012-09-25 16:54:50 -0700116 soa.Self()->SetThreadName(name.c_str());
Ian Rogers15bf2d32012-08-28 17:33:04 -0700117 return;
118 }
Elliott Hughes899e7892012-01-24 14:57:32 -0800119 }
Ian Rogers15bf2d32012-08-28 17:33:04 -0700120 // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the
121 // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock
122 // in the DDMS send code.
Elliott Hughesf327e072013-01-09 16:01:26 -0800123 bool timed_out;
124 Thread* thread = Thread::SuspendForDebugger(peer, true, &timed_out);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700125 if (thread != NULL) {
126 {
127 ScopedObjectAccess soa(env);
128 thread->SetThreadName(name.c_str());
129 }
130 Runtime::Current()->GetThreadList()->Resume(thread, true);
Elliott Hughesf327e072013-01-09 16:01:26 -0800131 } else if (timed_out) {
Ian Rogers15bf2d32012-08-28 17:33:04 -0700132 LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread "
133 "failed to suspend within a generous timeout.";
134 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700135}
136
137/*
Elliott Hughes57aba862012-06-20 14:00:47 -0700138 * Alter the priority of the specified thread. "new_priority" will range
Elliott Hughes8daa0922011-09-11 13:46:25 -0700139 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
140 * threads at Thread.NORM_PRIORITY (5).
141 */
Elliott Hughes57aba862012-06-20 14:00:47 -0700142static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -0700144 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700146 if (thread != NULL) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700147 thread->SetNativePriority(new_priority);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700148 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700149}
150
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800151static void Thread_sleep(JNIEnv* env, jclass, jobject java_lock, jlong ms, jint ns) {
152 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 mirror::Object* lock = soa.Decode<mirror::Object*>(java_lock);
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800154 Monitor::Wait(Thread::Current(), lock, ms, ns, true, kSleeping);
155}
156
Elliott Hughes8daa0922011-09-11 13:46:25 -0700157/*
158 * Causes the thread to temporarily pause and allow other threads to execute.
159 *
160 * The exact behavior is poorly defined. Some discussion here:
161 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
162 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700163static void Thread_yield(JNIEnv*, jobject) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700164 sched_yield();
165}
166
167static JNINativeMethod gMethods[] = {
168 NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"),
169 NATIVE_METHOD(Thread, interrupted, "()Z"),
170 NATIVE_METHOD(Thread, isInterrupted, "()Z"),
Ian Rogers52673ff2012-06-27 23:25:34 -0700171 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;JZ)V"),
Elliott Hughescf2b2d42012-03-27 17:11:42 -0700172 NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700173 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
174 NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
175 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
176 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800177 NATIVE_METHOD(Thread, sleep, "(Ljava/lang/Object;JI)V"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700178 NATIVE_METHOD(Thread, yield, "()V"),
179};
180
Elliott Hughes8daa0922011-09-11 13:46:25 -0700181void register_java_lang_Thread(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700182 REGISTER_NATIVE_METHODS("java/lang/Thread");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700183}
184
185} // namespace art