@@ -68,7 +68,7 @@ internal open class BufferedChannel<E>(
68
68
69
69
internal val sendersCounter: Long get() = sendersAndCloseStatus.value.sendersCounter
70
70
internal val receiversCounter: Long get() = receivers.value
71
- internal val bufferEndCounter: Long get() = bufferEnd.value
71
+ private val bufferEndCounter: Long get() = bufferEnd.value
72
72
73
73
/*
74
74
Additionally to the counters above, we need an extra one that
@@ -813,17 +813,11 @@ internal open class BufferedChannel<E>(
813
813
}
814
814
815
815
// TODO: method name, documentation, implementation.
816
- protected fun dropFirstElementsIfNeeded (s : Long ) {
816
+ protected fun dropFirstElementIfNeeded (s : Long ) {
817
817
// Read the segment reference before the counter increment;
818
818
// it is crucial to be able to find the required segment later.
819
819
var segment = receiveSegment.value
820
820
while (true ) {
821
- // Similar to the `send(e)` operation, `receive()` first checks
822
- // whether the channel is already closed for receiving.
823
- if (isClosedForReceiveImpl) {
824
- if (receiversCounter < sendersCounter) segment.cleanPrev()
825
- return
826
- }
827
821
// Atomically increments the `receivers` counter
828
822
// and obtain the value right before the increment.
829
823
val r = this .receivers.value
@@ -1534,7 +1528,8 @@ internal open class BufferedChannel<E>(
1534
1528
onCancellationConstructor = onUndeliveredElementReceiveCancellationConstructor
1535
1529
)
1536
1530
1537
- protected open fun registerSelectForReceive (select : SelectInstance <* >, ignoredParam : Any? ) =
1531
+ @Suppress(" UNUSED_PARAMETER" )
1532
+ private fun registerSelectForReceive (select : SelectInstance <* >, ignoredParam : Any? ) =
1538
1533
receiveImpl( // <-- this is an inline function
1539
1534
waiter = select,
1540
1535
onElementRetrieved = { elem -> select.selectInRegistrationPhase(elem) },
@@ -1606,7 +1601,7 @@ internal open class BufferedChannel<E>(
1606
1601
* and [SelectInstance.trySelect]. When the channel becomes closed,
1607
1602
* [tryResumeHasNextOnClosedChannel] should be used instead.
1608
1603
*/
1609
- protected open inner class BufferedChannelIterator : ChannelIterator <E >, BeforeResumeCancelHandler (), Waiter {
1604
+ private inner class BufferedChannelIterator : ChannelIterator <E >, BeforeResumeCancelHandler (), Waiter {
1610
1605
/* *
1611
1606
* Stores the element retrieved by [hasNext] or
1612
1607
* a special [CHANNEL_CLOSED] token if this channel is closed.
@@ -1775,7 +1770,7 @@ internal open class BufferedChannel<E>(
1775
1770
*/
1776
1771
private val _closeCause = atomic<Any ?>(NO_CLOSE_CAUSE )
1777
1772
// Should be called only if this channel is closed or cancelled.
1778
- protected val closeCause get() = _closeCause .value as Throwable ?
1773
+ internal val closeCause get() = _closeCause .value as Throwable ?
1779
1774
1780
1775
/* * Returns the closing cause if it is non-null, or [ClosedSendChannelException] otherwise. */
1781
1776
protected val sendException get() = closeCause ? : ClosedSendChannelException (DEFAULT_CLOSE_MESSAGE )
@@ -1953,17 +1948,21 @@ internal open class BufferedChannel<E>(
1953
1948
/* *
1954
1949
* Completes the started [close] or [cancel] procedure.
1955
1950
*/
1956
- protected open fun completeCloseOrCancel () {
1951
+ private fun completeCloseOrCancel () {
1957
1952
isClosedForSendImpl // must finish the started close/cancel if one is detected.
1958
1953
}
1959
1954
1955
+ protected open val isConflatedDropOldest get() = false
1956
+
1960
1957
/* *
1961
1958
* Completes the channel closing procedure.
1962
1959
*/
1963
- private fun completeClose (sendersCur : Long ): ChannelSegment <E > {
1960
+ protected open fun completeClose (sendersCur : Long ): ChannelSegment <E > {
1964
1961
// Close the linked list for further segment addition,
1965
1962
// obtaining the last segment in the data structure.
1966
1963
val lastSegment = closeLinkedList()
1964
+ // TODO
1965
+ if (isConflatedDropOldest) xxx(lastSegment)
1967
1966
// Resume waiting `receive()` requests,
1968
1967
// informing them that the channel is closed.
1969
1968
cancelSuspendedReceiveRequests(lastSegment, sendersCur)
@@ -1972,6 +1971,31 @@ internal open class BufferedChannel<E>(
1972
1971
return lastSegment
1973
1972
}
1974
1973
1974
+ private fun xxx (lastSegment : ChannelSegment <E >) {
1975
+ var segment = lastSegment
1976
+ traverse@while (true ) {
1977
+ for (index in SEGMENT_SIZE - 1 downTo 0 ) {
1978
+ cell_update@while (true ) {
1979
+ val state = segment.getState(index)
1980
+ when {
1981
+ state == = null || state == = IN_BUFFER -> {
1982
+ if (segment.casState(index, state, CHANNEL_CLOSED )) {
1983
+ segment.onSlotCleaned()
1984
+ break @cell_update
1985
+ }
1986
+ }
1987
+ state == = BUFFERED -> {
1988
+ dropFirstElementIfNeeded(segment.id * SEGMENT_SIZE + index)
1989
+ break @traverse
1990
+ }
1991
+ else -> break @cell_update
1992
+ }
1993
+ }
1994
+ }
1995
+ segment = segment.prev ? : break
1996
+ }
1997
+ }
1998
+
1975
1999
/* *
1976
2000
* Completes the channel cancellation procedure.
1977
2001
*/
0 commit comments