0 | module System.Posix.Poll.Prim
 1 |
 2 | import Data.C.Ptr
 3 | import Data.C.Array
 4 |
 5 | import System.Posix.File.Prim
 6 |
 7 | import public System.Posix.Poll.Struct
 8 | import public System.Posix.Poll.Types
 9 |
10 | %default total
11 |
12 | --------------------------------------------------------------------------------
13 | -- FFI
14 | --------------------------------------------------------------------------------
15 |
16 | %foreign "C__collect_safe:li_poll, posix-idris"
17 | prim__poll: AnyPtr -> Bits32 -> Int32 -> PrimIO CInt
18 |
19 | --------------------------------------------------------------------------------
20 | -- API
21 | --------------------------------------------------------------------------------
22 |
23 | ||| Polls the given array of poll file descriptors for file system events.
24 | export
25 | poll :
26 |      {n : _}
27 |   -> CArrayIO n PollFD
28 |   -> (timeout : Int32)
29 |   -> EPrim (List PollPair)
30 | poll arr timeout t =
31 |   let p     := unsafeUnwrap arr
32 |       r # t := ffi (prim__poll p (cast n) timeout) t
33 |    in case r of
34 |         0 => R [] t
35 |         _ => if r < 0
36 |                then E (inject $ fromNeg r) t
37 |                else f1ToE1 (pollResults arr) t
38 |
39 | ||| Polls the given list of poll file pairs for file system events.
40 | export
41 | pollList : List PollPair -> (timeout : Int32) -> EPrim (List PollPair)
42 | pollList pairs timeout =
43 |   withCArray PollFD (length pairs) $ \arr,t =>
44 |    let _ # t := writeFiles arr pairs t
45 |     in poll arr timeout t
46 |