1-- | Concurrent queue for single reader, single writer
2{-# LANGUAGE MagicHash, UnboxedTuples, PatternGuards #-}
3module Control.Distributed.Process.Internal.CQueue
14import Prelude hiding (length, reverse)
15import Control.Concurrent.STM
26import Control.Applicative ((<$>), (<*>))
27import Control.Exception (mask_, onException)
28import System.Timeout (timeout)
29import Control.Distributed.Process.Internal.StrictMVar
30 ( StrictMVar(StrictMVar)
35import Control.Distributed.Process.Internal.StrictList
39import Data.Maybe (fromJust)
40import GHC.MVar (MVar(MVar))
42import GHC.Prim (mkWeak#)
43import GHC.Weak (Weak(Weak))
45-- We use a TCHan rather than a Chan so that we have a non-blocking read
46data CQueue a = CQueue (StrictMVar (StrictList a)) -- Arrived
49newCQueue :: IO (CQueue a)
50newCQueue = CQueue <$> newMVar Nil <*> atomically newTChan
52-- | Enqueue an element
55enqueue :: CQueue a -> a -> IO ()
56enqueue (CQueue _arrived incoming) !a = atomically $ writeTChan incoming a
58-- | Variant of enqueue for use in the STM monad.
59enqueueSTM :: CQueue a -> a -> STM ()
60enqueueSTM (CQueue _arrived incoming) !a = writeTChan incoming a
68 = MatchMsg (m -> Maybe a)
71type MatchChunks m a = [Either [m -> Maybe a] [STM a]]
73chunkMatches :: [MatchOn m a] -> MatchChunks m a
75chunkMatches (MatchMsg m : ms) = Left (m : chk) : chunkMatches rest
76 where (chk, rest) = spanMatchMsg ms
77chunkMatches (MatchChan r : ms) = Right (r : chk) : chunkMatches rest
78 where (chk, rest) = spanMatchChan ms
80spanMatchMsg :: [MatchOn m a] -> ([m -> Maybe a], [MatchOn m a])
81spanMatchMsg [] = ([],[])
83 | MatchMsg msg <- m = (msg:msgs, rest)
84 | otherwise = ([], m:ms)
85 where !(msgs,rest) = spanMatchMsg ms
87spanMatchChan :: [MatchOn m a] -> ([STM a], [MatchOn m a])
88spanMatchChan [] = ([],[])
90 | MatchChan stm <- m = (stm:stms, rest)
91 | otherwise = ([], m:ms)
92 where !(stms,rest) = spanMatchChan ms
94-- | Dequeue an element
96-- The timeout (if any) is applied only to waiting for incoming messages, not
97-- to checking messages that have already arrived
100 -> BlockSpec -- ^ Blocking behaviour
101 -> [MatchOn m a] -- ^ List of matches
102 -> IO (Maybe a) -- ^ 'Nothing' only on timeout
103dequeue (CQueue arrived incoming) blockSpec matchons =
105 Timeout n -> timeout n $ fmap fromJust run
108 [Right ports] -> -- channels only, this is easy:
110 NonBlocking -> atomically $ waitChans ports (return Nothing)
111 _ -> atomically $ waitChans ports retry
112 -- no onException needed
115 chunks = chunkMatches matchons
118 arr <- takeMVar arrived
120 r <- atomically $ tryReadTChan incoming
123 Just x -> grabNew (Snoc xs x)
127 waitChans ports on_block =
128 foldr orElse on_block (map (fmap Just) ports)
131 -- First check the MatchChunks against the messages already in the
132 -- mailbox. For channel matches, we do a non-blocking check at
135 goCheck :: MatchChunks m a
136 -> StrictList m -- messages to check, in this order
139 goCheck [] old = goWait old
141 goCheck (Right ports : rest) old = do
142 r <- atomically $ waitChans ports (return Nothing) -- does not block
144 Just _ -> returnOld old r
145 Nothing -> goCheck rest old
147 goCheck (Left matches : rest) old = do
148 -- checkArrived might in principle take arbitrary time, so
149 -- we ought to call restore and use an exception handler. However,
150 -- the check is usually fast (just a comparison), and the overhead
151 -- of passing around restore and setting up exception handlers is
152 -- high. So just don't use expensive matchIfs!
153 case checkArrived matches old of
154 (old', Just r) -> returnOld old' (Just r)
155 (old', Nothing) -> goCheck rest old'
156 -- use the result list, which is now left-biased
159 -- Construct an STM transaction that looks at the relevant channels
160 -- in the correct order.
162 mkSTM :: MatchChunks m a -> STM (Either m a)
164 mkSTM (Left _ : rest)
165 = fmap Left (readTChan incoming) `orElse` mkSTM rest
166 mkSTM (Right ports : rest)
167 = foldr orElse (mkSTM rest) (map (fmap Right) ports)
169 waitIncoming :: IO (Maybe (Either m a))
170 waitIncoming = case blockSpec of
171 NonBlocking -> atomically $ fmap Just stm `orElse` return Nothing
172 _ -> atomically $ fmap Just stm
177 -- The initial pass didn't find a message, so now we go into blocking
180 -- Contents of 'arrived' from now on is (old ++ new), and
181 -- messages that arrive are snocced onto new.
184 r <- waitIncoming `onException` putMVar arrived old
186 -- Nothing => non-blocking and no message
187 Nothing -> returnOld old Nothing
190 -- Left => message arrived in the process mailbox. We now have to
191 -- run through the MatchChunks checking each one, because we might
192 -- have a situation where the first chunk fails to match and the
193 -- second chunk is a channel match and there *is* a message in the
194 -- channel. In that case the channel wins.
196 Left m -> goCheck1 chunks m old
198 -- Right => message arrived on a channel first
200 Right a -> returnOld old (Just a)
203 -- A message arrived in the process inbox; check the MatchChunks for
206 goCheck1 :: MatchChunks m a
207 -> m -- single message to check
208 -> StrictList m -- old messages we have already checked
211 goCheck1 [] m old = goWait (Snoc old m)
213 goCheck1 (Right ports : rest) m old = do
214 r <- atomically $ waitChans ports (return Nothing) -- does not block
216 Nothing -> goCheck1 rest m old
217 Just _ -> returnOld (Snoc old m) r
219 goCheck1 (Left matches : rest) m old = do
220 case checkMatches matches m of
221 Nothing -> goCheck1 rest m old
222 Just p -> returnOld old (Just p)
224 -- a common pattern for putting back the arrived queue at the end
225 returnOld :: StrictList m -> Maybe a -> IO (Maybe a)
226 returnOld old r = do putMVar arrived old; return r
228 -- as a side-effect, this left-biases the list
229 checkArrived :: [m -> Maybe a] -> StrictList m -> (StrictList m, Maybe a)
230 checkArrived matches list = go list Nil
232 go Nil Nil = (Nil, Nothing)
234 go (Append xs ys) tl = go xs (append ys tl)
235 go (Snoc xs x) tl = go xs (Cons x tl)
237 | Just y <- checkMatches matches x = (append xs tl, Just y)
238 | otherwise = let !(rest,r) = go xs tl in (Cons x rest, r)
240 checkMatches :: [m -> Maybe a] -> m -> Maybe a
241 checkMatches [] _ = Nothing
242 checkMatches (m:ms) a = case m a of Nothing -> checkMatches ms a
245-- | Weak reference to a CQueue
246mkWeakCQueue :: CQueue a -> IO () -> IO (Weak (CQueue a))
247mkWeakCQueue m@(CQueue (StrictMVar (MVar m#)) _) f = IO $ \s ->
248 case mkWeak# m# m f s of (# s1, w #) -> (# s1, Weak w #)
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