@@ -71,21 +71,16 @@ import CoreFoundation
7171 }
7272 }
7373
74- internal protocol WakeableRunLoop : AnyObject {
75- func wakeUp( )
76- }
77-
7874 private class RunLoopCallbackInfo {
79- private var task : SafeTask
80- private var runLoops : [ WakeableRunLoop ] = [ ]
75+ private let task : SafeTask
76+ private var runLoops : [ RunLoop ] = [ ]
8177
82- init ( _ task: SafeTask ) {
78+ init ( _ task: SafeTask ) {
8379 self . task = task
84- }
80+ }
8581
8682 func run( ) {
8783 task ( )
88- print ( " Task finished " )
8984 }
9085 }
9186
@@ -134,9 +129,19 @@ import CoreFoundation
134129 }
135130 }
136131
137- init ( _ task: SafeTask , priority: Int = 0 ) {
138- self . info = RunLoopCallbackInfo ( task)
139- self . priority = priority
132+ init ( _ task: SafeTask , priority: Int = 0 , runOnce: Bool = false ) {
133+ self . priority = priority
134+ if runOnce {
135+ var stopTask : SafeTask ?
136+ self . info = RunLoopCallbackInfo ( {
137+ task ( )
138+ stopTask ? ( )
139+ } )
140+ stopTask = { self . stop ( ) }
141+ } else {
142+ self . info = RunLoopCallbackInfo ( task)
143+ }
144+
140145 }
141146
142147 deinit {
@@ -209,19 +214,7 @@ import CoreFoundation
209214 }
210215 }
211216
212- private class CFRunLoopWakeupHolder : WakeableRunLoop {
213- private let loop : CFRunLoop !
214-
215- init ( loop: CFRunLoop ! ) {
216- self . loop = loop
217- }
218-
219- func wakeUp( ) {
220- CFRunLoopWakeUp ( loop)
221- }
222- }
223-
224- class RunLoop : WakeableRunLoop {
217+ class RunLoop {
225218 private let cfRunLoop : CFRunLoop !
226219
227220 private var taskQueueSource : RunLoopTaskQueueSource ? = nil
@@ -283,7 +276,7 @@ import CoreFoundation
283276 taskQueueLock. lock ( )
284277 self . taskQueueSource = RunLoopTaskQueueSource ( )
285278
286- addSource ( taskQueueSource!, mode: RunLoop . defaultMode, retainLoop : false )
279+ addSource ( taskQueueSource!, mode: RunLoop . defaultMode)
287280 }
288281
289282 func stopTaskQueue( ) {
@@ -336,15 +329,11 @@ import CoreFoundation
336329 while true { run ( ) }
337330 }
338331
339- func addSource( rls: RunLoopSource , mode: NSString , retainLoop : Bool = true ) {
332+ func addSource( rls: RunLoopSource , mode: NSString ) {
340333 let crls = unsafeBitCast ( rls. cfObject, CFRunLoopSource . self)
341334 if CFRunLoopSourceIsValid ( crls) {
342335 CFRunLoopAddSource ( cfRunLoop, crls, mode. cfString)
343- if retainLoop {
344- rls. info. runLoops. append ( self )
345- } else {
346- rls. info. runLoops. append ( CFRunLoopWakeupHolder ( loop: cfRunLoop) )
347- }
336+ rls. info. runLoops. append ( self )
348337 wakeUp ( )
349338 }
350339 }
@@ -366,14 +355,9 @@ import CoreFoundation
366355 if let queue = taskQueueSource {
367356 queue. addTask ( task)
368357 } else {
369- var source : RunLoopSource ? = nil
370- let stask = {
371- print ( " Task without queue run " )
372- task ( )
373- source? . stop ( )
374- }
375- source = RunLoopSource ( stask, priority: 0 )
376- addSource ( source!, mode: RunLoop . defaultMode)
358+ let source = RunLoopSource ( task, priority: 0 , runOnce: true )
359+ addSource ( source, mode: RunLoop . defaultMode)
360+ source. signal ( )
377361 }
378362 }
379363
0 commit comments