Copyright | (c) Tim Watson 2017 |
---|---|
License | BSD3 (see the file LICENSE) |
Maintainer | Tim Watson <watson.timothy@gmail.com> |
Stability | experimental |
Portability | non-portable (requires concurrency) |
Safe Haskell | None |
Language | Haskell98 |
This module provides a wrap around a simple Timer
that can be started,
stopped, reset, cleared, and read. A convenient function is provided for
creating a Match
expression for the timer.
- Notes
The timers defined in this module are based on a TVar Bool
. When the
client program is -threaded
(i.e. rtsSupportsBoundThreads == True
), then
the timers are set using registerDelay
, which is very efficient and relies
only no the RTS IO Manager. When we're not -threaded
, we fall back to using
Control.Distributed.Process.Extras.Timer to set the TVar
, which has much
the same effect, but requires us to spawn a process to handle setting the
TVar
- a process which could theoretically die before setting the variable.
- data Timer
- type TimerKey = Int
- delayTimer :: Delay -> Timer
- startTimer :: Delay -> Process Timer
- stopTimer :: Timer -> Process Timer
- resetTimer :: Timer -> Delay -> Process Timer
- clearTimer :: Maybe TimerRef -> Process ()
- matchTimeout :: Timer -> [Match (Either TimedOut Message)]
- matchKey :: TimerKey -> Timer -> [Match (Either TimedOut Message)]
- matchRun :: (TimerKey -> Process Message) -> TimerKey -> Timer -> [Match Message]
- isActive :: Timer -> Bool
- readTimer :: TVar Bool -> STM TimedOut
- data TimedOut
Documentation
We hold timers in 2 states, each described by a Delay. isActive = isJust . mtSignal the TimerRef is optional since we only use the Timer module from extras when we're unable to registerDelay (i.e. not running under -threaded)
delayTimer :: Delay -> Timer Source #
Creates a default Timer
which is inactive.
startTimer :: Delay -> Process Timer Source #
Starts a Timer
Will use the GHC registerDelay
API if rtsSupportsBoundThreads == True
stopTimer :: Timer -> Process Timer Source #
Stops a previously started Timer
. Has no effect if the Timer
is inactive.
clearTimer :: Maybe TimerRef -> Process () Source #
Clears/cancels a running timer. Has no effect if the Timer
is inactive.
matchTimeout :: Timer -> [Match (Either TimedOut Message)] Source #
Creates a Match
for a given timer, for use with Cloud Haskell's messaging
primitives for selective receives.
readTimer :: TVar Bool -> STM TimedOut Source #
Reads a given TVar Bool
for a timer, and returns STM TimedOut
once the
variable is set to true. Will retry
in the meanwhile.
Used during STM reads on Timers and to implement blocking. Since timers
can be associated with a TimerKey, the second constructor for this type
yields a key indicating whic Timer it refers to. Note that the user is
responsible for establishing and maintaining the mapping between Timer
s
and their keys.