0 | module System.Posix.Timer
 1 |
 2 | import Data.C.Ptr
 3 |
 4 | import System.Posix.Timer.Prim as P
 5 |
 6 | import public Data.C.Integer
 7 | import public System.Posix.Errno
 8 | import public System.Posix.Timer.Types
 9 | import public System.Posix.Time
10 |
11 | %default total
12 |
13 | ||| Returns an approximation of processor time used by the program.
14 | |||
15 | ||| Type `ClockT` measures time with a granularity of
16 | ||| `CLOCKS_PER_SEC`.
17 | export %inline
18 | clock : HasIO io => io ClockT
19 | clock = primIO P.clock
20 |
21 | ||| This sets `new` as the new timer and places the current timer for
22 | ||| `Which` in `old`.
23 | |||
24 | ||| Depending on `Which`, the timer will use a different clock and
25 | ||| will (possibly repeatedly) raise a different kind signal:
26 | |||
27 | ||| * ITIMER_REAL: Counts down in real (i.e. wall clock) time
28 | |||   and raises SIGALRM
29 | ||| * ITIMER_VIRTUAL: Counts down in process virtual time
30 | |||   (i.e. user-mode CPU time) and raises SIGVTALRM
31 | ||| * ITIMER_PROF: Counts down in process time
32 | |||   (i.e. the sum of kernel-mode and user-mode CPU time) and raises SIGPROF
33 | export %inline
34 | setTimer : Has Errno es => EIO1 f => Which -> Timerval -> f es ()
35 | setTimer w t = elift1 (P.setTimer w t)
36 |
37 | ||| Returns the currently set timer for `Which`.
38 | export %inline
39 | getTimer : Has Errno es => EIO1 f => Which -> f es Timerval
40 | getTimer w = elift1 (P.getTimer w)
41 |
42 | ||| A very basic version of `setitimer` that raises `SIGALRM`
43 | ||| after the given number of seconds.
44 | |||
45 | ||| The returned value is the remaining number of seconds on any
46 | ||| previously set timer. The timer can be disabled by setting
47 | ||| this to zero.
48 | export %inline
49 | alarm : HasIO io => UInt -> io UInt
50 | alarm u = primIO (P.alarm u)
51 |
52 | ||| Returns the current time for the given clock.
53 | export %inline
54 | getTime : Has Errno es => EIO1 f => (c : ClockId) -> f es (IClock c)
55 | getTime c = elift1 (P.getTime c)
56 |
57 | ||| Returns the resolution for the given clock.
58 | export %inline
59 | getResolution : Has Errno es => EIO1 f => (c : ClockId) -> f es (IClock c)
60 | getResolution c = elift1 (P.getResolution c)
61 |
62 | ||| Like `nanosleep` but without the capability of keeping track of the
63 | ||| remaining duration in case of a signal interrupt.
64 | export %inline
65 | nanosleep : Has Errno es => EIO1 f => (dur : Clock Monotonic) -> f es ()
66 | nanosleep cl = elift1 (P.nanosleep cl)
67 |