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
17 #include "common_throws.h"
18 #include "debugger.h"
19 #include "jni_internal.h"
20 #include "monitor.h"
21 #include "mirror/object.h"
22 #include "scoped_thread_state_change.h"
23 #include "ScopedUtfChars.h"
24 #include "thread.h"
25 #include "thread_list.h"
26
27 namespace art {
28
Thread_currentThread(JNIEnv * env,jclass)29 static jobject Thread_currentThread(JNIEnv* env, jclass) {
30 ScopedObjectAccess soa(env);
31 return soa.AddLocalReference<jobject>(soa.Self()->GetPeer());
32 }
33
Thread_interrupted(JNIEnv * env,jclass)34 static jboolean Thread_interrupted(JNIEnv* env, jclass) {
35 return static_cast<JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE;
36 }
37
Thread_isInterrupted(JNIEnv * env,jobject java_thread)38 static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) {
39 ScopedObjectAccess soa(env);
40 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
41 Thread* thread = Thread::FromManagedThread(soa, java_thread);
42 return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE;
43 }
44
Thread_nativeCreate(JNIEnv * env,jclass,jobject java_thread,jlong stack_size,jboolean daemon)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);
48 }
49
Thread_nativeGetStatus(JNIEnv * env,jobject java_thread,jboolean has_been_started)50 static jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) {
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
59 ScopedObjectAccess soa(env);
60 ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting);
61 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
62 Thread* thread = Thread::FromManagedThread(soa, java_thread);
63 if (thread != NULL) {
64 internal_thread_state = thread->GetState();
65 }
66 switch (internal_thread_state) {
67 case kTerminated: return kJavaTerminated;
68 case kRunnable: return kJavaRunnable;
69 case kTimedWaiting: return kJavaTimedWaiting;
70 case kSleeping: return kJavaTimedWaiting;
71 case kBlocked: return kJavaBlocked;
72 case kWaiting: return kJavaWaiting;
73 case kStarting: return kJavaNew;
74 case kNative: return kJavaRunnable;
75 case kWaitingForGcToComplete: return kJavaWaiting;
76 case kWaitingPerformingGc: return kJavaWaiting;
77 case kWaitingForCheckPointsToRun: return kJavaWaiting;
78 case kWaitingForDebuggerSend: return kJavaWaiting;
79 case kWaitingForDebuggerToAttach: return kJavaWaiting;
80 case kWaitingInMainDebuggerLoop: return kJavaWaiting;
81 case kWaitingForDebuggerSuspension: return kJavaWaiting;
82 case kWaitingForJniOnLoad: return kJavaWaiting;
83 case kWaitingForSignalCatcherOutput: return kJavaWaiting;
84 case kWaitingInMainSignalCatcherLoop: return kJavaWaiting;
85 case kSuspended: return kJavaRunnable;
86 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
87 }
88 return -1; // Unreachable.
89 }
90
Thread_nativeHoldsLock(JNIEnv * env,jobject java_thread,jobject java_object)91 static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) {
92 ScopedObjectAccess soa(env);
93 mirror::Object* object = soa.Decode<mirror::Object*>(java_object);
94 if (object == NULL) {
95 ThrowNullPointerException(NULL, "object == null");
96 return JNI_FALSE;
97 }
98 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
99 Thread* thread = Thread::FromManagedThread(soa, java_thread);
100 return thread->HoldsLock(object);
101 }
102
Thread_nativeInterrupt(JNIEnv * env,jobject java_thread)103 static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) {
104 ScopedObjectAccess soa(env);
105 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
106 Thread* thread = Thread::FromManagedThread(soa, java_thread);
107 if (thread != NULL) {
108 thread->Interrupt();
109 }
110 }
111
Thread_nativeSetName(JNIEnv * env,jobject peer,jstring java_name)112 static void Thread_nativeSetName(JNIEnv* env, jobject peer, jstring java_name) {
113 ScopedUtfChars name(env, java_name);
114 {
115 ScopedObjectAccess soa(env);
116 if (soa.Decode<mirror::Object*>(peer) == soa.Self()->GetPeer()) {
117 soa.Self()->SetThreadName(name.c_str());
118 return;
119 }
120 }
121 // Suspend thread to avoid it from killing itself while we set its name. We don't just hold the
122 // thread list lock to avoid this, as setting the thread name causes mutator to lock/unlock
123 // in the DDMS send code.
124 bool timed_out;
125 Thread* thread = Thread::SuspendForDebugger(peer, true, &timed_out);
126 if (thread != NULL) {
127 {
128 ScopedObjectAccess soa(env);
129 thread->SetThreadName(name.c_str());
130 }
131 Runtime::Current()->GetThreadList()->Resume(thread, true);
132 } else if (timed_out) {
133 LOG(ERROR) << "Trying to set thread name to '" << name.c_str() << "' failed as the thread "
134 "failed to suspend within a generous timeout.";
135 }
136 }
137
138 /*
139 * Alter the priority of the specified thread. "new_priority" will range
140 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
141 * threads at Thread.NORM_PRIORITY (5).
142 */
Thread_nativeSetPriority(JNIEnv * env,jobject java_thread,jint new_priority)143 static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) {
144 ScopedObjectAccess soa(env);
145 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
146 Thread* thread = Thread::FromManagedThread(soa, java_thread);
147 if (thread != NULL) {
148 thread->SetNativePriority(new_priority);
149 }
150 }
151
Thread_sleep(JNIEnv * env,jclass,jobject java_lock,jlong ms,jint ns)152 static void Thread_sleep(JNIEnv* env, jclass, jobject java_lock, jlong ms, jint ns) {
153 ScopedObjectAccess soa(env);
154 mirror::Object* lock = soa.Decode<mirror::Object*>(java_lock);
155 Monitor::Wait(Thread::Current(), lock, ms, ns, true, kSleeping);
156 }
157
158 /*
159 * Causes the thread to temporarily pause and allow other threads to execute.
160 *
161 * The exact behavior is poorly defined. Some discussion here:
162 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
163 */
Thread_yield(JNIEnv *,jobject)164 static void Thread_yield(JNIEnv*, jobject) {
165 sched_yield();
166 }
167
168 static JNINativeMethod gMethods[] = {
169 NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"),
170 NATIVE_METHOD(Thread, interrupted, "()Z"),
171 NATIVE_METHOD(Thread, isInterrupted, "()Z"),
172 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;JZ)V"),
173 NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"),
174 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
175 NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
176 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
177 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
178 NATIVE_METHOD(Thread, sleep, "(Ljava/lang/Object;JI)V"),
179 NATIVE_METHOD(Thread, yield, "()V"),
180 };
181
register_java_lang_Thread(JNIEnv * env)182 void register_java_lang_Thread(JNIEnv* env) {
183 REGISTER_NATIVE_METHODS("java/lang/Thread");
184 }
185
186 } // namespace art
187