module Haskus.Utils.STM.Future
( Future
, FutureSource
, newFuture
, newFutureIO
, waitFuture
, pollFuture
, pollFutureIO
, setFuture
, setFutureIO
, setFuture'
)
where
import Haskus.Utils.STM
import Haskus.Utils.Flow
newtype Future a = Future (TMVar a)
newtype FutureSource a = FutureSource (TMVar a)
newFuture :: STM (Future a, FutureSource a)
newFuture = do
m <- newEmptyTMVar
return (Future m, FutureSource m)
newFutureIO :: MonadIO m => m (Future a, FutureSource a)
newFutureIO = atomically newFuture
setFuture :: a -> FutureSource a -> STM ()
setFuture a m = void (setFuture' a m)
setFutureIO :: MonadIO m => a -> FutureSource a -> m ()
setFutureIO a m = atomically (setFuture a m)
setFuture' :: a -> FutureSource a -> STM Bool
setFuture' a (FutureSource m) = tryPutTMVar m a
waitFuture :: Future a -> STM a
waitFuture (Future m) = readTMVar m
pollFuture :: Future a -> STM (Maybe a)
pollFuture (Future m) = tryReadTMVar m
pollFutureIO :: MonadIO m => Future a -> m (Maybe a)
pollFutureIO = atomically . pollFuture