Skip to content

Commit 8f90638

Browse files
committed
tadada
1 parent e053e7c commit 8f90638

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

Diff for: kotlinx-coroutines-core/common/src/internal/SegmentQueueSynchronizer.kt

+39-39
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ import kotlin.native.concurrent.*
5959
* and used by default. In this mode, [resume] fails if it finds the cell in the `CANCELLED` state or if the waiter resumption
6060
* (see [CancellableContinuation.tryResume]) does not succeed. As we discussed, these failures are typically handled
6161
* 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
6363
* considered as `CANCELLED`). This way, even if a million of canceled continuations are stored in [SegmentQueueSynchronizer],
6464
* one [resume] invocation is sufficient to pass the value to a waiter since it skips all these canceled waiters.
6565
* 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.
6767
*
6868
* The main issue with skipping `CANCELLED` cells in [resume] is that it can become illegal to put the value into
6969
* the next cell. Consider the following execution: [suspend] is called, then [resume] starts, but the suspended
@@ -77,11 +77,11 @@ import kotlin.native.concurrent.*
7777
* [tryReturnRefusedValue] to return it back to the outer data structure. However, it is possible for
7878
* [tryReturnRefusedValue] to fail, and [returnValue] is called in this case. Typically, this [returnValue] function
7979
* 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
8181
* 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
8383
* 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
8585
* of the data structure for a while but is guaranteed to be processed eventually.
8686
*
8787
* To support prompt cancellation, [SegmentQueueSynchronizer] returns the value back to the data structure by calling
@@ -135,7 +135,7 @@ import kotlin.native.concurrent.*
135135
* between the counters for [resume] and [suspend], and does not need to store an infinite array of cells.
136136
*
137137
* 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
139139
* a node in a Michael-Scott queue. Following this structure, we can maintain the cells that are in the current active
140140
* range (between the counters), and access the cells similarly to an array. Specifically, we change the current working
141141
* 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> {
165165

166166
/**
167167
* 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
170170
* cancellation handler in order not to wait, so that the element can be "hung"
171171
* for a while, but it is guaranteed that the element will be processed eventually.
172172
*/
@@ -219,19 +219,19 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
219219

220220
@Suppress("UNCHECKED_CAST")
221221
internal fun suspend(cont: Continuation<T>): Boolean {
222-
// Increment `enqIdx` and find the segment
222+
// Increment `suspendIdx` and find the segment
223223
// with the corresponding id. It is guaranteed
224224
// that this segment is not removed since at
225225
// least the cell for this [suspend] invocation
226226
// 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,
230230
createNewSegment = ::createSegment).segment
231-
assert { segment.id == enqIdx / SEGMENT_SIZE }
231+
assert { segment.id == suspendIdx / SEGMENT_SIZE }
232232
// Try to install the continuation in the cell,
233233
// this is the regular path.
234-
val i = (enqIdx % SEGMENT_SIZE).toInt()
234+
val i = (suspendIdx % SEGMENT_SIZE).toInt()
235235
if (segment.cas(i, null, cont)) {
236236
if (useBackoff) {
237237
repeat(backoffSize) {
@@ -280,19 +280,19 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
280280

281281
@Suppress("UNCHECKED_CAST")
282282
internal fun suspendBlocking(waiter: Any): T? {
283-
// Increment `enqIdx` and find the segment
283+
// Increment `suspendIdx` and find the segment
284284
// with the corresponding id. It is guaranteed
285285
// that this segment is not removed since at
286286
// least the cell for this [suspend] invocation
287287
// 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,
291291
createNewSegment = ::createSegment).segment
292-
assert { segment.id == enqIdx / SEGMENT_SIZE }
292+
assert { segment.id == suspendIdx / SEGMENT_SIZE }
293293
// Try to install the continuation in the cell,
294294
// this is the regular path.
295-
val i = (enqIdx % SEGMENT_SIZE).toInt()
295+
val i = (suspendIdx % SEGMENT_SIZE).toInt()
296296
if (segment.cas(i, null, waiter)) {
297297
if (useBackoff) {
298298
repeat(backoffSize) {
@@ -345,7 +345,7 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
345345
// Should we skip cancelled cells?
346346
val skipCancelled = cancellationMode != SIMPLE
347347
while (true) {
348-
// Try to resume the next waiter, adjust [deqIdx] if
348+
// Try to resume the next waiter, adjust [resumeIdx] if
349349
// cancelled cells should be skipped anyway.
350350
when (tryResumeImpl(value, adjustDeqIdx = skipCancelled)) {
351351
TRY_RESUME_SUCCESS -> return true
@@ -361,7 +361,7 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
361361
* or [TRY_RESUME_FAIL_BROKEN] if the next cell is marked as broken by
362362
* this [tryResumeImpl] invocation due to the [SYNC] resumption mode.
363363
*
364-
* In the smart cancellation modes ([SMART_SYNC] and [SMART_ASYNC]) the
364+
* In the smart cancellation modes ([SMART] and [SMART]) the
365365
* cells marked as [cancelled][CANCELLED] should be skipped, so that
366366
* there is no need to increment [resumeIdx] one-by-one if there is a
367367
* removed segment (logically full of [cancelled][CANCELLED] cells);
@@ -374,29 +374,29 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
374374
// Check that `adjustDeqIdx` is `false`
375375
// in the simple cancellation mode.
376376
assert { !(cancellationMode == SIMPLE && adjustDeqIdx) }
377-
// Increment `deqIdx` and find the first segment with
377+
// Increment `resumeIdx` and find the first segment with
378378
// the corresponding or higher (if the required segment
379379
// 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,
384384
createNewSegment = ::createSegment).segment
385385
// The previous segments can be safely collected
386386
// by GC, clean the pointer to them.
387387
segment.cleanPrev()
388388
// Is the required segment physically removed?
389389
if (segment.id > id) {
390-
// Adjust `deqIdx` to the first
390+
// Adjust `resumeIdx` to the first
391391
// non-removed segment if needed.
392392
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,
394394
// return the corresponding failure.
395395
return TRY_RESUME_FAIL_CANCELLED
396396
}
397397
// Modify the cell according to the state machine,
398398
// all the transitions are performed atomically.
399-
val i = (deqIdx % SEGMENT_SIZE).toInt()
399+
val i = (resumeIdx % SEGMENT_SIZE).toInt()
400400
modify_cell@while (true) {
401401
val cellState = segment.get(i)
402402
when {
@@ -583,7 +583,7 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
583583
// The cell should be considered as cancelled.
584584
// Mark the cell correspondingly and help a
585585
// concurrent `resume` to process its value if
586-
// needed (see `SMART_ASYNC` cancellation mode).
586+
// needed (see `SMART` cancellation mode).
587587
val value = segment.markCancelled(index) ?: return
588588
if (value === REFUSE) return
589589
// Try to resume the next waiter with the value
@@ -598,7 +598,7 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
598598
// cell should be refused by this `SegmentQueueSynchronizer`.
599599
// Mark the cell correspondingly and help a concurrent
600600
// `resume` to process its value if needed
601-
// (see `SMART_ASYNC` cancellation mode).
601+
// (see `SMART` cancellation mode).
602602
val value = segment.markRefused(index) ?: return
603603
returnRefusedValue(value as T)
604604
}
@@ -622,7 +622,7 @@ internal abstract class SegmentQueueSynchronizer<T : Any> {
622622
if (curIdx == (curSegment.id + 1) * SEGMENT_SIZE)
623623
curSegment = curSegment.next ?: break
624624
}
625-
return "enqIdx=${suspendIdx.value},deqIdx=${resumeIdx.value},waiters=$waiters"
625+
return "suspendIdx=${suspendIdx.value},resumeIdx=${resumeIdx.value},waiters=$waiters"
626626
}
627627
}
628628

@@ -654,15 +654,15 @@ private class SQSSegment(id: Long, prev: SQSSegment?, pointers: Int) : Segment<S
654654
* Marks the cell as cancelled and returns `null`, so that the [resume]
655655
* that comes to this cell detects that it is in the `CANCELLED` state
656656
* 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
658658
* with cancelled continuation asynchronously puts its value into the cell,
659659
* and the cancellation handler completes the resumption.
660660
* In this case, [markCancelled] returns this non-null value.
661661
*
662662
* If the whole segment contains [CANCELLED] markers after
663663
* 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.
666666
* Note that the segments that are not stored physically are still
667667
* considered as logically stored but being full of cancelled waiters.
668668
*/
@@ -706,7 +706,7 @@ private class SQSSegment(id: Long, prev: SQSSegment?, pointers: Int) : Segment<S
706706
* that its value is refused by the [SegmentQueueSynchronizer],
707707
* and [SegmentQueueSynchronizer.tryReturnRefusedValue]
708708
* 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]
710710
* cancellation mode [resume] that comes to the cell with cancelled
711711
* continuation asynchronously puts its value into the cell.
712712
* In this case, [markRefused] returns this non-null value.
@@ -716,7 +716,7 @@ private class SQSSegment(id: Long, prev: SQSSegment?, pointers: Int) : Segment<S
716716
/**
717717
* Marks the cell with the specified [marker]
718718
* and returns `null` if the cell contains the
719-
* cancelled continuation. However, in the [SMART_ASYNC]
719+
* cancelled continuation. However, in the [SMART]
720720
* cancellation mode it is possible that [resume] comes
721721
* to the cell with cancelled continuation and asynchronously
722722
* 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
748748
* this value should be used for resuming the next waiter or be refused. When this
749749
* value is a continuation, it is hard to distinguish it with the one related to the cancelled
750750
* 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
752752
* and is used in the asynchronous race resolution logic between cancellation and [resume]
753753
* invocation; this way, it is used relatively rare.
754754
*/

0 commit comments

Comments
 (0)