@@ -59,11 +59,11 @@ import kotlin.native.concurrent.*
59
59
* and used by default. In this mode, [resume] fails if it finds the cell in the `CANCELLED` state or if the waiter resumption
60
60
* (see [CancellableContinuation.tryResume]) does not succeed. As we discussed, these failures are typically handled
61
61
* by re-starting the whole operation from the beginning. With the other two smart cancellation modes,
62
- * [SMART_SYNC ] and [SMART_ASYNC ], [resume] skips `CANCELLED` cells (the cells where waiter resumption failed are also
62
+ * [SMART ] and [SMART ], [resume] skips `CANCELLED` cells (the cells where waiter resumption failed are also
63
63
* considered as `CANCELLED`). This way, even if a million of canceled continuations are stored in [SegmentQueueSynchronizer],
64
64
* one [resume] invocation is sufficient to pass the value to a waiter since it skips all these canceled waiters.
65
65
* However, these modes provide less intuitive contracts and require users to write more complicated code;
66
- * the details are described further.
66
+ * the desuspendSegments are described further.
67
67
*
68
68
* The main issue with skipping `CANCELLED` cells in [resume] is that it can become illegal to put the value into
69
69
* the next cell. Consider the following execution: [suspend] is called, then [resume] starts, but the suspended
@@ -77,11 +77,11 @@ import kotlin.native.concurrent.*
77
77
* [tryReturnRefusedValue] to return it back to the outer data structure. However, it is possible for
78
78
* [tryReturnRefusedValue] to fail, and [returnValue] is called in this case. Typically, this [returnValue] function
79
79
* coincides with the one that resumes waiters (e.g., with [release][Semaphore.release] in [Semaphore]).
80
- * The difference between [SMART_SYNC ] and [SMART_ASYNC ] modes is that in the [SMART_SYNC ] mode, a [resume] that comes
80
+ * The difference between [SMART ] and [SMART ] modes is that in the [SMART ] mode, a [resume] that comes
81
81
* to a cell with a canceled waiter waits in a spin-loop until the cancellation handler is invoked and the cell
82
- * is moved to either `CANCELLED` or `REFUSE` state. In contrast, in the [SMART_ASYNC ] mode, [resume] replaces
82
+ * is moved to either `CANCELLED` or `REFUSE` state. In contrast, in the [SMART ] mode, [resume] replaces
83
83
* the canceled waiter with the value of this resumption and finishes immediately -- the cancellation handler
84
- * completes this [resume] eventually. This way, in [SMART_ASYNC ] mode, the value passed to [resume] can be out
84
+ * completes this [resume] eventually. This way, in [SMART ] mode, the value passed to [resume] can be out
85
85
* of the data structure for a while but is guaranteed to be processed eventually.
86
86
*
87
87
* To support prompt cancellation, [SegmentQueueSynchronizer] returns the value back to the data structure by calling
@@ -135,7 +135,7 @@ import kotlin.native.concurrent.*
135
135
* between the counters for [resume] and [suspend], and does not need to store an infinite array of cells.
136
136
*
137
137
* To make the implementation efficient we maintain a linked list of [segments][SQSSegment], see the basic [Segment]
138
- * class and the corresponding source file for details . In short, each segment has a unique id, and can be seen as
138
+ * class and the corresponding source file for desuspendSegments . In short, each segment has a unique id, and can be seen as
139
139
* a node in a Michael-Scott queue. Following this structure, we can maintain the cells that are in the current active
140
140
* range (between the counters), and access the cells similarly to an array. Specifically, we change the current working
141
141
* segment once every [SEGMENT_SIZE] operations, where [SEGMENT_SIZE] is the number of cells stored in each segment.
@@ -165,8 +165,8 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
165
165
166
166
/* *
167
167
* Specifies whether [resume] should fail on cancelled waiters ([SIMPLE]),
168
- * or skip them in either [synchronous][SMART_SYNC ] or [asynchronous][SMART_ASYNC ]
169
- * way. In the [asynchronous][SMART_ASYNC ] mode, [resume] may pass the element to the
168
+ * or skip them in either [synchronous][SMART ] or [asynchronous][SMART ]
169
+ * way. In the [asynchronous][SMART ] mode, [resume] may pass the element to the
170
170
* cancellation handler in order not to wait, so that the element can be "hung"
171
171
* for a while, but it is guaranteed that the element will be processed eventually.
172
172
*/
@@ -219,19 +219,19 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
219
219
220
220
@Suppress(" UNCHECKED_CAST" )
221
221
internal fun suspend (cont : Continuation <T >): Boolean {
222
- // Increment `enqIdx ` and find the segment
222
+ // Increment `suspendIdx ` and find the segment
223
223
// with the corresponding id. It is guaranteed
224
224
// that this segment is not removed since at
225
225
// least the cell for this [suspend] invocation
226
226
// is not in the `CANCELLED` state.
227
- val curTail = this .suspendSegment.value
228
- val enqIdx = suspendIdx.getAndIncrement()
229
- val segment = this .suspendSegment.findSegmentAndMoveForward(id = enqIdx / SEGMENT_SIZE , startFrom = curTail ,
227
+ val curSuspendSegm = this .suspendSegment.value
228
+ val suspendIdx = suspendIdx.getAndIncrement()
229
+ val segment = this .suspendSegment.findSegmentAndMoveForward(id = suspendIdx / SEGMENT_SIZE , startFrom = curSuspendSegm ,
230
230
createNewSegment = ::createSegment).segment
231
- assert { segment.id == enqIdx / SEGMENT_SIZE }
231
+ assert { segment.id == suspendIdx / SEGMENT_SIZE }
232
232
// Try to install the continuation in the cell,
233
233
// this is the regular path.
234
- val i = (enqIdx % SEGMENT_SIZE ).toInt()
234
+ val i = (suspendIdx % SEGMENT_SIZE ).toInt()
235
235
if (segment.cas(i, null , cont)) {
236
236
if (useBackoff) {
237
237
repeat(backoffSize) {
@@ -280,19 +280,19 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
280
280
281
281
@Suppress(" UNCHECKED_CAST" )
282
282
internal fun suspendBlocking (waiter : Any ): T ? {
283
- // Increment `enqIdx ` and find the segment
283
+ // Increment `suspendIdx ` and find the segment
284
284
// with the corresponding id. It is guaranteed
285
285
// that this segment is not removed since at
286
286
// least the cell for this [suspend] invocation
287
287
// is not in the `CANCELLED` state.
288
- val curTail = this .suspendSegment.value
289
- val enqIdx = suspendIdx.getAndIncrement()
290
- val segment = this .suspendSegment.findSegmentAndMoveForward(id = enqIdx / SEGMENT_SIZE , startFrom = curTail ,
288
+ val curSuspendSegm = this .suspendSegment.value
289
+ val suspendIdx = suspendIdx.getAndIncrement()
290
+ val segment = this .suspendSegment.findSegmentAndMoveForward(id = suspendIdx / SEGMENT_SIZE , startFrom = curSuspendSegm ,
291
291
createNewSegment = ::createSegment).segment
292
- assert { segment.id == enqIdx / SEGMENT_SIZE }
292
+ assert { segment.id == suspendIdx / SEGMENT_SIZE }
293
293
// Try to install the continuation in the cell,
294
294
// this is the regular path.
295
- val i = (enqIdx % SEGMENT_SIZE ).toInt()
295
+ val i = (suspendIdx % SEGMENT_SIZE ).toInt()
296
296
if (segment.cas(i, null , waiter)) {
297
297
if (useBackoff) {
298
298
repeat(backoffSize) {
@@ -345,7 +345,7 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
345
345
// Should we skip cancelled cells?
346
346
val skipCancelled = cancellationMode != SIMPLE
347
347
while (true ) {
348
- // Try to resume the next waiter, adjust [deqIdx ] if
348
+ // Try to resume the next waiter, adjust [resumeIdx ] if
349
349
// cancelled cells should be skipped anyway.
350
350
when (tryResumeImpl(value, adjustDeqIdx = skipCancelled)) {
351
351
TRY_RESUME_SUCCESS -> return true
@@ -361,7 +361,7 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
361
361
* or [TRY_RESUME_FAIL_BROKEN] if the next cell is marked as broken by
362
362
* this [tryResumeImpl] invocation due to the [SYNC] resumption mode.
363
363
*
364
- * In the smart cancellation modes ([SMART_SYNC ] and [SMART_ASYNC ]) the
364
+ * In the smart cancellation modes ([SMART ] and [SMART ]) the
365
365
* cells marked as [cancelled][CANCELLED] should be skipped, so that
366
366
* there is no need to increment [resumeIdx] one-by-one if there is a
367
367
* removed segment (logically full of [cancelled][CANCELLED] cells);
@@ -374,29 +374,29 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
374
374
// Check that `adjustDeqIdx` is `false`
375
375
// in the simple cancellation mode.
376
376
assert { ! (cancellationMode == SIMPLE && adjustDeqIdx) }
377
- // Increment `deqIdx ` and find the first segment with
377
+ // Increment `resumeIdx ` and find the first segment with
378
378
// the corresponding or higher (if the required segment
379
379
// is physically removed) id.
380
- val curHead = this .resumeSegment.value
381
- val deqIdx = resumeIdx.getAndIncrement()
382
- val id = deqIdx / SEGMENT_SIZE
383
- val segment = this .resumeSegment.findSegmentAndMoveForward(id, startFrom = curHead ,
380
+ val curResumeSegm = this .resumeSegment.value
381
+ val resumeIdx = resumeIdx.getAndIncrement()
382
+ val id = resumeIdx / SEGMENT_SIZE
383
+ val segment = this .resumeSegment.findSegmentAndMoveForward(id, startFrom = curResumeSegm ,
384
384
createNewSegment = ::createSegment).segment
385
385
// The previous segments can be safely collected
386
386
// by GC, clean the pointer to them.
387
387
segment.cleanPrev()
388
388
// Is the required segment physically removed?
389
389
if (segment.id > id) {
390
- // Adjust `deqIdx ` to the first
390
+ // Adjust `resumeIdx ` to the first
391
391
// non-removed segment if needed.
392
392
if (adjustDeqIdx) adjustDeqIdx(segment.id * SEGMENT_SIZE )
393
- // The cell #deqIdx is in the `CANCELLED` state,
393
+ // The cell #resumeIdx is in the `CANCELLED` state,
394
394
// return the corresponding failure.
395
395
return TRY_RESUME_FAIL_CANCELLED
396
396
}
397
397
// Modify the cell according to the state machine,
398
398
// all the transitions are performed atomically.
399
- val i = (deqIdx % SEGMENT_SIZE ).toInt()
399
+ val i = (resumeIdx % SEGMENT_SIZE ).toInt()
400
400
modify_cell@while (true ) {
401
401
val cellState = segment.get(i)
402
402
when {
@@ -583,7 +583,7 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
583
583
// The cell should be considered as cancelled.
584
584
// Mark the cell correspondingly and help a
585
585
// concurrent `resume` to process its value if
586
- // needed (see `SMART_ASYNC ` cancellation mode).
586
+ // needed (see `SMART ` cancellation mode).
587
587
val value = segment.markCancelled(index) ? : return
588
588
if (value == = REFUSE ) return
589
589
// Try to resume the next waiter with the value
@@ -598,7 +598,7 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
598
598
// cell should be refused by this `SegmentQueueSynchronizer`.
599
599
// Mark the cell correspondingly and help a concurrent
600
600
// `resume` to process its value if needed
601
- // (see `SMART_ASYNC ` cancellation mode).
601
+ // (see `SMART ` cancellation mode).
602
602
val value = segment.markRefused(index) ? : return
603
603
returnRefusedValue(value as T )
604
604
}
@@ -622,7 +622,7 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
622
622
if (curIdx == (curSegment.id + 1 ) * SEGMENT_SIZE )
623
623
curSegment = curSegment.next ? : break
624
624
}
625
- return " enqIdx =${suspendIdx.value} ,deqIdx =${resumeIdx.value} ,waiters=$waiters "
625
+ return " suspendIdx =${suspendIdx.value} ,resumeIdx =${resumeIdx.value} ,waiters=$waiters "
626
626
}
627
627
}
628
628
@@ -654,15 +654,15 @@ private class SQSSegment(id: Long, prev: SQSSegment?, pointers: Int) : Segment<S
654
654
* Marks the cell as cancelled and returns `null`, so that the [resume]
655
655
* that comes to this cell detects that it is in the `CANCELLED` state
656
656
* and should fail or skip it depending on the cancellation mode.
657
- * However, in [SMART_ASYNC ] cancellation mode [resume] that comes to the cell
657
+ * However, in [SMART ] cancellation mode [resume] that comes to the cell
658
658
* with cancelled continuation asynchronously puts its value into the cell,
659
659
* and the cancellation handler completes the resumption.
660
660
* In this case, [markCancelled] returns this non-null value.
661
661
*
662
662
* If the whole segment contains [CANCELLED] markers after
663
663
* this invocation, [onSlotCleaned] is invoked and this segment
664
- * is going to be removed if [head ][SegmentQueueSynchronizer.head ]
665
- * and [tail ][SegmentQueueSynchronizer.tail ] do not reference it.
664
+ * is going to be removed if [resumeSegment ][SegmentQueueSynchronizer.resumeSegment ]
665
+ * and [suspendSegment ][SegmentQueueSynchronizer.suspendSegment ] do not reference it.
666
666
* Note that the segments that are not stored physically are still
667
667
* considered as logically stored but being full of cancelled waiters.
668
668
*/
@@ -706,7 +706,7 @@ private class SQSSegment(id: Long, prev: SQSSegment?, pointers: Int) : Segment<S
706
706
* that its value is refused by the [SegmentQueueSynchronizer],
707
707
* and [SegmentQueueSynchronizer.tryReturnRefusedValue]
708
708
* is invoked in this case (if it fails, the value is put back via
709
- * [SegmentQueueSynchronizer.returnValue]). Since in [SMART_ASYNC ]
709
+ * [SegmentQueueSynchronizer.returnValue]). Since in [SMART ]
710
710
* cancellation mode [resume] that comes to the cell with cancelled
711
711
* continuation asynchronously puts its value into the cell.
712
712
* In this case, [markRefused] returns this non-null value.
@@ -716,7 +716,7 @@ private class SQSSegment(id: Long, prev: SQSSegment?, pointers: Int) : Segment<S
716
716
/* *
717
717
* Marks the cell with the specified [marker]
718
718
* and returns `null` if the cell contains the
719
- * cancelled continuation. However, in the [SMART_ASYNC ]
719
+ * cancelled continuation. However, in the [SMART ]
720
720
* cancellation mode it is possible that [resume] comes
721
721
* to the cell with cancelled continuation and asynchronously
722
722
* puts its value into the cell, so that the cancellation
@@ -748,7 +748,7 @@ private class SQSSegment(id: Long, prev: SQSSegment?, pointers: Int) : Segment<S
748
748
* this value should be used for resuming the next waiter or be refused. When this
749
749
* value is a continuation, it is hard to distinguish it with the one related to the cancelled
750
750
* waiter. Thus, such values are wrapped with [WrappedContinuationValue] in this case. Note that the
751
- * wrapper is required only in [SegmentQueueSynchronizer.CancellationMode.SMART_ASYNC ] mode
751
+ * wrapper is required only in [SegmentQueueSynchronizer.CancellationMode.SMART ] mode
752
752
* and is used in the asynchronous race resolution logic between cancellation and [resume]
753
753
* invocation; this way, it is used relatively rare.
754
754
*/
0 commit comments