@@ -46,16 +46,16 @@ import Control.Monad.Base (MonadBase (..), liftBaseDefault)
46import Control.Monad.Catch (MonadThrow, throwM)
46import Control.Monad.Catch (MonadThrow, throwM)
47import Control.Monad.State (MonadIO (..), MonadPlus (..),
47import Control.Monad.State (MonadIO (..), MonadPlus (..),
48 StateT (..), liftM, runStateT,
48 StateT (..), liftM, runStateT,
49− gets, modify)
49+ modify)
50import Control.Monad.Trans.Class (MonadTrans (lift))
50import Control.Monad.Trans.Class (MonadTrans (lift))
51import Control.Monad.Trans.Control (ComposeSt, MonadBaseControl (..),
51import Control.Monad.Trans.Control (ComposeSt, MonadBaseControl (..),
52 MonadTransControl (..),
52 MonadTransControl (..),
53 defaultLiftBaseWith,
53 defaultLiftBaseWith,
54 defaultRestoreM, liftBaseWith)
54 defaultRestoreM, liftBaseWith)
55+import Data.IORef (newIORef, readIORef, writeIORef)
55import Unsafe.Coerce (unsafeCoerce)
56import Unsafe.Coerce (unsafeCoerce)
57import Control.Monad.Trans.Recorder (MonadRecorder(..))
58import Control.Monad.Trans.Recorder (MonadRecorder(..))
58−import Data.IORef (newIORef, readIORef, writeIORef)
59--import Debug.Trace (traceM)
59--import Debug.Trace (traceM)
61import Asyncly.Threads
61import Asyncly.Threads
@@ -287,18 +287,8 @@ makeAsync = AsyncT . makeCont
287-- Controlling thread quota
287-- Controlling thread quota
288------------------------------------------------------------------------------
288------------------------------------------------------------------------------
290−-- XXX Should n be Word32 instead?
291-- | Runs a computation under a given thread limit. A limit of 0 means all new
290-- | Runs a computation under a given thread limit. A limit of 0 means all new
292-- tasks start synchronously in the current thread unless overridden by
291-- tasks start synchronously in the current thread unless overridden by
293-- 'async'.
292-- 'async'.
294threads :: MonadAsync m => Int -> AsyncT m a -> AsyncT m a
293threads :: MonadAsync m => Int -> AsyncT m a -> AsyncT m a
295−threads n process = AsyncT $ do
294+threads n action = AsyncT $ threadCtl n (runAsyncT action)
296− oldCr <- gets threadCredit
297− newCr <- liftIO $ newIORef n
298− modify $ \s -> s { threadCredit = newCr }
299− r <- runAsyncT $ process
301− modify $ \s -> s { threadCredit = oldCr }
303− ) -- restore old credit