blob: cf475e280cd6bc82ca29335d20ee74c940c2d123 [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 Rogers00f7d0e2012-07-19 15:28:27 -070020#include "scoped_thread_state_change.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
Elliott Hughes8daa0922011-09-11 13:46:25 -070025namespace art {
26
Elliott Hughes0512f022012-03-15 22:10:52 -070027static jobject Thread_currentThread(JNIEnv* env, jclass) {
Mathieu Chartierdbe6f462012-09-25 16:54:50 -070028 return reinterpret_cast<JNIEnvExt*>(env)->self->GetPeer();
Elliott Hughes8daa0922011-09-11 13:46:25 -070029}
30
Ian Rogers365c1022012-06-22 15:05:28 -070031static jboolean Thread_interrupted(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032 return reinterpret_cast<JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070033}
34
Elliott Hughes57aba862012-06-20 14:00:47 -070035static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -070037 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070039 return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070040}
41
Ian Rogers52673ff2012-06-27 23:25:34 -070042static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size,
43 jboolean daemon) {
44 Thread::CreateNativeThread(env, java_thread, stack_size, daemon == JNI_TRUE);
Elliott Hughes8daa0922011-09-11 13:46:25 -070045}
46
Elliott Hughes57aba862012-06-20 14:00:47 -070047static jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) {
Elliott Hughescf2b2d42012-03-27 17:11:42 -070048 // Ordinals from Java's Thread.State.
49 const jint kJavaNew = 0;
50 const jint kJavaRunnable = 1;
51 const jint kJavaBlocked = 2;
52 const jint kJavaWaiting = 3;
53 const jint kJavaTimedWaiting = 4;
54 const jint kJavaTerminated = 5;
55
Ian Rogers00f7d0e2012-07-19 15:28:27 -070056 ScopedObjectAccess soa(env);
Elliott Hughes57aba862012-06-20 14:00:47 -070057 ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting);
Ian Rogers50b35e22012-10-04 10:09:15 -070058 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059 Thread* thread = Thread::FromManagedThread(soa, java_thread);
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) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064 case kTerminated: return kJavaTerminated;
65 case kRunnable: return kJavaRunnable;
66 case kTimedWaiting: return kJavaTimedWaiting;
67 case kBlocked: return kJavaBlocked;
68 case kWaiting: return kJavaWaiting;
69 case kStarting: return kJavaNew;
70 case kNative: return kJavaRunnable;
71 case kWaitingForGcToComplete: return kJavaWaiting;
72 case kWaitingPerformingGc: return kJavaWaiting;
73 case kWaitingForDebuggerSend: return kJavaWaiting;
74 case kWaitingForDebuggerToAttach: return kJavaWaiting;
75 case kWaitingInMainDebuggerLoop: return kJavaWaiting;
76 case kWaitingForDebuggerSuspension: return kJavaWaiting;
77 case kWaitingForJniOnLoad: return kJavaWaiting;
78 case kWaitingForSignalCatcherOutput: return kJavaWaiting;
79 case kWaitingInMainSignalCatcherLoop: return kJavaWaiting;
80 case kSuspended: return kJavaRunnable;
Elliott Hughescf2b2d42012-03-27 17:11:42 -070081 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
82 }
83 return -1; // Unreachable.
Elliott Hughes8daa0922011-09-11 13:46:25 -070084}
85
Elliott Hughes57aba862012-06-20 14:00:47 -070086static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 ScopedObjectAccess soa(env);
88 Object* object = soa.Decode<Object*>(java_object);
Elliott Hughes5f791332011-09-15 17:45:30 -070089 if (object == NULL) {
90 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null");
91 return JNI_FALSE;
92 }
Ian Rogers50b35e22012-10-04 10:09:15 -070093 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -070095 return thread->HoldsLock(object);
Elliott Hughes8daa0922011-09-11 13:46:25 -070096}
97
Elliott Hughes57aba862012-06-20 14:00:47 -070098static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070099 ScopedObjectAccess soa(env);
Ian Rogers81d425b2012-09-27 16:03:43 -0700100 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -0700102 if (thread != NULL) {
103 thread->Interrupt();
104 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700105}
106
Ian Rogers15bf2d32012-08-28 17:33:04 -0700107static void Thread_nativeSetName(JNIEnv* env, jobject peer, jstring java_name) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700108 ScopedUtfChars name(env, java_name);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700109 {
110 ScopedObjectAccess soa(env);
Mathieu Chartierdbe6f462012-09-25 16:54:50 -0700111 if (soa.Env()->IsSameObject(peer, soa.Self()->GetPeer())) {
112 soa.Self()->SetThreadName(name.c_str());
Ian Rogers15bf2d32012-08-28 17:33:04 -0700113 return;
114 }
Elliott Hughes899e7892012-01-24 14:57:32 -0800115 }
Ian Rogers15bf2d32012-08-28 17:33:04 -0700116 // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the
117 // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock
118 // in the DDMS send code.
119 bool timeout;
120 Thread* thread = Thread::SuspendForDebugger(peer, true, &timeout);
121 if (thread != NULL) {
122 {
123 ScopedObjectAccess soa(env);
124 thread->SetThreadName(name.c_str());
125 }
126 Runtime::Current()->GetThreadList()->Resume(thread, true);
127 } else if (timeout) {
128 LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread "
129 "failed to suspend within a generous timeout.";
130 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700131}
132
133/*
Elliott Hughes57aba862012-06-20 14:00:47 -0700134 * Alter the priority of the specified thread. "new_priority" will range
Elliott Hughes8daa0922011-09-11 13:46:25 -0700135 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
136 * threads at Thread.NORM_PRIORITY (5).
137 */
Elliott Hughes57aba862012-06-20 14:00:47 -0700138static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -0700140 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700142 if (thread != NULL) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700143 thread->SetNativePriority(new_priority);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700144 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700145}
146
147/*
148 * Causes the thread to temporarily pause and allow other threads to execute.
149 *
150 * The exact behavior is poorly defined. Some discussion here:
151 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
152 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700153static void Thread_yield(JNIEnv*, jobject) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700154 sched_yield();
155}
156
157static JNINativeMethod gMethods[] = {
158 NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"),
159 NATIVE_METHOD(Thread, interrupted, "()Z"),
160 NATIVE_METHOD(Thread, isInterrupted, "()Z"),
Ian Rogers52673ff2012-06-27 23:25:34 -0700161 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;JZ)V"),
Elliott Hughescf2b2d42012-03-27 17:11:42 -0700162 NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700163 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
164 NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
165 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
166 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
167 NATIVE_METHOD(Thread, yield, "()V"),
168};
169
Elliott Hughes8daa0922011-09-11 13:46:25 -0700170void register_java_lang_Thread(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700171 REGISTER_NATIVE_METHODS("java/lang/Thread");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700172}
173
174} // namespace art