Debian Codegraph @

src/Control/Concurrent/Async/Lifted.hs

raw
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE RankNTypes #-}
4 {-# LANGUAGE TypeFamilies #-}
5 {-# LANGUAGE UndecidableInstances #-}
6
7 {- |
8 Module : Control.Concurrent.Async.Lifted
9 Copyright : Copyright (C) 2012-2018 Mitsutoshi Aoe
10 License : BSD-style (see the file LICENSE)
11 Maintainer : Mitsutoshi Aoe <[email protected]>
12 Stability : experimental
13
14 This is a wrapped version of @Control.Concurrent.Async@ with types generalized
15 from 'IO' to all monads in either 'MonadBase' or 'MonadBaseControl'.
16
17 All the functions restore the monadic effects in the forked computation
18 unless specified otherwise.
19
20 If your monad stack satisfies @'StM' m a ~ a@ (e.g. the reader monad), consider
21 using @Control.Concurrent.Async.Lifted.Safe@ module, which prevents you from
22 messing up monadic effects.
23 -}
24
25 module Control.Concurrent.Async.Lifted
26 ( -- * Asynchronous actions
27 A.Async
28 -- ** Spawning
29 , async, asyncBound, asyncOn
30 , asyncWithUnmask, asyncOnWithUnmask
31
32 -- ** Spawning with automatic 'cancel'ation
33 , withAsync, withAsyncBound, withAsyncOn
34 , withAsyncWithUnmask, withAsyncOnWithUnmask
35
36 -- ** Quering 'Async's
37 , wait, poll, waitCatch
38 , cancel
39 , uninterruptibleCancel
40 , cancelWith
41 , A.asyncThreadId
42 , A.AsyncCancelled(..)
43
44 -- ** STM operations
45 , A.waitSTM, A.pollSTM, A.waitCatchSTM
46
47 -- ** Waiting for multiple 'Async's
48 , waitAny, waitAnyCatch, waitAnyCancel, waitAnyCatchCancel
49 , waitEither, waitEitherCatch, waitEitherCancel, waitEitherCatchCancel
50 , waitEither_
51 , waitBoth
52
53 -- ** Waiting for multiple 'Async's in STM
54 , A.waitAnySTM
55 , A.waitAnyCatchSTM
56 , A.waitEitherSTM
57 , A.waitEitherCatchSTM
58 , A.waitEitherSTM_
59 , A.waitBothSTM
60
61 -- ** Linking
62 , link, link2
63 , A.ExceptionInLinkedThread(..)
64
65 -- * Convenient utilities
66 , race, race_, concurrently, concurrently_
67 , mapConcurrently, mapConcurrently_
68 , forConcurrently, forConcurrently_
69 , replicateConcurrently, replicateConcurrently_
70 , Concurrently(..)
71
72 , A.compareAsyncs
73 ) where
74
75 import Control.Applicative
76 import Control.Concurrent (threadDelay)
77 import Control.Monad ((>=>), forever, void)
78 import Data.Foldable (fold)
79 import GHC.IO (unsafeUnmask)
80 import Prelude
81
82 import Control.Concurrent.Async (Async)
83 import Control.Exception.Lifted (SomeException, Exception)
84 import Control.Monad.Base (MonadBase(..))
85 import Control.Monad.Trans.Control
86 import qualified Control.Concurrent.Async as A
87 import qualified Control.Exception.Lifted as E
88
89 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ < 710
90 import Data.Foldable
91 import Data.Traversable
92 #endif
93 #if !MIN_VERSION_base(4, 8, 0)
94 import Data.Monoid (Monoid(mappend, mempty))
95 #elif MIN_VERSION_base(4, 9, 0) && !MIN_VERSION_base(4, 13, 0)
96 import Data.Semigroup (Semigroup((<>)))
97 #endif
98
99 -- | Generalized version of 'A.async'.
100 async :: MonadBaseControl IO m => m a -> m (Async (StM m a))
101 async = asyncUsing A.async
102
103 -- | Generalized version of 'A.asyncBound'.
104 asyncBound :: MonadBaseControl IO m => m a -> m (Async (StM m a))
105 asyncBound = asyncUsing A.asyncBound
106
107 -- | Generalized version of 'A.asyncOn'.
108 asyncOn :: MonadBaseControl IO m => Int -> m a -> m (Async (StM m a))
109 asyncOn cpu = asyncUsing (A.asyncOn cpu)
110
111 -- | Generalized version of 'A.asyncWithUnmask'.
112 asyncWithUnmask
113 :: MonadBaseControl IO m
114 => ((forall b. m b -> m b) -> m a)
115 -> m (Async (StM m a))
116 asyncWithUnmask actionWith =
117 asyncUsing A.async (actionWith (liftBaseOp_ unsafeUnmask))
118
119 -- | Generalized version of 'A.asyncOnWithUnmask'.
120 asyncOnWithUnmask
121 :: MonadBaseControl IO m
122 => Int
123 -> ((forall b. m b -> m b) -> m a)
124 -> m (Async (StM m a))
125 asyncOnWithUnmask cpu actionWith =
126 asyncUsing (A.asyncOn cpu) (actionWith (liftBaseOp_ unsafeUnmask))
127
128 asyncUsing
129 :: MonadBaseControl IO m
130 => (IO (StM m a) -> IO (Async (StM m a)))
131 -> m a
132 -> m (Async (StM m a))
133 asyncUsing fork m =
134 liftBaseWith $ \runInIO -> fork (runInIO m)
135
136 -- | Generalized version of 'A.withAsync'.
137 withAsync
138 :: MonadBaseControl IO m
139 => m a
140 -> (Async (StM m a) -> m b)
141 -> m b
142 withAsync = liftWithAsync A.withAsync
143 {-# INLINABLE withAsync #-}
144
145 -- | Generalized version of 'A.withAsyncBound'.
146 withAsyncBound
147 :: MonadBaseControl IO m
148 => m a
149 -> (Async (StM m a) -> m b)
150 -> m b
151 withAsyncBound = liftWithAsync A.withAsyncBound
152 {-# INLINABLE withAsyncBound #-}
153
154 liftWithAsync
155 :: MonadBaseControl IO m
156 => (IO (StM m a) -> (Async (StM m a) -> IO (StM m b)) -> IO (StM m b))
157 -> (m a -> (Async (StM m a) -> m b) -> m b)
158 liftWithAsync withA action cont = restoreM =<< do
159 liftBaseWith $ \runInIO -> do
160 withA (runInIO action) (runInIO . cont)
161
162 -- | Generalized version of 'A.withAsyncOn'.
163 withAsyncOn
164 :: MonadBaseControl IO m
165 => Int
166 -> m a
167 -> (Async (StM m a) -> m b)
168 -> m b
169 withAsyncOn = withAsyncUsing . asyncOn
170 {-# INLINABLE withAsyncOn #-}
171
172 -- | Generalized version of 'A.withAsyncWithUnmask'.
173 withAsyncWithUnmask
174 :: MonadBaseControl IO m
175 => ((forall c. m c -> m c) -> m a)
176 -> (Async (StM m a) -> m b)
177 -> m b
178 withAsyncWithUnmask actionWith =
179 withAsyncUsing async (actionWith (liftBaseOp_ unsafeUnmask))
180 {-# INLINABLE withAsyncWithUnmask #-}
181
182 -- | Generalized version of 'A.withAsyncOnWithUnmask'.
183 withAsyncOnWithUnmask
184 :: MonadBaseControl IO m
185 => Int
186 -> ((forall c. m c -> m c) -> m a)
187 -> (Async (StM m a) -> m b)
188 -> m b
189 withAsyncOnWithUnmask cpu actionWith =
190 withAsyncUsing (asyncOn cpu) (actionWith (liftBaseOp_ unsafeUnmask))
191 {-# INLINABLE withAsyncOnWithUnmask #-}
192
193 withAsyncUsing
194 :: MonadBaseControl IO m
195 => (m a -> m (Async (StM m a)))
196 -> m a
197 -> (Async (StM m a) -> m b)
198 -> m b
199 withAsyncUsing fork action inner = E.mask $ \restore -> do
200 a <- fork $ restore action
201 r <- restore (inner a) `E.catch` \e -> do
202 cancel a
203 E.throwIO (e :: SomeException)
204 cancel a
205 return r
206
207 -- | Generalized version of 'A.wait'.
208 wait :: MonadBaseControl IO m => Async (StM m a) -> m a
209 wait = liftBase . A.wait >=> restoreM
210
211 -- | Generalized version of 'A.poll'.
212 poll
213 :: MonadBaseControl IO m
214 => Async (StM m a)
215 -> m (Maybe (Either SomeException a))
216 poll a =
217 liftBase (A.poll a) >>=
218 maybe (return Nothing) (fmap Just . sequenceEither)
219
220 -- | Generalized version of 'A.cancel'.
221 cancel :: MonadBase IO m => Async a -> m ()
222 cancel = liftBase . A.cancel
223
224 -- | Generalized version of 'A.cancelWith'.
225 cancelWith :: (MonadBase IO m, Exception e) => Async a -> e -> m ()
226 cancelWith = (liftBase .) . A.cancelWith
227
228 -- | Generalized version of 'A.uninterruptibleCancel'.
229 uninterruptibleCancel :: MonadBase IO m => Async a -> m ()
230 uninterruptibleCancel = liftBase . A.uninterruptibleCancel
231
232 -- | Generalized version of 'A.waitCatch'.
233 waitCatch
234 :: MonadBaseControl IO m
235 => Async (StM m a)
236 -> m (Either SomeException a)
237 waitCatch a = liftBase (A.waitCatch a) >>= sequenceEither
238
239 -- | Generalized version of 'A.waitAny'.
240 waitAny :: MonadBaseControl IO m => [Async (StM m a)] -> m (Async (StM m a), a)
241 waitAny as = do
242 (a, s) <- liftBase $ A.waitAny as
243 r <- restoreM s
244 return (a, r)
245
246 -- | Generalized version of 'A.waitAnyCatch'.
247 waitAnyCatch
248 :: MonadBaseControl IO m
249 => [Async (StM m a)]
250 -> m (Async (StM m a), Either SomeException a)
251 waitAnyCatch as = do
252 (a, s) <- liftBase $ A.waitAnyCatch as
253 r <- sequenceEither s
254 return (a, r)
255
256 -- | Generalized version of 'A.waitAnyCancel'.
257 waitAnyCancel
258 :: MonadBaseControl IO m
259 => [Async (StM m a)]
260 -> m (Async (StM m a), a)
261 waitAnyCancel as = do
262 (a, s) <- liftBase $ A.waitAnyCancel as
263 r <- restoreM s
264 return (a, r)
265
266 -- | Generalized version of 'A.waitAnyCatchCancel'.
267 waitAnyCatchCancel
268 :: MonadBaseControl IO m
269 => [Async (StM m a)]
270 -> m (Async (StM m a), Either SomeException a)
271 waitAnyCatchCancel as = do
272 (a, s) <- liftBase $ A.waitAnyCatchCancel as
273 r <- sequenceEither s
274 return (a, r)
275
276 -- | Generalized version of 'A.waitEither'.
277 waitEither
278 :: MonadBaseControl IO m
279 => Async (StM m a)
280 -> Async (StM m b)
281 -> m (Either a b)
282 waitEither a b =
283 liftBase (A.waitEither a b) >>=
284 either (fmap Left . restoreM) (fmap Right . restoreM)
285
286 -- | Generalized version of 'A.waitEitherCatch'.
287 waitEitherCatch
288 :: MonadBaseControl IO m
289 => Async (StM m a)
290 -> Async (StM m b)
291 -> m (Either (Either SomeException a) (Either SomeException b))
292 waitEitherCatch a b =
293 liftBase (A.waitEitherCatch a b) >>=
294 either (fmap Left . sequenceEither) (fmap Right . sequenceEither)
295
296 -- | Generalized version of 'A.waitEitherCancel'.
297 waitEitherCancel
298 :: MonadBaseControl IO m
299 => Async (StM m a)
300 -> Async (StM m b)
301 -> m (Either a b)
302 waitEitherCancel a b =
303 liftBase (A.waitEitherCancel a b) >>=
304 either (fmap Left . restoreM) (fmap Right . restoreM)
305
306 -- | Generalized version of 'A.waitEitherCatchCancel'.
307 waitEitherCatchCancel
308 :: MonadBaseControl IO m
309 => Async (StM m a)
310 -> Async (StM m b)
311 -> m (Either (Either SomeException a) (Either SomeException b))
312 waitEitherCatchCancel a b =
313 liftBase (A.waitEitherCatch a b) >>=
314 either (fmap Left . sequenceEither) (fmap Right . sequenceEither)
315
316 -- | Generalized version of 'A.waitEither_'.
317 --
318 -- NOTE: This function discards the monadic effects besides IO in the forked
319 -- computation.
320 waitEither_
321 :: MonadBase IO m
322 => Async a
323 -> Async b
324 -> m ()
325 waitEither_ a b = liftBase (A.waitEither_ a b)
326
327 -- | Generalized version of 'A.waitBoth'.
328 waitBoth
329 :: MonadBaseControl IO m
330 => Async (StM m a)
331 -> Async (StM m b)
332 -> m (a, b)
333 waitBoth a b = do
334 (sa, sb) <- liftBase (A.waitBoth a b)
335 ra <- restoreM sa
336 rb <- restoreM sb
337 return (ra, rb)
338 {-# INLINABLE waitBoth #-}
339
340 -- | Generalized version of 'A.link'.
341 link :: MonadBase IO m => Async a -> m ()
342 link = liftBase . A.link
343
344 -- | Generalized version of 'A.link2'.
345 link2 :: MonadBase IO m => Async a -> Async b -> m ()
346 link2 = (liftBase .) . A.link2
347
348 -- | Generalized version of 'A.race'.
349 race :: MonadBaseControl IO m => m a -> m b -> m (Either a b)
350 race left right =
351 withAsync left $ \a ->
352 withAsync right $ \b ->
353 waitEither a b
354 {-# INLINABLE race #-}
355
356 -- | Generalized version of 'A.race_'.
357 --
358 -- NOTE: This function discards the monadic effects besides IO in the forked
359 -- computation.
360 race_ :: MonadBaseControl IO m => m a -> m b -> m ()
361 race_ left right =
362 withAsync left $ \a ->
363 withAsync right $ \b ->
364 waitEither_ a b
365 {-# INLINABLE race_ #-}
366
367 -- | Generalized version of 'A.concurrently'.
368 concurrently :: MonadBaseControl IO m => m a -> m b -> m (a, b)
369 concurrently left right =
370 withAsync left $ \a ->
371 withAsync right $ \b ->
372 waitBoth a b
373 {-# INLINABLE concurrently #-}
374
375 -- | Generalized version of 'A.concurrently_'.
376 concurrently_ :: MonadBaseControl IO m => m a -> m b -> m ()
377 concurrently_ left right = void $ concurrently left right
378 {-# INLINABLE concurrently_ #-}
379
380 -- | Generalized version of 'A.mapConcurrently'.
381 mapConcurrently
382 :: (Traversable t, MonadBaseControl IO m)
383 => (a -> m b)
384 -> t a
385 -> m (t b)
386 mapConcurrently f = runConcurrently . traverse (Concurrently . f)
387
388 -- | Generalized version of 'A.mapConcurrently_'.
389 mapConcurrently_
390 :: (Foldable t, MonadBaseControl IO m)
391 => (a -> m b)
392 -> t a
393 -> m ()
394 mapConcurrently_ f = runConcurrently . foldMap (Concurrently . void . f)
395
396 -- | Generalized version of 'A.forConcurrently'.
397 forConcurrently
398 :: (Traversable t, MonadBaseControl IO m)
399 => t a
400 -> (a -> m b)
401 -> m (t b)
402 forConcurrently = flip mapConcurrently
403
404 -- | Generalized version of 'A.forConcurrently_'.
405 forConcurrently_
406 :: (Foldable t, MonadBaseControl IO m)
407 => t a
408 -> (a -> m b)
409 -> m ()
410 forConcurrently_ = flip mapConcurrently_
411
412 -- | Generalized version of 'A.replicateConcurrently'.
413 replicateConcurrently
414 :: MonadBaseControl IO m
415 => Int
416 -> m a
417 -> m [a]
418 replicateConcurrently n =
419 runConcurrently . sequenceA . replicate n . Concurrently
420
421 -- | Generalized version of 'A.replicateConcurrently_'.
422 replicateConcurrently_
423 :: MonadBaseControl IO m
424 => Int
425 -> m a
426 -> m ()
427 replicateConcurrently_ n =
428 runConcurrently . fold . replicate n . Concurrently . void
429
430 -- | Generalized version of 'A.Concurrently'.
431 --
432 -- A value of type @'Concurrently' m a@ is an IO-based operation that can be
433 -- composed with other 'Concurrently' values, using the 'Applicative' and
434 -- 'Alternative' instances.
435 --
436 -- Calling 'runConcurrently' on a value of type @'Concurrently' m a@ will
437 -- execute the IO-based lifted operations it contains concurrently, before
438 -- delivering the result of type 'a'.
439 --
440 -- For example
441 --
442 -- @
443 -- (page1, page2, page3) <- 'runConcurrently' $ (,,)
444 -- '<$>' 'Concurrently' (getURL "url1")
445 -- '<*>' 'Concurrently' (getURL "url2")
446 -- '<*>' 'Concurrently' (getURL "url3")
447 -- @
448 newtype Concurrently m a = Concurrently { runConcurrently :: m a }
449
450 instance Functor m => Functor (Concurrently m) where
451 fmap f (Concurrently a) = Concurrently $ f <$> a
452
453 instance MonadBaseControl IO m => Applicative (Concurrently m) where
454 pure = Concurrently . pure
455 Concurrently fs <*> Concurrently as =
456 Concurrently $ uncurry ($) <$> concurrently fs as
457
458 instance MonadBaseControl IO m => Alternative (Concurrently m) where
459 empty = Concurrently $ liftBaseWith $ \_ -> forever $ threadDelay maxBound
460 Concurrently as <|> Concurrently bs =
461 Concurrently $ either id id <$> race as bs
462
463 #if MIN_VERSION_base(4, 9, 0)
464 instance (MonadBaseControl IO m, Semigroup a) =>
465 Semigroup (Concurrently m a) where
466 (<>) = liftA2 (<>)
467
468 instance (MonadBaseControl IO m, Semigroup a, Monoid a) =>
469 Monoid (Concurrently m a) where
470 mempty = pure mempty
471 mappend = (<>)
472 #else
473 instance (MonadBaseControl IO m, Monoid a) => Monoid (Concurrently m a) where
474 mempty = pure mempty
475 mappend = liftA2 mappend
476 #endif
477
478 sequenceEither :: MonadBaseControl IO m => Either e (StM m a) -> m (Either e a)
479 sequenceEither = either (return . Left) (fmap Right . restoreM)