|
| 1 | +@file:OptIn(UnsafeWasmMemoryApi::class) |
| 2 | +package kotlinx.coroutines |
| 3 | + |
| 4 | +import kotlin.coroutines.CoroutineContext |
| 5 | +import kotlin.wasm.* |
| 6 | +import kotlin.wasm.unsafe.* |
| 7 | + |
| 8 | +@WasmImport("wasi_snapshot_preview1", "poll_oneoff") |
| 9 | +private external fun wasiPollOneOff(ptrToSubscription: Int, eventPtr: Int, nsubscriptions: Int, resultPtr: Int): Int |
| 10 | + |
| 11 | +@WasmImport("wasi_snapshot_preview1", "clock_time_get") |
| 12 | +private external fun wasiRawClockTimeGet(clockId: Int, precision: Long, resultPtr: Int): Int |
| 13 | + |
| 14 | +private const val CLOCKID_MONOTONIC = 1 |
| 15 | + |
| 16 | +internal actual fun createEventLoop(): EventLoop = DefaultExecutor |
| 17 | + |
| 18 | +internal actual fun nanoTime(): Long = withScopedMemoryAllocator { allocator: MemoryAllocator -> |
| 19 | + val ptrTo8Bytes = allocator.allocate(8) |
| 20 | + val returnCode = wasiRawClockTimeGet( |
| 21 | + clockId = CLOCKID_MONOTONIC, |
| 22 | + precision = 1, |
| 23 | + resultPtr = ptrTo8Bytes.address.toInt() |
| 24 | + ) |
| 25 | + check(returnCode == 0) { "clock_time_get failed with the return code $returnCode" } |
| 26 | + ptrTo8Bytes.loadLong() |
| 27 | +} |
| 28 | + |
| 29 | +private fun sleep(nanos: Long, ptrTo32Bytes: Pointer, ptrTo8Bytes: Pointer, ptrToSubscription: Pointer) { |
| 30 | + //__wasi_timestamp_t timeout; |
| 31 | + (ptrToSubscription + 24).storeLong(nanos) |
| 32 | + val returnCode = wasiPollOneOff( |
| 33 | + ptrToSubscription = ptrToSubscription.address.toInt(), |
| 34 | + eventPtr = ptrTo32Bytes.address.toInt(), |
| 35 | + nsubscriptions = 1, |
| 36 | + resultPtr = ptrTo8Bytes.address.toInt() |
| 37 | + ) |
| 38 | + check(returnCode == 0) { "poll_oneoff failed with the return code $returnCode" } |
| 39 | +} |
| 40 | + |
| 41 | +internal actual object DefaultExecutor : EventLoopImplBase() { |
| 42 | + override fun shutdown() { |
| 43 | + // don't do anything: on WASI, the event loop is the default executor, we can't shut it down |
| 44 | + } |
| 45 | + |
| 46 | + override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle = |
| 47 | + scheduleInvokeOnTimeout(timeMillis, block) |
| 48 | + |
| 49 | + actual override fun enqueue(task: Runnable) { |
| 50 | + if (kotlin.wasm.internal.onExportedFunctionExit == null) { |
| 51 | + kotlin.wasm.internal.onExportedFunctionExit = ::runEventLoop |
| 52 | + } |
| 53 | + super.enqueue(task) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +internal actual abstract class EventLoopImplPlatform : EventLoop() { |
| 58 | + protected actual fun unpark() { |
| 59 | + // do nothing: in WASI, no external callbacks can be invoked while `poll_oneoff` is running, |
| 60 | + // so it is both impossible and unnecessary to unpark the event loop |
| 61 | + } |
| 62 | + |
| 63 | + protected actual fun reschedule(now: Long, delayedTask: EventLoopImplBase.DelayedTask) { |
| 64 | + // throw; on WASI, the event loop is the default executor, we can't shut it down or reschedule tasks |
| 65 | + // to anyone else |
| 66 | + throw UnsupportedOperationException("runBlocking event loop is not supported") |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +internal actual inline fun platformAutoreleasePool(crossinline block: () -> Unit) = block() |
| 71 | + |
| 72 | +internal fun runEventLoop() { |
| 73 | + withScopedMemoryAllocator { allocator -> |
| 74 | + val ptrToSubscription = initializeSubscriptionPtr(allocator) |
| 75 | + val ptrTo32Bytes = allocator.allocate(32) |
| 76 | + val ptrTo8Bytes = allocator.allocate(8) |
| 77 | + val eventLoop = DefaultExecutor |
| 78 | + eventLoop.incrementUseCount() |
| 79 | + try { |
| 80 | + while (true) { |
| 81 | + val parkNanos = eventLoop.processNextEvent() |
| 82 | + if (parkNanos == Long.MAX_VALUE) { |
| 83 | + // no more events |
| 84 | + break |
| 85 | + } |
| 86 | + if (parkNanos > 0) { |
| 87 | + // sleep until the next event |
| 88 | + sleep( |
| 89 | + parkNanos, |
| 90 | + ptrTo32Bytes = ptrTo32Bytes, |
| 91 | + ptrTo8Bytes = ptrTo8Bytes, |
| 92 | + ptrToSubscription = ptrToSubscription |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | + } finally { // paranoia |
| 97 | + eventLoop.decrementUseCount() |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +private fun initializeSubscriptionPtr(allocator: MemoryAllocator): Pointer { |
| 103 | + val ptrToSubscription = allocator.allocate(48) |
| 104 | + //userdata |
| 105 | + ptrToSubscription.storeLong(0) |
| 106 | + //uint8_t tag; |
| 107 | + (ptrToSubscription + 8).storeByte(0) //EVENTTYPE_CLOCK |
| 108 | + //__wasi_clockid_t id; |
| 109 | + (ptrToSubscription + 16).storeInt(CLOCKID_MONOTONIC) //CLOCKID_MONOTONIC |
| 110 | + //__wasi_timestamp_t timeout; |
| 111 | + //(ptrToSubscription + 24).storeLong(timeout) |
| 112 | + //__wasi_timestamp_t precision; |
| 113 | + (ptrToSubscription + 32).storeLong(0) |
| 114 | + //__wasi_subclockflags_t |
| 115 | + (ptrToSubscription + 40).storeShort(0) //ABSOLUTE_TIME=1/RELATIVE=0 |
| 116 | + |
| 117 | + return ptrToSubscription |
| 118 | +} |
| 119 | + |
| 120 | +internal actual fun createDefaultDispatcher(): CoroutineDispatcher = DefaultExecutor |
0 commit comments