     delay-nanos
     interval-nanos
     iteration-start-nanos
-    { stop? boolean }
+    quotation-running?
     thread ;
 
 <PRIVATE

    over interval-nanos>> *
    over start-nanos>> +
    over delay-nanos>> [ + ] when*
    >>iteration-start-nanos ; inline

DEFER: call-alarm-loop


        swap >>quot ; inline

: start-alarm ( alarm -- )
    [
        '[
            _ nano-count >>start-nanos
            [ delay-nanos>> [ sleep ] when* ]
            [ nano-count >>iteration-start-nanos call-alarm-loop ] bi
        ] "Alarm execution" spawn
    ] keep thread<< ;

: stop-alarm ( alarm -- )
    dup quotation-running?>> [
        f >>thread drop
    ] [
        [ [ interrupt ] when* f ] change-thread drop
    ] if ;

<PRIVATE

