blob: 0b84005f5a5631c1cdae16bf94da3683e0702ea8 [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 Rogers1eb512d2013-10-18 15:42:20 -070022#include "scoped_fast_native_object_access.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070023#include "scoped_thread_state_change.h"
Elliott Hughes54643082011-09-25 17:48:00 -070024#include "ScopedUtfChars.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070025#include "thread.h"
26#include "thread_list.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080027#include "verify_object-inl.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070028
Elliott Hughes8daa0922011-09-11 13:46:25 -070029namespace art {
30
Elliott Hughes0512f022012-03-15 22:10:52 -070031static jobject Thread_currentThread(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070032 ScopedFastNativeObjectAccess soa(env);
Ian Rogerscfaa4552012-11-26 21:00:08 -080033 return soa.AddLocalReference<jobject>(soa.Self()->GetPeer());
Elliott Hughes8daa0922011-09-11 13:46:25 -070034}
35
Ian Rogers365c1022012-06-22 15:05:28 -070036static jboolean Thread_interrupted(JNIEnv* env, jclass) {
Ian Rogerscfaa4552012-11-26 21:00:08 -080037 return static_cast<JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070038}
39
Elliott Hughes57aba862012-06-20 14:00:47 -070040static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) {
Ian Rogers53b8b092014-03-13 23:45:53 -070041 ScopedFastNativeObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -070042 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070044 return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070045}
46
Ian Rogers52673ff2012-06-27 23:25:34 -070047static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size,
48 jboolean daemon) {
49 Thread::CreateNativeThread(env, java_thread, stack_size, daemon == JNI_TRUE);
Elliott Hughes8daa0922011-09-11 13:46:25 -070050}
51
Elliott Hughes57aba862012-06-20 14:00:47 -070052static jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) {
Elliott Hughescf2b2d42012-03-27 17:11:42 -070053 // Ordinals from Java's Thread.State.
54 const jint kJavaNew = 0;
55 const jint kJavaRunnable = 1;
56 const jint kJavaBlocked = 2;
57 const jint kJavaWaiting = 3;
58 const jint kJavaTimedWaiting = 4;
59 const jint kJavaTerminated = 5;
60
Ian Rogers00f7d0e2012-07-19 15:28:27 -070061 ScopedObjectAccess soa(env);
Elliott Hughes57aba862012-06-20 14:00:47 -070062 ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting);
Ian Rogers50b35e22012-10-04 10:09:15 -070063 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughescf2b2d42012-03-27 17:11:42 -070065 if (thread != NULL) {
66 internal_thread_state = thread->GetState();
Elliott Hughes8e4aac52011-09-26 17:03:36 -070067 }
Elliott Hughescf2b2d42012-03-27 17:11:42 -070068 switch (internal_thread_state) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069 case kTerminated: return kJavaTerminated;
70 case kRunnable: return kJavaRunnable;
71 case kTimedWaiting: return kJavaTimedWaiting;
Elliott Hughes4cd121e2013-01-07 17:35:41 -080072 case kSleeping: return kJavaTimedWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070073 case kBlocked: return kJavaBlocked;
74 case kWaiting: return kJavaWaiting;
75 case kStarting: return kJavaNew;
76 case kNative: return kJavaRunnable;
77 case kWaitingForGcToComplete: return kJavaWaiting;
78 case kWaitingPerformingGc: return kJavaWaiting;
Ian Rogers1d54e732013-05-02 21:10:01 -070079 case kWaitingForCheckPointsToRun: return kJavaWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070080 case kWaitingForDebuggerSend: return kJavaWaiting;
81 case kWaitingForDebuggerToAttach: return kJavaWaiting;
82 case kWaitingInMainDebuggerLoop: return kJavaWaiting;
83 case kWaitingForDebuggerSuspension: return kJavaWaiting;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010084 case kWaitingForDeoptimization: return kJavaWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070085 case kWaitingForJniOnLoad: return kJavaWaiting;
86 case kWaitingForSignalCatcherOutput: return kJavaWaiting;
87 case kWaitingInMainSignalCatcherLoop: return kJavaWaiting;
88 case kSuspended: return kJavaRunnable;
Elliott Hughescf2b2d42012-03-27 17:11:42 -070089 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
90 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -070091 return -1; // Unreachable.
Elliott Hughes8daa0922011-09-11 13:46:25 -070092}
93
Elliott Hughes57aba862012-06-20 14:00:47 -070094static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096 mirror::Object* object = soa.Decode<mirror::Object*>(java_object);
Elliott Hughes5f791332011-09-15 17:45:30 -070097 if (object == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -080098 ThrowNullPointerException(NULL, "object == null");
Elliott Hughes5f791332011-09-15 17:45:30 -070099 return JNI_FALSE;
100 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700101 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -0700103 return thread->HoldsLock(object);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700104}
105
Elliott Hughes57aba862012-06-20 14:00:47 -0700106static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700107 ScopedFastNativeObjectAccess soa(env);
Ian Rogers81d425b2012-09-27 16:03:43 -0700108 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700109 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -0700110 if (thread != NULL) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700111 thread->Interrupt(soa.Self());
Elliott Hughes5f791332011-09-15 17:45:30 -0700112 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700113}
114
Ian Rogers15bf2d32012-08-28 17:33:04 -0700115static void Thread_nativeSetName(JNIEnv* env, jobject peer, jstring java_name) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700116 ScopedUtfChars name(env, java_name);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700117 {
118 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119 if (soa.Decode<mirror::Object*>(peer) == soa.Self()->GetPeer()) {
Mathieu Chartierdbe6f462012-09-25 16:54:50 -0700120 soa.Self()->SetThreadName(name.c_str());
Ian Rogers15bf2d32012-08-28 17:33:04 -0700121 return;
122 }
Elliott Hughes899e7892012-01-24 14:57:32 -0800123 }
Ian Rogers15bf2d32012-08-28 17:33:04 -0700124 // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the
125 // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock
126 // in the DDMS send code.
Elliott Hughesf327e072013-01-09 16:01:26 -0800127 bool timed_out;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700128 Thread* thread = ThreadList::SuspendThreadByPeer(peer, true, false, &timed_out);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700129 if (thread != NULL) {
130 {
131 ScopedObjectAccess soa(env);
132 thread->SetThreadName(name.c_str());
133 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700134 Runtime::Current()->GetThreadList()->Resume(thread, false);
Elliott Hughesf327e072013-01-09 16:01:26 -0800135 } else if (timed_out) {
Ian Rogers15bf2d32012-08-28 17:33:04 -0700136 LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread "
137 "failed to suspend within a generous timeout.";
138 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700139}
140
141/*
Elliott Hughes57aba862012-06-20 14:00:47 -0700142 * Alter the priority of the specified thread. "new_priority" will range
Elliott Hughes8daa0922011-09-11 13:46:25 -0700143 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
144 * threads at Thread.NORM_PRIORITY (5).
145 */
Elliott Hughes57aba862012-06-20 14:00:47 -0700146static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -0700148 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700150 if (thread != NULL) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700151 thread->SetNativePriority(new_priority);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700152 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700153}
154
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800155static void Thread_sleep(JNIEnv* env, jclass, jobject java_lock, jlong ms, jint ns) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700156 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 mirror::Object* lock = soa.Decode<mirror::Object*>(java_lock);
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800158 Monitor::Wait(Thread::Current(), lock, ms, ns, true, kSleeping);
159}
160
Elliott Hughes8daa0922011-09-11 13:46:25 -0700161/*
162 * Causes the thread to temporarily pause and allow other threads to execute.
163 *
164 * The exact behavior is poorly defined. Some discussion here:
165 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
166 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700167static void Thread_yield(JNIEnv*, jobject) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700168 sched_yield();
169}
170
171static JNINativeMethod gMethods[] = {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700172 NATIVE_METHOD(Thread, currentThread, "!()Ljava/lang/Thread;"),
Ian Rogers53b8b092014-03-13 23:45:53 -0700173 NATIVE_METHOD(Thread, interrupted, "!()Z"),
174 NATIVE_METHOD(Thread, isInterrupted, "!()Z"),
Ian Rogers52673ff2012-06-27 23:25:34 -0700175 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;JZ)V"),
Elliott Hughescf2b2d42012-03-27 17:11:42 -0700176 NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700177 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700178 NATIVE_METHOD(Thread, nativeInterrupt, "!()V"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700179 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
180 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700181 NATIVE_METHOD(Thread, sleep, "!(Ljava/lang/Object;JI)V"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700182 NATIVE_METHOD(Thread, yield, "()V"),
183};
184
Elliott Hughes8daa0922011-09-11 13:46:25 -0700185void register_java_lang_Thread(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700186 REGISTER_NATIVE_METHODS("java/lang/Thread");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700187}
188
189} // namespace art