{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
module Capnp.Rpc.Promise
( Promise
, Fulfiller
, newPromise
, newPromiseSTM
, newPromiseWithCallback
, newPromiseWithCallbackSTM
, newCallback
, newCallbackSTM
, fulfill
, fulfillSTM
, breakPromise
, breakPromiseSTM
, ErrAlreadyResolved(..)
, wait
, waitSTM
) where
import Control.Concurrent.STM
import Control.Monad.IO.Class (MonadIO, liftIO)
import qualified Control.Exception.Safe as HsExn
import Capnp.Gen.Capnp.Rpc.Pure
import Capnp.Rpc.Errors ()
data ErrAlreadyResolved = ErrAlreadyResolved deriving(Show)
instance HsExn.Exception ErrAlreadyResolved
newtype Fulfiller a = Fulfiller
{ callback :: Either Exception a -> STM ()
}
fulfillSTM :: Fulfiller a -> a -> STM ()
fulfillSTM Fulfiller{callback} val = callback (Right val)
fulfill :: MonadIO m => Fulfiller a -> a -> m ()
fulfill fulfiller = liftIO . atomically . fulfillSTM fulfiller
breakPromiseSTM :: Fulfiller a -> Exception -> STM ()
breakPromiseSTM Fulfiller{callback} exn = callback (Left exn)
breakPromise :: MonadIO m => Fulfiller a -> Exception -> m ()
breakPromise fulfiller = liftIO . atomically . breakPromiseSTM fulfiller
waitSTM :: Promise a -> STM a
waitSTM Promise{var} = do
val <- readTVar var
case val of
Nothing ->
retry
Just (Right result) ->
pure result
Just (Left exn) ->
throwSTM exn
wait :: MonadIO m => Promise a -> m a
wait = liftIO . atomically . waitSTM
newPromiseSTM :: STM (Promise a, Fulfiller a)
newPromiseSTM = do
var <- newTVar Nothing
pure
( Promise{var}
, Fulfiller
{ callback = \result -> do
val <- readTVar var
case val of
Nothing ->
writeTVar var (Just result)
Just _ ->
throwSTM ErrAlreadyResolved
}
)
newPromiseWithCallbackSTM :: (Either Exception a -> STM ()) -> STM (Promise a, Fulfiller a)
newPromiseWithCallbackSTM callback = do
(promise, Fulfiller{callback=oldCallback}) <- newPromiseSTM
pure
( promise
, Fulfiller
{ callback = \result -> oldCallback result >> callback result
}
)
newPromiseWithCallback :: MonadIO m => (Either Exception a -> STM ()) -> m (Promise a, Fulfiller a)
newPromiseWithCallback = liftIO . atomically . newPromiseWithCallbackSTM
newCallbackSTM :: (Either Exception a -> STM ()) -> STM (Fulfiller a)
newCallbackSTM = fmap snd . newPromiseWithCallbackSTM
newCallback :: MonadIO m => (Either Exception a -> STM ()) -> m (Fulfiller a)
newCallback = liftIO . atomically . newCallbackSTM
newPromise :: MonadIO m => m (Promise a, Fulfiller a)
newPromise = liftIO $ atomically newPromiseSTM
newtype Promise a = Promise
{ var :: TVar (Maybe (Either Exception a))
}
deriving(Eq)