Skip to content

Commit 519e59d

Browse files
committed
Wait for parking handle to be published
1 parent a3e958a commit 519e59d

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/ThreadParkerTest.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,44 @@ class ThreadParkerTest {
1616
ParkingSupport.park(Duration.INFINITE)
1717
}
1818

19-
// Allow thread to be parked before park call
19+
// Allow thread to be parked before unpark call
2020
sleepMillis(100)
2121
ParkingSupport.unpark(parkingHandle!!)
2222

2323
f.waitThrowing()
2424
}
25-
2625

2726
@Test
2827
fun unparkPark() {
29-
var parkingHandle: ParkingHandle? = null
28+
val atomicHandle = AtomicParkingHandle()
3029
val unparked = AtomicBoolHolder(false)
3130

3231
val f = Fut {
33-
parkingHandle = ParkingSupport.currentThreadHandle()
32+
atomicHandle.atomicHandle.value = ParkingSupport.currentThreadHandle()
3433

3534
while (!unparked.atomicBool.value) {
3635
sleepMillis(10)
3736
}
3837

3938
ParkingSupport.park(Duration.INFINITE)
4039
}
41-
42-
// allow testThread to start and publish handle
43-
sleepMillis(100)
4440

45-
ParkingSupport.unpark(parkingHandle!!)
41+
while (atomicHandle.atomicHandle.value == null) {
42+
sleepMillis(10)
43+
}
44+
45+
ParkingSupport.unpark(atomicHandle.atomicHandle.value!!)
4646
unparked.atomicBool.value = true
4747

4848
f.waitThrowing()
4949

5050
}
51-
51+
52+
// Needs to be in class otherwise build complains having an atomic outside of constructor
53+
private class AtomicParkingHandle() {
54+
val atomicHandle = atomic<ParkingHandle?>(null)
55+
}
56+
5257
// Needs to be in class otherwise build complains having an atomic outside of constructor
5358
private class AtomicBoolHolder(initial: Boolean) {
5459
val atomicBool = atomic(initial)

0 commit comments

Comments
 (0)