Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions okio/src/commonMain/kotlin/okio/CommonPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ expect class Lock
expect inline fun <T> Lock.withLock(action: () -> T): T

internal expect fun newLock(): Lock
internal expect inline fun Lock.destroy()

expect open class IOException(message: String?, cause: Throwable?) : Exception {
constructor(message: String?)
Expand Down
1 change: 1 addition & 0 deletions okio/src/commonMain/kotlin/okio/FileHandle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ abstract class FileHandle(
closed = true
if (openStreamCount != 0) return
}
lock.destroy()
protectedClose()
}

Expand Down
16 changes: 16 additions & 0 deletions okio/src/jsMain/kotlin/okio/JsPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package okio

import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/*
* This file exposes Node.js `os` and `path` APIs to Kotlin/JS, using reasonable default behaviors
* if those symbols aren't available.
Expand Down Expand Up @@ -50,3 +53,16 @@ private val path: dynamic

internal val tmpdir: String
get() = os?.tmpdir() as? String ?: "/tmp"

actual typealias Lock = Unit

internal actual fun newLock(): Lock = Unit
internal actual inline fun Lock.destroy() = Unit

actual inline fun <T> Lock.withLock(action: () -> T): T {
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}

return action()
}
1 change: 1 addition & 0 deletions okio/src/jvmMain/kotlin/okio/-JvmPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ actual typealias ArrayIndexOutOfBoundsException = java.lang.ArrayIndexOutOfBound
actual typealias Lock = ReentrantLock

internal actual fun newLock(): Lock = ReentrantLock()
internal actual inline fun Lock.destroy() = Unit

actual inline fun <T> Lock.withLock(action: () -> T): T {
contract {
Expand Down
52 changes: 52 additions & 0 deletions okio/src/mingwX64Main/kotlin/okio/WindowsPlatform.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2026 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okio

import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import platform.windows.CloseHandle
import platform.windows.CreateMutexA
import platform.windows.INFINITE
import platform.windows.ReleaseMutex
import platform.windows.WaitForSingleObject

actual class Lock : Closeable {
val mutex = CreateMutexA(
null,
0,
null
) ?: throw lastErrorToIOException()

override fun close() {
CloseHandle(mutex)
}
}

internal actual fun newLock(): Lock = Lock()
internal actual inline fun Lock.destroy() = close()

actual inline fun <T> Lock.withLock(action: () -> T): T {
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}

try {
WaitForSingleObject(mutex, INFINITE)
return action()
} finally {
ReleaseMutex(mutex)
}
}
18 changes: 0 additions & 18 deletions okio/src/nonJvmMain/kotlin/okio/NonJvmPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package okio

import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import okio.internal.commonAsUtf8ToByteArray
import okio.internal.commonToUtf8String

Expand All @@ -31,22 +29,6 @@ actual open class ArrayIndexOutOfBoundsException actual constructor(
message: String?,
) : IndexOutOfBoundsException(message)

actual class Lock {
companion object {
val instance = Lock()
}
}

internal actual fun newLock(): Lock = Lock.instance

actual inline fun <T> Lock.withLock(action: () -> T): T {
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}

return action()
}

actual open class IOException actual constructor(
message: String?,
cause: Throwable?,
Expand Down
58 changes: 58 additions & 0 deletions okio/src/unixMain/kotlin/okio/UnixPlatform.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2026 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okio

import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlinx.cinterop.alloc
import kotlinx.cinterop.free
import kotlinx.cinterop.nativeHeap
import kotlinx.cinterop.ptr
import platform.posix.errno
import platform.posix.pthread_mutex_destroy
import platform.posix.pthread_mutex_init
import platform.posix.pthread_mutex_lock
import platform.posix.pthread_mutex_t
import platform.posix.pthread_mutex_unlock

actual class Lock : Closeable {
val mutex = nativeHeap.alloc<pthread_mutex_t>().apply {
if (pthread_mutex_init(ptr, null) != 0) {
throw errnoToIOException(errno)
}
}

override fun close() {
pthread_mutex_destroy(mutex.ptr)
nativeHeap.free(mutex)
}
}

internal actual fun newLock(): Lock = Lock()
internal actual inline fun Lock.destroy() = close()

actual inline fun <T> Lock.withLock(action: () -> T): T {
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}

try {
pthread_mutex_lock(mutex.ptr)
return action()
} finally {
pthread_mutex_unlock(mutex.ptr)
}
}
18 changes: 18 additions & 0 deletions okio/src/wasmMain/kotlin/okio/WasmPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,23 @@
*/
package okio

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

internal actual val PLATFORM_DIRECTORY_SEPARATOR: String
get() = "/"

actual typealias Lock = Unit

internal actual fun newLock(): Lock = Unit
internal actual inline fun Lock.destroy() = Unit

@OptIn(ExperimentalContracts::class)
actual inline fun <T> Lock.withLock(action: () -> T): T {
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}

return action()
}