Skip to content

Commit 81e1166

Browse files
committed
Update AndroidMainActor
1 parent 9006e56 commit 81e1166

File tree

2 files changed

+35
-55
lines changed

2 files changed

+35
-55
lines changed

Sources/AndroidChoreographer/AndroidChoreographer.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import Android
33
import AndroidNDK
44
import AndroidLogging
5+
import CoreFoundation
56

67
let logger = Logger(subsystem: "AndroidChoreographer", category: "AndroidChoreographer")
78

@@ -33,9 +34,9 @@ public final class AndroidChoreographer : @unchecked Sendable {
3334
}
3435
}
3536

36-
// public func postFrameCallback(_ callback: @convention(c)(Int64, UnsafeMutableRawPointer?) -> ()) {
37-
// AChoreographer_postFrameCallback64(_choreographer, callback, nil)
38-
// }
37+
public func postFrameCallback(_ callback: @convention(c)(Int64, UnsafeMutableRawPointer?) -> ()) {
38+
AChoreographer_postFrameCallback64(_choreographer, callback, nil)
39+
}
3940
}
4041

4142
// no longer used: we use the AndroidLooper instead, which will be more efficient than trying to drain the main queue on every frame render
@@ -47,12 +48,15 @@ public final class AndroidChoreographer : @unchecked Sendable {
4748
//// C-compatible callback wrapper
4849
//private var choreographerCallback: AChoreographer_frameCallback64 = { _, _ in
4950
// // Drain the main queue
50-
// _dispatch_main_queue_callback_4CF()
51+
// //_dispatch_main_queue_callback_4CF()
52+
// while CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.0, true) == CFRunLoopRunResult.handledSource {
53+
// // continue handling queued events without a timeout
54+
// }
5155
//
5256
// // AChoreographer_postFrameCallback64 is single-shot, so we need to re-enqueue the callback each frame
5357
// enqueueMainChoreographer()
5458
//}
55-
//
59+
5660
//// https://github.com/apple-oss-distributions/libdispatch/blob/bd82a60ee6a73b4eca50af028b48643d51aaf1ea/src/queue.c#L8237
5761
//// https://forums.swift.org/t/main-dispatch-queue-in-linux-sdl-app/31708/3
5862
//@_silgen_name("_dispatch_main_queue_callback_4CF")

Sources/AndroidLooper/AndroidLooper.swift

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,9 @@ open class AndroidLooperExecutor: SerialExecutor, @unchecked Sendable {
189189
}
190190
}
191191

