@@ -66,7 +66,7 @@ data Future a = Future {-# UNPACK #-} !(IORef (IVar a))
6666
6767data IVar a
6868 = Full ! a
69- | Pending {- # UNPACK #-} !Event ! (Maybe ( Lifetime FunctionTable )) ! ( IO () ) ! a
69+ | Pending {- # UNPACK #-} !Event ! (IO () ) ! a
7070 | Empty ! (IO () )
7171
7272
@@ -117,9 +117,12 @@ instance Async PTX where
117117 kernel <- asksParState ptxKernel
118118 event <- liftPar (Event. waypoint stream)
119119 ready <- liftIO (Event. query event)
120+ let cleanupK = case kernel of
121+ Just k -> touchLifetime k
122+ Nothing -> return ()
120123 liftIO . atomicModifyIORef' ref $ \ case
121124 Empty cleanup -> if ready then (Full v, () )
122- else (Pending event kernel cleanup v, () )
125+ else (Pending event ( cleanup >> cleanupK) v, () )
123126 _ -> internalError " multiple put"
124127
125128 -- Get the value of Future. Since the actual cross-stream synchronisation
@@ -134,14 +137,11 @@ instance Async PTX where
134137 ivar <- readIORef ref
135138 case ivar of
136139 Full v -> return v
137- Pending event k cleanup v -> do
140+ Pending event cleanup v -> do
138141 ready <- Event. query event
139142 if ready
140143 then do
141144 writeIORef ref (Full v)
142- case k of
143- Just f -> touchLifetime f
144- Nothing -> return ()
145145 cleanup
146146 else
147147 Event. after event stream
@@ -164,12 +164,9 @@ wait (Future ref) = do
164164 ivar <- readIORef ref
165165 case ivar of
166166 Full v -> return v
167- Pending event k cleanup v -> do
167+ Pending event cleanup v -> do
168168 Event. block event
169169 writeIORef ref (Full v)
170- case k of
171- Just f -> touchLifetime f
172- Nothing -> return ()
173170 cleanup
174171 return v
175172 Empty _ -> internalError " blocked on an IVar"
@@ -181,17 +178,20 @@ putCleanup (Future ref) cleanup v = do
181178 kernel <- asksParState ptxKernel
182179 event <- liftPar (Event. waypoint stream)
183180 ready <- liftIO (Event. query event)
181+ let cleanupK = case kernel of
182+ Just k -> touchLifetime k
183+ Nothing -> return ()
184184 liftIO . atomicModifyIORef' ref $ \ case
185185 Empty cleanup2 -> if ready then (Full v, () )
186- else (Pending event kernel (cleanup2 >> cleanup) v, () )
186+ else (Pending event (cleanup2 >> cleanup >> cleanupK ) v, () )
187187 _ -> internalError " multiple put"
188188
189189{-# INLINEABLE addCleanup #-}
190190addCleanup :: HasCallStack => FutureR PTX a -> IO () -> Par PTX ()
191191addCleanup (Future ref) cleanup = liftIO $ do
192192 toRunNow <- atomicModifyIORef' ref $ \ case
193193 Full v -> (Full v, cleanup)
194- Pending event k cleanup2 v -> (Pending event k (cleanup2 >> cleanup) v, return () )
194+ Pending event cleanup2 v -> (Pending event (cleanup2 >> cleanup) v, return () )
195195 Empty cleanup2 -> (Empty (cleanup2 >> cleanup), return () )
196196 toRunNow
197197
0 commit comments