blob: 13edd67b5bc2c3cfefc4f8ad580d89797db78eb0 [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 Hughes8daa0922011-09-11 13:46:25 -070020#include "jni_internal.h"
Elliott Hughes4cd121e2013-01-07 17:35:41 -080021#include "monitor.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/object.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070023#include "scoped_fast_native_object_access.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070024#include "scoped_thread_state_change.h"
Elliott Hughes54643082011-09-25 17:48:00 -070025#include "ScopedUtfChars.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070026#include "thread.h"
27#include "thread_list.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080028#include "verify_object-inl.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070029
Elliott Hughes8daa0922011-09-11 13:46:25 -070030namespace art {
31
Elliott Hughes0512f022012-03-15 22:10:52 -070032static jobject Thread_currentThread(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070033 ScopedFastNativeObjectAccess soa(env);
Ian Rogerscfaa4552012-11-26 21:00:08 -080034 return soa.AddLocalReference<jobject>(soa.Self()->GetPeer());
Elliott Hughes8daa0922011-09-11 13:46:25 -070035}
36
Ian Rogers365c1022012-06-22 15:05:28 -070037static jboolean Thread_interrupted(JNIEnv* env, jclass) {
Ian Rogerscfaa4552012-11-26 21:00:08 -080038 return static_cast<JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070039}
40
Elliott Hughes57aba862012-06-20 14:00:47 -070041static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) {
Ian Rogers53b8b092014-03-13 23:45:53 -070042 ScopedFastNativeObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -070043 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070044 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070045 return (thread != nullptr) ? thread->IsInterrupted() : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070046}
47
Ian Rogers52673ff2012-06-27 23:25:34 -070048static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size,
49 jboolean daemon) {
50 Thread::CreateNativeThread(env, java_thread, stack_size, daemon == JNI_TRUE);
Elliott Hughes8daa0922011-09-11 13:46:25 -070051}
52
Elliott Hughes57aba862012-06-20 14:00:47 -070053static jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) {
Elliott Hughescf2b2d42012-03-27 17:11:42 -070054 // Ordinals from Java's Thread.State.
55 const jint kJavaNew = 0;
56 const jint kJavaRunnable = 1;
57 const jint kJavaBlocked = 2;
58 const jint kJavaWaiting = 3;
59 const jint kJavaTimedWaiting = 4;
60 const jint kJavaTerminated = 5;
61
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 ScopedObjectAccess soa(env);
Elliott Hughes57aba862012-06-20 14:00:47 -070063 ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting);
Ian Rogers50b35e22012-10-04 10:09:15 -070064 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070066 if (thread != nullptr) {
Elliott Hughescf2b2d42012-03-27 17:11:42 -070067 internal_thread_state = thread->GetState();
Elliott Hughes8e4aac52011-09-26 17:03:36 -070068 }
Elliott Hughescf2b2d42012-03-27 17:11:42 -070069 switch (internal_thread_state) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070070 case kTerminated: return kJavaTerminated;
71 case kRunnable: return kJavaRunnable;
72 case kTimedWaiting: return kJavaTimedWaiting;
Elliott Hughes4cd121e2013-01-07 17:35:41 -080073 case kSleeping: return kJavaTimedWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 case kBlocked: return kJavaBlocked;
75 case kWaiting: return kJavaWaiting;
76 case kStarting: return kJavaNew;
77 case kNative: return kJavaRunnable;
78 case kWaitingForGcToComplete: return kJavaWaiting;
79 case kWaitingPerformingGc: return kJavaWaiting;
Ian Rogers1d54e732013-05-02 21:10:01 -070080 case kWaitingForCheckPointsToRun: return kJavaWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 case kWaitingForDebuggerSend: return kJavaWaiting;
82 case kWaitingForDebuggerToAttach: return kJavaWaiting;
83 case kWaitingInMainDebuggerLoop: return kJavaWaiting;
84 case kWaitingForDebuggerSuspension: return kJavaWaiting;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010085 case kWaitingForDeoptimization: return kJavaWaiting;
Mathieu Chartierb43390c2015-05-12 10:47:11 -070086 case kWaitingForGetObjectsAllocated: 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;
Hiroshi Yamauchi0c8c3032015-01-16 16:54:35 -080091 case kWaitingForVisitObjects: return kJavaWaiting;
Hiroshi Yamauchi2ddc6bf2015-12-21 14:06:23 -080092 case kWaitingWeakGcRootRead: return kJavaRunnable;
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -070093 case kWaitingForGcThreadFlip: return kJavaWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094 case kSuspended: return kJavaRunnable;
Elliott Hughescf2b2d42012-03-27 17:11:42 -070095 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
96 }
Ian Rogers0ec77d62014-04-22 10:51:17 -070097 LOG(ERROR) << "Unexpected thread state: " << internal_thread_state;
Brian Carlstrom7934ac22013-07-26 10:54:15 -070098 return -1; // Unreachable.
Elliott Hughes8daa0922011-09-11 13:46:25 -070099}
100
Elliott Hughes57aba862012-06-20 14:00:47 -0700101static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 mirror::Object* object = soa.Decode<mirror::Object*>(java_object);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700104 if (object == nullptr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000105 ThrowNullPointerException("object == null");
Elliott Hughes5f791332011-09-15 17:45:30 -0700106 return JNI_FALSE;
107 }
Ian Rogers50b35e22012-10-04 10:09:15 -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 return thread->HoldsLock(object);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700111}
112
Elliott Hughes57aba862012-06-20 14:00:47 -0700113static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700114 ScopedFastNativeObjectAccess soa(env);
Ian Rogers81d425b2012-09-27 16:03:43 -0700115 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700117 if (thread != nullptr) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700118 thread->Interrupt(soa.Self());
Elliott Hughes5f791332011-09-15 17:45:30 -0700119 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700120}
121
Ian Rogers15bf2d32012-08-28 17:33:04 -0700122static void Thread_nativeSetName(JNIEnv* env, jobject peer, jstring java_name) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700123 ScopedUtfChars name(env, java_name);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700124 {
125 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 if (soa.Decode<mirror::Object*>(peer) == soa.Self()->GetPeer()) {
Mathieu Chartierdbe6f462012-09-25 16:54:50 -0700127 soa.Self()->SetThreadName(name.c_str());
Ian Rogers15bf2d32012-08-28 17:33:04 -0700128 return;
129 }
Elliott Hughes899e7892012-01-24 14:57:32 -0800130 }
Ian Rogers15bf2d32012-08-28 17:33:04 -0700131 // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the
132 // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock
133 // in the DDMS send code.
Brian Carlstromba32de42014-08-27 23:43:46 -0700134 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Elliott Hughesf327e072013-01-09 16:01:26 -0800135 bool timed_out;
Ian Rogersf3d874c2014-07-17 18:52:42 -0700136 // Take suspend thread lock to avoid races with threads trying to suspend this one.
Ian Rogers4ad5cd32014-11-11 23:08:07 -0800137 Thread* thread = thread_list->SuspendThreadByPeer(peer, true, false, &timed_out);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700138 if (thread != nullptr) {
Ian Rogers15bf2d32012-08-28 17:33:04 -0700139 {
140 ScopedObjectAccess soa(env);
141 thread->SetThreadName(name.c_str());
142 }
Brian Carlstromba32de42014-08-27 23:43:46 -0700143 thread_list->Resume(thread, false);
Elliott Hughesf327e072013-01-09 16:01:26 -0800144 } else if (timed_out) {
Ian Rogers15bf2d32012-08-28 17:33:04 -0700145 LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread "
146 "failed to suspend within a generous timeout.";
147 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700148}
149
150/*
Elliott Hughes57aba862012-06-20 14:00:47 -0700151 * Alter the priority of the specified thread. "new_priority" will range
Elliott Hughes8daa0922011-09-11 13:46:25 -0700152 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
153 * threads at Thread.NORM_PRIORITY (5).
154 */
Elliott Hughes57aba862012-06-20 14:00:47 -0700155static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700156 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -0700157 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700158 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700159 if (thread != nullptr) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700160 thread->SetNativePriority(new_priority);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700161 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700162}
163
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800164static void Thread_sleep(JNIEnv* env, jclass, jobject java_lock, jlong ms, jint ns) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700165 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166 mirror::Object* lock = soa.Decode<mirror::Object*>(java_lock);
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800167 Monitor::Wait(Thread::Current(), lock, ms, ns, true, kSleeping);
168}
169
Elliott Hughes8daa0922011-09-11 13:46:25 -0700170/*
171 * Causes the thread to temporarily pause and allow other threads to execute.
172 *
173 * The exact behavior is poorly defined. Some discussion here:
174 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
175 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700176static void Thread_yield(JNIEnv*, jobject) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700177 sched_yield();
178}
179
180static JNINativeMethod gMethods[] = {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700181 NATIVE_METHOD(Thread, currentThread, "!()Ljava/lang/Thread;"),
Ian Rogers53b8b092014-03-13 23:45:53 -0700182 NATIVE_METHOD(Thread, interrupted, "!()Z"),
183 NATIVE_METHOD(Thread, isInterrupted, "!()Z"),
Ian Rogers52673ff2012-06-27 23:25:34 -0700184 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;JZ)V"),
Elliott Hughescf2b2d42012-03-27 17:11:42 -0700185 NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700186 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
Ian Rogersdd7624d2014-03-14 17:43:00 -0700187 NATIVE_METHOD(Thread, nativeInterrupt, "!()V"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700188 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
189 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700190 NATIVE_METHOD(Thread, sleep, "!(Ljava/lang/Object;JI)V"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700191 NATIVE_METHOD(Thread, yield, "()V"),
192};
193
Elliott Hughes8daa0922011-09-11 13:46:25 -0700194void register_java_lang_Thread(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700195 REGISTER_NATIVE_METHODS("java/lang/Thread");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700196}
197
198} // namespace art