0 | ||| This module provides the raw primitive bindings to the
  1 | ||| timer API from libuv.
  2 | |||
  3 | ||| No additional security measures are implemented. For a nicer but
  4 | ||| more restrictive API, see module `System.UV.Timer`.
  5 | module System.UV.Raw.Timer
  6 |
  7 | import System.UV.Raw.Callback
  8 | import System.UV.Raw.Handle
  9 | import System.UV.Raw.Loop
 10 | import System.UV.Raw.Pointer
 11 | import System.UV.Raw.Util
 12 |
 13 | %default total
 14 |
 15 | --------------------------------------------------------------------------------
 16 | -- FFI
 17 | --------------------------------------------------------------------------------
 18 |
 19 | %foreign (idris_uv "uv_timer_init")
 20 | prim__uv_timer_init : Ptr Loop -> Ptr Timer -> PrimIO Int32
 21 |
 22 | %foreign (idris_uv "uv_timer_start")
 23 | prim__uv_timer_start : Ptr Timer -> AnyPtr -> Bits64 -> Bits64 -> PrimIO Int32
 24 |
 25 | %foreign (idris_uv "uv_timer_stop")
 26 | prim__uv_timer_stop : Ptr Timer -> PrimIO Int32
 27 |
 28 | %foreign (idris_uv "uv_timer_set_repeat")
 29 | prim__uv_timer_set_repeat : Ptr Timer -> Bits64 -> PrimIO ()
 30 |
 31 | %foreign (idris_uv "uv_timer_get_repeat")
 32 | prim__uv_timer_get_repeat : Ptr Timer -> PrimIO Bits64
 33 |
 34 | %foreign (idris_uv "uv_timer_get_due_in")
 35 | prim__uv_timer_get_due_in : Ptr Timer -> PrimIO Bits64
 36 |
 37 | %foreign (idris_uv "uv_timer_again")
 38 | prim__uv_timer_again : Ptr Timer -> PrimIO Int32
 39 |
 40 | --------------------------------------------------------------------------------
 41 | -- API
 42 | --------------------------------------------------------------------------------
 43 |
 44 | parameters {auto has : HasIO io}
 45 |
 46 |   ||| Initialize the handle.
 47 |   export %inline
 48 |   uv_timer_init : Ptr Loop -> Ptr Timer -> io Int32
 49 |   uv_timer_init ptr ti = primIO $ prim__uv_timer_init ptr ti
 50 |
 51 |   ||| Start the timer. timeout and repeat are in milliseconds.
 52 |   |||
 53 |   ||| If timeout is zero, the callback fires on the next
 54 |   ||| event loop iteration. If repeat is non-zero, the callback fires
 55 |   ||| first after timeout milliseconds and then repeatedly after repeat
 56 |   ||| milliseconds.
 57 |   export
 58 |   uv_timer_start :
 59 |        Ptr Timer
 60 |     -> (Ptr Timer -> IO ())
 61 |     -> (timeout,repeat : Bits64)
 62 |     -> io Int32
 63 |   uv_timer_start p f t r = do
 64 |     cb <- ptrCB f
 65 |     uv_handle_set_data p cb
 66 |     primIO $ prim__uv_timer_start p cb t r
 67 |
 68 |   ||| Stop the timer, the callback will not be called anymore.
 69 |   export %inline
 70 |   uv_timer_stop : Ptr Timer -> io Int32
 71 |   uv_timer_stop ptr = primIO $ prim__uv_timer_stop ptr
 72 |
 73 |   ||| Stop the timer, and if it is repeating restart it using
 74 |   ||| the repeat value as the timeout. If the timer has
 75 |   ||| never been started before it returns UV_EINVAL.
 76 |   export %inline
 77 |   uv_timer_again : Ptr Timer -> io Int32
 78 |   uv_timer_again ptr = primIO $ prim__uv_timer_again ptr
 79 |
 80 |   ||| Set the repeat interval value in milliseconds. The timer
 81 |   ||| will be scheduled to run on the given interval, regardless
 82 |   ||| of the callback execution duration, and will follow normal
 83 |   ||| timer semantics in the case of a  time-slice overrun.
 84 |   |||
 85 |   ||| For  example, if a 50ms repeating timer first runs for 17ms,
 86 |   ||| it will be scheduled to run again 33ms later. If other tasks
 87 |   ||| consume more than the 33ms following the first timer callback,
 88 |   ||| then the callback will run as soon as possible.
 89 |   |||
 90 |   ||| NOTE:
 91 |   |||    If the repeat value is set from a timer callback it does
 92 |   |||    not immediately take effect.  If the timer was non-repeating
 93 |   |||    before, it will have been stopped. If it was repeating, then the
 94 |   |||    old repeat value will  have  been used to schedule the next timeout.
 95 |   export %inline
 96 |   uv_timer_set_repeat : Ptr Timer -> Bits64 -> io ()
 97 |   uv_timer_set_repeat ptr rep = primIO $ prim__uv_timer_set_repeat ptr rep
 98 |
 99 |   ||| Get the timer repeat value.
100 |   export %inline
101 |   uv_timer_get_repeat : Ptr Timer -> io Bits64
102 |   uv_timer_get_repeat ptr = primIO $ prim__uv_timer_get_repeat ptr
103 |
104 |   ||| Get the timer due value or 0 if it has expired. The time is relative to
105 |   ||| uv_now.
106 |   export %inline
107 |   uv_timer_get_due_in : Ptr Timer -> io Bits64
108 |   uv_timer_get_due_in ptr = primIO $ prim__uv_timer_get_due_in ptr
109 |