0 | module System.Linux.Eventfd.Prim
 1 |
 2 | import Data.C.Ptr
 3 | import public System.Linux.Eventfd.Eventfd
 4 | import public System.Linux.Eventfd.Flags
 5 | import public System.Posix.File.Prim
 6 |
 7 | %default total
 8 |
 9 | --------------------------------------------------------------------------------
10 | -- FFI
11 | --------------------------------------------------------------------------------
12 |
13 | %foreign "C:li_eventfd, linux-idris"
14 | prim__eventfd : Bits64 -> Bits32 -> PrimIO CInt
15 |
16 | %foreign "C:li_eventfd_write, linux-idris"
17 | prim__eventfd_write : Bits32 -> Bits64 -> PrimIO CInt
18 |
19 | %foreign "C:li_eventfd_read, linux-idris"
20 | prim__eventfd_read : Bits32 -> PrimIO CInt
21 |
22 | --------------------------------------------------------------------------------
23 | -- API
24 | --------------------------------------------------------------------------------
25 |
26 | ||| Opens a new `eventfd` file descriptor writing the given value
27 | ||| to it.
28 | |||
29 | ||| Notes:
30 | ||| * An `eventfd` should be closed using `close` just like other file
31 | |||   descriptors.
32 | ||| * In general, use `readEventfd` instead of the `read` functions
33 | |||   from `System.Posix.File` to read from an `eventfd`.
34 | ||| * Likewise, use `writeEventfd` instead of `System.Posix.File.write`
35 | export %inline
36 | eventfd : (init : Bits64) -> EventfdFlags -> EPrim Eventfd
37 | eventfd init (F f) = toVal cast $ prim__eventfd init f
38 |
39 | ||| Writes a value to the given event file descriptor.
40 | export %inline
41 | writeEventfd : Eventfd -> Bits64 -> EPrim ()
42 | writeEventfd t val = toUnit $ prim__eventfd_write (fileDesc t) val
43 |
44 | ||| Reads the current value from an event file descriptor.
45 | |||
46 | ||| If the current value is 0, this will block until a non-zero value
47 | ||| is ready. If opened with the `EFD_NONBLOCK` flag, this fails with `EAGAIN`
48 | ||| if no value is ready. If opened with the `EFD_SEMAPHORE` flag, this will
49 | ||| return 1 if a value is ready and reduce the value by 1.
50 | export %inline
51 | readEventfd : Eventfd -> EPrim Bits64
52 | readEventfd t = toVal cast $ prim__eventfd_read (fileDesc t)
53 |