Merge "Revert "AndroidRuntime: Expose javaAttachThread / javaDetachThread.""
This commit is contained in:
@@ -297,7 +297,6 @@ cc_library_shared {
|
|||||||
header_libs: [
|
header_libs: [
|
||||||
"bionic_libc_platform_headers",
|
"bionic_libc_platform_headers",
|
||||||
"dnsproxyd_protocol_headers",
|
"dnsproxyd_protocol_headers",
|
||||||
"libandroid_runtime_threads_headers",
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
host: {
|
host: {
|
||||||
@@ -339,18 +338,3 @@ cc_library_shared {
|
|||||||
never: true,
|
never: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cc_library_headers {
|
|
||||||
name: "libandroid_runtime_threads_headers",
|
|
||||||
host_supported: true,
|
|
||||||
vendor_available: true,
|
|
||||||
// TODO(b/153609531): remove when libbinder is not native_bridge_supported
|
|
||||||
native_bridge_supported: true,
|
|
||||||
// Allow only modules from the following list to create threads that can be
|
|
||||||
// attached to the JVM. This list should be a subset of the dependencies of
|
|
||||||
// libandroid_runtime.
|
|
||||||
visibility: [
|
|
||||||
"//frameworks/native/libs/binder",
|
|
||||||
],
|
|
||||||
export_include_dirs: ["include_threads"],
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -22,7 +22,6 @@
|
|||||||
#include <android-base/properties.h>
|
#include <android-base/properties.h>
|
||||||
#include <android/graphics/jni_runtime.h>
|
#include <android/graphics/jni_runtime.h>
|
||||||
#include <android_runtime/AndroidRuntime.h>
|
#include <android_runtime/AndroidRuntime.h>
|
||||||
#include <android_runtime/threads.h>
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <binder/IBinder.h>
|
#include <binder/IBinder.h>
|
||||||
#include <binder/IPCThreadState.h>
|
#include <binder/IPCThreadState.h>
|
||||||
@@ -1339,15 +1338,14 @@ void AndroidRuntime::onVmCreated(JNIEnv* env)
|
|||||||
return env;
|
return env;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Makes the current thread visible to the VM.
|
* Makes the current thread visible to the VM.
|
||||||
*
|
*
|
||||||
* The JNIEnv pointer returned is only valid for the current thread, and
|
* The JNIEnv pointer returned is only valid for the current thread, and
|
||||||
* thus must be tucked into thread-local storage.
|
* thus must be tucked into thread-local storage.
|
||||||
*/
|
*/
|
||||||
bool androidJavaAttachThread(const char* threadName) {
|
static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
|
||||||
|
{
|
||||||
JavaVMAttachArgs args;
|
JavaVMAttachArgs args;
|
||||||
JavaVM* vm;
|
JavaVM* vm;
|
||||||
jint result;
|
jint result;
|
||||||
@@ -1359,17 +1357,18 @@ bool androidJavaAttachThread(const char* threadName) {
|
|||||||
args.name = (char*) threadName;
|
args.name = (char*) threadName;
|
||||||
args.group = NULL;
|
args.group = NULL;
|
||||||
|
|
||||||
JNIEnv* env;
|
result = vm->AttachCurrentThread(pEnv, (void*) &args);
|
||||||
result = vm->AttachCurrentThread(&env, (void*)&args);
|
if (result != JNI_OK)
|
||||||
if (result != JNI_OK) ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
|
ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
|
||||||
|
|
||||||
return result == JNI_OK;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Detach the current thread from the set visible to the VM.
|
* Detach the current thread from the set visible to the VM.
|
||||||
*/
|
*/
|
||||||
bool androidJavaDetachThread(void) {
|
static int javaDetachThread(void)
|
||||||
|
{
|
||||||
JavaVM* vm;
|
JavaVM* vm;
|
||||||
jint result;
|
jint result;
|
||||||
|
|
||||||
@@ -1377,12 +1376,11 @@ bool androidJavaDetachThread(void) {
|
|||||||
assert(vm != NULL);
|
assert(vm != NULL);
|
||||||
|
|
||||||
result = vm->DetachCurrentThread();
|
result = vm->DetachCurrentThread();
|
||||||
if (result != JNI_OK) ALOGE("ERROR: thread detach failed\n");
|
if (result != JNI_OK)
|
||||||
return result == JNI_OK;
|
ALOGE("ERROR: thread detach failed\n");
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // extern "C"
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* When starting a native thread that will be visible from the VM, we
|
* When starting a native thread that will be visible from the VM, we
|
||||||
* bounce through this to get the right attach/detach action.
|
* bounce through this to get the right attach/detach action.
|
||||||
@@ -1393,16 +1391,18 @@ bool androidJavaDetachThread(void) {
|
|||||||
void* userData = ((void **)args)[1];
|
void* userData = ((void **)args)[1];
|
||||||
char* name = (char*) ((void **)args)[2]; // we own this storage
|
char* name = (char*) ((void **)args)[2]; // we own this storage
|
||||||
free(args);
|
free(args);
|
||||||
|
JNIEnv* env;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
/* hook us into the VM */
|
/* hook us into the VM */
|
||||||
if (!androidJavaAttachThread(name)) return -1;
|
if (javaAttachThread(name, &env) != JNI_OK)
|
||||||
|
return -1;
|
||||||
|
|
||||||
/* start the thread running */
|
/* start the thread running */
|
||||||
result = (*(android_thread_func_t)start)(userData);
|
result = (*(android_thread_func_t)start)(userData);
|
||||||
|
|
||||||
/* unhook us */
|
/* unhook us */
|
||||||
(void)androidJavaDetachThread();
|
javaDetachThread();
|
||||||
free(name);
|
free(name);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2021 The Android Open Source Project
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Manages the interaction between threads and the Android Runtime. If these symbols do not exist
|
|
||||||
// at runtime, it means the current process does not link to libandroid_runtime.
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
|
|
||||||
// Attach current thread to JVM. Return true if successful, false otherwise.
|
|
||||||
bool androidJavaAttachThread(const char* threadName);
|
|
||||||
|
|
||||||
// Detach current thread to JVM. Return true if successful, false otherwise.
|
|
||||||
bool androidJavaDetachThread();
|
|
||||||
|
|
||||||
} // extern "C"
|
|
||||||
Reference in New Issue
Block a user