Skip to content

Commit 608e6ec

Browse files
committed
MonadAsync: removed linkTo and linkToOnly
1 parent 6ab2088 commit 608e6ec

File tree

1 file changed

+17
-40
lines changed

1 file changed

+17
-40
lines changed

io-classes/src/Control/Monad/Class/MonadAsync.hs

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ module Control.Monad.Class.MonadAsync
2020
, linkOnly
2121
, link2
2222
, link2Only
23-
, linkTo
24-
, linkToOnly
2523
, mapConcurrently
2624
, forConcurrently
2725
, mapConcurrently_
@@ -366,14 +364,10 @@ instance MonadAsync IO where
366364
-- Adapted from "Control.Concurrent.Async"
367365
--
368366
-- We don't use the implementation of linking from 'Control.Concurrent.Async'
369-
-- directly because:
370-
--
371-
-- 1. If we /did/ use the real implementation, then the mock implementation and
372-
-- the real implementation would not be able to throw the same exception,
373-
-- because the exception type used by the real implementation is
374-
-- 2. We need a generalized form of linking that links an async to an arbitrary
375-
-- thread ('linkTo'), which is exposed only if cabal flag `+non-standard` is
376-
-- used.
367+
-- directly because if we /did/ use the real implementation, then the mock
368+
-- implementation and the real implementation would not be able to throw the
369+
-- same exception, because the exception type used by the real implementation
370+
-- is
377371
--
378372
-- > data ExceptionInLinkedThread =
379373
-- > forall a . ExceptionInLinkedThread (Async a) SomeException
@@ -410,8 +404,19 @@ link = linkOnly (not . isCancel)
410404
linkOnly :: forall m a. (MonadAsync m, MonadFork m, MonadMask m)
411405
=> (SomeException -> Bool) -> Async m a -> m ()
412406
linkOnly shouldThrow a = do
413-
me <- myThreadId
414-
linkToOnly me shouldThrow a
407+
tid <- myThreadId
408+
void $ forkRepeat ("linkToOnly " <> show linkedThreadId) $ do
409+
r <- waitCatch a
410+
case r of
411+
Left e | shouldThrow e -> throwTo tid (exceptionInLinkedThread e)
412+
_otherwise -> return ()
413+
where
414+
linkedThreadId :: ThreadId m
415+
linkedThreadId = asyncThreadId a
416+
417+
exceptionInLinkedThread :: SomeException -> ExceptionInLinkedThread
418+
exceptionInLinkedThread =
419+
ExceptionInLinkedThread (show linkedThreadId)
415420

416421
link2 :: (MonadAsync m, MonadFork m, MonadMask m)
417422
=> Async m a -> Async m b -> m ()
@@ -432,34 +437,6 @@ link2Only shouldThrow left right =
432437
tl = asyncThreadId left
433438
tr = asyncThreadId right
434439

435-
-- | Generalization of 'link' that links an async to an arbitrary thread.
436-
--
437-
-- Non standard (not in 'async' library)
438-
--
439-
linkTo :: (MonadAsync m, MonadFork m, MonadMask m)
440-
=> ThreadId m -> Async m a -> m ()
441-
linkTo tid = linkToOnly tid (not . isCancel)
442-
443-
-- | Generalization of 'linkOnly' that links an async to an arbitrary thread.
444-
--
445-
-- Non standard (not in 'async' library).
446-
--
447-
linkToOnly :: forall m a. (MonadAsync m, MonadFork m, MonadMask m)
448-
=> ThreadId m -> (SomeException -> Bool) -> Async m a -> m ()
449-
linkToOnly tid shouldThrow a = do
450-
void $ forkRepeat ("linkToOnly " <> show linkedThreadId) $ do
451-
r <- waitCatch a
452-
case r of
453-
Left e | shouldThrow e -> throwTo tid (exceptionInLinkedThread e)
454-
_otherwise -> return ()
455-
where
456-
linkedThreadId :: ThreadId m
457-
linkedThreadId = asyncThreadId a
458-
459-
exceptionInLinkedThread :: SomeException -> ExceptionInLinkedThread
460-
exceptionInLinkedThread =
461-
ExceptionInLinkedThread (show linkedThreadId)
462-
463440
isCancel :: SomeException -> Bool
464441
isCancel e
465442
| Just AsyncCancelled <- fromException e = True

0 commit comments

Comments
 (0)