/Angel/Util.hs

http://github.com/jamwt/Angel · Haskell · 19 lines · 13 code · 3 blank · 3 comment · 1 complexity · 23d26a3afdcfc2a6474c86bce0b06361 MD5 · raw file

  1. -- |various utility functions
  2. module Angel.Util where
  3. import Control.Concurrent
  4. import Control.Concurrent.STM
  5. import Control.Concurrent.STM.TVar (readTVar, writeTVar)
  6. import Control.Concurrent (threadDelay, forkIO, forkOS)
  7. -- |sleep for `s` seconds in an thread
  8. sleepSecs :: Int -> IO ()
  9. sleepSecs s = threadDelay $ s * 1000000
  10. -- |wait for the STM TVar to be non-nothing
  11. waitForWake :: TVar (Maybe Int) -> IO ()
  12. waitForWake wakeSig = atomically $ do
  13. state <- readTVar wakeSig
  14. case state of
  15. Just x -> writeTVar wakeSig Nothing
  16. Nothing -> retry