Skip to content

Commit

Permalink
Add bolts task files as internal dependency (#36824)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #36824

After this diff, RNTester Android can build successfully, and it should be safe to land this stack to move Venice Android to OSS folders

Changelog:
[Android][Changed] - Add bolts task files as internal dependency

Reviewed By: cortinico

Differential Revision: D44729814

fbshipit-source-id: 80926dc548bb269bb9c69efab4c7620a3fb9fdc2
  • Loading branch information
Lulu Wu authored and facebook-github-bot committed Apr 20, 2023
1 parent 5428b55 commit 8f0307b
Show file tree
Hide file tree
Showing 19 changed files with 1,944 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import android.app.Activity;
import android.content.Context;
import android.view.View;
import bolts.Continuation;
import bolts.Task;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.JavaJSExecutor;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridgeless.internal.bolts.Continuation;
import com.facebook.react.bridgeless.internal.bolts.Task;
import com.facebook.react.devsupport.DevSupportManagerBase;
import com.facebook.react.devsupport.HMRClient;
import com.facebook.react.devsupport.ReactInstanceDevHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
import android.app.Activity;
import android.content.Context;
import androidx.annotation.Nullable;
import bolts.Continuation;
import bolts.Task;
import bolts.TaskCompletionSource;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.infer.annotation.Nullsafe;
Expand All @@ -41,6 +38,9 @@
import com.facebook.react.bridge.queue.QueueThreadExceptionHandler;
import com.facebook.react.bridge.queue.ReactQueueConfiguration;
import com.facebook.react.bridgeless.exceptionmanager.ReactJsExceptionHandler;
import com.facebook.react.bridgeless.internal.bolts.Continuation;
import com.facebook.react.bridgeless.internal.bolts.Task;
import com.facebook.react.bridgeless.internal.bolts.TaskCompletionSource;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.config.ReactFeatureFlags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
import android.view.View;
import android.view.View.MeasureSpec;
import androidx.annotation.UiThread;
import bolts.Task;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.infer.annotation.ThreadSafe;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.NativeMap;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.bridgeless.internal.bolts.Task;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.fabric.SurfaceHandler;
import com.facebook.react.fabric.SurfaceHandlerBinding;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.bridgeless.internal.bolts;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* Aggregates multiple {@code Throwable}s that may be thrown in the process of a task's execution.
*
* @see Task#whenAll(java.util.Collection)
*/
public class AggregateException extends Exception {
private static final long serialVersionUID = 1L;

private static final String DEFAULT_MESSAGE = "There were multiple errors.";

private List<Throwable> innerThrowables;

/**
* Constructs a new {@code AggregateException} with the current stack trace, the specified detail
* message and with references to the inner throwables that are the cause of this exception.
*
* @param detailMessage The detail message for this exception.
* @param innerThrowables The exceptions that are the cause of the current exception.
*/
public AggregateException(String detailMessage, Throwable[] innerThrowables) {
this(detailMessage, Arrays.asList(innerThrowables));
}

/**
* Constructs a new {@code AggregateException} with the current stack trace, the specified detail
* message and with references to the inner throwables that are the cause of this exception.
*
* @param detailMessage The detail message for this exception.
* @param innerThrowables The exceptions that are the cause of the current exception.
*/
public AggregateException(String detailMessage, List<? extends Throwable> innerThrowables) {
super(
detailMessage,
innerThrowables != null && innerThrowables.size() > 0 ? innerThrowables.get(0) : null);
this.innerThrowables = Collections.unmodifiableList(innerThrowables);
}

/**
* Constructs a new {@code AggregateException} with the current stack trace and with references to
* the inner throwables that are the cause of this exception.
*
* @param innerThrowables The exceptions that are the cause of the current exception.
*/
public AggregateException(List<? extends Throwable> innerThrowables) {
this(DEFAULT_MESSAGE, innerThrowables);
}

/**
* Returns a read-only {@link List} of the {@link Throwable} instances that caused the current
* exception.
*/
public List<Throwable> getInnerThrowables() {
return innerThrowables;
}

@Override
public void printStackTrace(PrintStream err) {
super.printStackTrace(err);

int currentIndex = -1;
for (Throwable throwable : innerThrowables) {
err.append("\n");
err.append(" Inner throwable #");
err.append(Integer.toString(++currentIndex));
err.append(": ");
throwable.printStackTrace(err);
err.append("\n");
}
}

@Override
public void printStackTrace(PrintWriter err) {
super.printStackTrace(err);

int currentIndex = -1;
for (Throwable throwable : innerThrowables) {
err.append("\n");
err.append(" Inner throwable #");
err.append(Integer.toString(++currentIndex));
err.append(": ");
throwable.printStackTrace(err);
err.append("\n");
}
}

/** @deprecated Please use {@link #getInnerThrowables()} instead. */
@Deprecated
public List<Exception> getErrors() {
List<Exception> errors = new ArrayList<Exception>();
if (innerThrowables == null) {
return errors;
}

for (Throwable cause : innerThrowables) {
if (cause instanceof Exception) {
errors.add((Exception) cause);
} else {
errors.add(new Exception(cause));
}
}
return errors;
}

/** @deprecated Please use {@link #getInnerThrowables()} instead. */
@Deprecated
public Throwable[] getCauses() {
return innerThrowables.toArray(new Throwable[innerThrowables.size()]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.bridgeless.internal.bolts;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* This was created because the helper methods in {@link java.util.concurrent.Executors} do not work
* as people would normally expect.
*
* <p>Normally, you would think that a cached thread pool would create new threads when necessary,
* queue them when the pool is full, and kill threads when they've been inactive for a certain
* period of time. This is not how {@link java.util.concurrent.Executors#newCachedThreadPool()}
* works.
*
* <p>Instead, {@link java.util.concurrent.Executors#newCachedThreadPool()} executes all tasks on a
* new or cached thread immediately because corePoolSize is 0, SynchronousQueue is a queue with size
* 0 and maxPoolSize is Integer.MAX_VALUE. This is dangerous because it can create an unchecked
* amount of threads.
*/
/* package */ final class AndroidExecutors {

private static final AndroidExecutors INSTANCE = new AndroidExecutors();

private final Executor uiThread;

private AndroidExecutors() {
uiThread = new UIThreadExecutor();
}

/**
* Nexus 5: Quad-Core Moto X: Dual-Core
*
* <p>AsyncTask: CORE_POOL_SIZE = CPU_COUNT + 1 MAX_POOL_SIZE = CPU_COUNT * 2 + 1
*
* <p>https://github.com/android/platform_frameworks_base/commit/719c44e03b97e850a46136ba336d729f5fbd1f47
*/
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
/* package */ static final int CORE_POOL_SIZE = CPU_COUNT + 1;
/* package */ static final int MAX_POOL_SIZE = CPU_COUNT * 2 + 1;
/* package */ static final long KEEP_ALIVE_TIME = 1L;

/**
* Creates a proper Cached Thread Pool. Tasks will reuse cached threads if available or create new
* threads until the core pool is full. tasks will then be queued. If an task cannot be queued, a
* new thread will be created unless this would exceed max pool size, then the task will be
* rejected. Threads will time out after 1 second.
*
* <p>Core thread timeout is only available on android-9+.
*
* @return the newly created thread pool
*/
public static ExecutorService newCachedThreadPool() {
ThreadPoolExecutor executor =
new ThreadPoolExecutor(
CORE_POOL_SIZE,
MAX_POOL_SIZE,
KEEP_ALIVE_TIME,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());

allowCoreThreadTimeout(executor, true);

return executor;
}

/**
* Creates a proper Cached Thread Pool. Tasks will reuse cached threads if available or create new
* threads until the core pool is full. tasks will then be queued. If an task cannot be queued, a
* new thread will be created unless this would exceed max pool size, then the task will be
* rejected. Threads will time out after 1 second.
*
* <p>Core thread timeout is only available on android-9+.
*
* @param threadFactory the factory to use when creating new threads
* @return the newly created thread pool
*/
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
ThreadPoolExecutor executor =
new ThreadPoolExecutor(
CORE_POOL_SIZE,
MAX_POOL_SIZE,
KEEP_ALIVE_TIME,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);

allowCoreThreadTimeout(executor, true);

return executor;
}

/**
* Compatibility helper function for {@link
* java.util.concurrent.ThreadPoolExecutor#allowCoreThreadTimeOut(boolean)}
*
* <p>Only available on android-9+.
*
* @param executor the {@link java.util.concurrent.ThreadPoolExecutor}
* @param value true if should time out, else false
*/
@SuppressLint("NewApi")
public static void allowCoreThreadTimeout(ThreadPoolExecutor executor, boolean value) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
executor.allowCoreThreadTimeOut(value);
}
}

/** An {@link java.util.concurrent.Executor} that executes tasks on the UI thread. */
public static Executor uiThread() {
return INSTANCE.uiThread;
}

/** An {@link java.util.concurrent.Executor} that runs tasks on the UI thread. */
private static class UIThreadExecutor implements Executor {
@Override
public void execute(Runnable command) {
new Handler(Looper.getMainLooper()).post(command);
}
}
}
Loading

0 comments on commit 8f0307b

Please sign in to comment.