2{-# LANGUAGE DeriveFunctor #-}
3{-# LANGUAGE BangPatterns #-}
4{-# LANGUAGE MagicHash, UnboxedTuples, PatternGuards, ScopedTypeVariables, RankNTypes #-}
5-- | Concurrent queue for single reader, single writer
6module Control.Distributed.Process.Internal.CQueue
18import Prelude hiding (length, reverse)
19import Control.Concurrent.STM
34import Control.Applicative ((<$>), (<*>))
35import Control.Exception (mask_, onException)
36import System.Timeout (timeout)
37import Control.Distributed.Process.Internal.StrictMVar
38 ( StrictMVar(StrictMVar)
43import Control.Distributed.Process.Internal.StrictList
47import Data.Maybe (fromJust)
48import Data.Traversable (traverse)
49import GHC.MVar (MVar(MVar))
50import GHC.IO (IO(IO), unIO)
51import GHC.Exts (mkWeak#)
52import GHC.Weak (Weak(Weak))
54-- We use a TCHan rather than a Chan so that we have a non-blocking read
55data CQueue a = CQueue (StrictMVar (StrictList a)) -- Arrived
57 (TVar Int) -- Queue size
59newCQueue :: IO (CQueue a)
60newCQueue = CQueue <$> newMVar Nil <*> atomically newTChan <*> newTVarIO 0
62-- | Enqueue an element
65enqueue :: CQueue a -> a -> IO ()
66enqueue c !a = atomically (enqueueSTM c a)
68-- | Variant of enqueue for use in the STM monad.
69enqueueSTM :: CQueue a -> a -> STM ()
70enqueueSTM (CQueue _arrived incoming size) !a = do
81-- They can be either a message match or a channel match.
83 = MatchMsg (m -> Maybe a)
87-- Lists of chunks of matches
89-- Two consecutive chunks never have the same kind of matches. i.e. if one chunk
90-- contains message matches then the next one must contain channel matches and
92type MatchChunks m a = [Either [m -> Maybe a] [STM a]]
94-- Splits a list of matches into chunks.
96-- > concatMap (either (map MatchMsg) (map MatchChan)) . chunkMatches == id
98chunkMatches :: [MatchOn m a] -> MatchChunks m a
100chunkMatches (MatchMsg m : ms) = Left (m : chk) : chunkMatches rest
101 where (chk, rest) = spanMatchMsg ms
102chunkMatches (MatchChan r : ms) = Right (r : chk) : chunkMatches rest
103 where (chk, rest) = spanMatchChan ms
105-- | @spanMatchMsg = first (map (\(MatchMsg x) -> x)) . span isMatchMsg@
106spanMatchMsg :: [MatchOn m a] -> ([m -> Maybe a], [MatchOn m a])
107spanMatchMsg [] = ([],[])
109 | MatchMsg msg <- m = (msg:msgs, rest)
110 | otherwise = ([], m:ms)
111 where !(msgs,rest) = spanMatchMsg ms
113-- | @spanMatchMsg = first (map (\(MatchChan x) -> x)) . span isMatchChan@
114spanMatchChan :: [MatchOn m a] -> ([STM a], [MatchOn m a])
115spanMatchChan [] = ([],[])
116spanMatchChan (m : ms)
117 | MatchChan stm <- m = (stm:stms, rest)
118 | otherwise = ([], m:ms)
119 where !(stms,rest) = spanMatchChan ms
121-- | Dequeue an element
123-- The timeout (if any) is applied only to waiting for incoming messages, not
124-- to checking messages that have already arrived
125dequeue :: forall m a.
127 -> BlockSpec -- ^ Blocking behaviour
128 -> [MatchOn m a] -- ^ List of matches
129 -> IO (Maybe a) -- ^ 'Nothing' only on timeout
130dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJust $
132 Timeout n -> timeout n $ fmap fromJust run
135 [Right ports] -> -- channels only, this is easy:
137 NonBlocking -> atomically $ waitChans ports (return Nothing)
138 _ -> atomically $ waitChans ports retry
139 -- no onException needed
142 -- Decrement counter is smth is returned from the queue,
143 -- this is safe to use as method is called under a mask
144 -- and there is no 'unmasked' operation inside
145 decrementJust :: IO (Maybe (Either a a)) -> IO (Maybe a)
147 traverse (either return (\x -> decrement >> return x)) =<< f
148 decrement = atomically $ modifyTVar' size pred
150 chunks = chunkMatches matchons
153 arr <- takeMVar arrived
155 r <- atomically $ tryReadTChan incoming
158 Just x -> grabNew (Snoc xs x)
162 -- Yields the value of the first succesful STM transaction as
163 -- @Just (Left v)@. If all transactions fail, yields the value of the second
165 waitChans :: [STM a] -> STM (Maybe (Either a a)) -> STM (Maybe (Either a a))
166 waitChans ports on_block =
167 foldr orElse on_block (map (fmap (Just . Left)) ports)
170 -- First check the MatchChunks against the messages already in the
171 -- mailbox. For channel matches, we do a non-blocking check at
174 -- Yields @Just (Left a)@ when a channel is matched, @Just (Right a)@
175 -- when a message is matched and @Nothing@ when there are no messages and we
178 goCheck :: MatchChunks m a
179 -> StrictList m -- messages to check, in this order
180 -> IO (Maybe (Either a a))
182 goCheck [] old = goWait old
184 goCheck (Right ports : rest) old = do
185 r <- atomically $ waitChans ports (return Nothing) -- does not block
187 Just _ -> returnOld old r
188 Nothing -> goCheck rest old
190 goCheck (Left matches : rest) old = do
191 -- checkArrived might in principle take arbitrary time, so
192 -- we ought to call restore and use an exception handler. However,
193 -- the check is usually fast (just a comparison), and the overhead
194 -- of passing around restore and setting up exception handlers is
195 -- high. So just don't use expensive matchIfs!
196 case checkArrived matches old of
197 (old', Just r) -> returnOld old' (Just (Right r))
198 (old', Nothing) -> goCheck rest old'
199 -- use the result list, which is now left-biased
202 -- Construct an STM transaction that looks at the relevant channels
203 -- in the correct order.
205 mkSTM :: MatchChunks m a -> STM (Either m a)
207 mkSTM (Left _ : rest)
208 = fmap Left (readTChan incoming) `orElse` mkSTM rest
209 mkSTM (Right ports : rest)
210 = foldr orElse (mkSTM rest) (map (fmap Right) ports)
212 waitIncoming :: IO (Maybe (Either m a))
213 waitIncoming = case blockSpec of
214 NonBlocking -> atomically $ fmap Just stm `orElse` return Nothing
215 _ -> atomically $ fmap Just stm
220 -- The initial pass didn't find a message, so now we go into blocking
223 -- Contents of 'arrived' from now on is (old ++ new), and
224 -- messages that arrive are snocced onto new.
226 goWait :: StrictList m -> IO (Maybe (Either a a))
228 r <- waitIncoming `onException` putMVar arrived old
230 -- Nothing => non-blocking and no message
231 Nothing -> returnOld old Nothing
234 -- Left => message arrived in the process mailbox. We now have to
235 -- run through the MatchChunks checking each one, because we might
236 -- have a situation where the first chunk fails to match and the
237 -- second chunk is a channel match and there *is* a message in the
238 -- channel. In that case the channel wins.
240 Left m -> goCheck1 chunks m old
242 -- Right => message arrived on a channel first
244 Right a -> returnOld old (Just (Left a))
247 -- A message arrived in the process inbox; check the MatchChunks for
250 goCheck1 :: MatchChunks m a
251 -> m -- single message to check
252 -> StrictList m -- old messages we have already checked
253 -> IO (Maybe (Either a a))
255 goCheck1 [] m old = goWait (Snoc old m)
257 goCheck1 (Right ports : rest) m old = do
258 r <- atomically $ waitChans ports (return Nothing) -- does not block
260 Nothing -> goCheck1 rest m old
261 Just _ -> returnOld (Snoc old m) r
263 goCheck1 (Left matches : rest) m old = do
264 case checkMatches matches m of
265 Nothing -> goCheck1 rest m old
266 Just p -> returnOld old (Just (Right p))
268 -- a common pattern for putting back the arrived queue at the end
269 returnOld :: StrictList m -> Maybe (Either a a) -> IO (Maybe (Either a a))
270 returnOld old r = do putMVar arrived old; return r
272 -- as a side-effect, this left-biases the list
273 checkArrived :: [m -> Maybe a] -> StrictList m -> (StrictList m, Maybe a)
274 checkArrived matches list = go list Nil
276 -- @go xs ys@ searches for a message match in @append xs ys@
277 go Nil Nil = (Nil, Nothing)
279 go (Append xs ys) tl = go xs (append ys tl)
280 go (Snoc xs x) tl = go xs (Cons x tl)
282 | Just y <- checkMatches matches x = (append xs tl, Just y)
283 | otherwise = let !(rest,r) = go xs tl in (Cons x rest, r)
285 checkMatches :: [m -> Maybe a] -> m -> Maybe a
286 checkMatches [] _ = Nothing
287 checkMatches (m:ms) a = case m a of Nothing -> checkMatches ms a
290-- | Weak reference to a CQueue
291mkWeakCQueue :: CQueue a -> IO () -> IO (Weak (CQueue a))
292mkWeakCQueue m@(CQueue (StrictMVar (MVar m#)) _ _) f = IO $ \s ->
293#if MIN_VERSION_base(4,9,0)
294 case mkWeak# m# m (unIO f) s of (# s1, w #) -> (# s1, Weak w #)
296 case mkWeak# m# m f s of (# s1, w #) -> (# s1, Weak w #)
299queueSize :: CQueue a -> IO Int
300queueSize (CQueue _ _ size) = readTVarIO size
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list