Skip to content

Commit

Permalink
RecThunk: Use newUnique, not myThreadId
Browse files Browse the repository at this point in the history
I couldn’t quite break the previous code, but trying hard enogh it
should be possible, due to reentranc. So this seems safer.

Testing with dejafu suffers a bit, see barrucadu/dejafu#368
  • Loading branch information
nomeata committed Sep 12, 2022
1 parent 0acebbd commit 53f6ee1
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions System/IO/RecThunk.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,39 @@ where

#ifdef DEJAFU

#define Ctxt MonadConc m =>
#define Ctxt (MonadConc m, MonadIO m) =>
#define Thunk_ (Thunk m)
#define ResolvingState_ (ResolvingState m)
#define KickedThunk_ (KickedThunk m)
#define ThreadId_ (ThreadId m)
#define IORef_ IORef m
#define MVar_ MVar m
#define M m

import Control.Concurrent.Classy hiding (wait)
import Data.Unique
import Control.Monad.IO.Class

#else

#define Ctxt
#define Thunk_ Thunk
#define ResolvingState_ ResolvingState
#define KickedThunk_ KickedThunk
#define ThreadId_ ThreadId
#define IORef_ IORef
#define MVar_ MVar
#define M IO

import Control.Concurrent.MVar
import Control.Concurrent
import Data.IORef
import Data.Unique
import Control.Monad.IO.Class

#endif



-- | An @IO@ action that is to be run at most once
newtype Thunk_ = Thunk (MVar_ (Either (M [Thunk_]) KickedThunk_))
data ResolvingState_ = NotStarted | ProcessedBy ThreadId_ (MVar_ ()) | Done
data ResolvingState_ = NotStarted | ProcessedBy Unique (MVar_ ()) | Done
-- | A 'Thunk' that is being evaluated
data KickedThunk_ = KickedThunk (MVar_ [KickedThunk_]) (MVar_ ResolvingState_)

Expand Down Expand Up @@ -118,9 +118,8 @@ kick (Thunk t) = takeMVar t >>= \case
putMVar t (Right kt)
pure kt

wait :: Ctxt KickedThunk_ -> M ()
wait (KickedThunk mv_deps mv_s) = do
my_id <- myThreadId
wait :: Ctxt Unique -> KickedThunk_ -> M ()
wait my_id (KickedThunk mv_deps mv_s) = do
s <- takeMVar mv_s
case s of
-- Thunk and all dependences are done
Expand All @@ -138,7 +137,7 @@ wait (KickedThunk mv_deps mv_s) = do
done_mv <- newEmptyMVar
putMVar mv_s (ProcessedBy my_id done_mv)
ts <- readMVar mv_deps
mapM_ wait ts
mapM_ (wait my_id) ts
-- Mark kicked thunk as done
_ <- swapMVar mv_s Done
-- Wake up waiting threads
Expand All @@ -150,4 +149,5 @@ wait (KickedThunk mv_deps mv_s) = do
force :: Ctxt Thunk_ -> M ()
force t = do
rt <- kick t
wait rt
my_id <- liftIO newUnique
wait my_id rt

0 comments on commit 53f6ee1

Please sign in to comment.