@@ -15,8 +15,12 @@ import kotlin.coroutines.experimental.*
1515
1616/* *
1717 * A background job. Conceptually, a job is a cancellable thing with a life-cycle that
18- * culminates in its completion. Jobs can be arranged into parent-child hierarchies where cancellation
19- * of parent lead to an immediate cancellation of all its [children] and vice versa.
18+ * culminates in its completion.
19+ *
20+ * Jobs can be arranged into parent-child hierarchies where cancellation
21+ * of parent lead to an immediate cancellation of all its [children]. Failure or cancellation of a child
22+ * with an exception other than [CancellationException] immediately cancels its parent. This way, parent
23+ * can [cancel] its own children (including all their children recursively) without cancelling itself.
2024 *
2125 * The most basic instances of [Job] are created with [launch][CoroutineScope.launch] coroutine builder or with a
2226 * `Job()` factory function.
@@ -38,7 +42,7 @@ import kotlin.coroutines.experimental.*
3842 * that provide an optional `start` parameter create a coroutine in _new_ state when this parameter is set to
3943 * [CoroutineStart.LAZY]. Such a job can be made _active_ by invoking [start] or [join].
4044 *
41- * A job is _active_ while the coroutine is working. Failure of the job makes it _cancelling_.
45+ * A job is _active_ while the coroutine is working. Failure of the job with exception makes it _cancelling_.
4246 * A job can be cancelled it at any time with [cancel] function that forces it to transition to
4347 * _cancelling_ state immediately. The job becomes _cancelled_ when it finishes executing it work.
4448 *
@@ -47,7 +51,7 @@ import kotlin.coroutines.experimental.*
4751 * +-----+ start +--------+ complete +-------------+ finish +-----------+
4852 * | New | -----> | Active | ---------> | Completing | -------> | Completed |
4953 * +-----+ +--------+ +-------------+ +-----------+
50- * | cancel |
54+ * | cancel / fail |
5155 * | +----------------+
5256 * | |
5357 * V V
@@ -65,6 +69,12 @@ import kotlin.coroutines.experimental.*
6569 * Note, that _completing_ state is purely internal to the job. For an outside observer a _completing_ job is still
6670 * active, while internally it is waiting for its children.
6771 *
72+ * Normal cancellation of a job is distinguished from its failure by the type of its cancellation exception cause.
73+ * If the cause of cancellation is [CancellationException], then the job is considered to be _cancelled normally_.
74+ * This usually happens when [cancel] is invoked without additional parameters. If the cause of cancellation is
75+ * a different exception, then the job is considered to have _failed_. This usually happens when the code of the job
76+ * encounters some problem and throws an exception.
77+ *
6878 * All functions on this interface and on all interfaces derived from it are **thread-safe** and can
6979 * be safely invoked from concurrent coroutines without external synchronization.
7080 */
@@ -94,25 +104,31 @@ public interface Job : CoroutineContext.Element {
94104 // ------------ state query ------------
95105
96106 /* *
97- * Returns `true` when this job is active -- it was already started and has not completed or failed yet.
107+ * Returns `true` when this job is active -- it was already started and has not completed nor was cancelled yet.
98108 * The job that is waiting for its [children] to complete is still considered to be active if it
99- * has not failed and was not cancelled.
109+ * was not cancelled nor failed.
110+ *
111+ * See [Job] documentation for more details on job states.
100112 */
101113 public val isActive: Boolean
102114
103115 /* *
104- * Returns `true` when this job has completed for any reason. A job that has failed or cancelled
116+ * Returns `true` when this job has completed for any reason. A job that was cancelled or failed
105117 * and has finished its execution is also considered complete. Job becomes complete only after
106118 * all its [children] complete.
119+ *
120+ * See [Job] documentation for more details on job states.
107121 */
108122 public val isCompleted: Boolean
109123
110124 /* *
111125 * Returns `true` if this job was cancelled for any reason, either by explicit invocation of [cancel] or
112- * because it had failed or its children or parent was cancelled.
126+ * because it had failed or its child or parent was cancelled.
113127 * In the general case, it does not imply that the
114128 * job has already [completed][isCompleted], because it may still be finishing whatever it was doing and
115129 * waiting for its [children] to complete.
130+ *
131+ * See [Job] documentation for more details on cancellation and failures.
116132 */
117133 public val isCancelled: Boolean
118134
@@ -203,7 +219,7 @@ public interface Job : CoroutineContext.Element {
203219 *
204220 * A parent-child relation has the following effect:
205221 * * Cancellation of parent with [cancel] or its exceptional completion (failure)
206- * immediately fails all its children.
222+ * immediately cancels all its children.
207223 * * Parent cannot complete until all its children are complete. Parent waits for all its children to
208224 * complete in _completing_ or _cancelling_ states.
209225 *
@@ -300,20 +316,20 @@ public interface Job : CoroutineContext.Element {
300316 public fun invokeOnCompletion (handler : CompletionHandler ): DisposableHandle
301317
302318 /* *
303- * Registers handler that is **synchronously** invoked once on failure or completion of this job.
304- * When job is already failing or complete , then the handler is immediately invoked
319+ * Registers handler that is **synchronously** invoked once on cancellation or completion of this job.
320+ * When job was already cancelled and is completed its execution , then the handler is immediately invoked
305321 * with a job's cancellation cause or `null` unless [invokeImmediately] is set to false.
306- * Otherwise, handler will be invoked once when this job is failing or is complete.
322+ * Otherwise, handler will be invoked once when this job is cancelled or is complete.
307323 *
308324 * The meaning of `cause` that is passed to the handler:
309325 * * Cause is `null` when job has completed normally.
310326 * * Cause is an instance of [CancellationException] when job was cancelled _normally_.
311327 * **It should not be treated as an error**. In particular, it should not be reported to error logs.
312328 * * Otherwise, the job had _failed_.
313329 *
314- * Invocation of this handler on a transition to a _failing_ state
330+ * Invocation of this handler on a transition to a _cancelling_ state
315331 * is controlled by [onCancelling] boolean parameter.
316- * The handler is invoked when the job is failing when [onCancelling] parameter is set to `true`.
332+ * The handler is invoked when the job becomes _cancelling_ if [onCancelling] parameter is set to `true`.
317333 *
318334 * The resulting [DisposableHandle] can be used to [dispose][DisposableHandle.dispose] the
319335 * registration of this handler and release its memory if its invocation is no longer needed.
@@ -328,7 +344,7 @@ public interface Job : CoroutineContext.Element {
328344 * This function should not be used in general application code.
329345 * Implementations of `CompletionHandler` must be fast and _lock-free_.
330346 *
331- * @param onCancelling when `true`, then the [handler] is invoked as soon as this job transitions to _failing_ state;
347+ * @param onCancelling when `true`, then the [handler] is invoked as soon as this job transitions to _cancelling_ state;
332348 * when `false` then the [handler] is invoked only when it transitions to _completed_ state.
333349 * @param invokeImmediately when `true` and this job is already in the desired state (depending on [onCancelling]),
334350 * then the [handler] is immediately and synchronously invoked and no-op [DisposableHandle] is returned;
@@ -391,32 +407,32 @@ public inline fun DisposableHandle(crossinline block: () -> Unit) =
391407// -------------------- Parent-child communication --------------------
392408
393409/* *
394- * A reference that parent receives from its child so that it can report its failure .
410+ * A reference that parent receives from its child so that it can report its cancellation .
395411 *
396412 * @suppress **This is unstable API and it is subject to change.**
397413 */
398414internal interface ChildJob : Job {
399415 /* *
400- * Parent is reporting failure to the child by invoking this method.
401- * Child finds the failure cause using [getCancellationException] of the [parentJob].
402- * This method does nothing is the child is already failing .
416+ * Parent is cancelling its child by invoking this method.
417+ * Child finds the cancellation cause using [getCancellationException] of the [parentJob].
418+ * This method does nothing is the child is already being cancelled .
403419 *
404420 * @suppress **This is unstable API and it is subject to change.**
405421 */
406422 public fun parentCancelled (parentJob : Job )
407423}
408424
409425/* *
410- * A handle that child keep onto its parent so that it is able to report its failure .
426+ * A handle that child keep onto its parent so that it is able to report its cancellation .
411427 *
412428 * @suppress **This is unstable API and it is subject to change.**
413429 */
414430internal interface ChildHandle : DisposableHandle {
415431 /* *
416- * Child is reporting failure to the parent by invoking this method.
432+ * Child is cancelling its parent by invoking this method.
417433 * This method is invoked by the child twice. The first time child report its root cause as soon as possible,
418- * so that all its siblings and the parent can start finishing their work asap on failure . The second time
419- * child invokes this method when it had aggregated and determined its final termination cause.
434+ * so that all its siblings and the parent can start cancelling their work asap. The second time
435+ * child invokes this method when it had aggregated and determined its final cancellation cause.
420436 *
421437 * @suppress **This is unstable API and it is subject to change.**
422438 */
0 commit comments