Skip to content

Commit 6be021e

Browse files
committed
Hide reentrant lock from jvm SynchronousMutex.
Add comment to explain interruptibility for tryLock(timeout) on jvm.
1 parent 3808584 commit 6be021e

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

atomicfu/src/commonMain/kotlin/kotlinx/atomicfu/locks/SynchronousMutex.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ expect class SynchronousMutex() {
3535
* It is recommended to use [withLock] for safety reasons, so that the acquired lock is always
3636
* released at the end of your critical section, and [unlock] is never invoked before a successful
3737
* lock acquisition.
38+
*
39+
* (JVM only) this call can potentially skip line.
3840
*/
3941
fun tryLock(): Boolean
4042

@@ -44,6 +46,8 @@ expect class SynchronousMutex() {
4446
*
4547
* Note: when [tryLock] succeeds the lock needs to be released by [unlock].
4648
* When [tryLock] does not succeed the lock does not have to be released.
49+
*
50+
* (JVM only) throws Interrupted exception when thread is interrupted while waiting for lock.
4751
*/
4852
fun tryLock(timeout: Duration): Boolean
4953

atomicfu/src/jvmMain/kotlin/kotlinx/atomicfu/locks/SynchronousMutex.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,12 @@ import kotlin.time.Duration
66
/**
77
* This mutex uses a [ReentrantLock].
88
*
9-
* [getReentrantLock] obtains the actual [ReentrantLock].
109
* Construct with `Mutex(reentrantLock)` to create a [SynchronousMutex] that uses an existing instance of [ReentrantLock].
1110
*/
12-
actual class SynchronousMutex(private val reentrantLock: java.util.concurrent.locks.ReentrantLock) {
13-
actual constructor(): this(ReentrantLock())
11+
actual class SynchronousMutex {
12+
private val reentrantLock = ReentrantLock()
1413
actual fun tryLock(timeout: Duration): Boolean = reentrantLock.tryLock(timeout.inWholeNanoseconds, TimeUnit.NANOSECONDS)
1514
actual fun tryLock(): Boolean = reentrantLock.tryLock()
1615
actual fun lock() = reentrantLock.lock()
1716
actual fun unlock() = reentrantLock.unlock()
18-
19-
/**
20-
* @return the underlying [ReentrantLock]
21-
*/
22-
fun getReentrantLock(): ReentrantLock = reentrantLock
2317
}

0 commit comments

Comments
 (0)