1{-# LANGUAGE BangPatterns #-}
2{-# LANGUAGE MagicHash, UnboxedTuples, PatternGuards, ScopedTypeVariables, RankNTypes #-}
3-- | Concurrent queue for single reader, single writer
4module Control.Distributed.Process.Internal.CQueue
16import Prelude hiding (length, reverse)
17import Control.Concurrent.STM
32import Control.Applicative ((<$>), (<*>))
33import Control.Exception (mask_, onException)
34import System.Timeout (timeout)
35import Control.Distributed.Process.Internal.StrictMVar
36 ( StrictMVar(StrictMVar)
41import Control.Distributed.Process.Internal.StrictList
45import Data.Maybe (fromJust)
46import GHC.MVar (MVar(MVar))
48import GHC.Prim (mkWeak#)
49import GHC.Weak (Weak(Weak))
51-- We use a TCHan rather than a Chan so that we have a non-blocking read
52data CQueue a = CQueue (StrictMVar (StrictList a)) -- Arrived
54 (TVar Int) -- Queue size
56newCQueue :: IO (CQueue a)
57newCQueue = CQueue <$> newMVar Nil <*> atomically newTChan <*> newTVarIO 0
59-- | Enqueue an element
62enqueue :: CQueue a -> a -> IO ()
63enqueue c !a = atomically (enqueueSTM c a)
65-- | Variant of enqueue for use in the STM monad.
66enqueueSTM :: CQueue a -> a -> STM ()
67enqueueSTM (CQueue _arrived incoming size) !a = do
77 = MatchMsg (m -> Maybe a)
80type MatchChunks m a = [Either [m -> Maybe a] [STM a]]
82chunkMatches :: [MatchOn m a] -> MatchChunks m a
84chunkMatches (MatchMsg m : ms) = Left (m : chk) : chunkMatches rest
85 where (chk, rest) = spanMatchMsg ms
86chunkMatches (MatchChan r : ms) = Right (r : chk) : chunkMatches rest
87 where (chk, rest) = spanMatchChan ms
89spanMatchMsg :: [MatchOn m a] -> ([m -> Maybe a], [MatchOn m a])
90spanMatchMsg [] = ([],[])
92 | MatchMsg msg <- m = (msg:msgs, rest)
93 | otherwise = ([], m:ms)
94 where !(msgs,rest) = spanMatchMsg ms
96spanMatchChan :: [MatchOn m a] -> ([STM a], [MatchOn m a])
97spanMatchChan [] = ([],[])
99 | MatchChan stm <- m = (stm:stms, rest)
100 | otherwise = ([], m:ms)
101 where !(stms,rest) = spanMatchChan ms
103-- | Dequeue an element
105-- The timeout (if any) is applied only to waiting for incoming messages, not
106-- to checking messages that have already arrived
107dequeue :: forall m a.
109 -> BlockSpec -- ^ Blocking behaviour
110 -> [MatchOn m a] -- ^ List of matches
111 -> IO (Maybe a) -- ^ 'Nothing' only on timeout
112dequeue (CQueue arrived incoming size) blockSpec matchons = mask_ $ decrementJust $
114 Timeout n -> timeout n $ fmap fromJust run
117 [Right ports] -> -- channels only, this is easy:
119 NonBlocking -> atomically $ waitChans ports (return Nothing)
120 _ -> atomically $ waitChans ports retry
121 -- no onException needed
124 -- Decrement counter is smth is returned from the queue,
125 -- this is safe to use as method is called under a mask
126 -- and there is no 'unmasked' operation inside
130 Just{} -> atomically $ modifyTVar' size pred
134 chunks = chunkMatches matchons
137 arr <- takeMVar arrived
139 r <- atomically $ tryReadTChan incoming
142 Just x -> grabNew (Snoc xs x)
146 waitChans ports on_block =
147 foldr orElse on_block (map (fmap Just) ports)
150 -- First check the MatchChunks against the messages already in the
151 -- mailbox. For channel matches, we do a non-blocking check at
154 goCheck :: MatchChunks m a
155 -> StrictList m -- messages to check, in this order
158 goCheck [] old = goWait old
160 goCheck (Right ports : rest) old = do
161 r <- atomically $ waitChans ports (return Nothing) -- does not block
163 Just _ -> returnOld old r
164 Nothing -> goCheck rest old
166 goCheck (Left matches : rest) old = do
167 -- checkArrived might in principle take arbitrary time, so
168 -- we ought to call restore and use an exception handler. However,
169 -- the check is usually fast (just a comparison), and the overhead
170 -- of passing around restore and setting up exception handlers is
171 -- high. So just don't use expensive matchIfs!
172 case checkArrived matches old of
173 (old', Just r) -> returnOld old' (Just r)
174 (old', Nothing) -> goCheck rest old'
175 -- use the result list, which is now left-biased
178 -- Construct an STM transaction that looks at the relevant channels
179 -- in the correct order.
181 mkSTM :: MatchChunks m a -> STM (Either m a)
183 mkSTM (Left _ : rest)
184 = fmap Left (readTChan incoming) `orElse` mkSTM rest
185 mkSTM (Right ports : rest)
186 = foldr orElse (mkSTM rest) (map (fmap Right) ports)
188 waitIncoming :: IO (Maybe (Either m a))
189 waitIncoming = case blockSpec of
190 NonBlocking -> atomically $ fmap Just stm `orElse` return Nothing
191 _ -> atomically $ fmap Just stm
196 -- The initial pass didn't find a message, so now we go into blocking
199 -- Contents of 'arrived' from now on is (old ++ new), and
200 -- messages that arrive are snocced onto new.
203 r <- waitIncoming `onException` putMVar arrived old
205 -- Nothing => non-blocking and no message
206 Nothing -> returnOld old Nothing
209 -- Left => message arrived in the process mailbox. We now have to
210 -- run through the MatchChunks checking each one, because we might
211 -- have a situation where the first chunk fails to match and the
212 -- second chunk is a channel match and there *is* a message in the
213 -- channel. In that case the channel wins.
215 Left m -> goCheck1 chunks m old
217 -- Right => message arrived on a channel first
219 Right a -> returnOld old (Just a)
222 -- A message arrived in the process inbox; check the MatchChunks for
225 goCheck1 :: MatchChunks m a
226 -> m -- single message to check
227 -> StrictList m -- old messages we have already checked
230 goCheck1 [] m old = goWait (Snoc old m)
232 goCheck1 (Right ports : rest) m old = do
233 r <- atomically $ waitChans ports (return Nothing) -- does not block
235 Nothing -> goCheck1 rest m old
236 Just _ -> returnOld (Snoc old m) r
238 goCheck1 (Left matches : rest) m old = do
239 case checkMatches matches m of
240 Nothing -> goCheck1 rest m old
241 Just p -> returnOld old (Just p)
243 -- a common pattern for putting back the arrived queue at the end
244 returnOld :: StrictList m -> Maybe a -> IO (Maybe a)
245 returnOld old r = do putMVar arrived old; return r
247 -- as a side-effect, this left-biases the list
248 checkArrived :: [m -> Maybe a] -> StrictList m -> (StrictList m, Maybe a)
249 checkArrived matches list = go list Nil
251 go Nil Nil = (Nil, Nothing)
253 go (Append xs ys) tl = go xs (append ys tl)
254 go (Snoc xs x) tl = go xs (Cons x tl)
256 | Just y <- checkMatches matches x = (append xs tl, Just y)
257 | otherwise = let !(rest,r) = go xs tl in (Cons x rest, r)
259 checkMatches :: [m -> Maybe a] -> m -> Maybe a
260 checkMatches [] _ = Nothing
261 checkMatches (m:ms) a = case m a of Nothing -> checkMatches ms a
264-- | Weak reference to a CQueue
265mkWeakCQueue :: CQueue a -> IO () -> IO (Weak (CQueue a))
266mkWeakCQueue m@(CQueue (StrictMVar (MVar m#)) _ _) f = IO $ \s ->
267 case mkWeak# m# m f s of (# s1, w #) -> (# s1, Weak w #)
269queueSize :: CQueue a -> IO Int
270queueSize (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