File tree Expand file tree Collapse file tree 5 files changed +37
-12
lines changed
commonMain/kotlin/kotlinx/atomicfu/locks
commonTest/kotlin/bytecode_test
integration-testing/examples
user-project/src/commonMain/kotlin Expand file tree Collapse file tree 5 files changed +37
-12
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
1
5
package kotlinx.atomicfu.locks
2
6
3
7
public expect open class SynchronizedObject () // marker abstract class
4
8
5
9
public expect fun reentrantLock (): ReentrantLock
6
10
7
- public expect class ReentrantLock {
11
+ public expect class ReentrantLock () {
8
12
fun lock (): Unit
9
13
fun tryLock (): Boolean
10
14
fun unlock (): Unit
11
15
}
12
16
13
17
public expect inline fun <T > ReentrantLock.withLock (block : () -> T ): T
14
18
15
- public expect inline fun <T > synchronized (lock : SynchronizedObject , block : () -> T ): T
19
+ public expect inline fun <T > synchronized (lock : SynchronizedObject , block : () -> T ): T
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
1
5
package bytecode_test
2
6
3
7
import kotlinx.atomicfu.locks.*
4
8
import kotlin.test.*
5
9
6
10
class ReentrantLockTest {
7
- private val lock = reentrantLock()
11
+ private val lock_constructor = ReentrantLock ()
12
+ private val lock_factory = reentrantLock()
8
13
private var state = 0
9
14
10
15
@Test
11
16
fun testLockField () {
12
- lock.withLock {
17
+ lock_constructor.withLock {
18
+ state = 6
19
+ }
20
+ assertEquals(6 , state)
21
+ lock_factory.withLock {
13
22
state = 1
14
23
}
15
24
assertEquals(1 , state)
16
25
}
17
- }
26
+ }
Original file line number Diff line number Diff line change 1
1
/*
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.
3
3
*/
4
4
5
5
package examples.mpp_sample
@@ -19,9 +19,17 @@ public class AtomicSampleClass {
19
19
assertTrue(_x .compareAndSet(3 , finalValue))
20
20
}
21
21
22
- private val lock = reentrantLock()
22
+ private val lock_factory = reentrantLock()
23
23
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
26
34
}
27
35
}
Original file line number Diff line number Diff line change 1
1
/*
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.
3
3
*/
4
4
5
5
import kotlin.test.*
@@ -12,6 +12,6 @@ class AtomicSampleTest {
12
12
val a = AtomicSampleClass ()
13
13
a.doWork(1234 )
14
14
assertEquals(1234 , a.x)
15
- assertEquals(42 , a.synchronizedFoo (42 ))
15
+ assertEquals(42 , a.synchronizedSetState (42 ))
16
16
}
17
17
}
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
1
5
import examples.mpp_sample.*
2
6
import kotlin.test.*
3
7
4
8
fun doWorld () {
5
9
val sampleClass = AtomicSampleClass ()
6
10
sampleClass.doWork(1234 )
7
11
assertEquals(1234 , sampleClass.x)
8
- assertEquals(42 , sampleClass.synchronizedFoo (42 ))
12
+ assertEquals(42 , sampleClass.synchronizedSetState (42 ))
9
13
}
You can’t perform that action at this time.
0 commit comments