{-# LINE 1 "System/Clock.hsc" #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
module System.Clock
( Clock(..)
, TimeSpec(..)
, getTime
, getRes
, fromNanoSecs
, toNanoSecs
, diffTimeSpec
, timeSpecAsNanoSecs
) where
import Control.Applicative ((<$>), (<*>))
import Data.Int
import Data.Word
import Data.Typeable (Typeable)
import Foreign.C
import Foreign.Ptr
import Foreign.Storable
import Foreign.Marshal.Alloc
import GHC.Generics (Generic)
{-# LINE 35 "System/Clock.hsc" #-}
{-# LINE 39 "System/Clock.hsc" #-}
{-# LINE 40 "System/Clock.hsc" #-}
{-# LINE 44 "System/Clock.hsc" #-}
data Clock
= Monotonic
| Realtime
| ProcessCPUTime
| ThreadCPUTime
{-# LINE 82 "System/Clock.hsc" #-}
| MonotonicRaw
{-# LINE 90 "System/Clock.hsc" #-}
{-# LINE 92 "System/Clock.hsc" #-}
| Boottime
{-# LINE 103 "System/Clock.hsc" #-}
{-# LINE 105 "System/Clock.hsc" #-}
| MonotonicCoarse
{-# LINE 111 "System/Clock.hsc" #-}
{-# LINE 113 "System/Clock.hsc" #-}
| RealtimeCoarse
{-# LINE 119 "System/Clock.hsc" #-}
deriving (Clock -> Clock -> Bool
(Clock -> Clock -> Bool) -> (Clock -> Clock -> Bool) -> Eq Clock
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Clock -> Clock -> Bool
$c/= :: Clock -> Clock -> Bool
== :: Clock -> Clock -> Bool
$c== :: Clock -> Clock -> Bool
Eq, Int -> Clock
Clock -> Int
Clock -> [Clock]
Clock -> Clock
Clock -> Clock -> [Clock]
Clock -> Clock -> Clock -> [Clock]
(Clock -> Clock)
-> (Clock -> Clock)
-> (Int -> Clock)
-> (Clock -> Int)
-> (Clock -> [Clock])
-> (Clock -> Clock -> [Clock])
-> (Clock -> Clock -> [Clock])
-> (Clock -> Clock -> Clock -> [Clock])
-> Enum Clock
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Clock -> Clock -> Clock -> [Clock]
$cenumFromThenTo :: Clock -> Clock -> Clock -> [Clock]
enumFromTo :: Clock -> Clock -> [Clock]
$cenumFromTo :: Clock -> Clock -> [Clock]
enumFromThen :: Clock -> Clock -> [Clock]
$cenumFromThen :: Clock -> Clock -> [Clock]
enumFrom :: Clock -> [Clock]
$cenumFrom :: Clock -> [Clock]
fromEnum :: Clock -> Int
$cfromEnum :: Clock -> Int
toEnum :: Int -> Clock
$ctoEnum :: Int -> Clock
pred :: Clock -> Clock
$cpred :: Clock -> Clock
succ :: Clock -> Clock
$csucc :: Clock -> Clock
Enum, (forall x. Clock -> Rep Clock x)
-> (forall x. Rep Clock x -> Clock) -> Generic Clock
forall x. Rep Clock x -> Clock
forall x. Clock -> Rep Clock x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Clock x -> Clock
$cfrom :: forall x. Clock -> Rep Clock x
Generic, ReadPrec [Clock]
ReadPrec Clock
Int -> ReadS Clock
ReadS [Clock]
(Int -> ReadS Clock)
-> ReadS [Clock]
-> ReadPrec Clock
-> ReadPrec [Clock]
-> Read Clock
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Clock]
$creadListPrec :: ReadPrec [Clock]
readPrec :: ReadPrec Clock
$creadPrec :: ReadPrec Clock
readList :: ReadS [Clock]
$creadList :: ReadS [Clock]
readsPrec :: Int -> ReadS Clock
$creadsPrec :: Int -> ReadS Clock
Read, Int -> Clock -> ShowS
[Clock] -> ShowS
Clock -> String
(Int -> Clock -> ShowS)
-> (Clock -> String) -> ([Clock] -> ShowS) -> Show Clock
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Clock] -> ShowS
$cshowList :: [Clock] -> ShowS
show :: Clock -> String
$cshow :: Clock -> String
showsPrec :: Int -> Clock -> ShowS
$cshowsPrec :: Int -> Clock -> ShowS
Show, Typeable)
{-# LINE 132 "System/Clock.hsc" #-}
foreign import ccall unsafe clock_gettime :: Int32 -> Ptr TimeSpec -> IO CInt
{-# LINE 133 "System/Clock.hsc" #-}
foreign import ccall unsafe clock_getres :: Int32 -> Ptr TimeSpec -> IO CInt
{-# LINE 134 "System/Clock.hsc" #-}
{-# LINE 135 "System/Clock.hsc" #-}
{-# LINE 137 "System/Clock.hsc" #-}
clockToConst :: Clock -> Int32
{-# LINE 138 "System/Clock.hsc" #-}
clockToConst Monotonic = 1
{-# LINE 139 "System/Clock.hsc" #-}
clockToConst Realtime = 0
{-# LINE 140 "System/Clock.hsc" #-}
clockToConst ProcessCPUTime = 2
{-# LINE 141 "System/Clock.hsc" #-}
clockToConst ThreadCPUTime = 3
{-# LINE 142 "System/Clock.hsc" #-}
{-# LINE 144 "System/Clock.hsc" #-}
clockToConst MonotonicRaw = 4
{-# LINE 145 "System/Clock.hsc" #-}
{-# LINE 146 "System/Clock.hsc" #-}
{-# LINE 147 "System/Clock.hsc" #-}
clockToConst Boottime = 7
{-# LINE 148 "System/Clock.hsc" #-}
{-# LINE 149 "System/Clock.hsc" #-}
{-# LINE 150 "System/Clock.hsc" #-}
clockToConst MonotonicCoarse = 6
{-# LINE 151 "System/Clock.hsc" #-}
{-# LINE 152 "System/Clock.hsc" #-}
{-# LINE 153 "System/Clock.hsc" #-}
clockToConst RealtimeCoarse = 5
{-# LINE 154 "System/Clock.hsc" #-}
{-# LINE 155 "System/Clock.hsc" #-}
{-# LINE 156 "System/Clock.hsc" #-}
allocaAndPeek :: Storable a => (Ptr a -> IO ()) -> IO a
allocaAndPeek :: (Ptr a -> IO ()) -> IO a
allocaAndPeek Ptr a -> IO ()
f = (Ptr a -> IO a) -> IO a
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr a -> IO a) -> IO a) -> (Ptr a -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> Ptr a -> IO ()
f Ptr a
ptr IO () -> IO a -> IO a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek Ptr a
ptr
getTime :: Clock -> IO TimeSpec
getRes :: Clock -> IO TimeSpec
{-# LINE 175 "System/Clock.hsc" #-}
getTime :: Clock -> IO TimeSpec
getTime Clock
clk = (Ptr TimeSpec -> IO ()) -> IO TimeSpec
forall a. Storable a => (Ptr a -> IO ()) -> IO a
allocaAndPeek ((Ptr TimeSpec -> IO ()) -> IO TimeSpec)
-> (Ptr TimeSpec -> IO ()) -> IO TimeSpec
forall a b. (a -> b) -> a -> b
$! String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1_ String
"clock_gettime" (IO CInt -> IO ())
-> (Ptr TimeSpec -> IO CInt) -> Ptr TimeSpec -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Ptr TimeSpec -> IO CInt
clock_gettime (Clock -> Int32
clockToConst Clock
clk)
{-# LINE 177 "System/Clock.hsc" #-}
{-# LINE 184 "System/Clock.hsc" #-}
getRes :: Clock -> IO TimeSpec
getRes Clock
clk = (Ptr TimeSpec -> IO ()) -> IO TimeSpec
forall a. Storable a => (Ptr a -> IO ()) -> IO a
allocaAndPeek ((Ptr TimeSpec -> IO ()) -> IO TimeSpec)
-> (Ptr TimeSpec -> IO ()) -> IO TimeSpec
forall a b. (a -> b) -> a -> b
$! String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1_ String
"clock_getres" (IO CInt -> IO ())
-> (Ptr TimeSpec -> IO CInt) -> Ptr TimeSpec -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Ptr TimeSpec -> IO CInt
clock_getres (Clock -> Int32
clockToConst Clock
clk)
{-# LINE 186 "System/Clock.hsc" #-}
data TimeSpec = TimeSpec
{ TimeSpec -> Int64
sec :: {-# UNPACK #-} !Int64
, TimeSpec -> Int64
nsec :: {-# UNPACK #-} !Int64
} deriving ((forall x. TimeSpec -> Rep TimeSpec x)
-> (forall x. Rep TimeSpec x -> TimeSpec) -> Generic TimeSpec
forall x. Rep TimeSpec x -> TimeSpec
forall x. TimeSpec -> Rep TimeSpec x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TimeSpec x -> TimeSpec
$cfrom :: forall x. TimeSpec -> Rep TimeSpec x
Generic, ReadPrec [TimeSpec]
ReadPrec TimeSpec
Int -> ReadS TimeSpec
ReadS [TimeSpec]
(Int -> ReadS TimeSpec)
-> ReadS [TimeSpec]
-> ReadPrec TimeSpec
-> ReadPrec [TimeSpec]
-> Read TimeSpec
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [TimeSpec]
$creadListPrec :: ReadPrec [TimeSpec]
readPrec :: ReadPrec TimeSpec
$creadPrec :: ReadPrec TimeSpec
readList :: ReadS [TimeSpec]
$creadList :: ReadS [TimeSpec]
readsPrec :: Int -> ReadS TimeSpec
$creadsPrec :: Int -> ReadS TimeSpec
Read, Int -> TimeSpec -> ShowS
[TimeSpec] -> ShowS
TimeSpec -> String
(Int -> TimeSpec -> ShowS)
-> (TimeSpec -> String) -> ([TimeSpec] -> ShowS) -> Show TimeSpec
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TimeSpec] -> ShowS
$cshowList :: [TimeSpec] -> ShowS
show :: TimeSpec -> String
$cshow :: TimeSpec -> String
showsPrec :: Int -> TimeSpec -> ShowS
$cshowsPrec :: Int -> TimeSpec -> ShowS
Show, Typeable)
{-# LINE 205 "System/Clock.hsc" #-}
instance Storable TimeSpec where
sizeOf :: TimeSpec -> Int
sizeOf TimeSpec
_ = (Int
16)
{-# LINE 207 "System/Clock.hsc" #-}
alignment _ = 8
{-# LINE 208 "System/Clock.hsc" #-}
poke ptr ts = do
let xs :: Int64 = fromIntegral $ sec ts
{-# LINE 210 "System/Clock.hsc" #-}
xn :: Int64 = fromIntegral $ nsec ts
{-# LINE 211 "System/Clock.hsc" #-}
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr (xs)
{-# LINE 212 "System/Clock.hsc" #-}
(\hsc_ptr -> pokeByteOff hsc_ptr 8) ptr (xn)
{-# LINE 213 "System/Clock.hsc" #-}
peek ptr = do
xs :: Int64 <- (\hsc_ptr -> peekByteOff hsc_ptr 0) ptr
{-# LINE 215 "System/Clock.hsc" #-}
xn :: Int64 <- (\hsc_ptr -> peekByteOff hsc_ptr 8) ptr
{-# LINE 216 "System/Clock.hsc" #-}
return $ TimeSpec (fromIntegral xs) (fromIntegral xn)
{-# LINE 218 "System/Clock.hsc" #-}
s2ns :: Num a => a
s2ns :: a
s2ns = a
10a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
9
normalize :: TimeSpec -> TimeSpec
normalize :: TimeSpec -> TimeSpec
normalize (TimeSpec Int64
xs Int64
xn) | Int64
xn Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< Int64
0 Bool -> Bool -> Bool
|| Int64
xn Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Int64
forall a. Num a => a
s2ns = Int64 -> Int64 -> TimeSpec
TimeSpec (Int64
xs Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int64
q) Int64
r
| Bool
otherwise = Int64 -> Int64 -> TimeSpec
TimeSpec Int64
xs Int64
xn
where (Int64
q, Int64
r) = Int64
xn Int64 -> Int64 -> (Int64, Int64)
forall a. Integral a => a -> a -> (a, a)
`divMod` Int64
forall a. Num a => a
s2ns
instance Num TimeSpec where
(TimeSpec Int64
xs Int64
xn) + :: TimeSpec -> TimeSpec -> TimeSpec
+ (TimeSpec Int64
ys Int64
yn) = TimeSpec -> TimeSpec
normalize (TimeSpec -> TimeSpec) -> TimeSpec -> TimeSpec
forall a b. (a -> b) -> a -> b
$! Int64 -> Int64 -> TimeSpec
TimeSpec (Int64
xs Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int64
ys) (Int64
xn Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int64
yn)
(TimeSpec Int64
xs Int64
xn) - :: TimeSpec -> TimeSpec -> TimeSpec
- (TimeSpec Int64
ys Int64
yn) = TimeSpec -> TimeSpec
normalize (TimeSpec -> TimeSpec) -> TimeSpec -> TimeSpec
forall a b. (a -> b) -> a -> b
$! Int64 -> Int64 -> TimeSpec
TimeSpec (Int64
xs Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
ys) (Int64
xn Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
yn)
(TimeSpec Int64
xs Int64
xn) * :: TimeSpec -> TimeSpec -> TimeSpec
* (TimeSpec Int64
ys Int64
yn) = TimeSpec -> TimeSpec
normalize (TimeSpec -> TimeSpec) -> TimeSpec -> TimeSpec
forall a b. (a -> b) -> a -> b
$! Int64 -> Int64 -> TimeSpec
TimeSpec (Int64
xsi_ysi) (Int64
xni_yni)
where xsi_ysi :: Int64
xsi_ysi = Integer -> Int64
forall a. Num a => Integer -> a
fromInteger (Integer -> Int64) -> Integer -> Int64
forall a b. (a -> b) -> a -> b
$! Integer
xsiInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
*Integer
ysi
xni_yni :: Int64
xni_yni = Integer -> Int64
forall a. Num a => Integer -> a
fromInteger (Integer -> Int64) -> Integer -> Int64
forall a b. (a -> b) -> a -> b
$! (Integer
xniInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
*Integer
yni Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ (Integer
xniInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
*Integer
ysi Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
xsiInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
*Integer
yni) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
forall a. Num a => a
s2ns) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
forall a. Num a => a
s2ns
xsi :: Integer
xsi = Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger Int64
xs
ysi :: Integer
ysi = Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger Int64
ys
xni :: Integer
xni = Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger Int64
xn
yni :: Integer
yni = Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger Int64
yn
negate :: TimeSpec -> TimeSpec
negate (TimeSpec Int64
xs Int64
xn) = TimeSpec -> TimeSpec
normalize (TimeSpec -> TimeSpec) -> TimeSpec -> TimeSpec
forall a b. (a -> b) -> a -> b
$! Int64 -> Int64 -> TimeSpec
TimeSpec (Int64 -> Int64
forall a. Num a => a -> a
negate Int64
xs) (Int64 -> Int64
forall a. Num a => a -> a
negate Int64
xn)
abs :: TimeSpec -> TimeSpec
abs (TimeSpec -> TimeSpec
normalize -> TimeSpec Int64
xs Int64
xn) | Int64
xs Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== Int64
0 = TimeSpec -> TimeSpec
normalize (TimeSpec -> TimeSpec) -> TimeSpec -> TimeSpec
forall a b. (a -> b) -> a -> b
$! Int64 -> Int64 -> TimeSpec
TimeSpec Int64
0 Int64
xn
| Bool
otherwise = TimeSpec -> TimeSpec
normalize (TimeSpec -> TimeSpec) -> TimeSpec -> TimeSpec
forall a b. (a -> b) -> a -> b
$! Int64 -> Int64 -> TimeSpec
TimeSpec (Int64 -> Int64
forall a. Num a => a -> a
abs Int64
xs) (Int64 -> Int64
forall a. Num a => a -> a
signum Int64
xs Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* Int64
xn)
signum :: TimeSpec -> TimeSpec
signum (TimeSpec -> TimeSpec
normalize -> TimeSpec Int64
xs Int64
xn) | Int64
xs Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== Int64
0 = Int64 -> Int64 -> TimeSpec
TimeSpec (Int64 -> Int64
forall a. Num a => a -> a
signum Int64
xn) Int64
0
| Bool
otherwise = Int64 -> Int64 -> TimeSpec
TimeSpec (Int64 -> Int64
forall a. Num a => a -> a
signum Int64
xs) Int64
0
fromInteger :: Integer -> TimeSpec
fromInteger Integer
x = Int64 -> Int64 -> TimeSpec
TimeSpec (Integer -> Int64
forall a. Num a => Integer -> a
fromInteger Integer
q) (Integer -> Int64
forall a. Num a => Integer -> a
fromInteger Integer
r) where (Integer
q, Integer
r) = Integer
x Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
`divMod` Integer
forall a. Num a => a
s2ns
instance Eq TimeSpec where
(TimeSpec -> TimeSpec
normalize -> TimeSpec Int64
xs Int64
xn) == :: TimeSpec -> TimeSpec -> Bool
== (TimeSpec -> TimeSpec
normalize -> TimeSpec Int64
ys Int64
yn) | Bool
True Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool
es = Int64
xn Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== Int64
yn
| Bool
otherwise = Bool
es
where es :: Bool
es = Int64
xs Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== Int64
ys
instance Ord TimeSpec where
compare :: TimeSpec -> TimeSpec -> Ordering
compare (TimeSpec -> TimeSpec
normalize -> TimeSpec Int64
xs Int64
xn) (TimeSpec -> TimeSpec
normalize -> TimeSpec Int64
ys Int64
yn) | Ordering
EQ Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
os = Int64 -> Int64 -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int64
xn Int64
yn
| Bool
otherwise = Ordering
os
where os :: Ordering
os = Int64 -> Int64 -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int64
xs Int64
ys
fromNanoSecs :: Integer -> TimeSpec
fromNanoSecs :: Integer -> TimeSpec
fromNanoSecs Integer
x = Int64 -> Int64 -> TimeSpec
TimeSpec (Integer -> Int64
forall a. Num a => Integer -> a
fromInteger Integer
q) (Integer -> Int64
forall a. Num a => Integer -> a
fromInteger Integer
r) where (Integer
q, Integer
r) = Integer
x Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
`divMod` Integer
forall a. Num a => a
s2ns
toNanoSecs :: TimeSpec -> Integer
toNanoSecs :: TimeSpec -> Integer
toNanoSecs (TimeSpec (Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger -> Integer
s) (Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger -> Integer
n)) = Integer
s Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
forall a. Num a => a
s2ns Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
n
diffTimeSpec :: TimeSpec -> TimeSpec -> TimeSpec
diffTimeSpec :: TimeSpec -> TimeSpec -> TimeSpec
diffTimeSpec TimeSpec
ts1 TimeSpec
ts2 = TimeSpec -> TimeSpec
forall a. Num a => a -> a
abs (TimeSpec
ts1 TimeSpec -> TimeSpec -> TimeSpec
forall a. Num a => a -> a -> a
- TimeSpec
ts2)
{-# DEPRECATED timeSpecAsNanoSecs "Use toNanoSecs instead! Replaced timeSpecAsNanoSecs with the same signature TimeSpec -> Integer" #-}
timeSpecAsNanoSecs :: TimeSpec -> Integer
timeSpecAsNanoSecs :: TimeSpec -> Integer
timeSpecAsNanoSecs (TimeSpec Int64
s Int64
n) = Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger Int64
s Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
forall a. Num a => a
s2ns Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger Int64
n