module Streamly.Internal.Data.Time
{-# DEPRECATED
"Please use the \"rate\" combinator instead of the functions in this module"
#-}
( periodic
, withClock
)
where
import Control.Monad (when)
import Control.Concurrent (threadDelay)
{-# DEPRECATED periodic "Please use the \"rate\" combinator instead" #-}
periodic :: Int -> IO () -> IO ()
periodic :: Int -> IO () -> IO ()
periodic Int
freq IO ()
action = do
IO ()
action
Int -> IO ()
threadDelay (Int
1000000 forall a. Integral a => a -> a -> a
`div` Int
freq)
Int -> IO () -> IO ()
periodic Int
freq IO ()
action
{-# DEPRECATED withClock "Please use the \"rate\" combinator instead" #-}
withClock :: IO Int -> Int -> (Int -> IO ()) -> IO ()
withClock :: IO Int -> Int -> (Int -> IO ()) -> IO ()
withClock IO Int
clock Int
freq Int -> IO ()
action = do
Int
t <- IO Int
clock
forall {b}. Int -> Int -> Int -> Int -> Int -> IO b
go Int
t Int
period Int
period Int
t Int
0
where
period :: Int
period = Int
1000000 forall a. Integral a => a -> a -> a
`div` Int
freq
go :: Int -> Int -> Int -> Int -> Int -> IO b
go Int
lastAdj Int
delay Int
tick Int
localTime Int
n = do
Int -> IO ()
action Int
localTime
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
delay forall a. Ord a => a -> a -> Bool
> Int
0) forall a b. (a -> b) -> a -> b
$ Int -> IO ()
threadDelay Int
delay
if Int
n forall a. Eq a => a -> a -> Bool
== Int
freq
then do
(Int
t, Int
newTick, Int
newDelay) <- Int -> Int -> Int -> IO (Int, Int, Int)
adjustClock Int
lastAdj Int
localTime Int
delay
Int -> Int -> Int -> Int -> Int -> IO b
go Int
t Int
newDelay Int
newTick (Int
localTime forall a. Num a => a -> a -> a
+ Int
newTick) Int
0
else Int -> Int -> Int -> Int -> Int -> IO b
go Int
lastAdj Int
delay Int
tick (Int
localTime forall a. Num a => a -> a -> a
+ Int
tick) (Int
n forall a. Num a => a -> a -> a
+ Int
1)
adjustClock :: Int -> Int -> Int -> IO (Int, Int, Int)
adjustClock Int
lastAdj Int
localTime Int
delay = do
Int
baseTime <- IO Int
clock
let newTick :: Int
newTick = Int
period forall a. Num a => a -> a -> a
+ (Int
baseTime forall a. Num a => a -> a -> a
- Int
localTime) forall a. Integral a => a -> a -> a
`div` Int
freq
lastPeriod :: Int
lastPeriod = (Int
baseTime forall a. Num a => a -> a -> a
- Int
lastAdj) forall a. Integral a => a -> a -> a
`div` Int
freq
newDelay :: Int
newDelay = forall a. Ord a => a -> a -> a
max Int
0 (Int
delay forall a. Num a => a -> a -> a
+ Int
period forall a. Num a => a -> a -> a
- Int
lastPeriod)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
baseTime, Int
newTick, Int
newDelay)