Andrea Vezzosi pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC

Commits:

3 changed files:

Changes:

  • libraries/ghci/GHCi/Message.hs
    ... ... @@ -28,7 +28,7 @@ module GHCi.Message
    28 28
       , getMessage, putMessage, getTHMessage, putTHMessage
    
    29 29
     
    
    30 30
       , Pipe(..), mkPipeFromHandles, mkPipeFromContinuations, closePipe, isPipeOpen
    
    31
    -  , remoteCall, remoteCallInterleaving, remoteTHCall
    
    31
    +  , remoteCall, remoteCallInterleaving, remoteTHCall, remoteTHCall'
    
    32 32
       , handleRemoteCall, handleRemoteTHCall, writeReply
    
    33 33
       , RemoteProcessException(..), PipeClosedException(..)
    
    34 34
       , disposeTHMessage
    
    ... ... @@ -803,6 +803,11 @@ handleRemoteCall pipe hook k =
    803 803
     remoteTHCall :: Binary a => Pipe -> InboxId -> THMessage a -> IO a
    
    804 804
     remoteTHCall pipe ii msg = either throwIO (pure . runGet get) =<< remoteAsyncReq pipe (putTHMessage msg) (Just ii) (disposeTHMessage msg)
    
    805 805
     
    
    806
    +remoteTHCall' :: Binary a => Pipe -> InboxId -> THMessage a -> IO (IO a)
    
    807
    +remoteTHCall' pipe ii msg =
    
    808
    +  remoteAsyncReqWithResponse pipe (putTHMessage msg) (Just ii) >>= \case
    
    809
    +    NotWritten e -> throwIO e
    
    810
    +    x -> pure $ mask_ $ either throwIO (pure . runGet get) =<< getAsyncReqReply (pure ()) (disposeTHMessage msg . fmap (runGet get)) x
    
    806 811
     
    
    807 812
     disposeTHMessage :: THMessage a -> Either SomeException LB.ByteString -> IO ()
    
    808 813
     -- TODO: setup THMessage cleanups, even though atm there's none.
    

  • libraries/ghci/GHCi/Server.hs
    ... ... @@ -10,6 +10,7 @@ module GHCi.Server
    10 10
     where
    
    11 11
     
    
    12 12
     import Prelude
    
    13
    +import GHCi.RemoteTypes (localRef)
    
    13 14
     import GHCi.Run
    
    14 15
     import GHCi.Signals
    
    15 16
     import GHCi.TH
    
    ... ... @@ -82,10 +83,12 @@ servWithCustom verbose hook pipe restore customHandler = do
    82 83
             Shutdown                  -> writeIORef doneRef True >> closingPipe pipe >>
    
    83 84
                                          reply ()                %>>= \r ->
    
    84 85
                                          runWriteReply wr (Right r) ignorePipeClosedException
    
    85
    -        RunTH ii st q ty loc      -> handleAsync wr
    
    86
    +        RunTH ii rst rq ty loc      -> localRef rst %>>= \ st -> localRef rq %>>= \ q ->
    
    87
    +                                     handleAsync wr
    
    86 88
                                          (restore $ wrapRunTH ii (runTH pipe ii st q ty loc))
    
    87 89
                                          (setCancel . void . forkIO . killThread)
    
    88
    -        RunModFinalizers ii st qs -> handleAsync wr
    
    90
    +        RunModFinalizers ii rst rqs -> localRef rst %>>= \ st -> mapM localRef rqs %>>= \ qs ->
    
    91
    +                                     handleAsync wr
    
    89 92
                                          (restore $ wrapRunTH ii (runModFinalizerRefs pipe ii st qs))
    
    90 93
                                          (setCancel . void . forkIO . killThread)
    
    91 94
             _other                    -> run msg >>= reply        %>>= writeReply wr
    

  • libraries/ghci/GHCi/TH.hs
    ... ... @@ -96,6 +96,7 @@ import GHCi.Message
    96 96
     import GHCi.RemoteTypes
    
    97 97
     import GHC.Serialized
    
    98 98
     
    
    99
    +import Control.Monad (join)
    
    99 100
     import Control.Exception
    
    100 101
     import Control.Monad.IO.Class (MonadIO (..))
    
    101 102
     import Data.Binary
    
    ... ... @@ -154,7 +155,7 @@ noLoc = TH.Loc "<no file>" "<no package>" "<no module>" (0,0) (0,0)
    154 155
     
    
    155 156
     -- | Send a 'THMessage' to GHC and return the result.
    
    156 157
     ghcCmd :: Binary a => THMessage (THResult a) -> GHCiQ a
    
    157
    -ghcCmd m = GHCiQ $ \ sRef -> handleTHResult sRef =<< ghcCmdResult sRef m
    
    158
    +ghcCmd m = GHCiQ $ \ sRef -> handleTHResult sRef =<< join (ghcCmdResult sRef m)
    
    158 159
     
    
    159 160
     -- | Throws 'GHCiQException' on 'THException'.
    
    160 161
     handleTHResult :: IORef QState -> THResult a -> IO a
    
    ... ... @@ -165,10 +166,10 @@ handleTHResult sRef r = do
    165 166
       THComplete res -> return res
    
    166 167
     
    
    167 168
     -- | Like 'ghcCmd' but returns the 'THResult a' as-is.
    
    168
    -ghcCmdResult :: Binary a => IORef QState -> THMessage (THResult a) -> IO (THResult a)
    
    169
    +ghcCmdResult :: Binary a => IORef QState -> THMessage (THResult a) -> IO (IO (THResult a))
    
    169 170
     ghcCmdResult sRef m = do
    
    170 171
       s <- readIORef sRef
    
    171
    -  remoteTHCall (qsPipe s) (qsInboxId s) m
    
    172
    +  remoteTHCall' (qsPipe s) (qsInboxId s) m
    
    172 173
     
    
    173 174
     
    
    174 175
     instance MonadIO GHCiQ where
    
    ... ... @@ -223,9 +224,10 @@ metaHandlersGHCiQ runInIO = TH.forceMetaHandlersArgs $ TH.MetaHandlers {
    223 224
       , mAddTempFile = \suffix -> runInIO $ ghcCmd (AddTempFile suffix)
    
    224 225
       , mAddTopDecls = \decls -> runInIO $ ghcCmd (AddTopDecls decls)
    
    225 226
       , mAddForeignFilePath = \lang fp -> runInIO $ ghcCmd (AddForeignFilePath lang fp)
    
    226
    -  , mAddModFinalizer = \fin -> runInIO $ GHCiQ $ \sRef -> handleTHResult sRef =<< do
    
    227
    -      bracketOnError (mkRemoteRef fin) freeRemoteRef $
    
    227
    +  , mAddModFinalizer = \fin -> runInIO $ GHCiQ $ \sRef -> do
    
    228
    +      waitReply <- bracketOnError (mkRemoteRef fin) freeRemoteRef $
    
    228 229
             ghcCmdResult sRef . AddModFinalizer
    
    230
    +      handleTHResult sRef =<< waitReply
    
    229 231
       , mAddCorePlugin = \str -> runInIO $ ghcCmd (AddCorePlugin str)
    
    230 232
       , mGetQ = runInIO $ do
    
    231 233
         s <- getState
    
    ... ... @@ -251,12 +253,10 @@ startTH = do
    251 253
     --
    
    252 254
     -- The references must be created on the caller process.
    
    253 255
     runModFinalizerRefs :: Pipe -> InboxId
    
    254
    -                    -> RemoteRef (IORef QState)
    
    255
    -                    -> [RemoteRef (TH.Q ())]
    
    256
    +                    -> (IORef QState)
    
    257
    +                    -> [TH.Q ()]
    
    256 258
                         -> IO ()
    
    257
    -runModFinalizerRefs pipe ii rstate qrefs = do
    
    258
    -  qs <- mapM localRef qrefs
    
    259
    -  qstateref <- localRef rstate
    
    259
    +runModFinalizerRefs pipe ii qstateref qs = do
    
    260 260
       qstate <- readIORef qstateref
    
    261 261
       qstate' <- newIORef $ qstate { qsInboxId = ii, qsPipe = pipe }
    
    262 262
       _ <- runGHCiQ (runQinGHCiQ $ sequence_ qs) qstate'
    
    ... ... @@ -266,9 +266,9 @@ runModFinalizerRefs pipe ii rstate qrefs = do
    266 266
     runTH
    
    267 267
       :: Pipe
    
    268 268
       -> InboxId
    
    269
    -  -> RemoteRef (IORef QState)
    
    269
    +  -> (IORef QState)
    
    270 270
           -- ^ The TH state, created by 'startTH'
    
    271
    -  -> HValueRef
    
    271
    +  -> HValue
    
    272 272
           -- ^ The splice to run
    
    273 273
       -> THResultType
    
    274 274
           -- ^ What kind of splice it is
    
    ... ... @@ -277,25 +277,22 @@ runTH
    277 277
       -> IO ByteString
    
    278 278
           -- ^ Returns an (encoded) result that depends on the THResultType
    
    279 279
     
    
    280
    -runTH pipe ii rstate rhv ty mb_loc = do
    
    281
    -  hv <- localRef rhv
    
    280
    +runTH pipe ii rstate hv ty mb_loc = do
    
    282 281
       case ty of
    
    283 282
         THExp -> runTHQ pipe ii rstate mb_loc (unsafeCoerce hv :: TH.Q TH.Exp)
    
    284 283
         THPat -> runTHQ pipe ii rstate mb_loc (unsafeCoerce hv :: TH.Q TH.Pat)
    
    285 284
         THType -> runTHQ pipe ii rstate mb_loc (unsafeCoerce hv :: TH.Q TH.Type)
    
    286 285
         THDec -> runTHQ pipe ii rstate mb_loc (unsafeCoerce hv :: TH.Q [TH.Dec])
    
    287 286
         THAnnWrapper -> do
    
    288
    -      hv <- unsafeCoerce <$> localRef rhv
    
    289
    -      case hv :: AnnotationWrapper of
    
    287
    +      case unsafeCoerce hv :: AnnotationWrapper of
    
    290 288
             AnnotationWrapper thing -> return $!
    
    291 289
               LB.toStrict (runPut (put (toSerialized serializeWithData thing)))
    
    292 290
     
    
    293 291
     -- | Run a Q computation.
    
    294 292
     runTHQ
    
    295
    -  :: Binary a => Pipe -> InboxId -> RemoteRef (IORef QState) -> Maybe TH.Loc -> TH.Q a
    
    293
    +  :: Binary a => Pipe -> InboxId -> (IORef QState) -> Maybe TH.Loc -> TH.Q a
    
    296 294
       -> IO ByteString
    
    297
    -runTHQ pipe ii rstate mb_loc ghciq = do
    
    298
    -  qstateref <- localRef rstate
    
    295
    +runTHQ pipe ii qstateref mb_loc ghciq = do
    
    299 296
       modifyIORef' qstateref (\qstate -> qstate { qsLocation = mb_loc, qsInboxId = ii, qsPipe = pipe })
    
    300 297
       r <- runGHCiQ (runQinGHCiQ ghciq) qstateref
    
    301 298
       return $! LB.toStrict (runPut (put r))