0 | module System.Linux.TimerFD
4 | import Derive.Prelude
5 | import System.Linux.Error
6 | import System.Linux.File
7 | import public System.Clock
10 | %language ElabReflection
16 | %foreign "C:ep_clock_realtime,epoll-idris"
17 | ep_clock_realtime : Bits32
19 | %foreign "C:ep_clock_monotonic,epoll-idris"
20 | ep_clock_monotonic : Bits32
22 | %foreign "C:ep_clock_boottime,epoll-idris"
23 | ep_clock_boottime : Bits32
25 | %foreign "C:ep_clock_realtime_alarm,epoll-idris"
26 | ep_clock_realtime_alarm : Bits32
28 | %foreign "C:ep_clock_boottime_alarm,epoll-idris"
29 | ep_clock_boottime_alarm : Bits32
31 | %foreign "C:ep_tfd_cloexec,epoll-idris"
32 | ep_tfd_cloexec : Bits32
34 | %foreign "C:ep_tfd_nonblock,epoll-idris"
35 | ep_tfd_nonblock : Bits32
37 | %foreign "C:timerfd_create,epoll-idris"
38 | prim__timerfd_create : Bits32 -> Bits32 -> PrimIO Bits32
40 | %foreign "C:ep_readTimer,epoll-idris"
41 | prim__ep_readTimer : Bits32 -> PrimIO Bits64
43 | %foreign "C:ep_setTime,epoll-idris"
44 | prim__ep_setTime : Bits32 -> Int64 -> Bits32 -> PrimIO ()
51 | data ClockTpe : Type where
53 | MONOTONIC : ClockTpe
55 | REALTIME_ALARM : ClockTpe
56 | BOOTTIME_ALARM : ClockTpe
58 | %runElab derive "ClockTpe" [Show,Eq,Finite]
61 | clockCode : ClockTpe -> Bits32
62 | clockCode REALTIME = ep_clock_realtime
63 | clockCode MONOTONIC = ep_clock_monotonic
64 | clockCode BOOTTIME = ep_clock_boottime
65 | clockCode REALTIME_ALARM = ep_clock_realtime_alarm
66 | clockCode BOOTTIME_ALARM = ep_clock_boottime_alarm
76 | %runElab derive "Flags" [Show,Eq,Ord]
79 | flagCode : Flags -> Bits32
83 | Semigroup Flags where
84 | F x <+> F y = F (x .|. y)
87 | Monoid Flags where neutral = F 0
91 | TFD_CLOEXEC = F ep_tfd_cloexec
101 | TFD_NONBLOCK : Flags
102 | TFD_NONBLOCK = F ep_tfd_nonblock
106 | record TimerFD where
111 | FileDesc TimerFD where fileDesc = file
115 | timerCreate : ClockTpe -> Flags -> PrimIO TimerFD
116 | timerCreate c (F f) w =
117 | let MkIORes file w := prim__timerfd_create (clockCode c) f w
118 | in MkIORes (TFD file) w
126 | readTimer : TimerFD -> PrimIO (Either EpollErr Bits64)
127 | readTimer (TFD f) w =
128 | let MkIORes v w := prim__ep_readTimer f w
131 | n => MkIORes (Right n) w
134 | setTime : TimerFD -> Clock Duration -> PrimIO ()
135 | setTime (TFD f) c =
136 | prim__ep_setTime f (cast $
seconds c) (cast $
nanoseconds c)
140 | withTimer : ClockTpe -> Flags -> (TimerFD -> PrimIO a) -> PrimIO a
141 | withTimer ct fs f w =
142 | let MkIORes tf w := timerCreate ct fs w
143 | MkIORes res w := f tf w
144 | MkIORes _ w := close tf w
153 | zero : Clock Duration
154 | zero = makeDuration 0 0
158 | (.s) : (n : Nat) -> Clock Duration
159 | n.s = makeDuration (cast n) 0
163 | (.ns) : (n : Nat) -> Clock Duration
164 | n.ns = makeDuration 0 (cast n)
168 | (.us) : (n : Nat) -> Clock Duration
169 | n.us = (n * 1_000).ns
173 | (.ms) : (n : Nat) -> Clock Duration
174 | n.ms = (n * 1_000_000).ns