blob: adc246aa28b3fd7faccc184280035e4849981ce8 [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) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070028 ScopedObjectAccess soa(env);
29 return soa.AddLocalReference<jobject>(soa.Self()->GetPeer());
Elliott Hughes8daa0922011-09-11 13:46:25 -070030}
31
Ian Rogers365c1022012-06-22 15:05:28 -070032static jboolean Thread_interrupted(JNIEnv* env, jclass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033 return reinterpret_cast<JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070034}
35
Elliott Hughes57aba862012-06-20 14:00:47 -070036static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037 ScopedObjectAccess soa(env);
Ian Rogersb726dcb2012-09-05 08:57:23 -070038 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070039 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070040 return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070041}
42
Ian Rogers52673ff2012-06-27 23:25:34 -070043static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size,
44 jboolean daemon) {
45 Thread::CreateNativeThread(env, java_thread, stack_size, daemon == JNI_TRUE);
Elliott Hughes8daa0922011-09-11 13:46:25 -070046}
47
Elliott Hughes57aba862012-06-20 14:00:47 -070048static jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) {
Elliott Hughescf2b2d42012-03-27 17:11:42 -070049 // 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
Ian Rogers00f7d0e2012-07-19 15:28:27 -070057 ScopedObjectAccess soa(env);
Elliott Hughes57aba862012-06-20 14:00:47 -070058 ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting);
Ian Rogersb726dcb2012-09-05 08:57:23 -070059 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughescf2b2d42012-03-27 17:11:42 -070061 if (thread != NULL) {
Ian Rogersb726dcb2012-09-05 08:57:23 -070062 MutexLock mu(*Locks::thread_suspend_count_lock_);
Elliott Hughescf2b2d42012-03-27 17:11:42 -070063 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) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -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 kWaitingForGcToComplete: return kJavaWaiting;
74 case kWaitingPerformingGc: return kJavaWaiting;
75 case kWaitingForDebuggerSend: return kJavaWaiting;
76 case kWaitingForDebuggerToAttach: return kJavaWaiting;
77 case kWaitingInMainDebuggerLoop: return kJavaWaiting;
78 case kWaitingForDebuggerSuspension: return kJavaWaiting;
79 case kWaitingForJniOnLoad: return kJavaWaiting;
80 case kWaitingForSignalCatcherOutput: return kJavaWaiting;
81 case kWaitingInMainSignalCatcherLoop: return kJavaWaiting;
82 case kSuspended: return kJavaRunnable;
Elliott Hughescf2b2d42012-03-27 17:11:42 -070083 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
84 }
85 return -1; // Unreachable.
Elliott Hughes8daa0922011-09-11 13:46:25 -070086}
87
Elliott Hughes57aba862012-06-20 14:00:47 -070088static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070089 ScopedObjectAccess soa(env);
90 Object* object = soa.Decode<Object*>(java_object);
Elliott Hughes5f791332011-09-15 17:45:30 -070091 if (object == NULL) {
92 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null");
93 return JNI_FALSE;
94 }
Ian Rogersb726dcb2012-09-05 08:57:23 -070095 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070096 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -070097 return thread->HoldsLock(object);
Elliott Hughes8daa0922011-09-11 13:46:25 -070098}
99
Elliott Hughes57aba862012-06-20 14:00:47 -0700100static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 ScopedObjectAccess soa(env);
Ian Rogersb726dcb2012-09-05 08:57:23 -0700102 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700103 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -0700104 if (thread != NULL) {
105 thread->Interrupt();
106 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700107}
108
Ian Rogers15bf2d32012-08-28 17:33:04 -0700109static void Thread_nativeSetName(JNIEnv* env, jobject peer, jstring java_name) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700110 ScopedUtfChars name(env, java_name);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700111 {
112 ScopedObjectAccess soa(env);
113 Thread* self = Thread::Current();
114 if (soa.Decode<Object*>(peer) == self->GetPeer()) {
115 self->SetThreadName(name.c_str());
116 return;
117 }
Elliott Hughes899e7892012-01-24 14:57:32 -0800118 }
Ian Rogers15bf2d32012-08-28 17:33:04 -0700119 // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the
120 // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock
121 // in the DDMS send code.
122 bool timeout;
123 Thread* thread = Thread::SuspendForDebugger(peer, true, &timeout);
124 if (thread != NULL) {
125 {
126 ScopedObjectAccess soa(env);
127 thread->SetThreadName(name.c_str());
128 }
129 Runtime::Current()->GetThreadList()->Resume(thread, true);
130 } else if (timeout) {
131 LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread "
132 "failed to suspend within a generous timeout.";
133 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700134}
135
136/*
Elliott Hughes57aba862012-06-20 14:00:47 -0700137 * Alter the priority of the specified thread. "new_priority" will range
Elliott Hughes8daa0922011-09-11 13:46:25 -0700138 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
139 * threads at Thread.NORM_PRIORITY (5).
140 */
Elliott Hughes57aba862012-06-20 14:00:47 -0700141static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700142 ScopedObjectAccess soa(env);
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700145 if (thread != NULL) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700146 thread->SetNativePriority(new_priority);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700147 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700148}
149
150/*
151 * Causes the thread to temporarily pause and allow other threads to execute.
152 *
153 * The exact behavior is poorly defined. Some discussion here:
154 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
155 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700156static void Thread_yield(JNIEnv*, jobject) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700157 sched_yield();
158}
159
160static JNINativeMethod gMethods[] = {
161 NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"),
162 NATIVE_METHOD(Thread, interrupted, "()Z"),
163 NATIVE_METHOD(Thread, isInterrupted, "()Z"),
Ian Rogers52673ff2012-06-27 23:25:34 -0700164 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;JZ)V"),
Elliott Hughescf2b2d42012-03-27 17:11:42 -0700165 NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700166 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
167 NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
168 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
169 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
170 NATIVE_METHOD(Thread, yield, "()V"),
171};
172
Elliott Hughes8daa0922011-09-11 13:46:25 -0700173void register_java_lang_Thread(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700174 REGISTER_NATIVE_METHODS("java/lang/Thread");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700175}
176
177} // namespace art