Skip to content

Commit 00dfe57

Browse files
committed
fix documentation
1 parent 5ea4571 commit 00dfe57

File tree

1 file changed

+21
-17
lines changed
  • kotlinx-coroutines-core/common/src/selects

1 file changed

+21
-17
lines changed

kotlinx-coroutines-core/common/src/selects/Select.kt

+21-17
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,15 @@ internal open class SelectImplementation<R> constructor(
240240
* == Phase 1: REGISTRATION ==
241241
* In the first REGISTRATION phase, the user-specified [SelectBuilder] is applied, and all the listed clauses
242242
* are registered via the provided [registration functions][SelectClause.regFunc]. Intuitively, `select` clause
243-
* registration is similar to the plain blocking operation, with the only difference that the corresponding
244-
* [SelectInstance] is stored instead of continuation, and [SelectInstance.trySelect] is used to make a rendezvous.
243+
* registration is similar to the plain blocking operation, with the only difference that this [SelectInstance]
244+
* is stored as a waiter instead of continuation, and [SelectInstance.trySelect] is used to make a rendezvous.
245245
* Also, when registering, it is possible for the operation to complete immediately, without waiting. In this case,
246-
* [SelectInstance.selectInRegistrationPhase] should be used. Otherwise, when this `select` instance is stored
247-
* as a waiter, a completion handler should be specified via [SelectInstance.disposeOnCompletion].
246+
* [SelectInstance.selectInRegistrationPhase] should be used. Otherwise, when no rendezvous happens and this `select`
247+
* instance is stored as a waiter, a completion handler for the registering clause should be specified via
248+
* [SelectInstance.disposeOnCompletion]; this handler specifies how to remove this `select` instance from the
249+
* clause object when another clause becomes selected or the operation cancels.
248250
*
249-
* After one clause registration is completed, another coroutine can attempt to make a rendezvous with this `select`.
251+
* After a clause registration is completed, another coroutine can attempt to make a rendezvous with this `select`.
250252
* However, to resolve a race between clauses registration and [SelectInstance.trySelect], the latter fails when
251253
* this `select` is still in REGISTRATION phase. Thus, the corresponding clause has to be registered again.
252254
*
@@ -255,24 +257,26 @@ internal open class SelectImplementation<R> constructor(
255257
*
256258
* == Phase 2: WAITING ==
257259
* If no rendezvous happens in REGISTRATION phase, the `select` operation moves to WAITING one and suspends until
258-
* [SelectInstance.trySelect] is called. Also, when waiting, this `select` operation can be cancelled. In the latter
259-
* case, further [SelectInstance.trySelect] calls fail, and all the completion handlers, specified via
260-
* [SelectInstance.disposeOnCompletion] during clauses registration, are invoked to remove this `select` instance
261-
* as a waiter from the corresponding clause objects.
260+
* [SelectInstance.trySelect] is called. Also, when waiting, this `select` can be cancelled. In the latter case,
261+
* further [SelectInstance.trySelect] attempts fail, and all the completion handlers, specified via
262+
* [SelectInstance.disposeOnCompletion], are invoked to remove this `select` instance from the corresponding
263+
* clause objects.
262264
*
263265
* In this phase, the `state` field stores either the continuation to be later resumed or a special `Cancelled`
264-
* object when this `select` becomes cancelled.
266+
* object (with the cancellation cause inside) when this `select` becomes cancelled.
265267
*
266268
* == Phase 3: COMPLETION ==
267269
* Once a rendezvous happens either in REGISTRATION phase (via [SelectInstance.selectInRegistrationPhase]) or
268-
* in WAITING phase (via [SelectInstance.trySelect]), this `select` moves to `COMPLETION` phase. First,
269-
* the provided internal result is processed via the clause-specified [ProcessResultFunction], which returns
270-
* the argument for the user-specified block. After that, this `select` should be removed from all other
271-
* clause objects by calling the [DisposableHandle]-s provided via [SelectInstance.disposeOnCompletion]
272-
* during registration. At the end, the user-specified block is called.
270+
* in WAITING phase (via [SelectInstance.trySelect]), this `select` moves to the final `COMPLETION` phase.
271+
* First, the provided internal result is processed via the [ProcessResultFunction] of the selected clause;
272+
* it returns the argument for the user-specified block or throws an exception (see [SendChannel.onSend] as
273+
* an example). After that, this `select` should be removed from all other clause objects by calling the
274+
* corresponding [DisposableHandle]-s, provided via [SelectInstance.disposeOnCompletion] during registration.
275+
* At the end, the user-specified block is called and this `select` finishes.
276+
*
277+
* In this phase, once a rendezvous is happened, the `state` field stores the corresponding clause.
278+
* After that, it moves to [STATE_COMPLETED] to avoid memory leaks.
273279
*
274-
* In this phase, one a rendezvous is happened, the `state` field stores the corresponding clause. After that,
275-
* to avoid memory leaks, the state moves to [STATE_COMPLETED].
276280
*
277281
*
278282
* The state machine is listed below:

0 commit comments

Comments
 (0)