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 | |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 17 | #include "debugger.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 18 | #include "jni_internal.h" |
| 19 | #include "object.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 20 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 5464308 | 2011-09-25 17:48:00 -0700 | [diff] [blame] | 21 | #include "ScopedUtfChars.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 22 | #include "thread.h" |
| 23 | #include "thread_list.h" |
| 24 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 27 | static jobject Thread_currentThread(JNIEnv* env, jclass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 28 | ScopedObjectAccess soa(env); |
| 29 | return soa.AddLocalReference<jobject>(soa.Self()->GetPeer()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 32 | static jboolean Thread_interrupted(JNIEnv* env, jclass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 33 | return reinterpret_cast<JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 36 | static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 37 | ScopedObjectAccess soa(env); |
| 38 | MutexLock mu(*GlobalSynchronization::thread_list_lock_); |
| 39 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 40 | return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 43 | static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size, |
| 44 | jboolean daemon) { |
| 45 | Thread::CreateNativeThread(env, java_thread, stack_size, daemon == JNI_TRUE); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 48 | 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] | 49 | // Ordinals from Java's Thread.State. |
| 50 | const jint kJavaNew = 0; |
| 51 | const jint kJavaRunnable = 1; |
| 52 | const jint kJavaBlocked = 2; |
| 53 | const jint kJavaWaiting = 3; |
| 54 | const jint kJavaTimedWaiting = 4; |
| 55 | const jint kJavaTerminated = 5; |
| 56 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 57 | ScopedObjectAccess soa(env); |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 58 | ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 59 | MutexLock mu(*GlobalSynchronization::thread_list_lock_); |
| 60 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 61 | if (thread != NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 62 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 63 | internal_thread_state = thread->GetState(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 64 | } |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 65 | switch (internal_thread_state) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 66 | case kTerminated: return kJavaTerminated; |
| 67 | case kRunnable: return kJavaRunnable; |
| 68 | case kTimedWaiting: return kJavaTimedWaiting; |
| 69 | case kBlocked: return kJavaBlocked; |
| 70 | case kWaiting: return kJavaWaiting; |
| 71 | case kStarting: return kJavaNew; |
| 72 | case kNative: return kJavaRunnable; |
| 73 | case kWaitingForGcToComplete: return kJavaWaiting; |
| 74 | case kWaitingPerformingGc: return kJavaWaiting; |
| 75 | case kWaitingForDebuggerSend: return kJavaWaiting; |
| 76 | case kWaitingForDebuggerToAttach: return kJavaWaiting; |
| 77 | case kWaitingInMainDebuggerLoop: return kJavaWaiting; |
| 78 | case kWaitingForDebuggerSuspension: return kJavaWaiting; |
| 79 | case kWaitingForJniOnLoad: return kJavaWaiting; |
| 80 | case kWaitingForSignalCatcherOutput: return kJavaWaiting; |
| 81 | case kWaitingInMainSignalCatcherLoop: return kJavaWaiting; |
| 82 | case kSuspended: return kJavaRunnable; |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 83 | // Don't add a 'default' here so the compiler can spot incompatible enum changes. |
| 84 | } |
| 85 | return -1; // Unreachable. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 88 | static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 89 | ScopedObjectAccess soa(env); |
| 90 | Object* object = soa.Decode<Object*>(java_object); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 91 | if (object == NULL) { |
| 92 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null"); |
| 93 | return JNI_FALSE; |
| 94 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 95 | MutexLock mu(*GlobalSynchronization::thread_list_lock_); |
| 96 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 97 | return thread->HoldsLock(object); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 100 | static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 101 | ScopedObjectAccess soa(env); |
| 102 | MutexLock mu(*GlobalSynchronization::thread_list_lock_); |
| 103 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 104 | if (thread != NULL) { |
| 105 | thread->Interrupt(); |
| 106 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 109 | static void Thread_nativeSetName(JNIEnv* env, jobject java_thread, jstring java_name) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 110 | ScopedObjectAccess soa(env); |
| 111 | MutexLock mu(*GlobalSynchronization::thread_list_lock_); |
| 112 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Elliott Hughes | 8218847 | 2011-11-07 18:11:48 -0800 | [diff] [blame] | 113 | if (thread == NULL) { |
| 114 | return; |
Elliott Hughes | 5464308 | 2011-09-25 17:48:00 -0700 | [diff] [blame] | 115 | } |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 116 | ScopedUtfChars name(env, java_name); |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 117 | if (name.c_str() == NULL) { |
| 118 | return; |
| 119 | } |
| 120 | thread->SetThreadName(name.c_str()); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 124 | * Alter the priority of the specified thread. "new_priority" will range |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 125 | * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal" |
| 126 | * threads at Thread.NORM_PRIORITY (5). |
| 127 | */ |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 128 | static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 129 | ScopedObjectAccess soa(env); |
| 130 | MutexLock mu(*GlobalSynchronization::thread_list_lock_); |
| 131 | Thread* thread = Thread::FromManagedThread(soa, java_thread); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 132 | if (thread != NULL) { |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 133 | thread->SetNativePriority(new_priority); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 134 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Causes the thread to temporarily pause and allow other threads to execute. |
| 139 | * |
| 140 | * The exact behavior is poorly defined. Some discussion here: |
| 141 | * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html |
| 142 | */ |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 143 | static void Thread_yield(JNIEnv*, jobject) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 144 | sched_yield(); |
| 145 | } |
| 146 | |
| 147 | static JNINativeMethod gMethods[] = { |
| 148 | NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"), |
| 149 | NATIVE_METHOD(Thread, interrupted, "()Z"), |
| 150 | NATIVE_METHOD(Thread, isInterrupted, "()Z"), |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 151 | NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;JZ)V"), |
Elliott Hughes | cf2b2d4 | 2012-03-27 17:11:42 -0700 | [diff] [blame] | 152 | NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 153 | NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"), |
| 154 | NATIVE_METHOD(Thread, nativeInterrupt, "()V"), |
| 155 | NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"), |
| 156 | NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"), |
| 157 | NATIVE_METHOD(Thread, yield, "()V"), |
| 158 | }; |
| 159 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 160 | void register_java_lang_Thread(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 161 | REGISTER_NATIVE_METHODS("java/lang/Thread"); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | } // namespace art |