blob: 34728248c0e86448ee00364d74b5674088b874e2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NoImplicitPrelude #-}
module GHC.Event.Windows.Clock (
Clock,
Seconds,
getTime,
getClock,
-- * Specific implementations
queryPerformanceCounter,
getTickCount64
) where
import qualified GHC.Event.Windows.FFI as FFI
import Data.Maybe
import GHC.Base
import GHC.Real
-- | Monotonic clock
newtype Clock = Clock (IO Seconds)
type Seconds = Double
-- | Get the current time, in seconds since some fixed time in the past.
getTime :: Clock -> IO Seconds
getTime (Clock io) = io
-- | Figure out what time API to use, and return a 'Clock' for accessing it.
getClock :: IO Clock
getClock = tryInOrder
[ queryPerformanceCounter
, fmap Just getTickCount64
]
tryInOrder :: Monad m => [m (Maybe a)] -> m a
tryInOrder (x:xs) = x >>= maybe (tryInOrder xs) return
tryInOrder [] = undefined
mapJust :: Monad m => m (Maybe a) -> (a -> b) -> m (Maybe b)
mapJust m f = liftM (fmap f) m
queryPerformanceCounter :: IO (Maybe Clock)
queryPerformanceCounter =
FFI.queryPerformanceFrequency `mapJust` \freq ->
Clock $! do
count <- FFI.queryPerformanceCounter
let !secs = fromIntegral count / fromIntegral freq
return secs
getTickCount64 :: IO Clock
getTickCount64 =
return $! Clock $! do
msecs <- FFI.getTickCount64
return $! fromIntegral msecs / 1000
|