Skip to content

Commit

Permalink
Add dirsync for rendercore-thread-utils
Browse files Browse the repository at this point in the history
Summary:
Copied rendercore-thread-utils into litho-rendercore-thread-utils using hg copy.
```
hg copy fbandroid/libraries/rendercore/rendercore-thread-utils/src fbandroid/libraries/components/litho-rendercore-thread-utils/ --force
```

Also enabled dir sync of rendercore-thread-utils directory so that the source code remains in sync between rendercore and litho.

Reviewed By: astreet

Differential Revision: D69525059

fbshipit-source-id: 0431c7ac50d176bbad3be5c798e08e5a5a9e2a93
  • Loading branch information
zielinskimz authored and facebook-github-bot committed Feb 15, 2025
1 parent f6c3f1c commit 65e6bb1
Show file tree
Hide file tree
Showing 10 changed files with 454 additions and 0 deletions.
1 change: 1 addition & 0 deletions litho-rendercore-thread-utils/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
23 changes: 23 additions & 0 deletions litho-rendercore-thread-utils/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@fbsource//xplat/pfh/FBApp_UIFrameworks_Litho:DEFS.bzl", "FBApp_UIFrameworks_Litho")
load(
"//tools/build_defs/oss:litho_defs.bzl",
"fb_android_library",
)

oncall("litho_components_for_android")

fb_android_library(
name = "rendercore-thread-utils",
srcs = glob(
["src/main/java/**/*.kt"],
),
autoglob = False,
feature = FBApp_UIFrameworks_Litho,
labels = [],
required_for_source_only_abi = True,
visibility = [
"PUBLIC",
],
deps = [
],
)
55 changes: 55 additions & 0 deletions litho-rendercore-thread-utils/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* 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.
*/

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'com.facebook.kotlin.compilerplugins.dataclassgenerate'

project.group = GROUP
version = VERSION_NAME

android {
compileSdkVersion rootProject.compileSdkVersion
buildToolsVersion rootProject.buildToolsVersion

// We're on an old version of Robolectric which requires this, sadly.
useLibrary 'org.apache.http.legacy'

defaultConfig {
minSdkVersion rootProject.minSdkVersion
}

buildTypes {

debug {
}

release {
}
}
namespace 'com.facebook.rendercore.thread.utils'
lint {
abortOnError false
}

// TODO(#62): Re-enable abort on error.
}

dependencies {
compileOnly deps.inferAnnotations
}

apply plugin: "com.vanniktech.maven.publish"
18 changes: 18 additions & 0 deletions litho-rendercore-thread-utils/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# 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.

POM_NAME=RenderCoreThreadUtils
POM_DESCRIPTION=Stub RenderCore Thread Utils
POM_ARTIFACT_ID=litho-rendercore-thread-utils
POM_PACKAGING=aar
24 changes: 24 additions & 0 deletions litho-rendercore-thread-utils/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
-->

<!--
~ Copyright (c) Meta Platforms, Inc. and affiliates.
~
~ 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.
-->

<manifest
package="com.facebook.rendercore.thread.utils">
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* 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.
*/

package com.facebook.rendercore.thread.utils

/** Config class to enable or disable specific features. */
object RenderCoreThreadUtilsConfig {

/**
* Defaults to the presence of an
* <pre>IS_TESTING</pre>
*
* system property at startup but can be overridden at runtime.
*/
@JvmField var isEndToEndTestRun: Boolean = System.getProperty("IS_TESTING") != null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* 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.
*/

package com.facebook.rendercore.thread.utils

import android.os.Process
import com.facebook.rendercore.thread.utils.ThreadUtils.getResultInheritingPriority
import com.facebook.rendercore.thread.utils.instrumentation.FutureInstrumenter
import java.util.concurrent.Callable
import java.util.concurrent.FutureTask
import java.util.concurrent.RunnableFuture
import java.util.concurrent.atomic.AtomicInteger

/**
* A future that lets the thread running the computation inherit the priority of any thread waiting
* on it (if greater).
*
* @param <T> The type that is returned from the future
*/
open class ThreadInheritingPriorityFuture<T>(callable: Callable<T>, tag: String) {
private var futureTask: RunnableFuture<T>? =
FutureInstrumenter.instrument(FutureTask(callable), tag)
private var resolvedResult: T? = null
private val runningThreadId = AtomicInteger(-1)

fun runAndGet(): T {
val runnableFuture: RunnableFuture<T>?
val existingResult: T?
synchronized(this) {
runnableFuture = futureTask
existingResult = resolvedResult
}
if (existingResult != null) {
return existingResult
}
requireNotNull(runnableFuture)
if (runningThreadId.compareAndSet(-1, Process.myTid())) {
runnableFuture.run()
}
val newResult = getResultInheritingPriority(runnableFuture, runningThreadId.get())
synchronized(this) {
resolvedResult = newResult
futureTask = null
}
onResultReady(newResult)

return newResult
}

open fun onResultReady(result: T) {
// Override this method to do something when result is ready.
}

val isRunning: Boolean
get() = runningThreadId.get() != -1

val isDone: Boolean
get() {
val futureTask: RunnableFuture<T>?
synchronized(this) { futureTask = this.futureTask }
return futureTask == null || futureTask.isDone
}

fun cancel() {
val futureTask: RunnableFuture<T>?
synchronized(this) { futureTask = this.futureTask }
futureTask?.cancel(false)
}

val isCanceled: Boolean
get() {
val futureTask: RunnableFuture<T>?
synchronized(this) { futureTask = this.futureTask }
return futureTask != null && futureTask.isCancelled
}
}
Loading

0 comments on commit 65e6bb1

Please sign in to comment.