File tree Expand file tree Collapse file tree 2 files changed +23
-7
lines changed
kotlinx-coroutines-core/native Expand file tree Collapse file tree 2 files changed +23
-7
lines changed Original file line number Diff line number Diff line change 4
4
5
5
package kotlinx.coroutines
6
6
7
+ import kotlinx.cinterop.*
8
+ import platform.posix.*
7
9
import kotlin.coroutines.*
8
10
9
11
/* *
@@ -56,21 +58,24 @@ private class BlockingCoroutine<T>(
56
58
get() = false // it throws exception to parent instead of cancelling it
57
59
58
60
@Suppress(" UNCHECKED_CAST" )
59
- fun joinBlocking (): T {
61
+ fun joinBlocking (): T = memScoped {
60
62
try {
61
63
eventLoop?.incrementUseCount()
64
+ val timespec = alloc< timespec> ()
62
65
while (true ) {
63
66
val parkNanos = eventLoop?.processNextEvent() ? : Long .MAX_VALUE
64
67
// note: process next even may loose unpark flag, so check if completed before parking
65
68
if (isCompleted) break
66
- // todo: LockSupport.parkNanos(this, parkNanos)
69
+ timespec.tv_sec = parkNanos / 1000000000L // 1e9 ns -> sec
70
+ timespec.tv_nsec = (parkNanos % 1000000000L ).convert() // % 1e9
71
+ nanosleep(timespec.ptr, null )
67
72
}
68
73
} finally { // paranoia
69
74
eventLoop?.decrementUseCount()
70
75
}
71
76
// now return result
72
- val state = this . state
77
+ val state = state
73
78
(state as ? CompletedExceptionally )?.let { throw it.cause }
74
- return state as T
79
+ state as T
75
80
}
76
81
}
Original file line number Diff line number Diff line change 5
5
package kotlinx.coroutines
6
6
7
7
import kotlin.coroutines.*
8
- import kotlin.test.Test
9
- import kotlin.test.assertTrue
8
+ import kotlin.test.*
10
9
11
- class DelayExceptionTest {
10
+ class DelayExceptionTest : TestBase () {
12
11
private object Dispatcher : CoroutineDispatcher() {
13
12
override fun isDispatchNeeded (context : CoroutineContext ): Boolean = true
14
13
override fun dispatch (context : CoroutineContext , block : Runnable ) { block.run () }
@@ -25,4 +24,16 @@ class DelayExceptionTest {
25
24
26
25
assertTrue(exception is IllegalStateException )
27
26
}
27
+
28
+ @Test
29
+ fun testMaxDelay () = runBlocking {
30
+ expect(1 )
31
+ val job = launch {
32
+ expect(2 )
33
+ delay(Long .MAX_VALUE )
34
+ }
35
+ yield ()
36
+ job.cancel()
37
+ finish(3 )
38
+ }
28
39
}
You can’t perform that action at this time.
0 commit comments