blob: ed95a6cf1861c51687362d1c0165f675e84e0aa2 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
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 Hughes82188472011-11-07 18:11:48 -080017#include "debugger.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070018#include "jni_internal.h"
19#include "object.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070020#include "scoped_thread_list_lock.h"
Elliott Hughes54643082011-09-25 17:48:00 -070021#include "ScopedUtfChars.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070022#include "thread.h"
23#include "thread_list.h"
24
Elliott Hughes8daa0922011-09-11 13:46:25 -070025namespace art {
26
Elliott Hughes0512f022012-03-15 22:10:52 -070027static jobject Thread_currentThread(JNIEnv* env, jclass) {
Elliott Hughesd369bb72011-09-12 14:41:14 -070028 return AddLocalReference<jobject>(env, Thread::Current()->GetPeer());
Elliott Hughes8daa0922011-09-11 13:46:25 -070029}
30
Elliott Hughes1bac54f2012-03-16 12:48:31 -070031static jboolean Thread_interrupted(JNIEnv*, jclass) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070032 return Thread::Current()->Interrupted();
33}
34
Elliott Hughes57aba862012-06-20 14:00:47 -070035static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080036 ScopedThreadListLock thread_list_lock;
Elliott Hughes57aba862012-06-20 14:00:47 -070037 Thread* thread = Thread::FromManagedThread(env, java_thread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070038 return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070039}
40
Elliott Hughes57aba862012-06-20 14:00:47 -070041static void Thread_nativeCreate(JNIEnv* env, jclass, jobject java_thread, jlong stack_size) {
42 Object* managedThread = Decode<Object*>(env, java_thread);
43 Thread::CreateNativeThread(managedThread, stack_size);
Elliott Hughes8daa0922011-09-11 13:46:25 -070044}
45
Elliott Hughes57aba862012-06-20 14:00:47 -070046static jint Thread_nativeGetStatus(JNIEnv* env, jobject java_thread, jboolean has_been_started) {
Elliott Hughescf2b2d42012-03-27 17:11:42 -070047 // Ordinals from Java's Thread.State.
48 const jint kJavaNew = 0;
49 const jint kJavaRunnable = 1;
50 const jint kJavaBlocked = 2;
51 const jint kJavaWaiting = 3;
52 const jint kJavaTimedWaiting = 4;
53 const jint kJavaTerminated = 5;
54
Elliott Hughes57aba862012-06-20 14:00:47 -070055 ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting);
Elliott Hughesbbd9d832011-11-07 14:40:00 -080056 ScopedThreadListLock thread_list_lock;
Elliott Hughes57aba862012-06-20 14:00:47 -070057 Thread* thread = Thread::FromManagedThread(env, java_thread);
Elliott Hughescf2b2d42012-03-27 17:11:42 -070058 if (thread != NULL) {
59 internal_thread_state = thread->GetState();
Elliott Hughes8e4aac52011-09-26 17:03:36 -070060 }
Elliott Hughescf2b2d42012-03-27 17:11:42 -070061 switch (internal_thread_state) {
Elliott Hughes34e06962012-04-09 13:55:55 -070062 case kTerminated: return kJavaTerminated;
63 case kRunnable: return kJavaRunnable;
64 case kTimedWaiting: return kJavaTimedWaiting;
65 case kBlocked: return kJavaBlocked;
66 case kWaiting: return kJavaWaiting;
67 case kStarting: return kJavaNew;
68 case kNative: return kJavaRunnable;
69 case kVmWait: return kJavaWaiting;
70 case kSuspended: return kJavaRunnable;
Elliott Hughescf2b2d42012-03-27 17:11:42 -070071 // Don't add a 'default' here so the compiler can spot incompatible enum changes.
72 }
73 return -1; // Unreachable.
Elliott Hughes8daa0922011-09-11 13:46:25 -070074}
75
Elliott Hughes57aba862012-06-20 14:00:47 -070076static jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject java_thread, jobject java_object) {
77 Object* object = Decode<Object*>(env, java_object);
Elliott Hughes5f791332011-09-15 17:45:30 -070078 if (object == NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -070079 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Elliott Hughes5f791332011-09-15 17:45:30 -070080 Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null");
81 return JNI_FALSE;
82 }
Elliott Hughesbbd9d832011-11-07 14:40:00 -080083 ScopedThreadListLock thread_list_lock;
Elliott Hughes57aba862012-06-20 14:00:47 -070084 Thread* thread = Thread::FromManagedThread(env, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -070085 return thread->HoldsLock(object);
Elliott Hughes8daa0922011-09-11 13:46:25 -070086}
87
Elliott Hughes57aba862012-06-20 14:00:47 -070088static void Thread_nativeInterrupt(JNIEnv* env, jobject java_thread) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080089 ScopedThreadListLock thread_list_lock;
Elliott Hughes57aba862012-06-20 14:00:47 -070090 Thread* thread = Thread::FromManagedThread(env, java_thread);
Elliott Hughes5f791332011-09-15 17:45:30 -070091 if (thread != NULL) {
92 thread->Interrupt();
93 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070094}
95
Elliott Hughes57aba862012-06-20 14:00:47 -070096static void Thread_nativeSetName(JNIEnv* env, jobject java_thread, jstring java_name) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080097 ScopedThreadListLock thread_list_lock;
Elliott Hughes57aba862012-06-20 14:00:47 -070098 Thread* thread = Thread::FromManagedThread(env, java_thread);
Elliott Hughes82188472011-11-07 18:11:48 -080099 if (thread == NULL) {
100 return;
Elliott Hughes54643082011-09-25 17:48:00 -0700101 }
Elliott Hughes57aba862012-06-20 14:00:47 -0700102 ScopedUtfChars name(env, java_name);
Elliott Hughes899e7892012-01-24 14:57:32 -0800103 if (name.c_str() == NULL) {
104 return;
105 }
106 thread->SetThreadName(name.c_str());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700107}
108
109/*
Elliott Hughes57aba862012-06-20 14:00:47 -0700110 * Alter the priority of the specified thread. "new_priority" will range
Elliott Hughes8daa0922011-09-11 13:46:25 -0700111 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
112 * threads at Thread.NORM_PRIORITY (5).
113 */
Elliott Hughes57aba862012-06-20 14:00:47 -0700114static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800115 ScopedThreadListLock thread_list_lock;
Elliott Hughes57aba862012-06-20 14:00:47 -0700116 Thread* thread = Thread::FromManagedThread(env, java_thread);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700117 if (thread != NULL) {
Elliott Hughes57aba862012-06-20 14:00:47 -0700118 thread->SetNativePriority(new_priority);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700119 }
Elliott Hughes8daa0922011-09-11 13:46:25 -0700120}
121
122/*
123 * Causes the thread to temporarily pause and allow other threads to execute.
124 *
125 * The exact behavior is poorly defined. Some discussion here:
126 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
127 */
Elliott Hughes0512f022012-03-15 22:10:52 -0700128static void Thread_yield(JNIEnv*, jobject) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700129 sched_yield();
130}
131
132static JNINativeMethod gMethods[] = {
133 NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"),
134 NATIVE_METHOD(Thread, interrupted, "()Z"),
135 NATIVE_METHOD(Thread, isInterrupted, "()Z"),
136 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;J)V"),
Elliott Hughescf2b2d42012-03-27 17:11:42 -0700137 NATIVE_METHOD(Thread, nativeGetStatus, "(Z)I"),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700138 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
139 NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
140 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
141 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
142 NATIVE_METHOD(Thread, yield, "()V"),
143};
144
Elliott Hughes8daa0922011-09-11 13:46:25 -0700145void register_java_lang_Thread(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700146 REGISTER_NATIVE_METHODS("java/lang/Thread");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700147}
148
149} // namespace art