blob: 286a21118b546b6e776b3282a7aa9402668d929e [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
17#include "jni_internal.h"
18#include "object.h"
19#include "thread.h"
20#include "thread_list.h"
21
22#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
23
24namespace art {
25
26namespace {
27
28jobject Thread_currentThread(JNIEnv* env, jclass) {
Elliott Hughesd369bb72011-09-12 14:41:14 -070029 return AddLocalReference<jobject>(env, Thread::Current()->GetPeer());
Elliott Hughes8daa0922011-09-11 13:46:25 -070030}
31
32jboolean Thread_interrupted(JNIEnv* env, jclass) {
33 return Thread::Current()->Interrupted();
34}
35
36jboolean Thread_isInterrupted(JNIEnv* env, jobject javaThread) {
37 ThreadListLock lock;
38 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070039 return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE;
Elliott Hughes8daa0922011-09-11 13:46:25 -070040}
41
42void Thread_nativeCreate(JNIEnv* env, jclass, jobject javaThread, jlong stackSize) {
Elliott Hughesd369bb72011-09-12 14:41:14 -070043 Object* managedThread = Decode<Object*>(env, javaThread);
44 Thread::Create(managedThread, stackSize);
Elliott Hughes8daa0922011-09-11 13:46:25 -070045}
46
47jint Thread_nativeGetStatus(JNIEnv* env, jobject javaThread) {
48 ThreadListLock lock;
49 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070050 Thread::State state = (thread != NULL) ? thread->GetState() : Thread::kUnknown;
51 return static_cast<jint>(state);
Elliott Hughes8daa0922011-09-11 13:46:25 -070052}
53
54jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject javaThread, jobject javaObject) {
55 ThreadListLock lock;
56 //Thread* thread = Thread::FromManagedThread(env, javaThread);
57 //Object* object = dvmDecodeIndirectRef(env, javaObject);
58 //if (object == NULL) {
59 //dvmThrowNullPointerException("object == null");
60 //return JNI_FALSE;
61 //}
62 //Thread* thread = Thread::FromManagedThread(env, javaThread);
63 //int result = dvmHoldsLock(thread, object);
64 //return result;
65 UNIMPLEMENTED(FATAL);
66 return JNI_FALSE;
67}
68
69void Thread_nativeInterrupt(JNIEnv* env, jobject javaThread) {
70 ThreadListLock lock;
71 UNIMPLEMENTED(FATAL);
72 //Thread* thread = Thread::FromManagedThread(env, javaThread);
73 //if (thread != NULL) {
74 //dvmThreadInterrupt(thread);
75 //}
76}
77
78void Thread_nativeSetName(JNIEnv* env, jobject javaThread, jstring javaName) {
79 ThreadListLock lock;
80 UNIMPLEMENTED(WARNING);
81 //Thread* thread = Thread::FromManagedThread(env, javaThread);
82 //StringObject* nameStr = (StringObject*) dvmDecodeIndirectRef(env, javaName);
83 //int threadId = (thread != NULL) ? thread->threadId : -1;
84 //dvmDdmSendThreadNameChange(threadId, nameStr);
85}
86
87/*
88 * Alter the priority of the specified thread. "newPriority" will range
89 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
90 * threads at Thread.NORM_PRIORITY (5).
91 */
92void Thread_nativeSetPriority(JNIEnv* env, jobject javaThread, jint newPriority) {
93 ThreadListLock lock;
94 Thread* thread = Thread::FromManagedThread(env, javaThread);
Elliott Hughesd369bb72011-09-12 14:41:14 -070095 if (thread != NULL) {
96 thread->SetNativePriority(newPriority);
97 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070098}
99
100/*
101 * Causes the thread to temporarily pause and allow other threads to execute.
102 *
103 * The exact behavior is poorly defined. Some discussion here:
104 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
105 */
106void Thread_yield(JNIEnv*, jobject) {
107 sched_yield();
108}
109
110static JNINativeMethod gMethods[] = {
111 NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"),
112 NATIVE_METHOD(Thread, interrupted, "()Z"),
113 NATIVE_METHOD(Thread, isInterrupted, "()Z"),
114 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;J)V"),
115 NATIVE_METHOD(Thread, nativeGetStatus, "()I"),
116 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
117 NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
118 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
119 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
120 NATIVE_METHOD(Thread, yield, "()V"),
121};
122
123} // namespace
124
125void register_java_lang_Thread(JNIEnv* env) {
126 jniRegisterNativeMethods(env, "java/lang/Thread", gMethods, NELEM(gMethods));
127}
128
129} // namespace art