0 | module System.UV.Raw.Handle
  1 |
  2 | import System.UV.Raw.Callback
  3 | import System.UV.Raw.Loop
  4 | import System.UV.Raw.Pointer
  5 | import System.UV.Raw.Util
  6 |
  7 | %default total
  8 |
  9 | --------------------------------------------------------------------------------
 10 | -- FFI
 11 | --------------------------------------------------------------------------------
 12 |
 13 | %foreign (idris_uv "uv_is_active")
 14 | prim__uv_is_active : Ptr Handle -> PrimIO Int32
 15 |
 16 | %foreign (idris_uv "uv_is_closing")
 17 | prim__uv_is_closing : Ptr Handle -> PrimIO Int32
 18 |
 19 | %foreign (idris_uv "uv_close")
 20 | prim__uv_close : Ptr Handle -> AnyPtr -> PrimIO ()
 21 |
 22 | %foreign (idris_uv "uv_ref")
 23 | prim__uv_ref : Ptr Handle -> PrimIO ()
 24 |
 25 | %foreign (idris_uv "uv_unref")
 26 | prim__uv_unref : Ptr Handle -> PrimIO ()
 27 |
 28 | %foreign (idris_uv "uv_has_ref")
 29 | prim__uv_has_ref : Ptr Handle -> PrimIO Int
 30 |
 31 | %foreign (idris_uv "uv_handle_get_data")
 32 | prim__uv_handle_get_data : Ptr Handle -> PrimIO AnyPtr
 33 |
 34 | %foreign (idris_uv "uv_handle_set_data")
 35 | prim__uv_handle_set_data : Ptr Handle -> AnyPtr -> PrimIO ()
 36 |
 37 | %foreign (idris_uv "uv_handle_type")
 38 | prim__uv_handle_type : Ptr Handle -> PrimIO Int
 39 |
 40 | ||| Returns the name of the handle type.
 41 | export %foreign (idris_uv "uv_handle_type_name")
 42 | uv_handle_type_name : Int -> String
 43 |
 44 | --------------------------------------------------------------------------------
 45 | -- API
 46 | --------------------------------------------------------------------------------
 47 |
 48 | export
 49 | record CloseCB where
 50 |   [noHints]
 51 |   constructor CC
 52 |   ptr : AnyPtr
 53 |
 54 | export %inline
 55 | freeCloseCB : HasIO io => CloseCB -> io ()
 56 | freeCloseCB (CC p) = unlockAnyPtr p
 57 |
 58 | parameters {auto has   : HasIO io}
 59 |            {auto 0 prf : PCast t Handle}
 60 |
 61 |   export %inline
 62 |   uv_is_active : Ptr t -> io Int32
 63 |   uv_is_active p = primIO (prim__uv_is_active $ castPtr p)
 64 |
 65 |   export %inline
 66 |   uv_is_closing : Ptr t -> io Int32
 67 |   uv_is_closing p = primIO (prim__uv_is_closing $ castPtr p)
 68 |
 69 |   ||| Request a handle to be closed.
 70 |   |||
 71 |   ||| This *must* be called before releasing the handle from memory,
 72 |   ||| which can be done from within the callback or after the callback
 73 |   ||| has returned.
 74 |   export
 75 |   uv_close : Ptr t -> CloseCB -> io ()
 76 |   uv_close p cb = primIO $ prim__uv_close (castPtr p) cb.ptr
 77 |
 78 |   ||| Reference a handle.
 79 |   |||
 80 |   ||| This is an idempotent action, so calling it several times has no
 81 |   ||| additional effect.
 82 |   export %inline
 83 |   uv_ref : Ptr t -> io ()
 84 |   uv_ref p = primIO $ prim__uv_ref (castPtr p)
 85 |
 86 |   ||| Un-reference a handle.
 87 |   |||
 88 |   ||| This is an idempotent action, so calling it several times has no
 89 |   ||| additional effect.
 90 |   export %inline
 91 |   uv_unref : Ptr t -> io ()
 92 |   uv_unref p = primIO $ prim__uv_unref (castPtr p)
 93 |
 94 |   ||| Returns `True` is the handle is currently referenced.
 95 |   export %inline
 96 |   uv_has_ref : Ptr t -> io Bool
 97 |   uv_has_ref p = intToBool <$> primIO (prim__uv_has_ref $ castPtr p)
 98 |
 99 |   ||| Returns a pointer to the data associated with a handle.
100 |   export %inline
101 |   uv_handle_get_data : Ptr t -> io AnyPtr
102 |   uv_handle_get_data p = primIO $ prim__uv_handle_get_data (castPtr p)
103 |
104 |   ||| Sets the data associated with a handle
105 |   export %inline
106 |   uv_handle_set_data : Ptr t -> AnyPtr -> io ()
107 |   uv_handle_set_data p ap = primIO $ prim__uv_handle_set_data (castPtr p) ap
108 |
109 |   ||| Returns the type the current handle.
110 |   export %inline
111 |   uv_handle_type : Ptr t -> io Int
112 |   uv_handle_type p = primIO $ prim__uv_handle_type (castPtr p)
113 |
114 |   export
115 |   freeHandle : Ptr t -> io ()
116 |   freeHandle p = do
117 |     d <- uv_handle_get_data p
118 |     unlockAnyPtr d
119 |     freePtr p
120 |
121 |   export %inline
122 |   freeHandleWithoutUnlocking : Ptr t -> io ()
123 |   freeHandleWithoutUnlocking = freePtr
124 |
125 | ||| Allocates and locks a callback for closing handles.
126 | |||
127 | ||| The callback will run any custom operations given in the
128 | ||| `handler` argument before freeing the `Ptr Handle` from memory.
129 | export %inline
130 | closeCB : HasIO io => (handler : Ptr Handle -> IO ()) -> io (CloseCB)
131 | closeCB f = map CC $ ptrCB (\x => f x >> freeHandle x)
132 |
133 | export
134 | defaultClose : HasIO io => io CloseCB
135 | defaultClose = closeCB $ \_ => pure ()
136 |
137 | export %inline
138 | closeWithoutUnlockingCB : HasIO io => io (CloseCB)
139 | closeWithoutUnlockingCB = map CC $ ptrCB (\x => freeHandleWithoutUnlocking {t = Handle} x)
140 |