core-foundation.run-loop: cleanup timer-thread code.

db4
John Benediktsson 2015-01-17 14:16:24 -08:00
parent ceebc1d6c1
commit be173c8a43
2 changed files with 17 additions and 25 deletions

View File

@ -3,8 +3,8 @@
USING: accessors alien alien.c-types alien.syntax
core-foundation core-foundation.file-descriptors
core-foundation.strings core-foundation.time
core-foundation.timers destructors kernel math sequences threads
;
core-foundation.timers destructors kernel math namespaces
sequences threads ;
FROM: calendar.unix => system-micros ;
IN: core-foundation.run-loop
@ -87,26 +87,13 @@ SYMBOL: run-loop
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 ]
[ get-run-loop timers>> push ]
[
CFRunLoopGetMain
swap CFRunLoopDefaultMode
CFRunLoopAddTimer
] tri ;
] bi ;
: invalidate-run-loop-timers ( -- )
get-run-loop [
@ -114,16 +101,20 @@ PRIVATE>
V{ } clone
] change-timers drop ;
: reset-run-loop ( -- )
get-run-loop
[ timers>> [ reset-timer ] each ]
[ fds>> [ enable-all-callbacks ] each ] bi ;
SYMBOL: thread-timer
: timer-callback ( -- callback )
[ drop reset-timer yield ] CFRunLoopTimerCallBack ;
: reset-thread-timer ( timer -- )
sleep-time
[ 1000 /f ] [ 1,000,000 ] if* system-micros +
>CFAbsoluteTime CFRunLoopTimerSetNextFireDate ;
: thread-timer-callback ( -- callback )
[ drop reset-thread-timer yield ] CFRunLoopTimerCallBack ;
: init-thread-timer ( -- )
60 timer-callback <CFTimer> add-timer-to-run-loop ;
60 thread-timer-callback <CFTimer>
[ add-timer-to-run-loop ]
[ thread-timer set-global ] bi ;
: run-one-iteration ( nanos -- handled? )
CFRunLoopDefaultMode

View File

@ -11,9 +11,10 @@ TUPLE: run-loop-mx kqueue-mx ;
: file-descriptor-callback ( -- callback )
[
3drop
2drop
0 mx get-global kqueue-mx>> wait-for-events
reset-run-loop
enable-all-callbacks
thread-timer get-global reset-thread-timer
yield
] CFFileDescriptorCallBack ;