From f548a2bc2cb9a174b63658feb01efc8f277a52bc Mon Sep 17 00:00:00 2001 From: Karol Lewandowski Date: Tue, 18 Feb 2025 13:02:37 +0100 Subject: [PATCH] coroutine_execution_contexts.md: Cleaner APIs presentation --- .../coroutine_execution_contexts.md | 203 +++++++++++------- 1 file changed, 125 insertions(+), 78 deletions(-) diff --git a/topics/basics/architectural_overview/coroutines/coroutine_execution_contexts.md b/topics/basics/architectural_overview/coroutines/coroutine_execution_contexts.md index 28f595b25..10ed06820 100644 --- a/topics/basics/architectural_overview/coroutines/coroutine_execution_contexts.md +++ b/topics/basics/architectural_overview/coroutines/coroutine_execution_contexts.md @@ -1,28 +1,24 @@ # Execution Contexts - Tracking execution progress, checking for cancellations, and switching between different execution contexts. - - The IntelliJ Platform provides APIs that allow tracking the progress of background processes and canceling their execution when they are canceled by a user, or they become obsolete due to some changes in the data model. Background processes can be executed in three contexts: -- [](#suspending-context) +- [](#suspending-context-coroutines) — available since 2024.1 - [](#blocking-context) -- [](#progress-indicator) (obsolete since 2024.1) +- [](#progress-indicator) — obsolete since 2024.1 Currently, the Progress Indicator context is the most widely used approach in the IntelliJ Platform. -As the platform's execution model moves towards coroutines, this approach can be considered obsolete. -Starting with 2024.1, it is recommended to execute new code in the [suspending context](#suspending-context). - -Once the client code switches to a suspending or a blocking context, it should not switch back to a progress indicator context. +As the platform's execution model moves towards [coroutines](launching_coroutines.md), this approach can be considered obsolete. +Starting with 2024.1, it is recommended to execute new code in the [suspending context](#suspending-context-coroutines). The following sections explain the contexts and provide information about process cancellation, progress tracking, and switching between different contexts. -## Suspending Context +## Suspending Context (Coroutines) + Code [executed in Kotlin coroutines](launching_coroutines.md) is executed in a suspending context. Since 2024.1, this context is recommended for executing background tasks to maximize CPU utilization. @@ -32,55 +28,17 @@ Since 2024.1, this context is recommended for executing background tasks to maxi {style="warning"} In a suspending context, methods such as `ProgressManager.checkCanceled()` or `ModalityState.defaultModalityState()` won't have any effect. -Therefore, if their behavior is required, [switch to a blocking context](#suspending-context-switching-to-other-contexts). +Therefore, if their behavior is required, [switch to a blocking context](#switching-between-contexts). > Inspection Plugin DevKit | Code | Forbidden in suspend context method usage reports calling blocking code from suspending context. -### Cancellation Check -{#suspending-context-cancellation-check} - -- [`ensureActive()`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/ensure-active.html) from Kotlin coroutine's API - -> Note that [`ProgressManager.checkCanceled()`](%gh-ic%/platform/core-api/src/com/intellij/openapi/progress/ProgressManager.java) does not work in a coroutine context. -> -{style="warning"} - -### Progress Reporting -{#suspending-context-progress-reporting} - -- [`ProgressStep`](%gh-ic%/platform/util/progress/src/impl/ProgressStep.kt) - a step-based progress reporting (see its KDoc for details) -- [`RawProgressReporter`](%gh-ic%/platform/util/progress/src/RawProgressReporter.kt) - a raw text, details, and fraction reporting (invoked via [`reportRawProgress()`](%gh-ic%/platform/util/progress/src/steps.kt)) - -Any [`report*Progress()`](%gh-ic%/platform/util/progress/src/steps.kt) function must be used inside [`withBackgroundProgress()`, `withModalProgress()`, or `runWithModalProgressBlocking()`](%gh-ic%/platform/progress/shared/src/tasks.kt). -Otherwise, if there is no reporter in the context, using `report*Progress()` will have no effect. - -Example: - -```kotlin -withBackgroundProgress(...) { // or withModalProgress/runWithModalProgressBlocking - // ... - reportProgress { reporter -> // or another report*Progress - // do tasks and report progress - } - // ... -} -``` - -### Switching to Other Contexts -{#suspending-context-switching-to-other-contexts} - -- to [blocking context](#blocking-context): [`blockingContext()`](%gh-ic%/platform/core-api/src/com/intellij/openapi/progress/coroutines.kt) - enables `ProgressManager.checkCanceled()`, forwards modality state, etc. This function has an opposite behavior to [`runBlockingCancellable()`](#blocking-context-switching-to-other-contexts). -- to [progress indicator](#progress-indicator): unavailable* - - *_[`coroutineToIndicator()`](%gh-ic%/platform/core-api/src/com/intellij/openapi/progress/coroutines.kt) is an internal API and exists only to aid platform migration_ - ## Blocking Context -Executing tasks in a blocking context means executing them on a thread without access to the [coroutine context](#suspending-context) (basically, in non-suspending functions) and not under [a progress indicator](#progress-indicator). +Executing tasks in a blocking context means executing them on a thread without access to the [coroutine context](#suspending-context-coroutines) (basically, in non-suspending functions) and not under [a progress indicator](#progress-indicator). Such tasks can't be canceled by using coroutines' or progress indicator's cancellation capabilities, which may block threads and consume resources even if the task is no longer relevant. Usually, plugins should not execute new code in the blocking context. -Always prefer executing tasks in the [suspending context](#suspending-context) or under the [progress indicator](#progress-indicator) if a plugin cannot use Kotlin. +Always prefer executing tasks in the [suspending context](#suspending-context-coroutines) or under the [progress indicator](#progress-indicator) if a plugin cannot use Kotlin. > Functions which schedule execution via [`Application.executeOnPooledThread()`](%gh-ic%/platform/core-api/src/com/intellij/openapi/application/Application.java) > and similar methods, and which rely on [`ProgressManager.checkCanceled()`](%gh-ic%/platform/core-api/src/com/intellij/openapi/progress/ProgressManager.java) @@ -89,24 +47,6 @@ Always prefer executing tasks in the [suspending context](#suspending-context) o > > Inspection Plugin DevKit | Code | Calling method should be annotated with @RequiresBlockingContext reports missing annotations. -### Cancellation Check -{#blocking-context-cancellation-check} - -- [`ProgressManager.checkCanceled()`](%gh-ic%/platform/core-api/src/com/intellij/openapi/progress/ProgressManager.java) - -### Progress Reporting -{#blocking-context-progress-reporting} - -- unavailable - -### Switching to Other Contexts -{#blocking-context-switching-to-other-contexts} - -- to [suspending context](#suspending-context): [`runBlockingCancellable()`](%gh-ic%/platform/core-api/src/com/intellij/openapi/progress/coroutines.kt). This function has an opposite behavior to [`blockingContext()`](#suspending-context-switching-to-other-contexts). -- to [progress indicator](#progress-indicator): unavailable*
- - *_[`blockingContextToIndicator()`](%gh-ic%/platform/core-api/src/com/intellij/openapi/progress/coroutines.kt) is an internal API used only to aid platform migration_ - ## Progress Indicator @@ -124,18 +64,125 @@ See the [](background_processes.md#progress-api) section for details. > {style="tip" title="Obsolete approach since 2024.1"} -### Cancellation Check -{#progress-indicator-cancellation-check} +## Execution Contexts APIs -- `ProgressManager.checkCanceled()` - as described in the [Background Processes: Cancellation](background_processes.md#cancellation) section +### Cancellation Check + +The following table presents APIs to use for checking whether a task was canceled in different execution contexts. + + + + + + + + + + + + + + +
Suspending + ensureActive() from Kotlin coroutine's API + + Note that ProgressManager.checkCanceled() does not work in a suspending context. + +
Blocking + ProgressManager.checkCanceled() +

