@@ -902,7 +902,7 @@ internal open class BufferedChannel<E>(
902
902
while (true ) {
903
903
// Similar to the `send(e)` operation, `receive()` first checks
904
904
// whether the channel is already closed for receiving.
905
- if (isClosedForReceiveImpl ) {
905
+ if (isClosedForReceive ) {
906
906
if (receiversCounter < sendersCounter) segment.cleanPrev()
907
907
return onClosed()
908
908
}
@@ -1958,7 +1958,7 @@ internal open class BufferedChannel<E>(
1958
1958
* Completes the started [close] or [cancel] procedure.
1959
1959
*/
1960
1960
private fun completeCloseOrCancel () {
1961
- isClosedForSendImpl // must finish the started close/cancel if one is detected.
1961
+ isClosedForSend // must finish the started close/cancel if one is detected.
1962
1962
}
1963
1963
1964
1964
protected open val isConflatedDropOldest get() = false
@@ -2009,7 +2009,9 @@ internal open class BufferedChannel<E>(
2009
2009
*/
2010
2010
private fun closeLinkedList (): ChannelSegment <E > {
2011
2011
// Choose the last segment.
2012
- val lastSegment = listOf (bufferEndSegment.value, sendSegment.value, receiveSegment.value).maxBy { it.id }
2012
+ var lastSegment = bufferEndSegment.value
2013
+ sendSegment.value.let { if (it.id > lastSegment.id) lastSegment = it }
2014
+ receiveSegment.value.let { if (it.id > lastSegment.id) lastSegment = it }
2013
2015
// Close the linked list of segment for new segment addition
2014
2016
// and return the last segment in the linked list.
2015
2017
return lastSegment.close()
@@ -2235,27 +2237,20 @@ internal open class BufferedChannel<E>(
2235
2237
2236
2238
@ExperimentalCoroutinesApi
2237
2239
override val isClosedForSend: Boolean
2238
- get() = isClosedForSendImpl
2239
-
2240
- private val isClosedForSendImpl: Boolean
2241
2240
get() = sendersAndCloseStatus.value.isClosedForSend0
2242
2241
2243
2242
private val Long .isClosedForSend0 get() =
2244
- isClosed(this , sendersCur = this .sendersCounter, isClosedForReceive = false )
2243
+ isClosed(this , isClosedForReceive = false )
2245
2244
2246
2245
@ExperimentalCoroutinesApi
2247
2246
override val isClosedForReceive: Boolean
2248
- get() = isClosedForReceiveImpl
2249
-
2250
- private val isClosedForReceiveImpl: Boolean
2251
2247
get() = sendersAndCloseStatus.value.isClosedForReceive0
2252
2248
2253
2249
private val Long .isClosedForReceive0 get() =
2254
- isClosed(this , sendersCur = this .sendersCounter, isClosedForReceive = true )
2250
+ isClosed(this , isClosedForReceive = true )
2255
2251
2256
2252
private fun isClosed (
2257
2253
sendersAndCloseStatusCur : Long ,
2258
- sendersCur : Long ,
2259
2254
isClosedForReceive : Boolean
2260
2255
) = when (sendersAndCloseStatusCur.sendersCloseStatus) {
2261
2256
// This channel is active and has not been closed.
@@ -2270,7 +2265,7 @@ internal open class BufferedChannel<E>(
2270
2265
// for senders or the flag whether there still
2271
2266
// exist elements to retrieve for receivers.
2272
2267
CLOSE_STATUS_CLOSED -> {
2273
- completeClose(sendersCur )
2268
+ completeClose(sendersAndCloseStatusCur.sendersCounter )
2274
2269
// When `isClosedForReceive` is `false`, always return `true`.
2275
2270
// Otherwise, it is possible that the channel is closed but
2276
2271
// still has elements to retrieve.
@@ -2280,7 +2275,7 @@ internal open class BufferedChannel<E>(
2280
2275
// Help to complete the cancellation procedure to
2281
2276
// guarantee linearizability and return `true`.
2282
2277
CLOSE_STATUS_CANCELLED -> {
2283
- completeCancel(sendersCur )
2278
+ completeCancel(sendersAndCloseStatusCur.sendersCounter )
2284
2279
true
2285
2280
}
2286
2281
else -> error(" unexpected close status: ${sendersAndCloseStatusCur.sendersCloseStatus} " )
@@ -2290,12 +2285,12 @@ internal open class BufferedChannel<E>(
2290
2285
override val isEmpty: Boolean get() {
2291
2286
// This function should return `false` if
2292
2287
// this channel is closed for `receive`.
2293
- if (isClosedForReceiveImpl ) return false
2288
+ if (isClosedForReceive ) return false
2294
2289
// Does this channel has elements to retrieve?
2295
2290
if (hasElements()) return false
2296
2291
// This channel does not have elements to retrieve;
2297
2292
// Check that it is still not closed for `receive`.
2298
- return ! isClosedForReceiveImpl
2293
+ return ! isClosedForReceive
2299
2294
}
2300
2295
2301
2296
/* *
@@ -2796,23 +2791,18 @@ internal class ChannelSegment<E>(id: Long, prev: ChannelSegment<E>?, channel: Bu
2796
2791
// Perform the cancellation; `onCancellationImpl(..)` return `true` if the
2797
2792
// cancelled operation had not been resumed. In this case, the `onUndeliveredElement`
2798
2793
// lambda should be called.
2799
- if (onCancellationImpl (index)) {
2794
+ if (onCancellation (index)) {
2800
2795
channel.onUndeliveredElement!! .callUndeliveredElement(element, context)
2801
2796
}
2802
2797
}
2803
2798
2804
- fun onCancellation (index : Int ) {
2805
- onCancellationImpl(index)
2806
- }
2807
-
2808
-
2809
2799
/* *
2810
2800
* Returns `true` if the request is successfully cancelled,
2811
2801
* and no rendezvous has happened. We need this knowledge
2812
2802
* to keep [BufferedChannel.onUndeliveredElement] correct.
2813
2803
*/
2814
2804
@Suppress(" ConvertTwoComparisonsToRangeCheck" )
2815
- private fun onCancellationImpl (index : Int ): Boolean {
2805
+ fun onCancellation (index : Int ): Boolean {
2816
2806
// Count the global index of this cell and read
2817
2807
// the current counters of send and receive operations.
2818
2808
val globalIndex = id * SEGMENT_SIZE + index
0 commit comments