SafeLock

expect class SafeLock : AutoCloseable(source)

A Lock that can be closed safely even while another thread might still be using it.

close is only really meaningful on platforms whose underlying lock frees native resources (destroying one while another thread is locking or unlocking it is undefined behavior there). Regardless of platform, close guarantees that every lock owner already holding this at the time it is called can still safely finish, and that every call to lock made after it throws IllegalStateException.

actual class SafeLock : AutoCloseable(source)

Lock is backed by java.util.concurrent.locks.ReentrantLock on the JVM and Android. There is no native resource to free, so close() there is a no-op only flipping closed.

actual class SafeLock : AutoCloseable(source)

On Linux and mingw, Lock.close() calls pthread_mutex_destroy unconditionally. That is undefined behavior while another thread is still locking or unlocking it. Stately's Lock does not check for this, which caused test failures on Linux. Apple and mingw silently ignore the same misuse.

This tracks how many lock/unlock pairs are currently in flight. It only destroys the underlying lock once none are left. The last matching unlock after close does it.

actual class SafeLock : AutoCloseable(source)

JS and wasmJs are single-threaded. There is nothing to actually lock. Only the closed check remains meaningful.

Constructors

Link copied to clipboard
expect constructor()
constructor()
constructor()
constructor()

Properties

Link copied to clipboard
expect val isClosed: Boolean

Whether close has been called.

actual val isClosed: Boolean

Whether close has been called.

actual val isClosed: Boolean

Whether close has been called.

actual val isClosed: Boolean

Whether close has been called.

Functions

Link copied to clipboard
expect open override fun close()

Marks this lock closed. Calling this again has no effect.

actual open override fun close()

Marks this lock closed. Calling this again has no effect.

actual open override fun close()

Marks this lock closed. Calling this again has no effect.

actual open override fun close()

Marks this lock closed. Calling this again has no effect.

Link copied to clipboard
expect fun lock()

Acquires this lock, blocking until it is available.

actual fun lock()

Acquires this lock, blocking until it is available.

actual fun lock()

Acquires this lock, blocking until it is available.

actual fun lock()

Acquires this lock, blocking until it is available.

Link copied to clipboard
expect fun unlock()

Releases this lock. Must be called exactly once for every successful call to lock.

actual fun unlock()

Releases this lock. Must be called exactly once for every successful call to lock.

actual fun unlock()

Releases this lock. Must be called exactly once for every successful call to lock.

actual fun unlock()

Releases this lock. Must be called exactly once for every successful call to lock.

Link copied to clipboard
inline fun <T> SafeLock.withLock(block: () -> T): T

Runs block with this lock held.