|
| 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