IJPL-210 add ChildContext.runAsCoroutine

GitOrigin-RevId: d22b6ac9b97c4ea2cc4965b195c748aee0acb0b5
This commit is contained in:
Daniil Ovchinnikov
2023-08-18 17:14:52 +02:00
committed by intellij-monorepo-bot
parent caa4ad0a0c
commit e3c5dd260a
6 changed files with 21 additions and 32 deletions

View File

@@ -461,12 +461,7 @@ public final class ProgressRunner<R> {
Runnable contextRunnable = context.equals(EmptyCoroutineContext.INSTANCE) ? runnable : (ContextAwareRunnable)() -> {
CoroutineContext effectiveContext = context.plus(asContextElement(progressIndicator.getModalityState()));
try (AccessToken ignored = ThreadContext.installThreadContext(effectiveContext, false)) {
if (job != null) {
Propagation.runAsCoroutine(job, runnable);
}
else {
runnable.run();
}
childContext.runAsCoroutine(runnable);
}
};
switch (myThreadToUse) {

View File

@@ -366,8 +366,7 @@ public class Alarm implements Disposable {
try (AccessToken ignored = ClientId.withClientId(myClientId)) {
if (myChildContext != null) {
try (AccessToken ignored2 = ThreadContext.installThreadContext(myChildContext.getContext(), true)) {
CompletableJob job = myChildContext.getJob();
QueueProcessor.runSafely(job == null ? task : () -> Propagation.runAsCoroutine(job, task));
QueueProcessor.runSafely(() -> myChildContext.runAsCoroutine(task));
}
}
else {

View File

@@ -110,13 +110,7 @@ public abstract class Update extends ComparableObject.Impl implements Runnable {
}
else {
try (AccessToken ignored = ThreadContext.installThreadContext(myChildContext.getContext(), true)) {
CompletableJob job = myChildContext.getJob();
if (job != null) {
Propagation.runAsCoroutine(job, this);
}
else {
run();
}
myChildContext.runAsCoroutine(this);
}
}
}

View File

@@ -60,7 +60,6 @@ import com.intellij.util.childScope
import com.intellij.util.concurrency.AppExecutorUtil
import com.intellij.util.concurrency.ChildContext
import com.intellij.util.concurrency.createChildContext
import com.intellij.util.concurrency.runAsCoroutine
import com.intellij.util.containers.ContainerUtil
import com.intellij.util.ui.StartupUiUtil.addAwtListener
import com.intellij.util.xml.dom.XmlElement
@@ -1311,14 +1310,8 @@ open class ActionManagerImpl protected constructor(private val coroutineScope: C
override fun run() {
installThreadContext(childContext.context).use {
val job = childContext.job
if (job == null) {
timerListener.run()
}
else {
// this is periodic runnable that is invoked on timer; it should not complete a parent job
runAsCoroutine(job = job, completeOnFinish = false, action = timerListener::run)
}
// this is periodic runnable that is invoked on timer; it should not complete a parent job
childContext.runAsCoroutine(completeOnFinish = false, timerListener::run)
}
}
}

View File

@@ -326,13 +326,7 @@ public class MergingTaskQueue<T extends MergeableQueueTask<T>> {
beforeTask();
if (AppExecutorUtil.propagateContextOrCancellation() && myChildContext != null) {
try (AccessToken ignored = ThreadContext.installThreadContext(myChildContext.getContext(), true)) {
CompletableJob job = myChildContext.getJob();
if (job != null) {
Propagation.runAsCoroutine(job, () -> myTask.perform(indicator));
}
else {
myTask.perform(indicator);
}
myChildContext.runAsCoroutine(() -> myTask.perform(indicator));
}
}
else {

View File

@@ -75,7 +75,21 @@ class BlockingJob(val blockingJob: Job) : AbstractCoroutineContextElement(Blocki
data class ChildContext internal constructor(
val context: CoroutineContext,
val job: CompletableJob?,
)
) {
fun runAsCoroutine(action: Runnable) {
runAsCoroutine(completeOnFinish = true, action::run)
}
fun <T> runAsCoroutine(completeOnFinish: Boolean, action: () -> T): T {
return if (job == null) {
action()
}
else {
runAsCoroutine(job, completeOnFinish, action)
}
}
}
@Internal
fun createChildContext(): ChildContext {