Skip to content

Commit 65e6bb1

Browse files
zielinskimzfacebook-github-bot
authored andcommitted
Add dirsync for rendercore-thread-utils
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
1 parent f6c3f1c commit 65e6bb1

File tree

10 files changed

+454
-0
lines changed

10 files changed

+454
-0
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

litho-rendercore-thread-utils/BUCK

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load("@fbsource//xplat/pfh/FBApp_UIFrameworks_Litho:DEFS.bzl", "FBApp_UIFrameworks_Litho")
2+
load(
3+
"//tools/build_defs/oss:litho_defs.bzl",
4+
"fb_android_library",
5+
)
6+
7+
oncall("litho_components_for_android")
8+
9+
fb_android_library(
10+
name = "rendercore-thread-utils",
11+
srcs = glob(
12+
["src/main/java/**/*.kt"],
13+
),
14+
autoglob = False,
15+
feature = FBApp_UIFrameworks_Litho,
16+
labels = [],
17+
required_for_source_only_abi = True,
18+
visibility = [
19+
"PUBLIC",
20+
],
21+
deps = [
22+
],
23+
)
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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+
apply plugin: 'com.android.library'
18+
apply plugin: 'kotlin-android'
19+
apply plugin: 'com.facebook.kotlin.compilerplugins.dataclassgenerate'
20+
21+
project.group = GROUP
22+
version = VERSION_NAME
23+
24+
android {
25+
compileSdkVersion rootProject.compileSdkVersion
26+
buildToolsVersion rootProject.buildToolsVersion
27+
28+
// We're on an old version of Robolectric which requires this, sadly.
29+
useLibrary 'org.apache.http.legacy'
30+
31+
defaultConfig {
32+
minSdkVersion rootProject.minSdkVersion
33+
}
34+
35+
buildTypes {
36+
37+
debug {
38+
}
39+
40+
release {
41+
}
42+
}
43+
namespace 'com.facebook.rendercore.thread.utils'
44+
lint {
45+
abortOnError false
46+
}
47+
48+
// TODO(#62): Re-enable abort on error.
49+
}
50+
51+
dependencies {
52+
compileOnly deps.inferAnnotations
53+
}
54+
55+
apply plugin: "com.vanniktech.maven.publish"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
POM_NAME=RenderCoreThreadUtils
16+
POM_DESCRIPTION=Stub RenderCore Thread Utils
17+
POM_ARTIFACT_ID=litho-rendercore-thread-utils
18+
POM_PACKAGING=aar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
4+
-->
5+
6+
<!--
7+
~ Copyright (c) Meta Platforms, Inc. and affiliates.
8+
~
9+
~ Licensed under the Apache License, Version 2.0 (the "License");
10+
~ you may not use this file except in compliance with the License.
11+
~ You may obtain a copy of the License at
12+
~
13+
~ http://www.apache.org/licenses/LICENSE-2.0
14+
~
15+
~ Unless required by applicable law or agreed to in writing, software
16+
~ distributed under the License is distributed on an "AS IS" BASIS,
17+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
~ See the License for the specific language governing permissions and
19+
~ limitations under the License.
20+
-->
21+
22+
<manifest
23+
package="com.facebook.rendercore.thread.utils">
24+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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+
package com.facebook.rendercore.thread.utils
18+
19+
/** Config class to enable or disable specific features. */
20+
object RenderCoreThreadUtilsConfig {
21+
22+
/**
23+
* Defaults to the presence of an
24+
* <pre>IS_TESTING</pre>
25+
*
26+
* system property at startup but can be overridden at runtime.
27+
*/
28+
@JvmField var isEndToEndTestRun: Boolean = System.getProperty("IS_TESTING") != null
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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+
package com.facebook.rendercore.thread.utils
18+
19+
import android.os.Process
20+
import com.facebook.rendercore.thread.utils.ThreadUtils.getResultInheritingPriority
21+
import com.facebook.rendercore.thread.utils.instrumentation.FutureInstrumenter
22+
import java.util.concurrent.Callable
23+
import java.util.concurrent.FutureTask
24+
import java.util.concurrent.RunnableFuture
25+
import java.util.concurrent.atomic.AtomicInteger
26+
27+
/**
28+
* A future that lets the thread running the computation inherit the priority of any thread waiting
29+
* on it (if greater).
30+
*
31+
* @param <T> The type that is returned from the future
32+
*/
33+
open class ThreadInheritingPriorityFuture<T>(callable: Callable<T>, tag: String) {
34+
private var futureTask: RunnableFuture<T>? =
35+
FutureInstrumenter.instrument(FutureTask(callable), tag)
36+
private var resolvedResult: T? = null
37+
private val runningThreadId = AtomicInteger(-1)
38+
39+
fun runAndGet(): T {
40+
val runnableFuture: RunnableFuture<T>?
41+
val existingResult: T?
42+
synchronized(this) {
43+
runnableFuture = futureTask
44+
existingResult = resolvedResult
45+
}
46+
if (existingResult != null) {
47+
return existingResult
48+
}
49+
requireNotNull(runnableFuture)
50+
if (runningThreadId.compareAndSet(-1, Process.myTid())) {
51+
runnableFuture.run()
52+
}
53+
val newResult = getResultInheritingPriority(runnableFuture, runningThreadId.get())
54+
synchronized(this) {
55+
resolvedResult = newResult
56+
futureTask = null
57+
}
58+
onResultReady(newResult)
59+
60+
return newResult
61+
}
62+
63+
open fun onResultReady(result: T) {
64+
// Override this method to do something when result is ready.
65+
}
66+
67+
val isRunning: Boolean
68+
get() = runningThreadId.get() != -1
69+
70+
val isDone: Boolean
71+
get() {
72+
val futureTask: RunnableFuture<T>?
73+
synchronized(this) { futureTask = this.futureTask }
74+
return futureTask == null || futureTask.isDone
75+
}
76+
77+
fun cancel() {
78+
val futureTask: RunnableFuture<T>?
79+
synchronized(this) { futureTask = this.futureTask }
80+
futureTask?.cancel(false)
81+
}
82+
83+
val isCanceled: Boolean
84+
get() {
85+
val futureTask: RunnableFuture<T>?
86+
synchronized(this) { futureTask = this.futureTask }
87+
return futureTask != null && futureTask.isCancelled
88+
}
89+
}

0 commit comments

Comments
 (0)