@@ -43,7 +43,7 @@ import Control.Distributed.Process.Internal.StrictList
4343 ( StrictList (.. )
4444 , append
4545 )
46- import Data.Maybe ( fromJust )
46+ import Control.Monad ( join )
4747import GHC.MVar (MVar (MVar ))
4848import GHC.IO (IO (IO ), unIO )
4949import GHC.Exts (mkWeak #)
@@ -126,16 +126,21 @@ dequeue :: forall m a.
126126 -> [MatchOn m a ] -- ^ List of matches
127127 -> IO (Maybe a ) -- ^ 'Nothing' only on timeout
128128dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJust $
129- case blockSpec of
130- Timeout n -> timeout n $ fmap fromJust run
131- _other ->
132- case chunks of
133- [Right ports] -> -- channels only, this is easy:
134- case blockSpec of
135- NonBlocking -> atomically $ waitChans ports (return Nothing )
136- _ -> atomically $ waitChans ports retry
137- -- no onException needed
138- _other -> run
129+ case chunks of
130+ [Right ports] -> -- channels only, this is easy:
131+ case blockSpec of
132+ NonBlocking -> atomically $ waitChans ports (return Nothing )
133+ Blocking -> atomically $ waitChans ports retry
134+ -- no onException needed
135+ Timeout n -> do
136+ -- Arming the timer is not cheap, and get can get
137+ -- much higher throughput in cases where the mailbox
138+ -- is not empty by first checking if we even need a timeout
139+ r <- atomically $ waitChans ports (return Nothing )
140+ case r of
141+ Just _ -> return r
142+ Nothing -> join <$> timeout n (atomically $ waitChans ports retry)
143+ _other -> run
139144 where
140145 -- Decrement counter is smth is returned from the queue,
141146 -- this is safe to use as method is called under a mask
@@ -155,7 +160,13 @@ dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJus
155160 Nothing -> return xs
156161 Just x -> grabNew (Snoc xs x)
157162 arr' <- grabNew arr
158- goCheck chunks arr'
163+ checked <- goCheck chunks arr'
164+ case checked of
165+ Left r -> return r
166+ Right old -> case blockSpec of
167+ NonBlocking -> returnOld old Nothing
168+ Blocking -> goWait old
169+ Timeout n -> join <$> timeout n (goWait old)
159170
160171 -- Yields the value of the first succesful STM transaction as
161172 -- @Just (Left v)@. If all transactions fail, yields the value of the second
@@ -169,20 +180,20 @@ dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJus
169180 -- mailbox. For channel matches, we do a non-blocking check at
170181 -- this point.
171182 --
172- -- Yields @Just (Left a)@ when a channel is matched, @Just (Right a)@
173- -- when a message is matched and @Nothing@ when there are no messages and we
174- -- aren't blocking.
175- --
183+ -- Yields @Left ( Just (Left a)) @ when a channel is matched and
184+ -- @Left (Just (Right a))@ when a message is matched. When nothing
185+ -- matched it yields @Right old@: the messages to hold on to, for the
186+ -- caller to decide whether to wait for more.
176187 goCheck :: MatchChunks m a
177188 -> StrictList m -- messages to check, in this order
178- -> IO (Maybe (Either a a ))
189+ -> IO (Either ( Maybe (Either a a )) ( StrictList m ))
179190
180- goCheck [] old = goWait old
191+ goCheck [] old = return ( Right old)
181192
182193 goCheck (Right ports : rest) old = do
183194 r <- atomically $ waitChans ports (return Nothing ) -- does not block
184195 case r of
185- Just _ -> returnOld old r
196+ Just _ -> Left <$> returnOld old r
186197 Nothing -> goCheck rest old
187198
188199 goCheck (Left matches : rest) old = do
@@ -192,7 +203,7 @@ dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJus
192203 -- of passing around restore and setting up exception handlers is
193204 -- high. So just don't use expensive matchIfs!
194205 case checkArrived matches old of
195- (old', Just r) -> returnOld old' (Just (Right r))
206+ (old', Just r) -> Left <$> returnOld old' (Just (Right r))
196207 (old', Nothing ) -> goCheck rest old'
197208 -- use the result list, which is now left-biased
198209
@@ -207,12 +218,8 @@ dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJus
207218 mkSTM (Right ports : rest)
208219 = foldr orElse (mkSTM rest) (map (fmap Right ) ports)
209220
210- waitIncoming :: IO (Maybe (Either m a ))
211- waitIncoming = case blockSpec of
212- NonBlocking -> atomically $ fmap Just stm `orElse` return Nothing
213- _ -> atomically $ fmap Just stm
214- where
215- stm = mkSTM chunks
221+ waitIncoming :: IO (Either m a )
222+ waitIncoming = atomically (mkSTM chunks)
216223
217224 --
218225 -- The initial pass didn't find a message, so now we go into blocking
@@ -223,23 +230,20 @@ dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJus
223230 --
224231 goWait :: StrictList m -> IO (Maybe (Either a a ))
225232 goWait old = do
226- r <- waitIncoming `onException` putMVar arrived old
227- case r of
228- -- Nothing => non-blocking and no message
229- Nothing -> returnOld old Nothing
230- Just e -> case e of
231- --
232- -- Left => message arrived in the process mailbox. We now have to
233- -- run through the MatchChunks checking each one, because we might
234- -- have a situation where the first chunk fails to match and the
235- -- second chunk is a channel match and there *is* a message in the
236- -- channel. In that case the channel wins.
237- --
238- Left m -> goCheck1 chunks m old
239- --
240- -- Right => message arrived on a channel first
241- --
242- Right a -> returnOld old (Just (Left a))
233+ e <- waitIncoming `onException` putMVar arrived old
234+ case e of
235+ --
236+ -- Left => message arrived in the process mailbox. We now have to
237+ -- run through the MatchChunks checking each one, because we might
238+ -- have a situation where the first chunk fails to match and the
239+ -- second chunk is a channel match and there *is* a message in the
240+ -- channel. In that case the channel wins.
241+ --
242+ Left m -> goCheck1 chunks m old
243+ --
244+ -- Right => message arrived on a channel first
245+ --
246+ Right a -> returnOld old (Just (Left a))
243247
244248 --
245249 -- A message arrived in the process inbox; check the MatchChunks for
0 commit comments