Skip to content

Commit

Permalink
ReentrantLock expect class should declare a default constructor (#411)
Browse files Browse the repository at this point in the history
Fixes #401
  • Loading branch information
mvicsokolova authored Mar 25, 2024
1 parent 387c3db commit b19c23f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
/*
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.atomicfu.locks

public expect open class SynchronizedObject() // marker abstract class

public expect fun reentrantLock(): ReentrantLock

public expect class ReentrantLock {
public expect class ReentrantLock() {
fun lock(): Unit
fun tryLock(): Boolean
fun unlock(): Unit
}

public expect inline fun <T> ReentrantLock.withLock(block: () -> T): T

public expect inline fun <T> synchronized(lock: SynchronizedObject, block: () -> T): T
public expect inline fun <T> synchronized(lock: SynchronizedObject, block: () -> T): T
15 changes: 12 additions & 3 deletions atomicfu/src/commonTest/kotlin/bytecode_test/ReentrantLockTest.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
/*
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package bytecode_test

import kotlinx.atomicfu.locks.*
import kotlin.test.*

class ReentrantLockTest {
private val lock = reentrantLock()
private val lock_constructor = ReentrantLock()
private val lock_factory = reentrantLock()
private var state = 0

@Test
fun testLockField() {
lock.withLock {
lock_constructor.withLock {
state = 6
}
assertEquals(6, state)
lock_factory.withLock {
state = 1
}
assertEquals(1, state)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package examples.mpp_sample
Expand All @@ -19,9 +19,17 @@ public class AtomicSampleClass {
assertTrue(_x.compareAndSet(3, finalValue))
}

private val lock = reentrantLock()
private val lock_factory = reentrantLock()

public fun synchronizedFoo(value: Int): Int {
return lock.withLock { value }
private val lock_cons = ReentrantLock()

private var state: Int = 0

public fun synchronizedSetState(value: Int): Int {
lock_cons.withLock { state = 0 }
assertEquals(0, state)
lock_factory.withLock { state = value }
assertEquals(value, state)
return state
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

import kotlin.test.*
Expand All @@ -12,6 +12,6 @@ class AtomicSampleTest {
val a = AtomicSampleClass()
a.doWork(1234)
assertEquals(1234, a.x)
assertEquals(42, a.synchronizedFoo(42))
assertEquals(42, a.synchronizedSetState(42))
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/*
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

import examples.mpp_sample.*
import kotlin.test.*

fun doWorld() {
val sampleClass = AtomicSampleClass()
sampleClass.doWork(1234)
assertEquals(1234, sampleClass.x)
assertEquals(42, sampleClass.synchronizedFoo(42))
assertEquals(42, sampleClass.synchronizedSetState(42))
}

0 comments on commit b19c23f

Please sign in to comment.