@@ -1392,9 +1392,9 @@ internal open class BufferedChannel<E>(
1392
1392
/* *
1393
1393
* Waits in a spin-loop until the [expandBuffer] call that
1394
1394
* should process the [globalIndex]-th cell is completed.
1395
- * Essentially, it waits until the counters of started ([bufferEnd])
1396
- * and completed ([completedExpandBuffersAndPauseFlag]) [expandBuffer] attempts
1397
- * coincide and become equal or greater than [globalIndex].
1395
+ * Essentially, it waits until the numbers of started ([bufferEnd])
1396
+ * and completed ([completedExpandBuffersAndPauseFlag]) [expandBuffer]
1397
+ * attempts coincide and become equal or greater than [globalIndex].
1398
1398
* To avoid starvation, this function may set a flag
1399
1399
* that pauses further progress.
1400
1400
*/
@@ -1770,7 +1770,7 @@ internal open class BufferedChannel<E>(
1770
1770
*/
1771
1771
private val _closeCause = atomic<Any ?>(NO_CLOSE_CAUSE )
1772
1772
// Should be called only if this channel is closed or cancelled.
1773
- internal val closeCause get() = _closeCause .value as Throwable ?
1773
+ protected val closeCause get() = _closeCause .value as Throwable ?
1774
1774
1775
1775
/* * Returns the closing cause if it is non-null, or [ClosedSendChannelException] otherwise. */
1776
1776
protected val sendException get() = closeCause ? : ClosedSendChannelException (DEFAULT_CLOSE_MESSAGE )
@@ -1957,7 +1957,7 @@ internal open class BufferedChannel<E>(
1957
1957
/* *
1958
1958
* Completes the channel closing procedure.
1959
1959
*/
1960
- protected open fun completeClose (sendersCur : Long ): ChannelSegment <E > {
1960
+ private fun completeClose (sendersCur : Long ): ChannelSegment <E > {
1961
1961
// Close the linked list for further segment addition,
1962
1962
// obtaining the last segment in the data structure.
1963
1963
val lastSegment = closeLinkedList()
@@ -2040,7 +2040,7 @@ internal open class BufferedChannel<E>(
2040
2040
// order after that.
2041
2041
var suspendedSenders = InlineList <Waiter >()
2042
2042
var segment = lastSegment
2043
- traverse @ while (true ) {
2043
+ process_segments @ while (true ) {
2044
2044
for (index in SEGMENT_SIZE - 1 downTo 0 ) {
2045
2045
// Process the cell `segment[index]`.
2046
2046
val globalIndex = segment.id * SEGMENT_SIZE + index
@@ -2050,11 +2050,11 @@ internal open class BufferedChannel<E>(
2050
2050
val state = segment.getState(index)
2051
2051
when {
2052
2052
// The cell is already processed by a receiver.
2053
- state == = DONE_RCV -> break @traverse
2053
+ state == = DONE_RCV -> break @process_segments
2054
2054
// The cell stores a buffered element.
2055
2055
state == = BUFFERED -> {
2056
2056
// Is the cell already covered by a receiver?
2057
- if (globalIndex < receiversCounter) break @traverse
2057
+ if (globalIndex < receiversCounter) break @process_segments
2058
2058
// Update the cell state to `CHANNEL_CLOSED`.
2059
2059
if (segment.casState(index, state, CHANNEL_CLOSED )) {
2060
2060
// If `onUndeliveredElement` lambda is non-null, call it.
@@ -2081,7 +2081,7 @@ internal open class BufferedChannel<E>(
2081
2081
// The cell stores a suspended waiter.
2082
2082
state is Waiter || state is WaiterEB -> {
2083
2083
// Is the cell already covered by a receiver?
2084
- if (globalIndex < receiversCounter) break @traverse
2084
+ if (globalIndex < receiversCounter) break @process_segments
2085
2085
// Obtain the sender.
2086
2086
val sender: Waiter = if (state is WaiterEB ) state.waiter
2087
2087
else state as Waiter
@@ -2103,7 +2103,7 @@ internal open class BufferedChannel<E>(
2103
2103
}
2104
2104
// A concurrent receiver is resuming a suspended sender.
2105
2105
// As the cell is covered by a receiver, finish immediately.
2106
- state == = RESUMING_BY_EB || state == = RESUMING_BY_RCV -> break @traverse
2106
+ state == = RESUMING_BY_EB || state == = RESUMING_BY_RCV -> break @process_segments
2107
2107
// A concurrent `expandBuffer()` is resuming a suspended sender.
2108
2108
// Wait in a spin-loop until the cell state changes.
2109
2109
state == = RESUMING_BY_EB -> continue @update_cell
@@ -2284,18 +2284,14 @@ internal open class BufferedChannel<E>(
2284
2284
// obtained segment (in the beginning of this function) has lower id.
2285
2285
val id = r / SEGMENT_SIZE
2286
2286
if (segment.id != id) {
2287
- // Check that the channel is not closed yet
2288
- // and the required segment may be found.
2289
- // Otherwise, the `r`-th cell will not be physically
2290
- // created, and this function returns `false` immediately.
2291
- // We could perform this check only if `findSegmentReceive`
2292
- // below returns `null`, but it would make the code less clean.
2293
- segment.nextOrIfClosed {
2294
- segment.cleanPrev()
2295
- return false
2296
- }
2297
- // Find the required segment, restarting the operation if it has not been found.
2298
- segment = findSegmentReceive(id, segment) ? : continue
2287
+ // Try to find the required segment.
2288
+ segment = findSegmentReceive(id, segment) ? :
2289
+ // The required segment has not been found. Either it has already
2290
+ // been removed, or the underlying linked list is already closed
2291
+ // for segment additions. In the latter case, the channel is closed
2292
+ // and does not contain elements, so this operation returns `false`.
2293
+ // Otherwise, if the required segment is removed, the operation restarts.
2294
+ if (receiveSegment.value.id < id) return false else continue
2299
2295
}
2300
2296
segment.cleanPrev() // all the previous segments are no longer needed.
2301
2297
// Does the `r`-th cell contain waiting sender or buffered element?
0 commit comments