blob: 9a52f7002ba36ad40c492137546acac7bcba50ec [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/object.h"
Steven Morelande431e272017-07-18 16:53:49 -070022#include "monitor.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070023#include "native_util.h"
Steven Morelande431e272017-07-18 16:53:49 -070024#include "nativehelper/jni_macros.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070025#include "nativehelper/scoped_utf_chars.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070026#include "scoped_fast_native_object_access-inl.h"
27#include "scoped_thread_state_change-inl.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070028#include "thread.h"
29#include "thread_list.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080030#include "verify_object.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070031
Elliott Hughes8daa0922011-09-11 13:46:25 -070032namespace art {
33
Elliott Hughes0512f022012-03-15 22:10:52 -070034static jobject Thread_currentThread(JNIEnv* env, jclass) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070035 ScopedFastNativeObjectAccess soa(env);
Ian Rogerscfaa4552012-11-26 21:00:08 -080036 return soa.AddLocalReference<jobject>(soa.Self()->GetPeer());
Elliott Hughes8daa0922011-09-11 13:46:25 -070037}
38
Ian Rogers365c1022012-06-22 15:05:28 -070039static jboolean Thread_interrupted(JNIEnv* env, jclass) {
Ian Rogers55256cb2017-12-21 17:07:11 -080040 return static_cast<JNIEnvExt*>(env)->GetSelf()->Interrupted() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070041}
42
Elliott Hughes57aba862012-06-20 14:00:47 -070043static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) {
Ian Rogers53b8b092014-03-13 23:45:53 -070044 ScopedFastNativeObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -070045 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070047 return (thread != nullptr) ? thread->IsInterrupted() : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070048}
49
Ian Rogers52673ff2012-06-27 23:25:34 -070050static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size,
51 jboolean daemon) {
Andreas Gampe415d8072016-04-11 08:42:26 -070052 // There are sections in the zygote that forbid thread creation.
53 Runtime* runtime = Runtime::Current();
54 if (runtime->IsZygote() && runtime->IsZygoteNoThreadSection()) {
55 jclass internal_error = env->FindClass("java/lang/InternalError");
56 CHECK(internal_error != nullptr);
57 env->ThrowNew(internal_error, "Cannot create threads in zygote");
58 return;
59 }
60
Ian Rogers52673ff2012-06-27 23:25:34 -070061 Thread::CreateNativeThread(env, java_thread, stack_size, daemon == JNI_TRUE);
Elliott Hughes8daa0922011-09-11 13:46:25 -070062}
63
Elliott Hughes57aba862012-06-20 14:00:47 -070064static jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) {
Elliott Hughescf2b2d42012-03-27 17:11:42 -070065 // Ordinals from Java's Thread.State.
66 const jint kJavaNew = 0;
67 const jint kJavaRunnable = 1;
68 const jint kJavaBlocked = 2;
69 const jint kJavaWaiting = 3;
70 const jint kJavaTimedWaiting = 4;
71 const jint kJavaTerminated = 5;
72
Ian Rogers00f7d0e2012-07-19 15:28:27 -070073 ScopedObjectAccess soa(env);
Elliott Hughes57aba862012-06-20 14:00:47 -070074 ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting);
Ian Rogers50b35e22012-10-04 10:09:15 -070075 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070077 if (thread != nullptr) {
Elliott Hughescf2b2d42012-03-27 17:11:42 -070078 internal_thread_state = thread->GetState();
Elliott Hughes8e4aac52011-09-26 17:03:36 -070079 }
Elliott Hughescf2b2d42012-03-27 17:11:42 -070080 switch (internal_thread_state) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 case kTerminated: return kJavaTerminated;
82 case kRunnable: return kJavaRunnable;
83 case kTimedWaiting: return kJavaTimedWaiting;
Elliott Hughes4cd121e2013-01-07 17:35:41 -080084 case kSleeping: return kJavaTimedWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070085 case kBlocked: return kJavaBlocked;
86 case kWaiting: return kJavaWaiting;
87 case kStarting: return kJavaNew;
88 case kNative: return kJavaRunnable;
Alex Light77fee872017-09-05 14:51:49 -070089 case kWaitingForTaskProcessor: return kJavaWaiting;
90 case kWaitingForLockInflation: return kJavaWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091 case kWaitingForGcToComplete: return kJavaWaiting;
92 case kWaitingPerformingGc: return kJavaWaiting;
Ian Rogers1d54e732013-05-02 21:10:01 -070093 case kWaitingForCheckPointsToRun: return kJavaWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094 case kWaitingForDebuggerSend: return kJavaWaiting;
95 case kWaitingForDebuggerToAttach: return kJavaWaiting;
96 case kWaitingInMainDebuggerLoop: return kJavaWaiting;
97 case kWaitingForDebuggerSuspension: return kJavaWaiting;
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010098 case kWaitingForDeoptimization: return kJavaWaiting;
Mathieu Chartierb43390c2015-05-12 10:47:11 -070099 case kWaitingForGetObjectsAllocated: return kJavaWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700100 case kWaitingForJniOnLoad: return kJavaWaiting;
101 case kWaitingForSignalCatcherOutput: return kJavaWaiting;
102 case kWaitingInMainSignalCatcherLoop: return kJavaWaiting;
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100103 case kWaitingForMethodTracingStart: return kJavaWaiting;
Hiroshi Yamauchi0c8c3032015-01-16 16:54:35 -0800104 case kWaitingForVisitObjects: return kJavaWaiting;
Hiroshi Yamauchi2ddc6bf2015-12-21 14:06:23 -0800105 case kWaitingWeakGcRootRead: return kJavaRunnable;
Hiroshi Yamauchi76f55b02015-08-21 16:10:39 -0700106 case kWaitingForGcThreadFlip: return kJavaWaiting;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 case kSuspended: return kJavaRunnable;
Elliott Hughescf2b2d42012-03-27 17:11:42 -0700108 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
109 }
Ian Rogers0ec77d62014-04-22 10:51:17 -0700110 LOG(ERROR) << "Unexpected thread state: " << internal_thread_state;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700111 return -1; // Unreachable.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700112}
113
Elliott Hughes57aba862012-06-20 14:00:47 -0700114static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700115 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700116 ObjPtr<mirror::Object> object = soa.Decode<mirror::Object>(java_object);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700117 if (object == nullptr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000118 ThrowNullPointerException("object == null");
Elliott Hughes5f791332011-09-15 17:45:30 -0700119 return JNI_FALSE;
120 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700121 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700123 return thread->HoldsLock(object.Ptr());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700124}
125
Elliott Hughes57aba862012-06-20 14:00:47 -0700126static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700127 ScopedFastNativeObjectAccess soa(env);
Ian Rogers81d425b2012-09-27 16:03:43 -0700128 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700130 if (thread != nullptr) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700131 thread->Interrupt(soa.Self());
Elliott Hughes5f791332011-09-15 17:45:30 -0700132 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700133}
134
Ian Rogers15bf2d32012-08-28 17:33:04 -0700135static void Thread_nativeSetName(JNIEnv* env, jobject peer, jstring java_name) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700136 ScopedUtfChars name(env, java_name);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700137 {
138 ScopedObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700139 if (soa.Decode<mirror::Object>(peer) == soa.Self()->GetPeer()) {
Mathieu Chartierdbe6f462012-09-25 16:54:50 -0700140 soa.Self()->SetThreadName(name.c_str());
Ian Rogers15bf2d32012-08-28 17:33:04 -0700141 return;
142 }
Elliott Hughes899e7892012-01-24 14:57:32 -0800143 }
Ian Rogers15bf2d32012-08-28 17:33:04 -0700144 // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the
145 // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock
146 // in the DDMS send code.
Brian Carlstromba32de42014-08-27 23:43:46 -0700147 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Elliott Hughesf327e072013-01-09 16:01:26 -0800148 bool timed_out;
Ian Rogersf3d874c2014-07-17 18:52:42 -0700149 // Take suspend thread lock to avoid races with threads trying to suspend this one.
Alex Light46f93402017-06-29 11:59:50 -0700150 Thread* thread = thread_list->SuspendThreadByPeer(peer,
151 /* request_suspension */ true,
152 SuspendReason::kInternal,
153 &timed_out);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700154 if (thread != nullptr) {
Ian Rogers15bf2d32012-08-28 17:33:04 -0700155 {
156 ScopedObjectAccess soa(env);
157 thread->SetThreadName(name.c_str());
158 }
Alex Light88fd7202017-06-30 08:31:59 -0700159 bool resumed = thread_list->Resume(thread, SuspendReason::kInternal);
160 DCHECK(resumed);
Elliott Hughesf327e072013-01-09 16:01:26 -0800161 } else if (timed_out) {
Ian Rogers15bf2d32012-08-28 17:33:04 -0700162 LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread "
163 "failed to suspend within a generous timeout.";
164 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700165}
166
167/*
Elliott Hughes57aba862012-06-20 14:00:47 -0700168 * Alter the priority of the specified thread. "new_priority" will range
Elliott Hughes8daa0922011-09-11 13:46:25 -0700169 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
170 * threads at Thread.NORM_PRIORITY (5).
171 */
Elliott Hughes57aba862012-06-20 14:00:47 -0700172static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700173 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -0700174 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175 Thread* thread = Thread::FromManagedThread(soa, java_thread);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700176 if (thread != nullptr) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700177 thread->SetNativePriority(new_priority);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700178 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700179}
180
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800181static void Thread_sleep(JNIEnv* env, jclass, jobject java_lock, jlong ms, jint ns) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700182 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700183 ObjPtr<mirror::Object> lock = soa.Decode<mirror::Object>(java_lock);
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700184 Monitor::Wait(Thread::Current(), lock.Ptr(), ms, ns, true, kSleeping);
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800185}
186
Elliott Hughes8daa0922011-09-11 13:46:25 -0700187/*
188 * Causes the thread to temporarily pause and allow other threads to execute.
189 *
190 * The exact behavior is poorly defined. Some discussion here:
191 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
192 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700193static void Thread_yield(JNIEnv*, jobject) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700194 sched_yield();
195}
196
197static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800198 FAST_NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"),
199 FAST_NATIVE_METHOD(Thread, interrupted, "()Z"),
200 FAST_NATIVE_METHOD(Thread, isInterrupted, "()Z"),
Ian Rogers52673ff2012-06-27 23:25:34 -0700201 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;JZ)V"),
Elliott Hughescf2b2d42012-03-27 17:11:42 -0700202 NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700203 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800204 FAST_NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700205 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
206 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800207 FAST_NATIVE_METHOD(Thread, sleep, "(Ljava/lang/Object;JI)V"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700208 NATIVE_METHOD(Thread, yield, "()V"),
209};
210
Elliott Hughes8daa0922011-09-11 13:46:25 -0700211void register_java_lang_Thread(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700212 REGISTER_NATIVE_METHODS("java/lang/Thread");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700213}
214
215} // namespace art