Debian Codegraph @

Sound/Osc/Time/Thread.hs

raw symbols SCIP
1 -- | Thread operations.
2 module Sound.Osc.Time.Thread where
3
4 import Control.Concurrent {- base -}
5 import Control.Monad {- base -}
6
7 import qualified Sound.Osc.Time as Time {- hosc -}
8
9 {- | The 'pauseThread' limit (in seconds).
10 Values larger than this require a different thread delay mechanism, see 'sleepThread'.
11 The value is the number of microseconds in @maxBound::Int@.
12 -}
13 pauseThreadLimit :: Fractional n => n
14 pauseThreadLimit = fromIntegral (maxBound :: Int) / 1e6
15
16 -- | Pause current thread for the indicated duration (in seconds), see 'pauseThreadLimit'.
17 pauseThreadFor :: RealFrac n => n -> IO ()
18 pauseThreadFor n = when (n > 0) (threadDelay (floor (n * 1e6)))
19
20 -- | Pause current thread until the given time, see 'pauseThreadLimit'.
21 pauseThreadUntilTime :: RealFrac n => n -> IO ()
22 pauseThreadUntilTime t = pauseThreadFor . (t -) . realToFrac =<< Time.currentTime
23
24 {- | Sleep current thread for the indicated duration (in seconds).
25 Divides long sleeps into parts smaller than 'pauseThreadLimit'.
26 -}
27 sleepThreadFor :: RealFrac n => n -> IO ()
28 sleepThreadFor n =
29 if n >= pauseThreadLimit
30 then
31 let n' = pauseThreadLimit - 1
32 in pauseThreadFor n' >> sleepThreadFor (n - n')
33 else pauseThreadFor n
34
35 {- | Sleep current thread until the given time.
36 Divides long sleeps into parts smaller than 'pauseThreadLimit'.
37 -}
38 sleepThreadUntilTime :: RealFrac n => n -> IO ()
39 sleepThreadUntilTime t = sleepThreadFor . (t -) . realToFrac =<< Time.currentTime