Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "java_lang_Thread.h" |
| 18 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 19 | #include "common_throws.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 20 | #include "jni_internal.h" |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 21 | #include "monitor.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "mirror/object.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 23 | #include "scoped_fast_native_object_access-inl.h" |
| 24 | #include "scoped_thread_state_change-inl.h" |
Elliott Hughes | 5464308 | 2011-09-25 17:48:00 -0700 | [diff] [blame] | 25 | #include "ScopedUtfChars.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 26 | #include "thread.h" |
| 27 | #include "thread_list.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 28 | #include "verify_object.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 30 | namespace art { |
| 31 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 32 | static jobject Thread_currentThread(JNIEnv* env, jclass) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 33 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 34 | return soa.AddLocalReference<jobject>(soa.Self()->GetPeer()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 37 | static jboolean Thread_interrupted(JNIEnv* env, jclass) { |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 38 | return static_cast<JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 41 | static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 42 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 43 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 44 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 45 | return (thread != nullptr) ? thread->IsInterrupted() : JNI_FALSE; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 48 | static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size, |
| 49 | jboolean daemon) { |
Andreas Gampe | 415d807 | 2016-04-11 08:42:26 -0700 | [diff] [blame] | 50 | // There are sections in the zygote that forbid thread creation. |
| 51 | Runtime* runtime = Runtime::Current(); |
| 52 | if (runtime->IsZygote() && runtime->IsZygoteNoThreadSection()) { |
| 53 | jclass internal_error = env->FindClass("java/lang/InternalError"); |
| 54 | CHECK(internal_error != nullptr); |
| 55 | env->ThrowNew(internal_error, "Cannot create threads in zygote"); |
| 56 | return; |
| 57 | } |
| 58 | |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 59 | Thread::CreateNativeThread(env, java_thread, stack_size, daemon == JNI_TRUE); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 62 | static jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) { |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 63 | // Ordinals from Java's Thread.State. |
| 64 | const jint kJavaNew = 0; |
| 65 | const jint kJavaRunnable = 1; |
| 66 | const jint kJavaBlocked = 2; |
| 67 | const jint kJavaWaiting = 3; |
| 68 | const jint kJavaTimedWaiting = 4; |
| 69 | const jint kJavaTerminated = 5; |
| 70 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 71 | ScopedObjectAccess soa(env); |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 72 | ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 73 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 74 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 75 | if (thread != nullptr) { |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 76 | internal_thread_state = thread->GetState(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 77 | } |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 78 | switch (internal_thread_state) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 79 | case kTerminated: return kJavaTerminated; |
| 80 | case kRunnable: return kJavaRunnable; |
| 81 | case kTimedWaiting: return kJavaTimedWaiting; |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 82 | case kSleeping: return kJavaTimedWaiting; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 83 | case kBlocked: return kJavaBlocked; |
| 84 | case kWaiting: return kJavaWaiting; |
| 85 | case kStarting: return kJavaNew; |
| 86 | case kNative: return kJavaRunnable; |
| 87 | case kWaitingForGcToComplete: return kJavaWaiting; |
| 88 | case kWaitingPerformingGc: return kJavaWaiting; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 89 | case kWaitingForCheckPointsToRun: return kJavaWaiting; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 90 | case kWaitingForDebuggerSend: return kJavaWaiting; |
| 91 | case kWaitingForDebuggerToAttach: return kJavaWaiting; |
| 92 | case kWaitingInMainDebuggerLoop: return kJavaWaiting; |
| 93 | case kWaitingForDebuggerSuspension: return kJavaWaiting; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 94 | case kWaitingForDeoptimization: return kJavaWaiting; |
Mathieu Chartier | b43390c | 2015-05-12 10:47:11 -0700 | [diff] [blame] | 95 | case kWaitingForGetObjectsAllocated: return kJavaWaiting; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 96 | case kWaitingForJniOnLoad: return kJavaWaiting; |
| 97 | case kWaitingForSignalCatcherOutput: return kJavaWaiting; |
| 98 | case kWaitingInMainSignalCatcherLoop: return kJavaWaiting; |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 99 | case kWaitingForMethodTracingStart: return kJavaWaiting; |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 100 | case kWaitingForVisitObjects: return kJavaWaiting; |
Hiroshi Yamauchi | 2ddc6bf | 2015-12-21 14:06:23 -0800 | [diff] [blame] | 101 | case kWaitingWeakGcRootRead: return kJavaRunnable; |
Hiroshi Yamauchi | 76f55b0 | 2015-08-21 16:10:39 -0700 | [diff] [blame] | 102 | case kWaitingForGcThreadFlip: return kJavaWaiting; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 103 | case kSuspended: return kJavaRunnable; |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 104 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 105 | } |
Ian Rogers | 0ec77d6 | 2014-04-22 10:51:17 -0700 | [diff] [blame] | 106 | LOG(ERROR) << "Unexpected thread state: " << internal_thread_state; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 107 | return -1; // Unreachable. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 110 | static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 111 | ScopedObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 112 | ObjPtr<mirror::Object> object = soa.Decode<mirror::Object>(java_object); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 113 | if (object == nullptr) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 114 | ThrowNullPointerException("object == null"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 115 | return JNI_FALSE; |
| 116 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 117 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 118 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 119 | return thread->HoldsLock(object.Ptr()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 122 | static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 123 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 124 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 125 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 126 | if (thread != nullptr) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 127 | thread->Interrupt(soa.Self()); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 128 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 131 | static void Thread_nativeSetName(JNIEnv* env, jobject peer, jstring java_name) { |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 132 | ScopedUtfChars name(env, java_name); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 133 | { |
| 134 | ScopedObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 135 | if (soa.Decode<mirror::Object>(peer) == soa.Self()->GetPeer()) { |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 136 | soa.Self()->SetThreadName(name.c_str()); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 137 | return; |
| 138 | } |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 139 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 140 | // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the |
| 141 | // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock |
| 142 | // in the DDMS send code. |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 143 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 144 | bool timed_out; |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 145 | // Take suspend thread lock to avoid races with threads trying to suspend this one. |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 146 | Thread* thread = thread_list->SuspendThreadByPeer(peer, true, false, &timed_out); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 147 | if (thread != nullptr) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 148 | { |
| 149 | ScopedObjectAccess soa(env); |
| 150 | thread->SetThreadName(name.c_str()); |
| 151 | } |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 152 | thread_list->Resume(thread, false); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 153 | } else if (timed_out) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 154 | LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread " |
| 155 | "failed to suspend within a generous timeout."; |
| 156 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 160 | * Alter the priority of the specified thread. "new_priority" will range |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 161 | * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal" |
| 162 | * threads at Thread.NORM_PRIORITY (5). |
| 163 | */ |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 164 | static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 165 | ScopedObjectAccess soa(env); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 166 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 167 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 168 | if (thread != nullptr) { |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 169 | thread->SetNativePriority(new_priority); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 170 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 173 | static void Thread_sleep(JNIEnv* env, jclass, jobject java_lock, jlong ms, jint ns) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 174 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 175 | ObjPtr<mirror::Object> lock = soa.Decode<mirror::Object>(java_lock); |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 176 | Monitor::Wait(Thread::Current(), lock.Ptr(), ms, ns, true, kSleeping); |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 179 | /* |
| 180 | * Causes the thread to temporarily pause and allow other threads to execute. |
| 181 | * |
| 182 | * The exact behavior is poorly defined. Some discussion here: |
| 183 | * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html |
| 184 | */ |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 185 | static void Thread_yield(JNIEnv*, jobject) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 186 | sched_yield(); |
| 187 | } |
| 188 | |
| 189 | static JNINativeMethod gMethods[] = { |
Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 190 | FAST_NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"), |
| 191 | FAST_NATIVE_METHOD(Thread, interrupted, "()Z"), |
| 192 | FAST_NATIVE_METHOD(Thread, isInterrupted, "()Z"), |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 193 | NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;JZ)V"), |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 194 | NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 195 | NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"), |
Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 196 | FAST_NATIVE_METHOD(Thread, nativeInterrupt, "()V"), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 197 | NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"), |
| 198 | NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"), |
Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 199 | FAST_NATIVE_METHOD(Thread, sleep, "(Ljava/lang/Object;JI)V"), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 200 | NATIVE_METHOD(Thread, yield, "()V"), |
| 201 | }; |
| 202 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 203 | void register_java_lang_Thread(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 204 | REGISTER_NATIVE_METHODS("java/lang/Thread"); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | } // namespace art |