1 /*
2  * Copyright (C) 2020 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.h>
18 
19 #include <iostream>
20 
21 #include "android-base/logging.h"
22 
23 struct ThreadArgs {
24   JavaVM* jvm;
25   jobject consumer;
26   JavaVMAttachArgs* attach_args;
27   bool set_in_java;
28 };
29 
30 // The main method of the test thread. The ThreadArgs controls what this does.
ThreadMain(void * arg)31 void* ThreadMain(void* arg) {
32   ThreadArgs* args = reinterpret_cast<ThreadArgs*>(arg);
33   JNIEnv* env = nullptr;
34   pthread_t self = pthread_self();
35 
36   int err = pthread_setname_np(self, "native-thread");
37   CHECK_EQ(err, 0);
38 
39   args->jvm->AttachCurrentThread(&env, args->attach_args);
40 
41   jclass thread_class = env->FindClass("java/lang/Thread");
42   jclass consumer_class = env->FindClass("java/util/function/BiConsumer");
43   jmethodID current_thread_method =
44       env->GetStaticMethodID(thread_class, "currentThread", "()Ljava/lang/Thread;");
45   jmethodID accept_method =
46       env->GetMethodID(consumer_class, "accept", "(Ljava/lang/Object;Ljava/lang/Object;)V");
47   jobject current_thread = env->CallStaticObjectMethod(thread_class, current_thread_method);
48   if (args->set_in_java) {
49     jmethodID set_name_method = env->GetMethodID(thread_class, "setName", "(Ljava/lang/String;)V");
50     jobject str_name = env->NewStringUTF("native-thread-set-java");
51     env->CallVoidMethod(current_thread, set_name_method, str_name);
52   }
53 
54   char name_chars[1024];
55   err = pthread_getname_np(self, name_chars, sizeof(name_chars));
56   CHECK_EQ(err, 0);
57   jobject str_name = env->NewStringUTF(name_chars);
58 
59   env->CallVoidMethod(args->consumer, accept_method, str_name, current_thread);
60 
61   args->jvm->DetachCurrentThread();
62 
63   return nullptr;
64 }
65 
Java_Main_runThreadTestWithName(JNIEnv * env,jclass,jobject consumer)66 extern "C" JNIEXPORT void Java_Main_runThreadTestWithName(JNIEnv* env,
67                                                           jclass /*clazz*/,
68                                                           jobject consumer) {
69   jobject global_consumer = env->NewGlobalRef(consumer);
70   JavaVMAttachArgs args;
71   args.group = nullptr;
72   args.name = "java-native-thread";
73   args.version = JNI_VERSION_1_6;
74   ThreadArgs ta {
75     .jvm = nullptr, .consumer = global_consumer, .attach_args = &args, .set_in_java = false
76   };
77   env->GetJavaVM(&ta.jvm);
78   pthread_t child;
79   pthread_create(&child, nullptr, ThreadMain, &ta);
80   void* ret;
81   pthread_join(child, &ret);
82   env->DeleteGlobalRef(ta.consumer);
83 }
84 
Java_Main_runThreadTest(JNIEnv * env,jclass,jobject consumer)85 extern "C" JNIEXPORT void Java_Main_runThreadTest(JNIEnv* env, jclass /*clazz*/, jobject consumer) {
86   jobject global_consumer = env->NewGlobalRef(consumer);
87   ThreadArgs ta {
88     .jvm = nullptr, .consumer = global_consumer, .attach_args = nullptr, .set_in_java = false
89   };
90   env->GetJavaVM(&ta.jvm);
91   pthread_t child;
92   pthread_create(&child, nullptr, ThreadMain, &ta);
93   void* ret;
94   pthread_join(child, &ret);
95   env->DeleteGlobalRef(ta.consumer);
96 }
97 
Java_Main_runThreadTestSetJava(JNIEnv * env,jclass,jobject consumer)98 extern "C" JNIEXPORT void Java_Main_runThreadTestSetJava(JNIEnv* env,
99                                                          jclass /*clazz*/,
100                                                          jobject consumer) {
101   jobject global_consumer = env->NewGlobalRef(consumer);
102   ThreadArgs ta {
103     .jvm = nullptr, .consumer = global_consumer, .attach_args = nullptr, .set_in_java = true
104   };
105   env->GetJavaVM(&ta.jvm);
106   pthread_t child;
107   pthread_create(&child, nullptr, ThreadMain, &ta);
108   void* ret;
109   pthread_join(child, &ret);
110   env->DeleteGlobalRef(ta.consumer);
111 }
112