-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Improve CoroutineDispatcher documentation #3359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c052694
Improve CoroutineDispatcher documentation
qwwdfsad b7e2d4b
Update kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt
qwwdfsad 02c9097
Update kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt
qwwdfsad c92ce24
Update kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt
qwwdfsad 6180255
Update kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt
qwwdfsad 9fee066
Update kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt
qwwdfsad 8787e96
Update kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt
qwwdfsad 59f72fb
Update kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt
dkhalanskyjb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,9 @@ public abstract class CoroutineDispatcher : | |
| * potentially forming an event-loop to prevent stack overflows. | ||
| * The event loop is an advanced topic and its implications can be found in [Dispatchers.Unconfined] documentation. | ||
| * | ||
| * [context] parameter represents a context of the coroutine that is being dispatched | ||
| * or [EmptyCoroutineContext] if a non-coroutine-specific [Runnable] is dispatched instead. | ||
| * | ||
| * A dispatcher can override this method to provide a performance optimization and avoid paying a cost of an unnecessary dispatch. | ||
| * E.g. [MainCoroutineDispatcher.immediate] checks whether we are already in the required UI thread in this method and avoids | ||
| * an additional dispatch when it is not required. | ||
|
|
@@ -58,6 +61,9 @@ public abstract class CoroutineDispatcher : | |
| * | ||
| * This method should generally be exception-safe. An exception thrown from this method | ||
| * may leave the coroutines that use this dispatcher in the inconsistent and hard to debug state. | ||
| * | ||
| * @see dispatch | ||
| * @see Dispatchers.Unconfined | ||
| */ | ||
| public open fun isDispatchNeeded(context: CoroutineContext): Boolean = true | ||
|
|
||
|
|
@@ -102,18 +108,31 @@ public abstract class CoroutineDispatcher : | |
| } | ||
|
|
||
| /** | ||
| * Dispatches execution of a runnable [block] onto another thread in the given [context]. | ||
| * Requests execution of a runnable [block]. | ||
| * The dispatcher guarantees that [block] will eventually execute, typically by dispatching it to a thread pool, | ||
| * using a dedicated thread, or just executing the block in place. | ||
| * The [context] parameter represents the context of the coroutine that is being dispatched, | ||
| * or [EmptyCoroutineContext] if a non-coroutine-specific [Runnable] is dispatched instead. | ||
| * Implementations may use [context] for additional context-specific information, | ||
| * such as priority, whether the dispatched coroutine can be invoked in place, | ||
| * coroutine name, and additional diagnostic elements. | ||
| * | ||
| * This method should guarantee that the given [block] will be eventually invoked, | ||
| * otherwise the system may reach a deadlock state and never leave it. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's even more sinister than that: if we drop dispatched coroutines, |
||
| * Cancellation mechanism is transparent for [CoroutineDispatcher] and is managed by [block] internals. | ||
| * The cancellation mechanism is transparent for [CoroutineDispatcher] and is managed by [block] internals. | ||
| * | ||
| * This method should generally be exception-safe. An exception thrown from this method | ||
| * may leave the coroutines that use this dispatcher in the inconsistent and hard to debug state. | ||
| * may leave the coroutines that use this dispatcher in an inconsistent and hard-to-debug state. | ||
| * | ||
| * This method must not immediately call [block]. Doing so may result in `StackOverflowError` | ||
| * when `dispatch` is invoked repeatedly, for example when [yield] is called in a loop. | ||
| * In order to execute a block in place, it is required to return `false` from [isDispatchNeeded] | ||
| * and delegate the `dispatch` implementation to `Dispatchers.Unconfined.dispatch` in such cases. | ||
| * To support this, the coroutines machinery ensures in-place execution and forms an event-loop to | ||
| * avoid unbound recursion. | ||
| * | ||
| * This method must not immediately call [block]. Doing so would result in [StackOverflowError] | ||
| * when [yield] is repeatedly called from a loop. However, an implementation that returns `false` from | ||
| * [isDispatchNeeded] can delegate this function to `dispatch` method of [Dispatchers.Unconfined], which is | ||
| * integrated with [yield] to avoid this problem. | ||
| * @see isDispatchNeeded | ||
| * @see Dispatchers.Unconfined | ||
| */ | ||
| public abstract fun dispatch(context: CoroutineContext, block: Runnable) | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.