1
+ {-|
2
+ Module : Control.Concurrent.Async.Timer.Internal
3
+ Description : Implementation of asynchronous Timers
4
+ Copyright : (c) Moritz Clasmeier 2016, 2018
5
+ License : BSD3
6
+
7
+ Stability : experimental
8
+ Portability : POSIX
9
+
10
+ This module contains the internal implementation of asynchronous
11
+ timers.
12
+ -}
13
+
1
14
{-# LANGUAGE LambdaCase #-}
2
15
3
16
module Control.Concurrent.Async.Timer.Internal where
@@ -17,6 +30,8 @@ import UnliftIO.STM
17
30
data Timer = Timer { timerMVar :: MVar ()
18
31
, timerControl :: TBQueue TimerCommand }
19
32
33
+ -- | Timer commands that can be sent over a timer control channel to
34
+ -- an asynchronous timer.
20
35
data TimerCommand = TimerReset deriving (Show , Eq )
21
36
22
37
-- | Type of a timer configuration.
@@ -25,24 +40,29 @@ data TimerConf = TimerConf { _timerConfInitDelay :: Int
25
40
26
41
-- | Sleep 'dt' milliseconds.
27
42
millisleep :: MonadIO m => Int -> m ()
28
- millisleep dt = threadDelay (fromIntegral dt * 10 ^ 3 )
43
+ millisleep dt = threadDelay (dt * 10 ^ 3 )
29
44
30
45
-- | Default timer configuration specifies no initial delay and an
31
46
-- interval delay of 1s.
32
- defaultTimerConf :: TimerConf
33
- defaultTimerConf = TimerConf { _timerConfInitDelay = 0
34
- , _timerConfInterval = 1000 }
47
+ defaultConf :: TimerConf
48
+ defaultConf = TimerConf { _timerConfInitDelay = 0
49
+ , _timerConfInterval = 1000 }
35
50
36
51
-- | Set the initial delay in the provided timer configuration.
37
- timerConfSetInitDelay :: Int -> TimerConf -> TimerConf
38
- timerConfSetInitDelay n conf = conf { _timerConfInitDelay = n }
52
+ setInitDelay :: Int -> TimerConf -> TimerConf
53
+ setInitDelay n conf = conf { _timerConfInitDelay = n }
39
54
40
55
-- | Set the interval delay in the provided timer configuration.
41
- timerConfSetInterval :: Int -> TimerConf -> TimerConf
42
- timerConfSetInterval n conf = conf { _timerConfInterval = n }
56
+ setInterval :: Int -> TimerConf -> TimerConf
57
+ setInterval n conf = conf { _timerConfInterval = n }
43
58
44
59
-- | Timer loop to be executed within in a timer thread.
45
- timerLoop :: MonadUnliftIO m => Int -> Int -> Timer -> m ()
60
+ timerLoop
61
+ :: MonadUnliftIO m
62
+ => Int
63
+ -> Int
64
+ -> Timer
65
+ -> m ()
46
66
timerLoop initDelay intervalDelay timer = go initDelay
47
67
48
68
where go delay = do
@@ -59,12 +79,18 @@ timerLoop initDelay intervalDelay timer = go initDelay
59
79
readCmd = atomically $ readTBQueue (timerControl timer)
60
80
61
81
-- | Wait for the next synchronization event on the givem timer.
62
- timerWait :: MonadUnliftIO m => Timer -> m ()
63
- timerWait = void . takeMVar . timerMVar
82
+ wait
83
+ :: MonadUnliftIO m
84
+ => Timer
85
+ -> m ()
86
+ wait = void . takeMVar . timerMVar
64
87
65
88
-- | Reset the provided timer.
66
- timerReset :: MonadUnliftIO m => Timer -> m ()
67
- timerReset timer =
89
+ reset
90
+ :: MonadUnliftIO m
91
+ => Timer
92
+ -> m ()
93
+ reset timer =
68
94
atomically $ writeTBQueue (timerControl timer) TimerReset
69
95
70
96
-- | Spawn a timer thread based on the provided timer configuration
0 commit comments