This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Implement 'readMVar'

  • Loading branch information...
barrucadu committed Oct 15, 2017
1 parent 9c49f9d commit dabd84b1ed4f713889b607b142ecb2d1987ee804
Showing with 9 additions and 0 deletions.
  1. +5 −0 Test/MiniFu.hs
  2. +4 −0 Test/MiniFu/Internal.hs
@@ -39,6 +39,11 @@ newEmptyMVar = MiniFu (K.cont NewEmptyMVar)
putMVar :: MVar m a -> a -> MiniFu m ()
putMVar v a = MiniFu (K.cont (\k -> PutMVar v a (k ())))

-- | Block until a value is present in the @MVar@, and then return
-- it. This does not "remove" the value, multiple reads are possible.
readMVar :: MVar m a -> MiniFu m a
readMVar v = MiniFu (K.cont (ReadMVar v))

-- | Take a value from a @MVar@. This "empties" the @MVar@, allowing a
-- new value to be put in. This will block if there is no value in the
-- @MVar@ already, until one has been put.
@@ -26,6 +26,7 @@ data PrimOp m where
Fork :: MiniFu m () -> (ThreadId -> PrimOp m) -> PrimOp m
NewEmptyMVar :: (MVar m a -> PrimOp m) -> PrimOp m
PutMVar :: MVar m a -> a -> PrimOp m -> PrimOp m
ReadMVar :: MVar m a -> (a -> PrimOp m) -> PrimOp m
TakeMVar :: MVar m a -> (a -> PrimOp m) -> PrimOp m
NewCRef :: a -> (CRef m a -> PrimOp m) -> PrimOp m
ReadCRef :: CRef m a -> (a -> PrimOp m) -> PrimOp m
@@ -100,6 +101,9 @@ stepThread tid (threads, idsrc) = case M.lookup tid threads of
C.writeCRef ref Nothing
simple (goto (k a) . unblock mvid)
Nothing -> simple (block mvid)
go (ReadMVar (MVar mvid ref) k) = do
cur <- C.readCRef ref
simple $ maybe (block mvid) (goto . k) cur
go (NewCRef a k) = do
ref <- C.newCRef a
simple (goto (k (CRef ref)))

0 comments on commit dabd84b

Please sign in to comment.