@@ -36,8 +36,7 @@ import Data.IORef (IORef, atomicModifyIORef,
3636 modifyIORef, newIORef, readIORef,
3737 writeIORef)
3838import Data.List (delete)
39−import Data.Maybe (fromJust, isJust)
40−import Unsafe.Coerce (unsafeCoerce)
39+import Data.Maybe (isJust)
4140
4241import Strands.AsyncT
4342import Strands.Context
@@ -187,7 +186,7 @@ forkContext :: (MonadBaseControl IO m, MonadIO m, MonadThrow m)
187186 => Context -> StateT Context m ()
188187forkContext context = do
189188 child <- childContext context
190− tid <- forkFinally1 child (beforeExit child)
189+ tid <- forkFinally1 child (beforeExit child (childChannel context))
191190 updatePendingThreads context tid
192191
193192 where
@@ -205,12 +204,11 @@ forkContext context = do
205204 chan <- liftIO $ atomically newTChan
206205 -- shares the threadCredit of the parent by default
207206 return $ ctx
208− { parentChannel = Just (childChannel ctx)
209− , pendingThreads = pendingRef
207+ { pendingThreads = pendingRef
210208 , childChannel = chan
211209 }
212210
213− beforeExit ctx res = do
211+ beforeExit ctx pchan res = do
214212 tid <- myThreadId
215213 r <- case res of
216214 Left e -> do
@@ -219,10 +217,8 @@ forkContext context = do
219217 return (Just e)
220218 Right _ -> waitForChildren ctx
221219
222− -- We are guaranteed to have a parent because we are forked.
223− let p = fromJust (parentChannel ctx)
224220 signalQSemB (threadCredit ctx)
225− liftIO $ atomically $ writeTChan p (ChildDone tid r)
221+ liftIO $ atomically $ writeTChan pchan (ChildDone tid r)
226222
227223-- | Decide whether to resume the context in the same thread or a new thread
228224--
@@ -339,8 +335,7 @@ loopContextWith ioaction context = AsyncT $ do
339335 -- parameters from the current context.
340336 curCtx <- get
341337 loop context
342− { parentChannel = parentChannel curCtx
343− , pendingThreads = pendingThreads curCtx
338+ { pendingThreads = pendingThreads curCtx
344339 , childChannel = childChannel curCtx
345340 }
346341
@@ -500,7 +495,7 @@ noTrans x = AsyncT $ x >>= return . Just
500495
501496-- | Run an 'AsyncT m' computation and collect the results generated by each
502497-- thread of the computation in a list.
503−waitAsync :: forall m a b. (MonadIO m, MonadCatch m)
498+waitAsync :: forall m a. (MonadIO m, MonadCatch m)
504499 => (a -> AsyncT m a) -> AsyncT m a -> m ()
505500waitAsync finalizer m = do
506501 childChan <- liftIO $ atomically newTChan
@@ -528,7 +523,7 @@ waitAsync finalizer m = do
528523-- the computation when the computation is done
529524gatherResult :: MonadIO m => IORef [a] -> a -> AsyncT m a
530525gatherResult ref r = do
531− liftIO $ atomicModifyIORef ref $ \rs -> (r : rs, rs)
526+ liftIO $ atomicModifyIORef ref $ \rs -> (r : rs, ())
532527 mzero
533528
534529-- | Run an 'AsyncT m' computation and collect the results generated by each