on master help sign in
Do not use a blocking wait when dispatching workers
commit71b4134867bed3747e586afd86735f19b87707b4 authorHarendra Kumar <harendra.kumar@gmail.com> dateTue 17 Oct 2017, 07:36 (9 years ago) parentcef2f7d
2 files changed+12−15
src/Asyncly/AsyncT.hs+12−11 test/Main.hs−4
src/Asyncly/AsyncT.hs+12−11
@@ -379,19 +379,20 @@ pushWorker :: MonadAsync m => Context m a -> m ()
379379pushWorker ctx =
380380 doFork (runqueue ctx) (handleChildException ctx) >>= addThread ctx
381381
382+-- XXX When the queue is LIFO we can put a limit on the number of dispatches.
383+-- Also, if a worker blocks on the output queue we can decide if we want to
384+-- block or make it go away entirely, depending on the number of workers and
385+-- the type of the queue.
382386{-# INLINE sendWorkerWait #-}
383387sendWorkerWait :: MonadAsync m => Context m a -> m ()
384−sendWorkerWait ctx = dispatch >> void (liftIO $ takeMVar (doorBell ctx))
385−
386− where
387−
388− dispatch = do
389− liftIO $ threadDelay 200
390− output <- liftIO $ readIORef (outputQueue ctx)
391− when (null output) $ do
392− done <- queueEmpty ctx
393− when (not done) $ (pushWorker ctx) >> dispatch
394−
388+sendWorkerWait ctx = do
389+ liftIO $ threadDelay 200
390+ output <- liftIO $ readIORef (outputQueue ctx)
391+ when (null output) $ do
392+ done <- queueEmpty ctx
393+ if (not done)
394+ then (pushWorker ctx) >> sendWorkerWait ctx
395+ else void (liftIO $ takeMVar (doorBell ctx))
395396
396397data ContextUsedAfterEOF = ContextUsedAfterEOF deriving Show
397398instance Exception ContextUsedAfterEOF
test/Main.hs−4
@@ -150,9 +150,6 @@ main = hspec $ do
150150 ((t 4 <|> t 8) <> (t 1 <|> t 2))
151151 <|> ((t 4 <|> t 8) <> (t 1 <|> t 2)))
152152 `shouldReturn` ([4,4,8,8,1,1,2,2])
153− -- FIXME: These two keep failing intermittently on Mac OS X
154− -- Need to examine and fix the tests.
155− {-
156153 it "Nest <|>, <=>, <|> (1)" $
157154 let t = timed
158155 in toList (
@@ -165,7 +162,6 @@ main = hspec $ do
165162 ((t 4 <|> t 8) <=> (t 1 <|> t 2))
166163 <|> ((t 4 <|> t 9) <=> (t 1 <|> t 2)))
167164 `shouldReturn` ([4,4,1,1,8,2,9,2])
168− -}
169165 it "Nest <|>, <|>, <|>" $
170166 let t = timed
171167 in toList (