See Background Processes: Cancellation for details.

+
Progress Indicator + ProgressIndicator.checkCanceled(), ProgressManager.checkCanceled() +

See Background Processes: Cancellation for details.

+
### Progress Reporting -{#progress-indicator-progress-reporting} -- [`ProgressIndicator`](%gh-ic%/platform/core-api/src/com/intellij/openapi/progress/ProgressIndicator.java) or [`ProgressManager`'s](%gh-ic%/platform/core-api/src/com/intellij/openapi/progress/ProgressManager.java) methods as described in [Background Processes: Tracking Progress](background_processes.md#tracking-progress) +The following table presents the possibilities and APIs to use for reporting progress in different execution contexts. -### Switching to Other Contexts -{#progress-indicator-switching-to-other-contexts} + + + + + + + + + + + + + +
Suspending + +

+ Any report*Progress() function must be used inside withBackgroundProgress(), withModalProgress(), or runWithModalProgressBlocking() from tasks.kt. + Otherwise, if there is no reporter in the context, using `report*Progress()` will have no effect. + Example: +

+ + withBackgroundProgress(...) { // or other + // ... + reportProgress { reporter -> // or another report*Progress + // do tasks and report progress + } + // ... + } + +
Blocking + unavailable +
Progress Indicator + ProgressIndicator's or ProgressManager's methods +

See Background Processes: Tracking Progress for details.

+
-- to [suspending context](#suspending-context): [`runBlockingCancellable`](%gh-ic%/platform/core-api/src/com/intellij/openapi/progress/coroutines.kt) -- to [blocking context](#blocking-context): unavailable +### Switching Between Contexts + +The following table presents the possibilities and APIs to use for switching between different execution contexts. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
To SuspendingTo BlockingTo Progress Indicator
From Suspending-blockingContext() 1unavailable 3
From BlockingrunBlockingCancellable() 2-unavailable 4
From Progress IndicatorrunBlockingCancellable() 2unavailable-
+

1 blockingContext() enables ProgressManager.checkCanceled(), forwards modality state, etc.

+

2 runBlockingCancellable() has an opposite behavior to blockingContext()

+

3 coroutineToIndicator() is an internal API to aid platform migration

+

4 blockingContextToIndicator() is an internal API to aid platform migration

+
+ +It is only possible to: +- switch from the blocking context or progress indicator to the suspending context +- switch from the suspending context to the blocking context + +The lack of an API for switching from suspending and blocking contexts to progress indicator is intentional. +Cancellable and trackable tasks should be run in coroutines as the progress indicator is obsolete since 2024.1.