File tree Expand file tree Collapse file tree 5 files changed +96
-3
lines changed
compose/runtime/runtime/src
jvmTest/kotlin/androidx/compose/runtime/internal
nativeTest/kotlin/androidx/compose/runtime/internal
nonEmulatorCommonTest/kotlin/androidx/compose/runtime
webTest/kotlin/androidx/compose/runtime/internal Expand file tree Collapse file tree 5 files changed +96
-3
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2025 The Android Open Source Project
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package androidx.compose.runtime.internal
18
+
19
+ actual fun isSleepAvailable () = true
20
+
21
+ actual fun sleep (millis : UInt ) =
22
+ Thread .sleep(millis.toLong())
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2025 The Android Open Source Project
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package androidx.compose.runtime.internal
18
+
19
+ actual fun isSleepAvailable () = true
20
+
21
+ actual fun sleep (millis : UInt ) {
22
+ platform.posix.usleep(millis)
23
+ }
Original file line number Diff line number Diff line change 17
17
package androidx.compose.runtime
18
18
19
19
import androidx.compose.runtime.internal.AtomicInt
20
+ import androidx.compose.runtime.internal.isSleepAvailable
21
+ import androidx.compose.runtime.internal.sleep
20
22
import kotlin.test.Test
21
23
import kotlin.test.assertEquals
22
24
import kotlin.test.assertFalse
23
25
import kotlin.test.assertTrue
26
+ import kotlin.time.Duration.Companion.milliseconds
24
27
import kotlinx.coroutines.CancellationException
25
28
import kotlinx.coroutines.CoroutineStart.UNDISPATCHED
26
29
import kotlinx.coroutines.Deferred
@@ -84,16 +87,18 @@ class BroadcastFrameClockTest {
84
87
}
85
88
86
89
@OptIn(InternalCoroutinesApi ::class )
87
- @Test(timeout = 5_000 )
88
- fun locklessCancellation () = runTest {
90
+ @Test
91
+ fun locklessCancellation () = runTest(timeout = 5_000 .milliseconds) {
92
+ if (! isSleepAvailable()) return @runTest
93
+
89
94
val clock = BroadcastFrameClock ()
90
95
val cancellationGate = AtomicInt (1 )
91
96
92
97
var spin = true
93
98
async(start = UNDISPATCHED ) {
94
99
clock.withFrameNanos {
95
100
cancellationGate.add(- 1 )
96
- @Suppress( " BanThreadSleep " ) while (spin) Thread . sleep(100 )
101
+ while (spin) sleep(100u )
97
102
}
98
103
}
99
104
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2025 The Android Open Source Project
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package androidx.compose.runtime.internal
18
+
19
+ internal expect fun isSleepAvailable (): Boolean
20
+
21
+ internal expect fun sleep (millis : UInt )
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2025 The Android Open Source Project
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package androidx.compose.runtime.internal
18
+
19
+ actual fun isSleepAvailable () = false
20
+
21
+ actual fun sleep (millis : UInt ): Unit =
22
+ throw UnsupportedOperationException (" Sleep is not supported" )
You can’t perform that action at this time.
0 commit comments