module System.CPUTime.Clock (
Clock (Monotonic, Realtime, ProcessTime, ThreadTime),
TimeSpec (Time),
clock_gettime,
clock_getres,
sec,
nsec,
) where
import GHC.Ptr
import Foreign.Storable
import Foreign.Marshal.Alloc (free)
import Foreign.Marshal.Array
data Clock = Monotonic
| Realtime
| ProcessTime
| ThreadTime
data TimeSpec = Time Int Int deriving (Show, Read)
sec :: TimeSpec → Int
sec (Time s _) = s
nsec :: TimeSpec → Int
nsec (Time _ n) = n
clock_gettime :: Clock → IO TimeSpec
clock_gettime = call . time
clock_getres :: Clock → IO TimeSpec
clock_getres = call . res
type ReaderFunc = Ptr Int → IO ()
time :: Clock → ReaderFunc
time Monotonic = clock_readtime_monotonic
time Realtime = clock_readtime_realtime
time ProcessTime = clock_readtime_processtime
time ThreadTime = clock_readtime_threadtime
res :: Clock → ReaderFunc
res Monotonic = clock_readres_monotonic
res Realtime = clock_readres_realtime
res ProcessTime = clock_readres_processtime
res ThreadTime = clock_readres_threadtime
foreign import ccall clock_readtime_monotonic :: ReaderFunc
foreign import ccall clock_readtime_realtime :: ReaderFunc
foreign import ccall clock_readtime_processtime :: ReaderFunc
foreign import ccall clock_readtime_threadtime :: ReaderFunc
foreign import ccall clock_readres_monotonic :: ReaderFunc
foreign import ccall clock_readres_realtime :: ReaderFunc
foreign import ccall clock_readres_processtime :: ReaderFunc
foreign import ccall clock_readres_threadtime :: ReaderFunc
call :: ReaderFunc → IO TimeSpec
call read_ = do
t ← (2 ↑≣)
read_ t
s ← (t ≣→ 0)
n ← (t ≣→ 1)
(t ≣⊠)
return $ Time s n
(↑≣) :: Storable a ⇒ Int → IO (Ptr a)
(↑≣) = mallocArray
(≣→) :: Storable a ⇒ Ptr a → Int → IO a
(≣→) = peekElemOff
(≣⊠) :: Ptr a → IO ()
(≣⊠) = Foreign.Marshal.Alloc.free