make alarms use new accessors

db4
Doug Coleman 2008-08-28 23:42:59 -05:00
parent 98fafecaa7
commit e91129ba10
1 changed files with 21 additions and 20 deletions

View File

@ -1,11 +1,15 @@
! Copyright (C) 2005, 2008 Slava Pestov, Doug Coleman. ! Copyright (C) 2005, 2008 Slava Pestov, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: arrays calendar combinators generic init kernel math USING: accessors arrays calendar combinators generic init
namespaces sequences heaps boxes threads debugger quotations kernel math namespaces sequences heaps boxes threads debugger
assocs math.order ; quotations assocs math.order ;
IN: alarms IN: alarms
TUPLE: alarm quot time interval entry ; TUPLE: alarm
{ quot callable initial: [ ] }
{ time timestamp }
interval
{ entry box } ;
<PRIVATE <PRIVATE
@ -15,31 +19,28 @@ SYMBOL: alarm-thread
: notify-alarm-thread ( -- ) : notify-alarm-thread ( -- )
alarm-thread get-global interrupt ; alarm-thread get-global interrupt ;
: check-alarm ERROR: bad-alarm-frequency frequency ;
dup duration? over not or [ "Not a duration" throw ] unless : check-alarm ( frequency/f -- frequency/f )
over timestamp? [ "Not a timestamp" throw ] unless dup [ duration? ] [ not ] bi or [ bad-alarm-frequency ] unless ;
pick callable? [ "Not a quotation" throw ] unless ; inline
: <alarm> ( quot time frequency -- alarm ) : <alarm> ( quot time frequency -- alarm )
check-alarm <box> alarm boa ; check-alarm <box> alarm boa ;
: register-alarm ( alarm -- ) : register-alarm ( alarm -- )
dup dup alarm-time alarms get-global heap-push* dup dup time>> alarms get-global heap-push*
swap alarm-entry >box swap entry>> >box
notify-alarm-thread ; notify-alarm-thread ;
: alarm-expired? ( alarm now -- ? ) : alarm-expired? ( alarm now -- ? )
>r alarm-time r> before=? ; [ time>> ] dip before=? ;
: reschedule-alarm ( alarm -- ) : reschedule-alarm ( alarm -- )
dup alarm-time over alarm-interval time+ dup [ swap interval>> time+ ] change-time register-alarm ;
over set-alarm-time
register-alarm ;
: call-alarm ( alarm -- ) : call-alarm ( alarm -- )
dup alarm-entry box> drop [ entry>> box> drop ]
dup alarm-quot "Alarm execution" spawn drop [ quot>> "Alarm execution" spawn drop ]
dup alarm-interval [ reschedule-alarm ] [ drop ] if ; [ dup interval>> [ reschedule-alarm ] [ drop ] if ] tri ;
: (trigger-alarms) ( alarms now -- ) : (trigger-alarms) ( alarms now -- )
over heap-empty? [ over heap-empty? [
@ -57,7 +58,7 @@ SYMBOL: alarm-thread
: next-alarm ( alarms -- timestamp/f ) : next-alarm ( alarms -- timestamp/f )
dup heap-empty? dup heap-empty?
[ drop f ] [ heap-peek drop alarm-time ] if ; [ drop f ] [ heap-peek drop time>> ] if ;
: alarm-thread-loop ( -- ) : alarm-thread-loop ( -- )
alarms get-global alarms get-global
@ -66,7 +67,7 @@ SYMBOL: alarm-thread
: cancel-alarms ( alarms -- ) : cancel-alarms ( alarms -- )
[ [
heap-pop-all [ nip alarm-entry box> drop ] assoc-each heap-pop-all [ nip entry>> box> drop ] assoc-each
] when* ; ] when* ;
: init-alarms ( -- ) : init-alarms ( -- )
@ -88,4 +89,4 @@ PRIVATE>
[ hence ] keep add-alarm ; [ hence ] keep add-alarm ;
: cancel-alarm ( alarm -- ) : cancel-alarm ( alarm -- )
alarm-entry [ alarms get-global heap-delete ] if-box? ; entry>> [ alarms get-global heap-delete ] if-box? ;