File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ expect class Lock
3131expect inline fun <T > Lock.withLock (action : () -> T ): T
3232
3333internal expect fun newLock (): Lock
34+ internal expect inline fun Lock.destroy ()
3435
3536expect open class IOException (message : String? , cause : Throwable ? ) : Exception {
3637 constructor (message: String? )
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -57,6 +57,7 @@ internal val tmpdir: String
5757actual typealias Lock = Unit
5858
5959internal actual fun newLock (): Lock = Unit
60+ internal actual inline fun Lock.destroy () = Unit
6061
6162actual inline fun <T > Lock.withLock (action : () -> T ): T {
6263 contract {
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ actual typealias ArrayIndexOutOfBoundsException = java.lang.ArrayIndexOutOfBound
3131actual typealias Lock = ReentrantLock
3232
3333internal actual fun newLock (): Lock = ReentrantLock ()
34+ internal actual inline fun Lock.destroy () = Unit
3435
3536actual inline fun <T > Lock.withLock (action : () -> T ): T {
3637 contract {
Original file line number Diff line number Diff line change @@ -17,29 +17,26 @@ package okio
1717
1818import kotlin.contracts.InvocationKind
1919import kotlin.contracts.contract
20- import kotlin.experimental.ExperimentalNativeApi
21- import kotlin.native.ref.createCleaner
2220import platform.windows.CloseHandle
2321import platform.windows.CreateMutexA
2422import platform.windows.INFINITE
2523import platform.windows.ReleaseMutex
2624import 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
4238internal actual fun newLock (): Lock = Lock ()
39+ internal actual inline fun Lock.destroy () = close()
4340
4441actual inline fun <T > Lock.withLock (action : () -> T ): T {
4542 contract {
Original file line number Diff line number Diff line change @@ -17,8 +17,6 @@ package okio
1717
1818import kotlin.contracts.InvocationKind
1919import kotlin.contracts.contract
20- import kotlin.experimental.ExperimentalNativeApi
21- import kotlin.native.ref.createCleaner
2220import kotlinx.cinterop.alloc
2321import kotlinx.cinterop.free
2422import kotlinx.cinterop.nativeHeap
@@ -30,22 +28,21 @@ import platform.posix.pthread_mutex_lock
3028import platform.posix.pthread_mutex_t
3129import 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
4844internal actual fun newLock (): Lock = Lock ()
45+ internal actual inline fun Lock.destroy () = close()
4946
5047actual inline fun <T > Lock.withLock (action : () -> T ): T {
5148 contract {
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ internal actual val PLATFORM_DIRECTORY_SEPARATOR: String
2525actual typealias Lock = Unit
2626
2727internal actual fun newLock (): Lock = Unit
28+ internal actual inline fun Lock.destroy () = Unit
2829
2930@OptIn(ExperimentalContracts ::class )
3031actual inline fun <T > Lock.withLock (action : () -> T ): T {
You can’t perform that action at this time.
0 commit comments