Safe Haskell | Safe-Inferred |
---|
Choose between the return value of an IO action and an STM operation, depending on which is available first.
Documentation
runOrElse :: IO a -> STM b -> IO (Either a b)Source
runs the IO action runOrElse
io stmio
. If its result is
available when
itself returns, then that value is
used as the function's return value. If not, the STM operation
runOrElse
stm
is attempted. Then, whichever of io
's and stm
's return
value is then available first is returned from
, with
a preference to that of runOrElse
io
if both are available.
reverses this priority.
runOrElse'
It can happen that stm
is never attempted. If it is, however, its
result is used as return value only if it is available before
that of io
. Note that in that case, a long-running io
will keep
running until completed, even if
has already returned
with the result of runOrElse
stm
. A future version will probably kill off
the io
thread if its value is not needed (i.e. if that of stm
value is used), but that is not currently the case.
runOrElse' :: STM a -> IO b -> IO (Either a b)Source
A version of runOrElse
that prefers the STM operation to the IO
action. In this case, the IO action is always run, but its value
is only used if the return value of the STM operation is not
available when the function returns.
The same caveat regarding long-running IO operations as for
runOrElse
also applies here.
runOrTakeTMVar :: IO a -> TMVar b -> IO (Either a b)Source
.
runOrTakeTMVar
io tm = runOrElse
io (takeTMVar
tm)
runOrTakeTMVar' :: TMVar a -> IO b -> IO (Either a b)Source
.
runOrTakeTMVar'
tm io = runOrElse'
(takeTMVar
tm) io