Skip to content

Commit f56aae2

Browse files
committed
Don't use createCleaner for native Lock.
1 parent 6450cde commit f56aae2

7 files changed

Lines changed: 13 additions & 14 deletions

File tree

okio/src/commonMain/kotlin/okio/CommonPlatform.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ expect class Lock
3131
expect inline fun <T> Lock.withLock(action: () -> T): T
3232

3333
internal expect fun newLock(): Lock
34+
internal expect inline fun Lock.destroy()
3435

3536
expect open class IOException(message: String?, cause: Throwable?) : Exception {
3637
constructor(message: String?)

okio/src/commonMain/kotlin/okio/FileHandle.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ abstract class FileHandle(
289289
closed = true
290290
if (openStreamCount != 0) return
291291
}
292+
lock.destroy()
292293
protectedClose()
293294
}
294295

okio/src/jsMain/kotlin/okio/JsPlatform.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ internal val tmpdir: String
5757
actual typealias Lock = Unit
5858

5959
internal actual fun newLock(): Lock = Unit
60+
internal actual inline fun Lock.destroy() = Unit
6061

6162
actual inline fun <T> Lock.withLock(action: () -> T): T {
6263
contract {

okio/src/jvmMain/kotlin/okio/-JvmPlatform.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ actual typealias ArrayIndexOutOfBoundsException = java.lang.ArrayIndexOutOfBound
3131
actual typealias Lock = ReentrantLock
3232

3333
internal actual fun newLock(): Lock = ReentrantLock()
34+
internal actual inline fun Lock.destroy() = Unit
3435

3536
actual inline fun <T> Lock.withLock(action: () -> T): T {
3637
contract {

okio/src/mingwX64Main/kotlin/okio/WindowsPlatform.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,26 @@ package okio
1717

1818
import kotlin.contracts.InvocationKind
1919
import kotlin.contracts.contract
20-
import kotlin.experimental.ExperimentalNativeApi
21-
import kotlin.native.ref.createCleaner
2220
import platform.windows.CloseHandle
2321
import platform.windows.CreateMutexA
2422
import platform.windows.INFINITE
2523
import platform.windows.ReleaseMutex
2624
import platform.windows.WaitForSingleObject
2725

28-
actual class Lock {
26+
actual class Lock : Closeable {
2927
val mutex = CreateMutexA(
3028
null,
3129
0,
3230
null
3331
) ?: throw lastErrorToIOException()
3432

35-
@Suppress("unused")
36-
@OptIn(ExperimentalNativeApi::class)
37-
private val cleaner = createCleaner(mutex) {
33+
override fun close() {
3834
CloseHandle(mutex)
3935
}
4036
}
4137

4238
internal actual fun newLock(): Lock = Lock()
39+
internal actual inline fun Lock.destroy() = close()
4340

4441
actual inline fun <T> Lock.withLock(action: () -> T): T {
4542
contract {

okio/src/unixMain/kotlin/okio/UnixPlatform.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ package okio
1717

1818
import kotlin.contracts.InvocationKind
1919
import kotlin.contracts.contract
20-
import kotlin.experimental.ExperimentalNativeApi
21-
import kotlin.native.ref.createCleaner
2220
import kotlinx.cinterop.alloc
2321
import kotlinx.cinterop.free
2422
import kotlinx.cinterop.nativeHeap
@@ -30,22 +28,21 @@ import platform.posix.pthread_mutex_lock
3028
import platform.posix.pthread_mutex_t
3129
import platform.posix.pthread_mutex_unlock
3230

33-
actual class Lock {
31+
actual class Lock : Closeable {
3432
val mutex = nativeHeap.alloc<pthread_mutex_t>().apply {
3533
if (pthread_mutex_init(ptr, null) != 0) {
3634
throw errnoToIOException(errno)
3735
}
3836
}
3937

40-
@Suppress("unused")
41-
@OptIn(ExperimentalNativeApi::class)
42-
private val cleaner = createCleaner(mutex) {
43-
pthread_mutex_destroy(it.ptr)
44-
nativeHeap.free(it)
38+
override fun close() {
39+
pthread_mutex_destroy(mutex.ptr)
40+
nativeHeap.free(mutex)
4541
}
4642
}
4743

4844
internal actual fun newLock(): Lock = Lock()
45+
internal actual inline fun Lock.destroy() = close()
4946

5047
actual inline fun <T> Lock.withLock(action: () -> T): T {
5148
contract {

okio/src/wasmMain/kotlin/okio/WasmPlatform.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ internal actual val PLATFORM_DIRECTORY_SEPARATOR: String
2525
actual typealias Lock = Unit
2626

2727
internal actual fun newLock(): Lock = Unit
28+
internal actual inline fun Lock.destroy() = Unit
2829

2930
@OptIn(ExperimentalContracts::class)
3031
actual inline fun <T> Lock.withLock(action: () -> T): T {

0 commit comments

Comments
 (0)