blob: 44578ee89c35a5f3f0ff53cc664c1f4ca4dbbf99 [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) {
29 Object* peer = Decode<Object*>(env, Thread::Current()->GetPeer());
30 return AddLocalReference<jobject>(env, peer);
31}
32
33jboolean Thread_interrupted(JNIEnv* env, jclass) {
34 return Thread::Current()->Interrupted();
35}
36
37jboolean Thread_isInterrupted(JNIEnv* env, jobject javaThread) {
38 ThreadListLock lock;
39 Thread* thread = Thread::FromManagedThread(env, javaThread);
40 return thread->IsInterrupted();
41}
42
43void Thread_nativeCreate(JNIEnv* env, jclass, jobject javaThread, jlong stackSize) {
44 UNIMPLEMENTED(FATAL);
45 //Object* threadObj = dvmDecodeIndirectRef(env, javaThread);
46 //dvmCreateInterpThread(threadObj, (int) stackSize);
47}
48
49jint Thread_nativeGetStatus(JNIEnv* env, jobject javaThread) {
50 ThreadListLock lock;
51 Thread* thread = Thread::FromManagedThread(env, javaThread);
52 return static_cast<jint>(thread->GetState());
53}
54
55jboolean Thread_nativeHoldsLock(JNIEnv* env, jobject javaThread, jobject javaObject) {
56 ThreadListLock lock;
57 //Thread* thread = Thread::FromManagedThread(env, javaThread);
58 //Object* object = dvmDecodeIndirectRef(env, javaObject);
59 //if (object == NULL) {
60 //dvmThrowNullPointerException("object == null");
61 //return JNI_FALSE;
62 //}
63 //Thread* thread = Thread::FromManagedThread(env, javaThread);
64 //int result = dvmHoldsLock(thread, object);
65 //return result;
66 UNIMPLEMENTED(FATAL);
67 return JNI_FALSE;
68}
69
70void Thread_nativeInterrupt(JNIEnv* env, jobject javaThread) {
71 ThreadListLock lock;
72 UNIMPLEMENTED(FATAL);
73 //Thread* thread = Thread::FromManagedThread(env, javaThread);
74 //if (thread != NULL) {
75 //dvmThreadInterrupt(thread);
76 //}
77}
78
79void Thread_nativeSetName(JNIEnv* env, jobject javaThread, jstring javaName) {
80 ThreadListLock lock;
81 UNIMPLEMENTED(WARNING);
82 //Thread* thread = Thread::FromManagedThread(env, javaThread);
83 //StringObject* nameStr = (StringObject*) dvmDecodeIndirectRef(env, javaName);
84 //int threadId = (thread != NULL) ? thread->threadId : -1;
85 //dvmDdmSendThreadNameChange(threadId, nameStr);
86}
87
88/*
89 * Alter the priority of the specified thread. "newPriority" will range
90 * from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY (1-10), with "normal"
91 * threads at Thread.NORM_PRIORITY (5).
92 */
93void Thread_nativeSetPriority(JNIEnv* env, jobject javaThread, jint newPriority) {
94 ThreadListLock lock;
95 Thread* thread = Thread::FromManagedThread(env, javaThread);
96 thread->SetNativePriority(newPriority);
97}
98
99/*
100 * Causes the thread to temporarily pause and allow other threads to execute.
101 *
102 * The exact behavior is poorly defined. Some discussion here:
103 * http://www.cs.umd.edu/~pugh/java/memoryModel/archive/0944.html
104 */
105void Thread_yield(JNIEnv*, jobject) {
106 sched_yield();
107}
108
109static JNINativeMethod gMethods[] = {
110 NATIVE_METHOD(Thread, currentThread, "()Ljava/lang/Thread;"),
111 NATIVE_METHOD(Thread, interrupted, "()Z"),
112 NATIVE_METHOD(Thread, isInterrupted, "()Z"),
113 NATIVE_METHOD(Thread, nativeCreate, "(Ljava/lang/Thread;J)V"),
114 NATIVE_METHOD(Thread, nativeGetStatus, "()I"),
115 NATIVE_METHOD(Thread, nativeHoldsLock, "(Ljava/lang/Object;)Z"),
116 NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
117 NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
118 NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
119 NATIVE_METHOD(Thread, yield, "()V"),
120};
121
122} // namespace
123
124void register_java_lang_Thread(JNIEnv* env) {
125 jniRegisterNativeMethods(env, "java/lang/Thread", gMethods, NELEM(gMethods));
126}
127
128} // namespace art