Skip to content

Commit b19c23f

Browse files
authored
ReentrantLock expect class should declare a default constructor (#411)
Fixes #401
1 parent 387c3db commit b19c23f

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
/*
2+
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
15
package kotlinx.atomicfu.locks
26

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

59
public expect fun reentrantLock(): ReentrantLock
610

7-
public expect class ReentrantLock {
11+
public expect class ReentrantLock() {
812
fun lock(): Unit
913
fun tryLock(): Boolean
1014
fun unlock(): Unit
1115
}
1216

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

15-
public expect inline fun <T> synchronized(lock: SynchronizedObject, block: () -> T): T
19+
public expect inline fun <T> synchronized(lock: SynchronizedObject, block: () -> T): T
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1+
/*
2+
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
15
package bytecode_test
26

37
import kotlinx.atomicfu.locks.*
48
import kotlin.test.*
59

610
class ReentrantLockTest {
7-
private val lock = reentrantLock()
11+
private val lock_constructor = ReentrantLock()
12+
private val lock_factory = reentrantLock()
813
private var state = 0
914

1015
@Test
1116
fun testLockField() {
12-
lock.withLock {
17+
lock_constructor.withLock {
18+
state = 6
19+
}
20+
assertEquals(6, state)
21+
lock_factory.withLock {
1322
state = 1
1423
}
1524
assertEquals(1, state)
1625
}
17-
}
26+
}

integration-testing/examples/mpp-sample/src/commonMain/kotlin/AtomicSampleClass.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package examples.mpp_sample
@@ -19,9 +19,17 @@ public class AtomicSampleClass {
1919
assertTrue(_x.compareAndSet(3, finalValue))
2020
}
2121

22-
private val lock = reentrantLock()
22+
private val lock_factory = reentrantLock()
2323

24-
public fun synchronizedFoo(value: Int): Int {
25-
return lock.withLock { value }
24+
private val lock_cons = ReentrantLock()
25+
26+
private var state: Int = 0
27+
28+
public fun synchronizedSetState(value: Int): Int {
29+
lock_cons.withLock { state = 0 }
30+
assertEquals(0, state)
31+
lock_factory.withLock { state = value }
32+
assertEquals(value, state)
33+
return state
2634
}
2735
}

integration-testing/examples/mpp-sample/src/commonTest/kotlin/AtomicSampleTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
import kotlin.test.*
@@ -12,6 +12,6 @@ class AtomicSampleTest {
1212
val a = AtomicSampleClass()
1313
a.doWork(1234)
1414
assertEquals(1234, a.x)
15-
assertEquals(42, a.synchronizedFoo(42))
15+
assertEquals(42, a.synchronizedSetState(42))
1616
}
1717
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
/*
2+
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
15
import examples.mpp_sample.*
26
import kotlin.test.*
37

48
fun doWorld() {
59
val sampleClass = AtomicSampleClass()
610
sampleClass.doWork(1234)
711
assertEquals(1234, sampleClass.x)
8-
assertEquals(42, sampleClass.synchronizedFoo(42))
12+
assertEquals(42, sampleClass.synchronizedSetState(42))
913
}

0 commit comments

Comments
 (0)