@@ -15,8 +15,12 @@ import kotlin.coroutines.experimental.*
15
15
16
16
/* *
17
17
* 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.
20
24
*
21
25
* The most basic instances of [Job] are created with [launch][CoroutineScope.launch] coroutine builder or with a
22
26
* `Job()` factory function.
@@ -38,7 +42,7 @@ import kotlin.coroutines.experimental.*
38
42
* that provide an optional `start` parameter create a coroutine in _new_ state when this parameter is set to
39
43
* [CoroutineStart.LAZY]. Such a job can be made _active_ by invoking [start] or [join].
40
44
*
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_.
42
46
* A job can be cancelled it at any time with [cancel] function that forces it to transition to
43
47
* _cancelling_ state immediately. The job becomes _cancelled_ when it finishes executing it work.
44
48
*
@@ -47,7 +51,7 @@ import kotlin.coroutines.experimental.*
47
51
* +-----+ start +--------+ complete +-------------+ finish +-----------+
48
52
* | New | -----> | Active | ---------> | Completing | -------> | Completed |
49
53
* +-----+ +--------+ +-------------+ +-----------+
50
- * | cancel |
54
+ * | cancel / fail |
51
55
* | +----------------+
52
56
* | |
53
57
* V V
@@ -65,6 +69,12 @@ import kotlin.coroutines.experimental.*
65
69
* Note, that _completing_ state is purely internal to the job. For an outside observer a _completing_ job is still
66
70
* active, while internally it is waiting for its children.
67
71
*
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
+ *
68
78
* All functions on this interface and on all interfaces derived from it are **thread-safe** and can
69
79
* be safely invoked from concurrent coroutines without external synchronization.
70
80
*/
@@ -94,25 +104,31 @@ public interface Job : CoroutineContext.Element {
94
104
// ------------ state query ------------
95
105
96
106
/* *
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.
98
108
* 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.
100
112
*/
101
113
public val isActive: Boolean
102
114
103
115
/* *
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
105
117
* and has finished its execution is also considered complete. Job becomes complete only after
106
118
* all its [children] complete.
119
+ *
120
+ * See [Job] documentation for more details on job states.
107
121
*/
108
122
public val isCompleted: Boolean
109
123
110
124
/* *
111
125
* 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.
113
127
* In the general case, it does not imply that the
114
128
* job has already [completed][isCompleted], because it may still be finishing whatever it was doing and
115
129
* waiting for its [children] to complete.
130
+ *
131
+ * See [Job] documentation for more details on cancellation and failures.
116
132
*/
117
133
public val isCancelled: Boolean
118
134
@@ -203,7 +219,7 @@ public interface Job : CoroutineContext.Element {
203
219
*
204
220
* A parent-child relation has the following effect:
205
221
* * Cancellation of parent with [cancel] or its exceptional completion (failure)
206
- * immediately fails all its children.
222
+ * immediately cancels all its children.
207
223
* * Parent cannot complete until all its children are complete. Parent waits for all its children to
208
224
* complete in _completing_ or _cancelling_ states.
209
225
*
@@ -300,20 +316,20 @@ public interface Job : CoroutineContext.Element {
300
316
public fun invokeOnCompletion (handler : CompletionHandler ): DisposableHandle
301
317
302
318
/* *
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
305
321
* 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.
307
323
*
308
324
* The meaning of `cause` that is passed to the handler:
309
325
* * Cause is `null` when job has completed normally.
310
326
* * Cause is an instance of [CancellationException] when job was cancelled _normally_.
311
327
* **It should not be treated as an error**. In particular, it should not be reported to error logs.
312
328
* * Otherwise, the job had _failed_.
313
329
*
314
- * Invocation of this handler on a transition to a _failing_ state
330
+ * Invocation of this handler on a transition to a _cancelling_ state
315
331
* 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`.
317
333
*
318
334
* The resulting [DisposableHandle] can be used to [dispose][DisposableHandle.dispose] the
319
335
* registration of this handler and release its memory if its invocation is no longer needed.
@@ -328,7 +344,7 @@ public interface Job : CoroutineContext.Element {
328
344
* This function should not be used in general application code.
329
345
* Implementations of `CompletionHandler` must be fast and _lock-free_.
330
346
*
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;
332
348
* when `false` then the [handler] is invoked only when it transitions to _completed_ state.
333
349
* @param invokeImmediately when `true` and this job is already in the desired state (depending on [onCancelling]),
334
350
* 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) =
391
407
// -------------------- Parent-child communication --------------------
392
408
393
409
/* *
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 .
395
411
*
396
412
* @suppress **This is unstable API and it is subject to change.**
397
413
*/
398
414
internal interface ChildJob : Job {
399
415
/* *
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 .
403
419
*
404
420
* @suppress **This is unstable API and it is subject to change.**
405
421
*/
406
422
public fun parentCancelled (parentJob : Job )
407
423
}
408
424
409
425
/* *
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 .
411
427
*
412
428
* @suppress **This is unstable API and it is subject to change.**
413
429
*/
414
430
internal interface ChildHandle : DisposableHandle {
415
431
/* *
416
- * Child is reporting failure to the parent by invoking this method.
432
+ * Child is cancelling its parent by invoking this method.
417
433
* 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.
420
436
*
421
437
* @suppress **This is unstable API and it is subject to change.**
422
438
*/
0 commit comments