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 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 17 | #include "common_throws.h" |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 18 | #include "debugger.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 19 | #include "jni_internal.h" |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 20 | #include "monitor.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "mirror/object.h" |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 22 | #include "scoped_fast_native_object_access.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 23 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 5464308 | 2011-09-25 17:48:00 -0700 | [diff] [blame] | 24 | #include "ScopedUtfChars.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 25 | #include "thread.h" |
| 26 | #include "thread_list.h" |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 27 | #include "verify_object-inl.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 29 | namespace art { |
| 30 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 31 | static jobject Thread_currentThread(JNIEnv* env, jclass) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 32 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 33 | return soa.AddLocalReference<jobject>(soa.Self()->GetPeer()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 36 | static jboolean Thread_interrupted(JNIEnv* env, jclass) { |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 37 | return static_cast<JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 40 | static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 41 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 42 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 43 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 44 | return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 47 | static 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 Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 52 | 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] | 53 | // 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 Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 61 | ScopedObjectAccess soa(env); |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 62 | ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 63 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 64 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 65 | if (thread != NULL) { |
| 66 | internal_thread_state = thread->GetState(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 67 | } |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 68 | switch (internal_thread_state) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 69 | case kTerminated: return kJavaTerminated; |
| 70 | case kRunnable: return kJavaRunnable; |
| 71 | case kTimedWaiting: return kJavaTimedWaiting; |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 72 | case kSleeping: return kJavaTimedWaiting; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 73 | 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 Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 79 | case kWaitingForCheckPointsToRun: return kJavaWaiting; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 80 | case kWaitingForDebuggerSend: return kJavaWaiting; |
| 81 | case kWaitingForDebuggerToAttach: return kJavaWaiting; |
| 82 | case kWaitingInMainDebuggerLoop: return kJavaWaiting; |
| 83 | case kWaitingForDebuggerSuspension: return kJavaWaiting; |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 84 | case kWaitingForDeoptimization: return kJavaWaiting; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 85 | case kWaitingForJniOnLoad: return kJavaWaiting; |
| 86 | case kWaitingForSignalCatcherOutput: return kJavaWaiting; |
| 87 | case kWaitingInMainSignalCatcherLoop: return kJavaWaiting; |
| 88 | case kSuspended: return kJavaRunnable; |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 89 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 90 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 91 | return -1; // Unreachable. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 94 | static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 95 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 96 | mirror::Object* object = soa.Decode<mirror::Object*>(java_object); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 97 | if (object == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 98 | ThrowNullPointerException(NULL, "object == null"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 99 | return JNI_FALSE; |
| 100 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 101 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 102 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 103 | return thread->HoldsLock(object); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 106 | static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame^] | 107 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 108 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 109 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 110 | if (thread != NULL) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame^] | 111 | thread->Interrupt(soa.Self()); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 112 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 115 | static void Thread_nativeSetName(JNIEnv* env, jobject peer, jstring java_name) { |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 116 | ScopedUtfChars name(env, java_name); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 117 | { |
| 118 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 119 | if (soa.Decode<mirror::Object*>(peer) == soa.Self()->GetPeer()) { |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 120 | soa.Self()->SetThreadName(name.c_str()); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 121 | return; |
| 122 | } |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 123 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 124 | // 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 Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 127 | bool timed_out; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 128 | Thread* thread = ThreadList::SuspendThreadByPeer(peer, true, false, &timed_out); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 129 | if (thread != NULL) { |
| 130 | { |
| 131 | ScopedObjectAccess soa(env); |
| 132 | thread->SetThreadName(name.c_str()); |
| 133 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 134 | Runtime::Current()->GetThreadList()->Resume(thread, false); |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 135 | } else if (timed_out) { |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 136 | 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 Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 142 | * Alter the priority of the specified thread. "new_priority" will range |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 143 | * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal" |
| 144 | * threads at Thread.NORM_PRIORITY (5). |
| 145 | */ |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 146 | static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 147 | ScopedObjectAccess soa(env); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 148 | MutexLock mu(soa.Self(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 149 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 150 | if (thread != NULL) { |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 151 | thread->SetNativePriority(new_priority); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 152 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 155 | 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] | 156 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 157 | mirror::Object* lock = soa.Decode<mirror::Object*>(java_lock); |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 158 | Monitor::Wait(Thread::Current(), lock, ms, ns, true, kSleeping); |
| 159 | } |
| 160 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 161 | /* |
| 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 Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 167 | static void Thread_yield(JNIEnv*, jobject) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 168 | sched_yield(); |
| 169 | } |
| 170 | |
| 171 | static JNINativeMethod gMethods[] = { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 172 | NATIVE_METHOD(Thread, currentThread, "!()Ljava/lang/Thread;"), |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 173 | NATIVE_METHOD(Thread, interrupted, "!()Z"), |
| 174 | NATIVE_METHOD(Thread, isInterrupted, "!()Z"), |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 175 | NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;JZ)V"), |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 176 | NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 177 | NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"), |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame^] | 178 | NATIVE_METHOD(Thread, nativeInterrupt, "!()V"), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 179 | NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"), |
| 180 | NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"), |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 181 | NATIVE_METHOD(Thread, sleep, "!(Ljava/lang/Object;JI)V"), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 182 | NATIVE_METHOD(Thread, yield, "()V"), |
| 183 | }; |
| 184 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 185 | void register_java_lang_Thread(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 186 | REGISTER_NATIVE_METHODS("java/lang/Thread"); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | } // namespace art |