tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

clock.tri (1186B)


      1 !import base as B
      2 !import native_math as N
      3 !import io_v2 as IO
      4 !export elapsed
      5 !export yielded
      6 !export invalidSleep
      7 !export cancelledWait
      8 !export scopeWait
      9 elapsed milliseconds = IO.io (do IO.require
     10   before <- IO.monotonicNanoseconds
     11   IO.sleepMilliseconds milliseconds
     12   after <- IO.monotonicNanoseconds
     13   IO.pure (N.subtract after before))
     14 yielded = IO.io IO.yield
     15 invalidSleep value = IO.io (IO.sleepMilliseconds value)
     16 cancelledWait = IO.io (IO.scope (_ : IO.require
     17   (IO.spawn 1 1 [] (_ : IO.require (IO.sleepMilliseconds 9223372036854775807) (_ : IO.pure 99)))
     18   (spawned : case spawned of
     19     [task ticket] = do IO.require
     20       IO.sleepMilliseconds 20
     21       IO.cancel task "timer-stop"
     22       outcome <- IO.join ticket
     23       IO.pure outcome
     24     _ = IO.fail "Invalid timer spawn reply")))
     25 -- Normal scope exit waits for its child even without a retained result ticket.
     26 scopeWait = IO.io (do IO.require
     27   before <- IO.monotonicNanoseconds
     28   IO.bind (IO.scope (_ : IO.require
     29     (IO.spawn 0 0 [] (_ : IO.require (IO.sleepMilliseconds 20) (_ : IO.pure t)))
     30     (_ : IO.pure t))) (_ : do IO.require
     31       after <- IO.monotonicNanoseconds
     32       IO.pure (N.subtract after before)))