: add-fd-to-run-loop ( fd callback -- )
[
<CFFileDescriptor> |CFRelease
+ [ enable-all-callbacks ]
[ run-loop fds>> push ]
[ create-fd-source |CFRelease add-source-to-run-loop ]
- bi
+ tri
] with-destructors ;
+<PRIVATE
+
+: (reset-timer) ( timer timestamp -- )
+ >CFAbsoluteTime CFRunLoopTimerSetNextFireDate ;
+
+: reset-timer ( timer -- )
+ sleep-time
+ [ 1000 /f ] [ 1,000,000 ] if* system-micros +
+ (reset-timer) ;
+
+PRIVATE>
+
: add-timer-to-run-loop ( timer -- )
+ [ reset-timer ]
[ run-loop timers>> push ]
[
CFRunLoopGetMain
swap CFRunLoopDefaultMode
CFRunLoopAddTimer
- ] bi ;
+ ] tri ;
: invalidate-run-loop-timers ( -- )
run-loop [
V{ } clone
] change-timers drop ;
-<PRIVATE
-
-: (reset-timer) ( timer timestamp -- )
- >CFAbsoluteTime CFRunLoopTimerSetNextFireDate ;
-
-: reset-timer ( timer -- )
- sleep-time
- [ 1000 /f ] [ 1,000,000 ] if* system-micros +
- (reset-timer) ;
-
-PRIVATE>
-
: reset-run-loop ( -- )
run-loop
[ timers>> [ reset-timer ] each ]
: timer-callback ( -- callback )
void { CFRunLoopTimerRef void* } cdecl
- [ 2drop reset-run-loop yield ] alien-callback ;
+ [ drop reset-timer yield ] alien-callback ;
: init-thread-timer ( -- )
60 timer-callback <CFTimer> add-timer-to-run-loop ;
: run-one-iteration ( nanos -- handled? )
- reset-run-loop
CFRunLoopDefaultMode
swap [ nanoseconds ] [ 5 minutes ] if* >CFTimeInterval
t CFRunLoopRunInMode kCFRunLoopRunHandledSource = ;