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 "java_lang_Thread.h"
18
19 #include "common_throws.h"
20 #include "jni_internal.h"
21 #include "monitor.h"
22 #include "mirror/object.h"
23 #include "scoped_fast_native_object_access.h"
24 #include "scoped_thread_state_change.h"
25 #include "ScopedUtfChars.h"
26 #include "thread.h"
27 #include "thread_list.h"
28 #include "verify_object-inl.h"
29
30 namespace art {
31
Thread_currentThread(JNIEnv * env,jclass)32 static jobject Thread_currentThread(JNIEnv* env, jclass) {
33 ScopedFastNativeObjectAccess soa(env);
34 return soa.AddLocalReference<jobject>(soa.Self()->GetPeer());
35 }
36
Thread_interrupted(JNIEnv * env,jclass)37 static jboolean Thread_interrupted(JNIEnv* env, jclass) {
38 return static_cast<JNIEnvExt*>(env)->self->Interrupted() ? JNI_TRUE : JNI_FALSE;
39 }
40
Thread_isInterrupted(JNIEnv * env,jobject java_thread)41 static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) {
42 ScopedFastNativeObjectAccess soa(env);
43 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
44 Thread* thread = Thread::FromManagedThread(soa, java_thread);
45 return (thread != nullptr) ? thread->IsInterrupted() : JNI_FALSE;
46 }
47
Thread_nativeCreate(JNIEnv * env,jclass,jobject java_thread,jlong stack_size,jboolean daemon)48 static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size,
49 jboolean daemon) {
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
59 Thread::CreateNativeThread(env, java_thread, stack_size, daemon == JNI_TRUE);
60 }
61
Thread_nativeGetStatus(JNIEnv * env,jobject java_thread,jboolean has_been_started)62 static jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) {
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
71 ScopedObjectAccess soa(env);
72 ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting);
73 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
74 Thread* thread = Thread::FromManagedThread(soa, java_thread);
75 if (thread != nullptr) {
76 internal_thread_state = thread->GetState();
77 }
78 switch (internal_thread_state) {
79 case kTerminated: return kJavaTerminated;
80 case kRunnable: return kJavaRunnable;
81 case kTimedWaiting: return kJavaTimedWaiting;
82 case kSleeping: return kJavaTimedWaiting;
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;
89 case kWaitingForCheckPointsToRun: return kJavaWaiting;
90 case kWaitingForDebuggerSend: return kJavaWaiting;
91 case kWaitingForDebuggerToAttach: return kJavaWaiting;
92 case kWaitingInMainDebuggerLoop: return kJavaWaiting;
93 case kWaitingForDebuggerSuspension: return kJavaWaiting;
94 case kWaitingForDeoptimization: return kJavaWaiting;
95 case kWaitingForGetObjectsAllocated: return kJavaWaiting;
96 case kWaitingForJniOnLoad: return kJavaWaiting;
97 case kWaitingForSignalCatcherOutput: return kJavaWaiting;
98 case kWaitingInMainSignalCatcherLoop: return kJavaWaiting;
99 case kWaitingForMethodTracingStart: return kJavaWaiting;
100 case kWaitingForVisitObjects: return kJavaWaiting;
101 case kWaitingWeakGcRootRead: return kJavaRunnable;
102 case kWaitingForGcThreadFlip: return kJavaWaiting;
103 case kSuspended: return kJavaRunnable;
104 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
105 }
106 LOG(ERROR) << "Unexpected thread state: " << internal_thread_state;
107 return -1; // Unreachable.
108 }
109
Thread_nativeHoldsLock(JNIEnv * env,jobject java_thread,jobject java_object)110 static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) {
111 ScopedObjectAccess soa(env);
112 mirror::Object* object = soa.Decode<mirror::Object*>(java_object);
113 if (object == nullptr) {
114 ThrowNullPointerException("object == null");
115 return JNI_FALSE;
116 }
117 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
118 Thread* thread = Thread::FromManagedThread(soa, java_thread);
119 return thread->HoldsLock(object);
120 }
121
Thread_nativeInterrupt(JNIEnv * env,jobject java_thread)122 static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) {
123 ScopedFastNativeObjectAccess soa(env);
124 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
125 Thread* thread = Thread::FromManagedThread(soa, java_thread);
126 if (thread != nullptr) {
127 thread->Interrupt(soa.Self());
128 }
129 }
130
Thread_nativeSetName(JNIEnv * env,jobject peer,jstring java_name)131 static void Thread_nativeSetName(JNIEnv* env, jobject peer, jstring java_name) {
132 ScopedUtfChars name(env, java_name);
133 {
134 ScopedObjectAccess soa(env);
135 if (soa.Decode<mirror::Object*>(peer) == soa.Self()->GetPeer()) {
136 soa.Self()->SetThreadName(name.c_str());
137 return;
138 }
139 }
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.
143 ThreadList* thread_list = Runtime::Current()->GetThreadList();
144 bool timed_out;
145 // Take suspend thread lock to avoid races with threads trying to suspend this one.
146 Thread* thread = thread_list->SuspendThreadByPeer(peer, true, false, &timed_out);
147 if (thread != nullptr) {
148 {
149 ScopedObjectAccess soa(env);
150 thread->SetThreadName(name.c_str());
151 }
152 thread_list->Resume(thread, false);
153 } else if (timed_out) {
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 }
157 }
158
159 /*
160 * Alter the priority of the specified thread. "new_priority" will range
161 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
162 * threads at Thread.NORM_PRIORITY (5).
163 */
Thread_nativeSetPriority(JNIEnv * env,jobject java_thread,jint new_priority)164 static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) {
165 ScopedObjectAccess soa(env);
166 MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
167 Thread* thread = Thread::FromManagedThread(soa, java_thread);
168 if (thread != nullptr) {
169 thread->SetNativePriority(new_priority);
170 }
171 }
172
Thread_sleep(JNIEnv * env,jclass,jobject java_lock,jlong ms,jint ns)173 static void Thread_sleep(JNIEnv* env, jclass, jobject java_lock, jlong ms, jint ns) {
174 ScopedFastNativeObjectAccess soa(env);
175 mirror::Object* lock = soa.Decode<mirror::Object*>(java_lock);
176 Monitor::Wait(Thread::Current(), lock, ms, ns, true, kSleeping);
177 }
178
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 */
Thread_yield(JNIEnv *,jobject)185 static void Thread_yield(JNIEnv*, jobject) {
186 sched_yield();
187 }
188
189 static JNINativeMethod gMethods[] = {
190 NATIVE_METHOD(Thread, currentThread, "!()Ljava/lang/Thread;"),
191 NATIVE_METHOD(Thread, interrupted, "!()Z"),
192 NATIVE_METHOD(Thread, isInterrupted, "!()Z"),
193 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;JZ)V"),
194 NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"),
195 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
196 NATIVE_METHOD(Thread, nativeInterrupt, "!()V"),
197 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
198 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
199 NATIVE_METHOD(Thread, sleep, "!(Ljava/lang/Object;JI)V"),
200 NATIVE_METHOD(Thread, yield, "()V"),
201 };
202
register_java_lang_Thread(JNIEnv * env)203 void register_java_lang_Thread(JNIEnv* env) {
204 REGISTER_NATIVE_METHODS("java/lang/Thread");
205 }
206
207 } // namespace art
208