Scoop API

scoop:scoop.asyncio:0.1.0

platform/darwin/main_dispatch_host.scoop

Selected production source · platform/darwin/main_dispatch_host.scoop

1package scoop.asyncio.host
3import scoop.asyncio.NativeCreated
4import scoop.asyncio.backendFailure
5import scoop.platform.io.standardError
6import scoop.platform.io.writeLine
8/** Uses the main dispatch queue served by the application's AppKit or CFRunLoop host. */
9public class MainDispatchHost public constructor(): EventLoopHost {
10 public override fun isCurrentThread(): Boolean {
11 @Unsafe
12 {
13 return nativeMainThread()
14 }
15 }
17 @Unsafe
18 public override fun watch(
19 notification: EventLoopNotification,
20 ready: () -> Unit
21 ): 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) -> value
27 case WindowsEvent(_) -> throw IllegalArgumentException(
28 Some("main dispatch requires a descriptor")
29 )
30 }
31 val watch = MainDispatchWatch(ready)
32 watch.install(descriptor)
33 return watch
34 }
37private class MainDispatchWatch(private val ready: () -> Unit): EventLoopWatch {
38 private var pointer: Ptr<Unit>? = None
39 private var notification: ForeignCallback<(Ptr<Unit>) -> Int>? = None
40 private var observer: ForeignCallback<(Ptr<Unit>, Int) -> Int>? = None
41 private var removal: ForeignCallback<(Ptr<Unit>) -> Int>? = None
42 private var removing: Boolean = false
44 @Unsafe
45 fun install(descriptor: Int) {
46 val notification =
47 foreignCallback<(Ptr<Unit>) -> Int>(
48 {
49 ready();
50 0
51 },
52 0,
53 ForeignCallbackMode.Reusable
54 )
55 val observer =
56 foreignCallback<(Ptr<Unit>, Int) -> Int>(
57 {
58 stage: Int ->
59 observe(stage);
60 0
61 },
62 0,
63 ForeignCallbackMode.Reusable
64 )
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.context
74 )
75 if (created.status.code != 0) {
76 releaseForeignCallback(notification)
77 releaseForeignCallback(observer)
78 this.notification = None
79 this.observer = None
80 throw backendFailure("install main dispatch watch", created.status)
81 }
82 pointer = created.pointer
83 }
85 public override fun remove(onRemoved: () -> Unit) {
86 if (removing)
87 return
88 removing = true
89 @Unsafe
90 {
91 val callback =
92 foreignCallback<(Ptr<Unit>) -> Int>(
93 {
94 pointer = None;
95 onRemoved();
96 0
97 },
98 0,
99 ForeignCallbackMode.OneShot
100 )
101 removal = Some(retainForeignCallback(callback))
102 nativeMainRemove(pointer!!, callback.function, callback.context)
103 }
104 }
106 private fun observe(stage: Int) {
107 @Unsafe
108 {
109 val callback =
110 if (stage == 0)
111 notification!!
112 else
113 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 = None
124 notification = None
125 observer = None
126 }
127 }
128 }
130 private fun report(error: Throwable) {
131 try {
132 val message =
133 if (error is Exception)
134 error.message ?: "callback failed"
135 else
136 "callback failed"
137 standardError.writeLine("scoop.asyncio MainDispatchHost: " + message)
138 } catch (_: Throwable) {}
139 }
142@Extern(name = "scoop_io_main_thread")
143private fun nativeMainThread(): Boolean
145@Extern(name = "scoop_io_main_watch")
146private 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): NativeCreated
154@Extern(name = "scoop_io_main_remove")
155private fun nativeMainRemove(
156 watch: Ptr<Unit>,
157 removed: FunPtr<(Ptr<Unit>) -> Int>,
158 context: Ptr<Unit>