Something like this. If someone inserts a timer while we are doing our checking we can always catch it on the next iteration of the loop.

--- Now runs unblocked
checkTimers :: IO ()
checkTimers =
    do t <- readMVar timers -- takes it and puts it back
       case M.size t of
         -- no timers
         0 -> threadDelay timeout
         -- some timers
         n -> do let (key@(time, name), io) = M.findMin t
                 now <- getClockTime
                 if (time <= now)
                    then do modifyMVar_ timers $ \a ->
                                return $! M.delete key a
                            try $ io -- don't think we care
                            return ()
                    else threadDelay timeout
       checkTimers

On Dec 15, 2005, at 9:39 AM, Tomasz Zielonka wrote:

Perhaps you could use modifyMVar:

http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control- Concurrent-MVar.html#v%3AmodifyMVar

  modifyMVar_ :: MVar a -> (a -> IO a) -> IO ()

  A safe wrapper for modifying the contents of an MVar. Like withMVar,
  modifyMVar will replace the original contents of the MVar if an
  exception is raised during the operation.

--
http://wagerlabs.com/





_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to