package kotlinx.coroutines.channels; import kotlin.ExceptionsKt__ExceptionsKt; import kotlin.Unit; import kotlin.coroutines.Continuation; import kotlin.jvm.functions.Function1; import kotlin.jvm.internal.Reflection; import kotlinx.coroutines.Waiter; import kotlinx.coroutines.channels.ChannelResult; import kotlinx.coroutines.internal.OnUndeliveredElementKt; import kotlinx.coroutines.internal.UndeliveredElementException; /* loaded from: classes5.dex */ public class ConflatedBufferedChannel extends BufferedChannel { public final int capacity; public final BufferOverflow onBufferOverflow; @Override // kotlinx.coroutines.channels.BufferedChannel, kotlinx.coroutines.channels.SendChannel public Object send(Object obj, Continuation continuation) { return send$suspendImpl(this, obj, continuation); } public ConflatedBufferedChannel(int i, BufferOverflow bufferOverflow, Function1 function1) { super(i, function1); this.capacity = i; this.onBufferOverflow = bufferOverflow; if (bufferOverflow == BufferOverflow.SUSPEND) { throw new IllegalArgumentException(("This implementation does not support suspension for senders, use " + Reflection.getOrCreateKotlinClass(BufferedChannel.class).getSimpleName() + " instead").toString()); } if (i >= 1) { return; } throw new IllegalArgumentException(("Buffered channel capacity must be at least 1, but " + i + " was specified").toString()); } @Override // kotlinx.coroutines.channels.BufferedChannel public boolean isConflatedDropOldest() { return this.onBufferOverflow == BufferOverflow.DROP_OLDEST; } public static /* synthetic */ Object send$suspendImpl(ConflatedBufferedChannel conflatedBufferedChannel, Object obj, Continuation continuation) { UndeliveredElementException callUndeliveredElementCatchingException$default; Object m4142trySendImplMj0NB7M = conflatedBufferedChannel.m4142trySendImplMj0NB7M(obj, true); if (m4142trySendImplMj0NB7M instanceof ChannelResult.Closed) { ChannelResult.m4130exceptionOrNullimpl(m4142trySendImplMj0NB7M); Function1 function1 = conflatedBufferedChannel.onUndeliveredElement; if (function1 != null && (callUndeliveredElementCatchingException$default = OnUndeliveredElementKt.callUndeliveredElementCatchingException$default(function1, obj, null, 2, null)) != null) { ExceptionsKt__ExceptionsKt.addSuppressed(callUndeliveredElementCatchingException$default, conflatedBufferedChannel.getSendException()); throw callUndeliveredElementCatchingException$default; } throw conflatedBufferedChannel.getSendException(); } return Unit.INSTANCE; } @Override // kotlinx.coroutines.channels.BufferedChannel, kotlinx.coroutines.channels.SendChannel /* renamed from: trySend-JP2dKIU */ public Object mo4126trySendJP2dKIU(Object obj) { return m4142trySendImplMj0NB7M(obj, false); } /* renamed from: trySendImpl-Mj0NB7M, reason: not valid java name */ public final Object m4142trySendImplMj0NB7M(Object obj, boolean z) { return this.onBufferOverflow == BufferOverflow.DROP_LATEST ? m4140trySendDropLatestMj0NB7M(obj, z) : m4141trySendDropOldestJP2dKIU(obj); } /* renamed from: trySendDropLatest-Mj0NB7M, reason: not valid java name */ public final Object m4140trySendDropLatestMj0NB7M(Object obj, boolean z) { Function1 function1; UndeliveredElementException callUndeliveredElementCatchingException$default; Object mo4126trySendJP2dKIU = super.mo4126trySendJP2dKIU(obj); if (ChannelResult.m4134isSuccessimpl(mo4126trySendJP2dKIU) || ChannelResult.m4133isClosedimpl(mo4126trySendJP2dKIU)) { return mo4126trySendJP2dKIU; } if (z && (function1 = this.onUndeliveredElement) != null && (callUndeliveredElementCatchingException$default = OnUndeliveredElementKt.callUndeliveredElementCatchingException$default(function1, obj, null, 2, null)) != null) { throw callUndeliveredElementCatchingException$default; } return ChannelResult.Companion.m4139successJP2dKIU(Unit.INSTANCE); } /* renamed from: trySendDropOldest-JP2dKIU, reason: not valid java name */ public final Object m4141trySendDropOldestJP2dKIU(Object obj) { ChannelSegment channelSegment; Object obj2 = BufferedChannelKt.BUFFERED; ChannelSegment channelSegment2 = (ChannelSegment) BufferedChannel.sendSegment$FU.get(this); while (true) { long andIncrement = BufferedChannel.sendersAndCloseStatus$FU.getAndIncrement(this); long j = andIncrement & 1152921504606846975L; boolean isClosedForSend0 = isClosedForSend0(andIncrement); int i = BufferedChannelKt.SEGMENT_SIZE; long j2 = j / i; int i2 = (int) (j % i); if (channelSegment2.id != j2) { ChannelSegment findSegmentSend = findSegmentSend(j2, channelSegment2); if (findSegmentSend != null) { channelSegment = findSegmentSend; } else if (isClosedForSend0) { return ChannelResult.Companion.m4137closedJP2dKIU(getSendException()); } } else { channelSegment = channelSegment2; } int updateCellSend = updateCellSend(channelSegment, i2, obj, j, obj2, isClosedForSend0); if (updateCellSend == 0) { channelSegment.cleanPrev(); return ChannelResult.Companion.m4139successJP2dKIU(Unit.INSTANCE); } if (updateCellSend == 1) { return ChannelResult.Companion.m4139successJP2dKIU(Unit.INSTANCE); } if (updateCellSend == 2) { if (isClosedForSend0) { channelSegment.onSlotCleaned(); return ChannelResult.Companion.m4137closedJP2dKIU(getSendException()); } Waiter waiter = obj2 instanceof Waiter ? (Waiter) obj2 : null; if (waiter != null) { prepareSenderForSuspension(waiter, channelSegment, i2); } dropFirstElementUntilTheSpecifiedCellIsInTheBuffer((channelSegment.id * i) + i2); return ChannelResult.Companion.m4139successJP2dKIU(Unit.INSTANCE); } if (updateCellSend == 3) { throw new IllegalStateException("unexpected".toString()); } if (updateCellSend == 4) { if (j < getReceiversCounter$kotlinx_coroutines_core()) { channelSegment.cleanPrev(); } return ChannelResult.Companion.m4137closedJP2dKIU(getSendException()); } if (updateCellSend == 5) { channelSegment.cleanPrev(); } channelSegment2 = channelSegment; } } }