scoop:scoop.asyncio:0.1.0
platform/darwin/main_dispatch_host.scoop
Selected production source · platform/darwin/main_dispatch_host.scoop
1
package scoop.asyncio.host3
import scoop.asyncio.NativeCreated4
import scoop.asyncio.backendFailure5
import scoop.platform.io.standardError6
import scoop.platform.io.writeLine8
/** Uses the main dispatch queue served by the application's AppKit or CFRunLoop host. */9
public class MainDispatchHost public constructor(): EventLoopHost {10
public override fun isCurrentThread(): Boolean {11
@Unsafe12
{13
return nativeMainThread()14
}15
}17
@Unsafe18
public override fun watch(19
notification: EventLoopNotification,20
ready: () -> Unit21
): EventLoopWatch {22
if (!isCurrentThread())23
throw IllegalStateException(Some("main dispatch watch requires the main thread"))24
val descriptor =25
when (notification) {26
case ReadableDescriptor(val value) -> value27
case WindowsEvent(_) -> throw IllegalArgumentException(28
Some("main dispatch requires a descriptor")29
)30
}31
val watch = MainDispatchWatch(ready)32
watch.install(descriptor)33
return watch34
}35
}37
private class MainDispatchWatch(private val ready: () -> Unit): EventLoopWatch {38
private var pointer: Ptr<Unit>? = None39
private var notification: ForeignCallback<(Ptr<Unit>) -> Int>? = None40
private var observer: ForeignCallback<(Ptr<Unit>, Int) -> Int>? = None41
private var removal: ForeignCallback<(Ptr<Unit>) -> Int>? = None42
private var removing: Boolean = false44
@Unsafe45
fun install(descriptor: Int) {46
val notification =47
foreignCallback<(Ptr<Unit>) -> Int>(48
{49
ready();50
051
},52
0,53
ForeignCallbackMode.Reusable54
)55
val observer =56
foreignCallback<(Ptr<Unit>, Int) -> Int>(57
{58
stage: Int ->59
observe(stage);60
061
},62
0,63
ForeignCallbackMode.Reusable64
)65
this.notification = Some(notification)66
this.observer = Some(observer)67
val created =68
nativeMainWatch(69
descriptor,70
notification.function,71
notification.context,72
observer.function,73
observer.context74
)75
if (created.status.code != 0) {76
releaseForeignCallback(notification)77
releaseForeignCallback(observer)78
this.notification = None79
this.observer = None80
throw backendFailure("install main dispatch watch", created.status)81
}82
pointer = created.pointer83
}85
public override fun remove(onRemoved: () -> Unit) {86
if (removing)87
return88
removing = true89
@Unsafe90
{91
val callback =92
foreignCallback<(Ptr<Unit>) -> Int>(93
{94
pointer = None;95
onRemoved();96
097
},98
0,99
ForeignCallbackMode.OneShot100
)101
removal = Some(retainForeignCallback(callback))102
nativeMainRemove(pointer!!, callback.function, callback.context)103
}104
}106
private fun observe(stage: Int) {107
@Unsafe108
{109
val callback =110
if (stage == 0)111
notification!!112
else113
removal!!114
when (foreignCallbackFailure(callback)) {115
case Some(val error) -> report(error)116
case None -> {}117
}118
if (stage == 1) {119
releaseForeignCallback(removal!!)120
releaseForeignCallback(notification!!)121
// This observer invocation owns a lease until the native bridge returns.122
releaseForeignCallback(observer!!)123
removal = None124
notification = None125
observer = None126
}127
}128
}130
private fun report(error: Throwable) {131
try {132
val message =133
if (error is Exception)134
error.message ?: "callback failed"135
else136
"callback failed"137
standardError.writeLine("scoop.asyncio MainDispatchHost: " + message)138
} catch (_: Throwable) {}139
}140
}142
@Extern(name = "scoop_io_main_thread")143
private fun nativeMainThread(): Boolean145
@Extern(name = "scoop_io_main_watch")146
private fun nativeMainWatch(147
descriptor: Int,148
ready: FunPtr<(Ptr<Unit>) -> Int>,149
readyContext: Ptr<Unit>,150
observer: FunPtr<(Ptr<Unit>, Int) -> Int>,151
observerContext: Ptr<Unit>152
): NativeCreated154
@Extern(name = "scoop_io_main_remove")155
private fun nativeMainRemove(156
watch: Ptr<Unit>,157
removed: FunPtr<(Ptr<Unit>) -> Int>,158
context: Ptr<Unit>159
)