File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change 10
10
- Implement ` MonadLabelledMVar ` instance for ` (IOSim s) `
11
11
- ` TVarId ` is now a sum type with one constructor per ` TVar ` role, e.g. ` TVar ` ,
12
12
` TMVar ` , ` MVar ` and a few others - except for ` TChan ` .
13
+ - A blocked ` takeTVar ` is now safe in the presence of exceptions. It will relay
14
+ the value to other waiting threads.
13
15
14
16
## 1.6.0.0
15
17
Original file line number Diff line number Diff line change @@ -398,7 +398,24 @@ takeMVarDefault (MVar tv) = mask_ $ do
398
398
-- takevar; we need to remove it from 'takeq', otherwise we
399
399
-- will have a space leak.
400
400
let takeq' = Deque. filter (/= takevar) takeq
401
- writeTVar tv (MVarEmpty takeq' readq)
401
+ takevalue <- readTVar takevar
402
+ case takevalue of
403
+ Nothing ->
404
+ writeTVar tv (MVarEmpty takeq' readq)
405
+ -- we were given a value before we could read it. Relay it to any
406
+ -- new reading threads and possible the next take thread.
407
+ Just x -> do
408
+ -- notify readers
409
+ mapM_ (\ readvar -> writeTVar readvar (Just x)) readq
410
+
411
+ -- notify first `takeMVar` thread
412
+ case Deque. uncons takeq' of
413
+ Nothing ->
414
+ writeTVar tv (MVarFull x mempty )
415
+
416
+ Just (takevar', takeq'') -> do
417
+ writeTVar takevar' (Just x)
418
+ writeTVar tv (MVarEmpty takeq'' mempty )
402
419
403
420
-- This case is unlikely but possible if another thread ran
404
421
-- first and modified the mvar. This situation is fine as far as
You can’t perform that action at this time.
0 commit comments