An updatable timer is a timer for which it is possible to update the remaining time.
Methods are exposed in STM to give composability. IO wrappers for those are exported also.
Parallel and serial update politics are implemented.
In the example we start a timer with a time to wait of 10 seconds, hang 2 threads which will wait for it to finish, and update it after 5 seconds to wait for other 6 seconds. It will complete and run its action and the hanged threads after 11 seconds because of its parallel nature. The serial timer would have ringed after 16 seconds.
import Control.Concurrent import System.Timer.Updatable import Data.Maybe main = do t <- parallel (return 5) $ 10^7 forkIO $ waitIO t >>= print . (+1) . fromJust forkIO $ waitIO t >>= print . (+2) . fromJust threadDelay $ 5 * 10 ^ 6 renewIO t $ 6 * 10 ^ 6 waitIO t >>= print . fromJust
- type Delay = Int64
- data Updatable a
- wait :: Updatable a -> STM (Maybe a)
- renew :: Updatable a -> Delay -> STM ()
- waitIO :: Updatable a -> IO (Maybe a)
- renewIO :: Updatable a -> Delay -> IO ()
- parallel :: IO a -> Delay -> IO (Updatable a)
- serial :: IO a -> Delay -> IO (Updatable a)
- replacer :: IO a -> Delay -> IO (Updatable a)
- longThreadDelay :: Delay -> IO ()
Documentation
Datatype
Abstract timers that can be updated. Hanging via wait function can be done by any number of threads, which is synchronization.
wait :: Updatable a -> STM (Maybe a)Source
wait until the timer rings, or signal Nothing if timer is destroyed
IO wrappers
Builders
:: IO a | the action to run when timer rings |
-> Delay | time to wait |
-> IO (Updatable a) | the updatable parallel timer |
Create and start a parallel updatable timer. The renew action for this timer will start parallel timers. The last timer that is over will compute the given action.
:: IO a | the action to run when timer rings |
-> Delay | time to wait |
-> IO (Updatable a) | the updatable parallel timer |
Create and start a serial updatable timer. The renew action for this timer will schedule new timer after the running one. The timer will run the given action after the sum of all scheduled times is over.
:: IO a | the action to run when timer rings |
-> Delay | time to wait |
-> IO (Updatable a) | the updatable parallel timer |
Create and start a replacer updatable timer. The renew action for this timer will insert a new timer replacing the running one. The timer will run the given action after this time
Utility
longThreadDelay :: Delay -> IO ()Source
Pause the thread for the given number of microseconds. There is no guarantee that the thread will be restarted promptly after the delay, but it will not be started before then.
Similar to threadDelay, but takes a 64-bit argument. The Haskell 2010 specification says that (maxBound :: Int) is at least 2^29-1. However 2^29 microseconds is only about 538 seconds. GHC on a 32-bit machine has a 32 bit Int, but that is still less than 36 minutes. 64-bit signed integers give a maximum delay of over 292 million years, which should be sufficient.