Debian Codegraph @

src/Control/Concurrent/Async/Lifted/Safe.hs

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