blob: 760eb9bc96f51e93bfcee8ac620369c672c03d4e [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
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "java_lang_Thread.h"
18
Ian Rogers62d6c772013-02-27 08:32:07 -080019#include "common_throws.h"
Elliott Hughes82188472011-11-07 18:11:48 -080020#include "debugger.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070021#include "jni_internal.h"
Elliott Hughes4cd121e2013-01-07 17:35:41 -080022#include "monitor.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/object.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070024#include "scoped_fast_native_object_access.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.h"
Elliott Hughes54643082011-09-25 17:48:00 -070026#include "ScopedUtfChars.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070027#include "thread.h"
28#include "thread_list.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080029#include "verify_object-inl.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070030
Elliott Hughes8daa0922011-09-11 13:46:25 -070031namespace art {
32
Elliott Hughes0512f022012-03-15 22:10:52 -070033static jobject Thread_currentThread(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070034 ScopedFastNativeObjectAccess soa(env);
Ian Rogerscfaa4552012-11-26 21:00:08 -080035 return soa.AddLocalReference<jobject>(soa.Self()->GetPeer());
Elliott Hughes8daa0922011-09-11 13:46:25 -070036}
37
Ian Rogers365c1022012-06-22 15:05:28 -070038static jboolean Thread_interrupted(JNIEnv* env, jclass) {
Ian Rogerscfaa4552012-11-26 21:00:08 -080039 return static_cast<JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070040}
41
Elliott Hughes57aba862012-06-20 14:00:47 -070042static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) {
Ian Rogers53b8b092014-03-13 23:45:53 -070043 ScopedFastNativeObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -070044 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070046 return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070047}
48
Ian Rogers52673ff2012-06-27 23:25:34 -070049static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size,
50 jboolean daemon) {
51 Thread::CreateNativeThread(env, java_thread, stack_size, daemon == JNI_TRUE);
Elliott Hughes8daa0922011-09-11 13:46:25 -070052}
53
Elliott Hughes57aba862012-06-20 14:00:47 -070054static jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) {
Elliott Hughescf2b2d42012-03-27 17:11:42 -070055 // Ordinals from Java's Thread.State.
56 const jint kJavaNew = 0;
57 const jint kJavaRunnable = 1;
58 const jint kJavaBlocked = 2;
59 const jint kJavaWaiting = 3;
60 const jint kJavaTimedWaiting = 4;
61 const jint kJavaTerminated = 5;
62
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063 ScopedObjectAccess soa(env);
Elliott Hughes57aba862012-06-20 14:00:47 -070064 ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting);
Ian Rogers50b35e22012-10-04 10:09:15 -070065 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughescf2b2d42012-03-27 17:11:42 -070067 if (thread != NULL) {
68 internal_thread_state = thread->GetState();
Elliott Hughes8e4aac52011-09-26 17:03:36 -070069 }
Elliott Hughescf2b2d42012-03-27 17:11:42 -070070 switch (internal_thread_state) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071 case kTerminated: return kJavaTerminated;
72 case kRunnable: return kJavaRunnable;
73 case kTimedWaiting: return kJavaTimedWaiting;
Elliott Hughes4cd121e2013-01-07 17:35:41 -080074 case kSleeping: return kJavaTimedWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070075 case kBlocked: return kJavaBlocked;
76 case kWaiting: return kJavaWaiting;
77 case kStarting: return kJavaNew;
78 case kNative: return kJavaRunnable;
79 case kWaitingForGcToComplete: return kJavaWaiting;
80 case kWaitingPerformingGc: return kJavaWaiting;
Ian Rogers1d54e732013-05-02 21:10:01 -070081 case kWaitingForCheckPointsToRun: return kJavaWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070082 case kWaitingForDebuggerSend: return kJavaWaiting;
83 case kWaitingForDebuggerToAttach: return kJavaWaiting;
84 case kWaitingInMainDebuggerLoop: return kJavaWaiting;
85 case kWaitingForDebuggerSuspension: return kJavaWaiting;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010086 case kWaitingForDeoptimization: return kJavaWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 case kWaitingForJniOnLoad: return kJavaWaiting;
88 case kWaitingForSignalCatcherOutput: return kJavaWaiting;
89 case kWaitingInMainSignalCatcherLoop: return kJavaWaiting;
Sebastien Hertzbae182c2013-12-17 10:42:03 +010090 case kWaitingForMethodTracingStart: return kJavaWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091 case kSuspended: return kJavaRunnable;
Elliott Hughescf2b2d42012-03-27 17:11:42 -070092 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
93 }
Ian Rogers0ec77d62014-04-22 10:51:17 -070094 LOG(ERROR) << "Unexpected thread state: " << internal_thread_state;
Brian Carlstrom7934ac22013-07-26 10:54:15 -070095 return -1; // Unreachable.
Elliott Hughes8daa0922011-09-11 13:46:25 -070096}
97
Elliott Hughes57aba862012-06-20 14:00:47 -070098static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070099 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 mirror::Object* object = soa.Decode<mirror::Object*>(java_object);
Elliott Hughes5f791332011-09-15 17:45:30 -0700101 if (object == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800102 ThrowNullPointerException(NULL, "object == null");
Elliott Hughes5f791332011-09-15 17:45:30 -0700103 return JNI_FALSE;
104 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700105 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -0700107 return thread->HoldsLock(object);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700108}
109
Elliott Hughes57aba862012-06-20 14:00:47 -0700110static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700111 ScopedFastNativeObjectAccess soa(env);
Ian Rogers81d425b2012-09-27 16:03:43 -0700112 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700113 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -0700114 if (thread != NULL) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700115 thread->Interrupt(soa.Self());
Elliott Hughes5f791332011-09-15 17:45:30 -0700116 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700117}
118
Ian Rogers15bf2d32012-08-28 17:33:04 -0700119static void Thread_nativeSetName(JNIEnv* env, jobject peer, jstring java_name) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700120 ScopedUtfChars name(env, java_name);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700121 {
122 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123 if (soa.Decode<mirror::Object*>(peer) == soa.Self()->GetPeer()) {
Mathieu Chartierdbe6f462012-09-25 16:54:50 -0700124 soa.Self()->SetThreadName(name.c_str());
Ian Rogers15bf2d32012-08-28 17:33:04 -0700125 return;
126 }
Elliott Hughes899e7892012-01-24 14:57:32 -0800127 }
Ian Rogers15bf2d32012-08-28 17:33:04 -0700128 // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the
129 // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock
130 // in the DDMS send code.
Brian Carlstromba32de42014-08-27 23:43:46 -0700131 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Elliott Hughesf327e072013-01-09 16:01:26 -0800132 bool timed_out;
Ian Rogersf3d874c2014-07-17 18:52:42 -0700133 // Take suspend thread lock to avoid races with threads trying to suspend this one.
Ian Rogers4ad5cd32014-11-11 23:08:07 -0800134 Thread* thread = thread_list->SuspendThreadByPeer(peer, true, false, &timed_out);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700135 if (thread != NULL) {
136 {
137 ScopedObjectAccess soa(env);
138 thread->SetThreadName(name.c_str());
139 }
Brian Carlstromba32de42014-08-27 23:43:46 -0700140 thread_list->Resume(thread, false);
Elliott Hughesf327e072013-01-09 16:01:26 -0800141 } else if (timed_out) {
Ian Rogers15bf2d32012-08-28 17:33:04 -0700142 LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread "
143 "failed to suspend within a generous timeout.";
144 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700145}
146
147/*
Elliott Hughes57aba862012-06-20 14:00:47 -0700148 * Alter the priority of the specified thread. "new_priority" will range
Elliott Hughes8daa0922011-09-11 13:46:25 -0700149 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
150 * threads at Thread.NORM_PRIORITY (5).
151 */
Elliott Hughes57aba862012-06-20 14:00:47 -0700152static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700153 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -0700154 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700155 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700156 if (thread != NULL) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700157 thread->SetNativePriority(new_priority);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700158 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700159}
160
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800161static void Thread_sleep(JNIEnv* env, jclass, jobject java_lock, jlong ms, jint ns) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700162 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 mirror::Object* lock = soa.Decode<mirror::Object*>(java_lock);
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800164 Monitor::Wait(Thread::Current(), lock, ms, ns, true, kSleeping);
165}
166
Elliott Hughes8daa0922011-09-11 13:46:25 -0700167/*
168 * Causes the thread to temporarily pause and allow other threads to execute.
169 *
170 * The exact behavior is poorly defined. Some discussion here:
171 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
172 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700173static void Thread_yield(JNIEnv*, jobject) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700174 sched_yield();
175}
176
177static JNINativeMethod gMethods[] = {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700178 NATIVE_METHOD(Thread, currentThread, "!()Ljava/lang/Thread;"),
Ian Rogers53b8b092014-03-13 23:45:53 -0700179 NATIVE_METHOD(Thread, interrupted, "!()Z"),
180 NATIVE_METHOD(Thread, isInterrupted, "!()Z"),
Ian Rogers52673ff2012-06-27 23:25:34 -0700181 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;JZ)V"),
Elliott Hughescf2b2d42012-03-27 17:11:42 -0700182 NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700183 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700184 NATIVE_METHOD(Thread, nativeInterrupt, "!()V"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700185 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
186 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700187 NATIVE_METHOD(Thread, sleep, "!(Ljava/lang/Object;JI)V"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700188 NATIVE_METHOD(Thread, yield, "()V"),
189};
190
Elliott Hughes8daa0922011-09-11 13:46:25 -0700191void register_java_lang_Thread(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700192 REGISTER_NATIVE_METHODS("java/lang/Thread");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700193}
194
195} // namespace art