192-
//var _signalCount = UInt64(0)
193-
194192
/// Increment number of remaining events on eventFd
195193
func signal() throws {
196194
var value = UInt64(1)
197-
//logger.info("### AndroidLooperExecutor.signal(): \(value)")
198195
try withUnsafeBytes(of: &value) {
199196
guard try _eventFd.write($0) == MemoryLayout<UInt64>.size else {
200197
throw Errno.outOfRange
@@ -205,43 +202,16 @@ open class AndroidLooperExecutor: SerialExecutor, @unchecked Sendable {
205202
/// Drain job queue
206203
fileprivate func drain() {
207204
if let eventsRemaining = try? eventsRemaining {
208-
//logger.info("### AndroidLooperExecutor.drain(): \(eventsRemaining)")
209-
//var handledSource = 0
210205
for _ in 0..<eventsRemaining {
211-
//handledSource += CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.0, true) == CFRunLoopRunResult.handledSource ? 1 : 0
212-
//logger.info("### CFRunLoopRunInMode result: \(result)")
213-
214206
let job = dequeue()
215207
guard let job else { break }
216208
job.runSynchronously(on: asUnownedSerialExecutor())
217209
}
218-
219-
// if handledSource < eventsRemaining {
220-
// try! signal()
221-
//// CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01, false)
222-
// }
223210
}
224211

225-
// always dispatch the pending main queue callbacks
226-
// https://forums.swift.org/t/main-dispatch-queue-in-linux-sdl-app/31708/3
227-
//_dispatch_main_queue_callback_4CF() // check for DispatchQueue.main
228-
229212
while CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.0, true) == CFRunLoopRunResult.handledSource {
230213
// continue handling queued events without a timeout
231214
}
232-
233-
// block and wait for a source; this is needed because the no-timeout version is sometimes run before the action gets added
234-
//CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.001, false)
235-
236-
//assert(Thread.isMainThread)
237-
238-
239-
// while CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.0, true) == CFRunLoopRunResult.handledSource {
240-
// // keep running until we have processed all the pending sources
241-
// //DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: drain)
242-
// //_dispatch_main_queue_callback_4CF()
243-
// }
244-
245215
}
246216

247217
/// Dequeue a single job
@@ -293,23 +263,29 @@ private extension AndroidMainActor {
293263
guard !didInstallGlobalExecutor else { return }
294264
didInstallGlobalExecutor = true
295265

296-
typealias swift_task_enqueueGlobal_hook_Fn = @convention(thin) (UnsafeMutablePointer<Job>, swift_task_enqueueGlobal_original) -> Void
297-
let swift_task_enqueueGlobal_hook_impl: swift_task_enqueueGlobal_hook_Fn = { job, original in
298-
//logger.info("### swift_task_enqueueGlobal_hook_impl: job: \(job)")
299-
original(job)
300-
try! AndroidMainActor._executor.signal()
266+
// typealias swift_task_enqueueGlobal_hook_Fn = @convention(thin) (UnsafeMutablePointer<Job>, swift_task_enqueueGlobal_original) -> Void
267+
// let swift_task_enqueueGlobal_hook_impl: swift_task_enqueueGlobal_hook_Fn = { job, original in
268+
// logger.info("### swift_task_enqueueGlobal_hook_impl: job: \(job.pointee)")
269+
//
270+
//// let flags = job.pointee.Flags
271+
//// let unownedJob = unsafeBitCast(job.pointee, to: UnownedJob.self) // Fatal error: Can't unsafeBitCast between types of different sizes
272+
//// logger.info("### swift_task_enqueueGlobal_hook_impl: unownedJob: \(unownedJob)")
273+
//
274+
// //AndroidMainActor._executor.enqueue(unownedJob)
275+
//
276+
// original(job)
277+
// try! AndroidMainActor._executor.signal()
278+
// }
279+
// swift_task_enqueueGlobal_hook = unsafeBitCast(swift_task_enqueueGlobal_hook_impl, to: UnsafeMutableRawPointer?.self)
301280

302-
}
303-
swift_task_enqueueGlobal_hook = unsafeBitCast(swift_task_enqueueGlobal_hook_impl, to: UnsafeMutableRawPointer?.self)
304-
305-
// this would be a better way to signal the main looper, but unfortunately it is never called: https://github.com/swiftlang/swift/issues/63104
306-
typealias swift_task_enqueueMainExecutor_hook_Fn = @convention(thin) (UnsafeMutablePointer<Job>, swift_task_enqueueMainExecutor_original) -> Void
307-
let swift_task_enqueueMainExecutor_hook_impl: swift_task_enqueueMainExecutor_hook_Fn = { job, original in
308-
//logger.info("### swift_task_enqueueMainExecutor_hook_Fn")
309-
original(job)
310-
try! AndroidMainActor._executor.signal() // signal the main looper to wake a drain the main queue
311-
}
312-
swift_task_enqueueMainExecutor_hook = unsafeBitCast(swift_task_enqueueMainExecutor_hook_impl, to: UnsafeMutableRawPointer?.self)
281+
// // this would be a better way to signal the main looper, but unfortunately it is never called: https://github.com/swiftlang/swift/issues/63104
282+
// typealias swift_task_enqueueMainExecutor_hook_Fn = @convention(thin) (UnsafeMutablePointer<Job>, swift_task_enqueueMainExecutor_original) -> Void
283+
// let swift_task_enqueueMainExecutor_hook_impl: swift_task_enqueueMainExecutor_hook_Fn = { job, original in
284+
// //logger.info("### swift_task_enqueueMainExecutor_hook_Fn")
285+
// original(job)
286+
// try! AndroidMainActor._executor.signal() // signal the main looper to wake a drain the main queue
287+
// }
288+
// swift_task_enqueueMainExecutor_hook = unsafeBitCast(swift_task_enqueueMainExecutor_hook_impl, to: UnsafeMutableRawPointer?.self)
313289

314290

315291
// typealias swift_task_enqueueGlobalWithDelay_hook_Fn = @convention(thin) (UInt64, UnsafeMutablePointer<Job>, swift_task_enqueueGlobalWithDelay_original) -> Void
@@ -339,8 +315,8 @@ private extension AndroidMainActor {
339315
}
340316
}
341317

342-
// https://github.com/apple-oss-distributions/libdispatch/blob/bd82a60ee6a73b4eca50af028b48643d51aaf1ea/src/queue.c#L8237
343-
// https://forums.swift.org/t/main-dispatch-queue-in-linux-sdl-app/31708/3
344-
@_silgen_name("_dispatch_main_queue_callback_4CF")
345-
func _dispatch_main_queue_callback_4CF()
318+
//// https://github.com/apple-oss-distributions/libdispatch/blob/bd82a60ee6a73b4eca50af028b48643d51aaf1ea/src/queue.c#L8237
319+
//// https://forums.swift.org/t/main-dispatch-queue-in-linux-sdl-app/31708/3
320+
//@_silgen_name("_dispatch_main_queue_callback_4CF")
321+
//func _dispatch_main_queue_callback_4CF()
346322
#endif

0 commit comments

Comments
 (0)