{-# options_haddock prune #-}
module Polysemy.Conc.Sync (
module Polysemy.Conc.Sync,
module Polysemy.Conc.Effect.Sync,
) where
import qualified Polysemy.Time as Time
import Polysemy.Time (Time, TimeUnit)
import Polysemy.Conc.Effect.Mask (Mask, mask, restore)
import Polysemy.Scoped (scoped_)
import qualified Polysemy.Conc.Effect.Sync as Sync
import Polysemy.Conc.Effect.Sync
whileEmpty ::
∀ a r .
Member (Sync a) r =>
Sem r () ->
Sem r ()
whileEmpty :: forall a (r :: EffectRow).
Member (Sync a) r =>
Sem r () -> Sem r ()
whileEmpty Sem r ()
action =
Sem r ()
spin
where
spin :: Sem r ()
spin = do
Sem r ()
action
Sem r Bool -> Sem r () -> Sem r ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM (Bool -> Bool
not (Bool -> Bool) -> Sem r Bool -> Sem r Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall d (r :: EffectRow). Member (Sync d) r => Sem r Bool
Sync.empty @a) Sem r ()
spin
whileEmptyInterval ::
∀ a u t d r .
TimeUnit u =>
Members [Time t d, Sync a] r =>
u ->
Sem r () ->
Sem r ()
whileEmptyInterval :: forall a u t d (r :: EffectRow).
(TimeUnit u, Members '[Time t d, Sync a] r) =>
u -> Sem r () -> Sem r ()
whileEmptyInterval u
interval Sem r ()
action =
Sem r ()
spin
where
spin :: Sem r ()
spin = do
Sem r ()
action
Sem r Bool -> Sem r () -> Sem r ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM (Bool -> Bool
not (Bool -> Bool) -> Sem r Bool -> Sem r Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall d (r :: EffectRow). Member (Sync d) r => Sem r Bool
Sync.empty @a) (forall t d u (r :: EffectRow).
(TimeUnit u, Member (Time t d) r) =>
u -> Sem r ()
Time.sleep @t @d u
interval Sem r () -> Sem r () -> Sem r ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Sem r ()
spin)
withSync ::
∀ d r .
Member (ScopedSync d) r =>
InterpreterFor (Sync d) r
withSync :: forall d (r :: EffectRow).
Member (ScopedSync d) r =>
InterpreterFor (Sync d) r
withSync =
Sem (Sync d : r) a -> Sem r a
forall (effect :: (* -> *) -> * -> *) (r :: EffectRow).
Member (Scoped_ effect) r =>
InterpreterFor effect r
scoped_
lock ::
∀ l r a .
Members [Sync l, Resource] r =>
l ->
Sem r a ->
Sem r a
lock :: forall l (r :: EffectRow) a.
Members '[Sync l, Resource] r =>
l -> Sem r a -> Sem r a
lock l
l Sem r a
ma =
Sem r a -> Sem r Bool -> Sem r a
forall (r :: EffectRow) a b.
Member Resource r =>
Sem r a -> Sem r b -> Sem r a
finally (forall d (r :: EffectRow). Member (Sync d) r => Sem r d
takeBlock @l Sem r l -> Sem r a -> Sem r a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Sem r a
ma) (l -> Sem r Bool
forall d (r :: EffectRow). Member (Sync d) r => d -> Sem r Bool
putTry l
l)
{-# inline lock #-}
clear ::
∀ a r .
Member (Sync a) r =>
Sem r ()
clear :: forall a (r :: EffectRow). Member (Sync a) r => Sem r ()
clear =
Sem r (Maybe a) -> Sem r ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (forall d (r :: EffectRow). Member (Sync d) r => Sem r (Maybe d)
takeTry @a)
{-# inline clear #-}
modify ::
∀ a b r .
Members [Sync a, Mask, Resource] r =>
(a -> Sem r (a, b)) ->
Sem r b
modify :: forall a b (r :: EffectRow).
Members '[Sync a, Mask, Resource] r =>
(a -> Sem r (a, b)) -> Sem r b
modify a -> Sem r (a, b)
m =
Sem (RestoreMask : r) b -> Sem r b
forall (r :: EffectRow).
Member Mask r =>
InterpreterFor RestoreMask r
mask do
a
a <- Sem (RestoreMask : r) a
forall d (r :: EffectRow). Member (Sync d) r => Sem r d
takeBlock
(a
a', b
b) <- Sem (RestoreMask : r) (a, b)
-> Sem (RestoreMask : r) () -> Sem (RestoreMask : r) (a, b)
forall (r :: EffectRow) a b.
Member Resource r =>
Sem r a -> Sem r b -> Sem r a
onException (Sem (RestoreMask : r) (a, b) -> Sem (RestoreMask : r) (a, b)
forall (r :: EffectRow) a.
Member RestoreMask r =>
Sem r a -> Sem r a
restore (Sem r (a, b) -> Sem (RestoreMask : r) (a, b)
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
Sem r a -> Sem (e : r) a
raise (a -> Sem r (a, b)
m a
a))) (a -> Sem (RestoreMask : r) ()
forall d (r :: EffectRow). Member (Sync d) r => d -> Sem r ()
putBlock a
a)
b
b b -> Sem (RestoreMask : r) () -> Sem (RestoreMask : r) b
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ a -> Sem (RestoreMask : r) ()
forall d (r :: EffectRow). Member (Sync d) r => d -> Sem r ()
putBlock a
a'
{-# inline modify #-}
modify_ ::
∀ a r .
Members [Sync a, Mask, Resource] r =>
(a -> Sem r a) ->
Sem r ()
modify_ :: forall a (r :: EffectRow).
Members '[Sync a, Mask, Resource] r =>
(a -> Sem r a) -> Sem r ()
modify_ a -> Sem r a
m =
Sem (RestoreMask : r) () -> Sem r ()
forall (r :: EffectRow).
Member Mask r =>
InterpreterFor RestoreMask r
mask do
a
a <- Sem (RestoreMask : r) a
forall d (r :: EffectRow). Member (Sync d) r => Sem r d
takeBlock
a
a' <- Sem (RestoreMask : r) a
-> Sem (RestoreMask : r) () -> Sem (RestoreMask : r) a
forall (r :: EffectRow) a b.
Member Resource r =>
Sem r a -> Sem r b -> Sem r a
onException (Sem (RestoreMask : r) a -> Sem (RestoreMask : r) a
forall (r :: EffectRow) a.
Member RestoreMask r =>
Sem r a -> Sem r a
restore (Sem r a -> Sem (RestoreMask : r) a
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
Sem r a -> Sem (e : r) a
raise (a -> Sem r a
m a
a))) (a -> Sem (RestoreMask : r) ()
forall d (r :: EffectRow). Member (Sync d) r => d -> Sem r ()
putBlock a
a)
a -> Sem (RestoreMask : r) ()
forall d (r :: EffectRow). Member (Sync d) r => d -> Sem r ()
putBlock a
a'
{-# inline modify_ #-}
modifyMasked ::
∀ a b r .
Members [Sync a, Mask, Resource] r =>
(a -> Sem r (a, b)) ->
Sem r b
modifyMasked :: forall a b (r :: EffectRow).
Members '[Sync a, Mask, Resource] r =>
(a -> Sem r (a, b)) -> Sem r b
modifyMasked a -> Sem r (a, b)
m =
Sem (RestoreMask : r) b -> Sem r b
forall (r :: EffectRow).
Member Mask r =>
InterpreterFor RestoreMask r
mask do
a
a <- Sem (RestoreMask : r) a
forall d (r :: EffectRow). Member (Sync d) r => Sem r d
takeBlock
(a
a', b
b) <- Sem (RestoreMask : r) (a, b)
-> Sem (RestoreMask : r) () -> Sem (RestoreMask : r) (a, b)
forall (r :: EffectRow) a b.
Member Resource r =>
Sem r a -> Sem r b -> Sem r a
onException (Sem r (a, b) -> Sem (RestoreMask : r) (a, b)
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
Sem r a -> Sem (e : r) a
raise (a -> Sem r (a, b)
m a
a)) (a -> Sem (RestoreMask : r) ()
forall d (r :: EffectRow). Member (Sync d) r => d -> Sem r ()
putBlock a
a)
b
b b -> Sem (RestoreMask : r) () -> Sem (RestoreMask : r) b
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ a -> Sem (RestoreMask : r) ()
forall d (r :: EffectRow). Member (Sync d) r => d -> Sem r ()
putBlock a
a'
{-# inline modifyMasked #-}
modifyMasked_ ::
∀ a r .
Members [Sync a, Mask, Resource] r =>
(a -> Sem r a) ->
Sem r ()
modifyMasked_ :: forall a (r :: EffectRow).
Members '[Sync a, Mask, Resource] r =>
(a -> Sem r a) -> Sem r ()
modifyMasked_ a -> Sem r a
m =
Sem (RestoreMask : r) () -> Sem r ()
forall (r :: EffectRow).
Member Mask r =>
InterpreterFor RestoreMask r
mask do
a
a <- Sem (RestoreMask : r) a
forall d (r :: EffectRow). Member (Sync d) r => Sem r d
takeBlock
a
a' <- Sem (RestoreMask : r) a
-> Sem (RestoreMask : r) () -> Sem (RestoreMask : r) a
forall (r :: EffectRow) a b.
Member Resource r =>
Sem r a -> Sem r b -> Sem r a
onException (Sem r a -> Sem (RestoreMask : r) a
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
Sem r a -> Sem (e : r) a
raise (a -> Sem r a
m a
a)) (a -> Sem (RestoreMask : r) ()
forall d (r :: EffectRow). Member (Sync d) r => d -> Sem r ()
putBlock a
a)
a -> Sem (RestoreMask : r) ()
forall d (r :: EffectRow). Member (Sync d) r => d -> Sem r ()
putBlock a
a'
{-# inline modifyMasked_ #-}
use ::
∀ a b r .
Members [Sync a, Mask, Resource] r =>
(a -> Sem r b) ->
Sem r b
use :: forall a b (r :: EffectRow).
Members '[Sync a, Mask, Resource] r =>
(a -> Sem r b) -> Sem r b
use a -> Sem r b
m =
Sem (RestoreMask : r) b -> Sem r b
forall (r :: EffectRow).
Member Mask r =>
InterpreterFor RestoreMask r
mask do
a
a <- Sem (RestoreMask : r) a
forall d (r :: EffectRow). Member (Sync d) r => Sem r d
takeBlock
Sem (RestoreMask : r) b
-> Sem (RestoreMask : r) () -> Sem (RestoreMask : r) b
forall (r :: EffectRow) a b.
Member Resource r =>
Sem r a -> Sem r b -> Sem r a
finally (Sem (RestoreMask : r) b -> Sem (RestoreMask : r) b
forall (r :: EffectRow) a.
Member RestoreMask r =>
Sem r a -> Sem r a
restore (Sem r b -> Sem (RestoreMask : r) b
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
Sem r a -> Sem (e : r) a
raise (a -> Sem r b
m a
a))) (a -> Sem (RestoreMask : r) ()
forall d (r :: EffectRow). Member (Sync d) r => d -> Sem r ()
putBlock a
a)
{-# inline use #-}
useMasked ::
∀ a b r .
Members [Sync a, Mask, Resource] r =>
(a -> Sem r b) ->
Sem r b
useMasked :: forall a b (r :: EffectRow).
Members '[Sync a, Mask, Resource] r =>
(a -> Sem r b) -> Sem r b
useMasked a -> Sem r b
m =
Sem (RestoreMask : r) b -> Sem r b
forall (r :: EffectRow).
Member Mask r =>
InterpreterFor RestoreMask r
mask do
a
a <- Sem (RestoreMask : r) a
forall d (r :: EffectRow). Member (Sync d) r => Sem r d
takeBlock
Sem (RestoreMask : r) b
-> Sem (RestoreMask : r) () -> Sem (RestoreMask : r) b
forall (r :: EffectRow) a b.
Member Resource r =>
Sem r a -> Sem r b -> Sem r a
finally (Sem r b -> Sem (RestoreMask : r) b
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
Sem r a -> Sem (e : r) a
raise (a -> Sem r b
m a
a)) (a -> Sem (RestoreMask : r) ()
forall d (r :: EffectRow). Member (Sync d) r => d -> Sem r ()
putBlock a
a)
{-# inline useMasked #-}