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