Skip to content

Commit

Permalink
Small style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
osipxd committed Feb 13, 2025
1 parent 140d14a commit 8a50989
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
19 changes: 10 additions & 9 deletions ktor-io/common/src/io/ktor/utils/io/ByteChannel.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
/*
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.utils.io

import io.ktor.utils.io.locks.*
import kotlinx.atomicfu.*
import kotlinx.coroutines.*
import kotlinx.io.*
import kotlinx.atomicfu.AtomicRef
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.io.Buffer
import kotlinx.io.Sink
import kotlinx.io.Source
import kotlin.concurrent.Volatile
import kotlin.coroutines.*
import kotlin.jvm.*
import kotlin.coroutines.Continuation
import kotlin.jvm.JvmStatic

internal expect val DEVELOPMENT_MODE: Boolean
internal const val CHANNEL_MAX_SIZE: Int = 1024 * 1024
Expand Down Expand Up @@ -171,9 +174,7 @@ public class ByteChannel(public val autoFlush: Boolean = false) : ByteReadChanne
private fun closeSlot(cause: Throwable?) {
val closeContinuation = if (cause != null) Slot.Closed(cause) else Slot.CLOSED
val continuation = suspensionSlot.getAndSet(closeContinuation)
if (continuation !is Slot.Task) return

continuation.resume(cause)
if (continuation is Slot.Task) continuation.resume(cause)
}

private inline fun <reified TaskType : Slot.Task> trySuspend(
Expand Down
6 changes: 3 additions & 3 deletions ktor-io/common/src/io/ktor/utils/io/CloseToken.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.utils.io
Expand All @@ -13,7 +13,7 @@ internal val CLOSED = CloseToken(null)
@OptIn(ExperimentalCoroutinesApi::class)
internal class CloseToken(private val origin: Throwable?) {

fun wrapCause(wrap: (Throwable?) -> Throwable = ::ClosedByteChannelException): Throwable? {
fun wrapCause(wrap: (Throwable) -> Throwable = ::ClosedByteChannelException): Throwable? {
return when (origin) {
null -> null
is CopyableThrowable<*> -> origin.createCopy()
Expand All @@ -22,6 +22,6 @@ internal class CloseToken(private val origin: Throwable?) {
}
}

fun throwOrNull(wrap: (Throwable?) -> Throwable): Unit? =
fun throwOrNull(wrap: (Throwable) -> Throwable): Unit? =
wrapCause(wrap)?.let { throw it }
}
6 changes: 4 additions & 2 deletions ktor-io/common/src/io/ktor/utils/io/SinkByteWriteChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package io.ktor.utils.io

import kotlinx.atomicfu.AtomicRef
import kotlinx.atomicfu.atomic
import kotlinx.io.*
import kotlinx.io.IOException
import kotlinx.io.RawSink
import kotlinx.io.Sink
import kotlinx.io.buffered

/**
* Creates a [ByteWriteChannel] that writes to this [Sink].
Expand Down Expand Up @@ -58,7 +61,6 @@ internal class SinkByteWriteChannel(origin: RawSink) : ByteWriteChannel {
if (!closed.compareAndSet(expect = null, update = CLOSED)) return
}

@OptIn(InternalAPI::class)
override fun cancel(cause: Throwable?) {
val token = if (cause == null) CLOSED else CloseToken(cause)
if (!closed.compareAndSet(expect = null, update = token)) return
Expand Down

0 comments on commit 8a50989

Please sign in to comment.