module Control.Concurrent.Async.Lifted.Extra where
import Control.Concurrent.Async.Lifted
import Control.Concurrent.STM
import Control.Concurrent
import Control.Concurrent.MSem (new, with)
import Data.Traversable
import Control.Applicative
import Control.Monad
import Control.Monad.Trans.Control
import Control.Monad.Fix
import Control.Monad.Base
import Data.Foldable (Foldable, traverse_)
sequencePool :: (Traversable t, MonadBaseControl IO m)
=> Int -> t (m a) -> m (t a)
sequencePool max xs = do
sem <- liftBase $ new max
runConcurrently $ traverse (Concurrently . liftBaseOp_ (with sem)) xs
mapPool :: (Traversable t, MonadBaseControl IO m)
=> Int
-> (a -> m b)
-> t a
-> m (t b)
mapPool max f xs = do
sem <- liftBase $ new max
mapConcurrently (liftBaseOp_ (with sem) . f) xs
sequenceConcurrently :: (Traversable t, MonadBaseControl IO m)
=> t (m a) -> m (t a)
sequenceConcurrently = runConcurrently . traverse Concurrently
mapConcurrently_ :: (Foldable t, MonadBaseControl IO m) => (a -> m b) -> t a -> m ()
mapConcurrently_ f = runConcurrently . traverse_ (Concurrently . f)
forConcurrently_ :: (Foldable t, MonadBaseControl IO m) => t a -> (a -> m b) -> m ()
forConcurrently_ = flip mapConcurrently_
fixAsync :: (MonadFix m, MonadBaseControl IO m)
=> (Async (StM m a) -> m a) -> m (Async (StM m a))
fixAsync f = mdo
this <- async $ f this
return this
fixAsyncBound :: (MonadFix m, MonadBaseControl IO m)
=> (Async (StM m a) -> m a) -> m (Async (StM m a))
fixAsyncBound f = mdo
this <- asyncBound $ f this
return this
fixAsyncOn :: (MonadFix m, MonadBaseControl IO m)
=> Int -> (Async (StM m a) -> m a) -> m (Async (StM m a))
fixAsyncOn cpu f = mdo
this <- asyncOn cpu $ f this
return this
fixAsyncWithUnmask :: (MonadFix m, MonadBaseControl IO m)
=> (Async (StM m a) -> (forall b . m b -> m b) -> m a)
-> m (Async (StM m a))
fixAsyncWithUnmask f = mdo
this <- asyncWithUnmask $ f this
return this
fixAsyncOnWithUnmask :: (MonadFix m, MonadBaseControl IO m)
=> Int -> (Async (StM m a) -> (forall b . m b -> m b) -> m a)
-> m (Async (StM m a))
fixAsyncOnWithUnmask cpu f = mdo
this <- asyncWithUnmask $ f this
return this
withParent :: MonadBaseControl IO m
=> Async a -> m b -> m (Async (StM m b))
withParent parent act = async $ link parent >> act