Document and clean up alarms
parent
b7ba2d77a2
commit
bbad200a01
|
@ -0,0 +1,22 @@
|
||||||
|
IN: alarms
|
||||||
|
USING: help.markup help.syntax calendar ;
|
||||||
|
|
||||||
|
HELP: alarm
|
||||||
|
{ $class-description "An alarm. Cancel passed to " { $link cancel-alarm } "." } ;
|
||||||
|
|
||||||
|
HELP: add-alarm
|
||||||
|
{ $values { "time" timestamp } { "frequency" "a " { $link dt } " or " { $link f } } { "quot" quotation } { "alarm" alarm } }
|
||||||
|
{ $description "Creates and registers an alarm. If " { $snippet "frequency" } " is " { $link f } ", this will be a one-time alarm, otherwise it will fire with the given frequency. The quotation will be called from the alarm thread." } ;
|
||||||
|
|
||||||
|
HELP: cancel-alarm
|
||||||
|
{ $values { "alarm" alarm } }
|
||||||
|
{ $description "Cancels an alarm." }
|
||||||
|
{ $errors "Throws an error if the alarm is not active." } ;
|
||||||
|
|
||||||
|
ARTICLE: "alarms" "Alarms"
|
||||||
|
"Alarms provide a lightweight way to schedule one-time and recurring tasks without spawning a new thread."
|
||||||
|
{ $subsection alarm }
|
||||||
|
{ $subsection add-alarm }
|
||||||
|
{ $subsection cancel-alarm } ;
|
||||||
|
|
||||||
|
ABOUT: "alarms"
|
|
@ -6,6 +6,8 @@ IN: alarms
|
||||||
|
|
||||||
TUPLE: alarm time interval quot entry ;
|
TUPLE: alarm time interval quot entry ;
|
||||||
|
|
||||||
|
<PRIVATE
|
||||||
|
|
||||||
: check-alarm
|
: check-alarm
|
||||||
pick timestamp? [ "Not a timestamp" throw ] unless
|
pick timestamp? [ "Not a timestamp" throw ] unless
|
||||||
over dup dt? swap not or [ "Not a dt" throw ] unless
|
over dup dt? swap not or [ "Not a dt" throw ] unless
|
||||||
|
@ -21,16 +23,6 @@ SYMBOL: alarm-thread
|
||||||
: notify-alarm-thread ( -- )
|
: notify-alarm-thread ( -- )
|
||||||
alarm-thread get-global interrupt ;
|
alarm-thread get-global interrupt ;
|
||||||
|
|
||||||
: add-alarm ( time delay quot -- alarm )
|
|
||||||
<alarm> [
|
|
||||||
dup dup alarm-time alarms get-global heap-push*
|
|
||||||
swap alarm-entry >box
|
|
||||||
notify-alarm-thread
|
|
||||||
] keep ;
|
|
||||||
|
|
||||||
: cancel-alarm ( alarm -- )
|
|
||||||
alarm-entry box> alarms get-global heap-delete ;
|
|
||||||
|
|
||||||
: alarm-expired? ( alarm now -- ? )
|
: alarm-expired? ( alarm now -- ? )
|
||||||
>r alarm-time r> <=> 0 <= ;
|
>r alarm-time r> <=> 0 <= ;
|
||||||
|
|
||||||
|
@ -78,3 +70,15 @@ SYMBOL: alarm-thread
|
||||||
alarm-thread set-global ;
|
alarm-thread set-global ;
|
||||||
|
|
||||||
[ init-alarms ] "alarms" add-init-hook
|
[ init-alarms ] "alarms" add-init-hook
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
|
: add-alarm ( time delay quot -- alarm )
|
||||||
|
<alarm> [
|
||||||
|
dup dup alarm-time alarms get-global heap-push*
|
||||||
|
swap alarm-entry >box
|
||||||
|
notify-alarm-thread
|
||||||
|
] keep ;
|
||||||
|
|
||||||
|
: cancel-alarm ( alarm -- )
|
||||||
|
alarm-entry box> alarms get-global heap-delete ;
|
||||||
|
|
Loading…
Reference in New Issue