Cleanup JBackgroundExecutor and support thread naming (#33780)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/33780

Currently this just has a default ThreadPool name, which can be confusing in systraces and crash reports.

Changelog: [Internal]

Reviewed By: ryancat

Differential Revision: D36200090

fbshipit-source-id: 22918993e7c822ed721ccaf79cdcd9d2a972193d
This commit is contained in:
Pieter De Baets 2022-05-09 02:07:26 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 94341f0380
Коммит 0d9e054ec4
5 изменённых файлов: 35 добавлений и 25 удалений

Просмотреть файл

@ -10,15 +10,32 @@ package com.facebook.react.bridge;
import com.facebook.proguard.annotations.DoNotStrip;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
@DoNotStrip
public class BackgroundExecutor {
private static final String TAG = "FabricBackgroundExecutor";
private static class NamedThreadFactory implements ThreadFactory {
private final String mName;
public NamedThreadFactory(String name) {
mName = name;
}
@Override
public Thread newThread(Runnable r) {
Thread thread = Executors.defaultThreadFactory().newThread(r);
thread.setName(mName);
return thread;
}
}
private final ExecutorService mExecutorService;
@DoNotStrip
private BackgroundExecutor() {
mExecutorService = Executors.newFixedThreadPool(1);
private BackgroundExecutor(String name) {
mExecutorService = Executors.newFixedThreadPool(1, new NamedThreadFactory(name));
}
@DoNotStrip

Просмотреть файл

@ -6,8 +6,10 @@
*/
#include "Binding.h"
#include "AsyncEventBeat.h"
#include "EventEmitterWrapper.h"
#include "JBackgroundExecutor.h"
#include "ReactNativeConfigHolder.h"
#include "StateWrapperImpl.h"
@ -455,8 +457,8 @@ void Binding::installFabricUIManager(
toolbox.synchronousEventBeatFactory = synchronousBeatFactory;
toolbox.asynchronousEventBeatFactory = asynchronousBeatFactory;
backgroundExecutor_ = std::make_unique<JBackgroundExecutor>();
toolbox.backgroundExecutor = backgroundExecutor_->get();
backgroundExecutor_ = JBackgroundExecutor::create("fabric_bg");
toolbox.backgroundExecutor = backgroundExecutor_;
animationDriver_ = std::make_shared<LayoutAnimationDriver>(
runtimeExecutor, contextContainer, this);
@ -558,8 +560,7 @@ void Binding::preallocateView(
};
if (dispatchPreallocationInBackground_) {
auto backgroundExecutor = backgroundExecutor_->get();
backgroundExecutor(preallocationFunction);
backgroundExecutor_(preallocationFunction);
} else {
preallocationFunction();
}

Просмотреть файл

@ -9,6 +9,9 @@
#include "FabricMountingManager.h"
#include <memory>
#include <mutex>
#include <fbjni/fbjni.h>
#include <react/jni/JRuntimeExecutor.h>
#include <react/jni/JRuntimeScheduler.h>
@ -18,12 +21,9 @@
#include <react/renderer/scheduler/SchedulerDelegate.h>
#include <react/renderer/uimanager/LayoutAnimationStatusDelegate.h>
#include <memory>
#include <mutex>
#include "ComponentFactory.h"
#include "EventBeatManager.h"
#include "EventEmitterWrapper.h"
#include "JBackgroundExecutor.h"
#include "SurfaceHandlerBinding.h"
namespace facebook {
@ -144,7 +144,7 @@ class Binding : public jni::HybridClass<Binding>,
std::shared_ptr<LayoutAnimationDriver> animationDriver_;
std::unique_ptr<JBackgroundExecutor> backgroundExecutor_;
BackgroundExecutor backgroundExecutor_;
butter::map<SurfaceId, SurfaceHandler> surfaceHandlerRegistry_{};
butter::shared_mutex

Просмотреть файл

@ -18,16 +18,15 @@ using namespace facebook::jni;
using facebook::react::JNativeRunnable;
using facebook::react::Runnable;
BackgroundExecutor JBackgroundExecutor::get() {
auto self = JBackgroundExecutor::create();
return [self](std::function<void()> &&runnable) {
BackgroundExecutor JBackgroundExecutor::create(const std::string &name) {
auto instance = make_global(newInstance(name));
return [instance = std::move(instance)](std::function<void()> &&runnable) {
static auto method =
findClassStatic(JBackgroundExecutor::JBackgroundExecutorJavaDescriptor)
->getMethod<void(Runnable::javaobject)>("queueRunnable");
javaClassStatic()->getMethod<void(Runnable::javaobject)>(
"queueRunnable");
auto jrunnable = JNativeRunnable::newObjectCxxArgs(std::move(runnable));
method(self, static_ref_cast<Runnable::javaobject>(jrunnable).get());
method(instance, jrunnable.get());
};
}

Просмотреть файл

@ -20,14 +20,7 @@ class JBackgroundExecutor : public JavaClass<JBackgroundExecutor> {
static auto constexpr kJavaDescriptor =
"Lcom/facebook/react/bridge/BackgroundExecutor;";
constexpr static auto JBackgroundExecutorJavaDescriptor =
"com/facebook/react/bridge/BackgroundExecutor";
static global_ref<javaobject> create() {
return make_global(newInstance());
}
BackgroundExecutor get();
static BackgroundExecutor create(const std::string &name);
};
} // namespace react