RE: [Haskell-cafe] STM, orElse and timed read from a channel

threadDelay is IO-only; there's no way to use threadDelay in an STM transaction. For example, if you want to wait for a TVar to go from Nothing to Just x with a timeout, you could do this: readOrTimeout :: TVar (Maybe a) -> Int -> STM (Maybe a) readOrTimeout t secs = do timeout <- registerTimeout secs let check_timeout = do b <- readTVar timeout if b then return Nothing else retry check_t = do m <- readTVar t case m of Nothing -> retry Just x -> return x atomically $ check_timeout `orElse` check_t Cheers, Simon On 29 November 2005 10:11, Joel Reymont wrote:
Simon,
How is this easier than just calling threadDelay?
Ideally, I would be looking for something like reading from a TVar with a timeout. So that you either get a Nothing (timeout) or the value from the TVar. Can I implement it using the GHC timeout thread?
Thanks, Joel
On Nov 29, 2005, at 9:20 AM, Simon Marlow wrote:
Interestingly, GHC already has a timeout thread - the I/O manager thread handles threadDelay too. It wouldn't be too hard to adapt it to do STM timeouts too, with a function like
registerTimeout :: Int -> STM (TVar Bool)
and you wait for your timeout by waiting for the TVar to contain True.

On Tue, Nov 29, 2005 at 12:00:03PM -0000, Simon Marlow wrote:
threadDelay is IO-only; there's no way to use threadDelay in an STM transaction. For example, if you want to wait for a TVar to go from Nothing to Just x with a timeout, you could do this:
readOrTimeout :: TVar (Maybe a) -> Int -> STM (Maybe a) readOrTimeout t secs = do timeout <- registerTimeout secs let check_timeout = do b <- readTVar timeout if b then return Nothing else retry check_t = do m <- readTVar t case m of Nothing -> retry Just x -> return x atomically $ check_timeout `orElse` check_t
Wouldn't it be readOrTimeout :: TVar (Maybe a) -> Int -> IO (Maybe a) ^^ ? Alternatively, it would be nice to have a new STM primitive: wailUntil :: ClockTime -> STM () so you would wait until some time-point passes, not for a number of time-units (waiting for a number of time-units wouldn't work because of retries). I think it could be efficiently implemented, wouldn't it? Best regards Tomasz -- I am searching for a programmer who is good at least in some of [Haskell, ML, C++, Linux, FreeBSD, math] for work in Warsaw, Poland

While we are waiting for ghc 6.6, could the same effect be achieved by launching the computation that writes to t with forkIO? On Nov 29, 2005, at 12:00 PM, Simon Marlow wrote:
threadDelay is IO-only; there's no way to use threadDelay in an STM transaction. For example, if you want to wait for a TVar to go from Nothing to Just x with a timeout, you could do this:
readOrTimeout :: TVar (Maybe a) -> Int -> STM (Maybe a) readOrTimeout t secs = do timeout <- registerTimeout secs let check_timeout = do b <- readTVar timeout if b then return Nothing else retry check_t = do m <- readTVar t case m of Nothing -> retry Just x -> return x atomically $ check_timeout `orElse` check_t

On Mon, Dec 12, 2005 at 10:08:56AM +0000, Joel Reymont wrote:
While we are waiting for ghc 6.6, could the same effect be achieved by launching the computation that writes to t with forkIO?
Yes, it was proposed before. Booo... nobody likes my TimeVar implementation... :-( Best regards Tomasz -- I am searching for a programmer who is good at least in some of [Haskell, ML, C++, Linux, FreeBSD, math] for work in Warsaw, Poland

If you are talking about the STM one then I saved it for later use. I'm not using timers at all now but it got to the point where I had about 3-5 different implementations stashed away. Yours is one of two remaining, Simon's being the second one :-). On Dec 12, 2005, at 10:09 AM, Tomasz Zielonka wrote:
Booo... nobody likes my TimeVar implementation... :-(
participants (3)
-
Joel Reymont
-
Simon Marlow
-
Tomasz Zielonka