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