| ... |
... |
@@ -50,6 +50,7 @@ import qualified Data.ByteString.Lazy as LB |
|
50
|
50
|
import Data.IORef
|
|
51
|
51
|
import Data.IntMap (IntMap)
|
|
52
|
52
|
import qualified Data.IntMap as IntMap
|
|
|
53
|
+import Data.Maybe
|
|
53
|
54
|
import GHC.Generics
|
|
54
|
55
|
import System.Exit
|
|
55
|
56
|
import System.IO
|
| ... |
... |
@@ -77,6 +78,10 @@ data Pipe = Pipe |
|
77
|
78
|
, pipeReadClosed :: !(TVar Bool)
|
|
78
|
79
|
-- ^ After the pipe is closed, messages can no longer be received from the
|
|
79
|
80
|
-- other end, but the 'AsyncManager' queues might not be empty yet.
|
|
|
81
|
+ , pipeWriteClosed :: !(TVar Bool)
|
|
|
82
|
+ -- ^ After the write end of the pipe is closed we can no longer submit
|
|
|
83
|
+ -- requests or respond to ones we receive, but we might still receive
|
|
|
84
|
+ -- replies.
|
|
80
|
85
|
}
|
|
81
|
86
|
|
|
82
|
87
|
-- | Make a 'Pipe' from a 'Handle' to read and a 'Handle' to write.
|
| ... |
... |
@@ -101,6 +106,7 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo |
|
101
|
106
|
pipeLeftovers <- newIORef Nothing
|
|
102
|
107
|
pipeLock <- newMVar ()
|
|
103
|
108
|
pipeReadClosed <- newTVarIO False
|
|
|
109
|
+ pipeWriteClosed <- newTVarIO False
|
|
104
|
110
|
mdo
|
|
105
|
111
|
pipeAsyncMngr <- newAsyncManager p
|
|
106
|
112
|
let p = Pipe { .. }
|
| ... |
... |
@@ -114,20 +120,31 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo |
|
114
|
120
|
-- write. Only then do we stop the drain thread, which closes the read end
|
|
115
|
121
|
-- itself once its read has finished. On Windows its read can't be
|
|
116
|
122
|
-- interrupted, so this waits for the other side to close its write end too.
|
|
|
123
|
+-- Finally we stop the write thread, which could be blocked waiting for more to write.
|
|
117
|
124
|
closePipe :: Pipe -> IO ()
|
|
118
|
125
|
closePipe p@Pipe{..} = do
|
|
119
|
126
|
readAlreadyClosed <- atomically $ do
|
|
120
|
127
|
closed <- readTVar pipeReadClosed
|
|
121
|
128
|
failPendingReplies p (toException PipeReadClosedException)
|
|
122
|
129
|
pure closed
|
|
123
|
|
- withLock p pipeCloseWrite `finally`
|
|
124
|
|
- unless readAlreadyClosed
|
|
125
|
|
- (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException)
|
|
|
130
|
+
|
|
|
131
|
+ writeAlreadyClosed <- readTVarIO pipeWriteClosed
|
|
|
132
|
+
|
|
|
133
|
+ unless writeAlreadyClosed (withLock p pipeCloseWrite)
|
|
|
134
|
+ `finally` do
|
|
|
135
|
+ unless readAlreadyClosed
|
|
|
136
|
+ (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException)
|
|
|
137
|
+ unless writeAlreadyClosed
|
|
|
138
|
+ (throwTo (asyncFillTid pipeAsyncMngr) PipeWriteClosedException)
|
|
126
|
139
|
|
|
127
|
140
|
-- | We've closed the pipe read end on this process
|
|
128
|
141
|
data PipeReadClosedException = PipeReadClosedException deriving Show
|
|
129
|
142
|
instance Exception PipeReadClosedException
|
|
130
|
143
|
|
|
|
144
|
+-- | We've closed the pipe write end on this process
|
|
|
145
|
+data PipeWriteClosedException = PipeWriteClosedException deriving Show
|
|
|
146
|
+instance Exception PipeWriteClosedException
|
|
|
147
|
+
|
|
131
|
148
|
--------------------------------------------------------------------------------
|
|
132
|
149
|
-- ** Pipe thread-safe read/write
|
|
133
|
150
|
--------------------------------------------------------------------------------
|
| ... |
... |
@@ -135,11 +152,11 @@ instance Exception PipeReadClosedException |
|
135
|
152
|
-- and identify whether they are requests or replies.
|
|
136
|
153
|
|
|
137
|
154
|
writePipe :: Pipe -> IddMsg -> IO ()
|
|
138
|
|
-writePipe p@Pipe{..} msg = withLock p $ do
|
|
139
|
|
- putAll (execPut (put msg)) `catch` \(e :: IOError) -> do
|
|
140
|
|
- pipeCloseWrite
|
|
141
|
|
- -- TODO: do we need a pipeCloseWrite TVar ?
|
|
142
|
|
- throwIO e
|
|
|
155
|
+writePipe p@Pipe{..} msg = atomically $ do
|
|
|
156
|
+ readTVar pipeWriteClosed >>= \case
|
|
|
157
|
+ True -> throwSTM PipeWriteClosedException
|
|
|
158
|
+ False -> pure ()
|
|
|
159
|
+ writeTQueue (writeQueue pipeAsyncMngr) msg
|
|
143
|
160
|
|
|
144
|
161
|
-- | Only the 'drainPipe' thread should read from the pipe directly. Other
|
|
145
|
162
|
-- functions should wait for the matching reply to be delivered asynchronously.
|
| ... |
... |
@@ -233,6 +250,10 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do |
|
233
|
250
|
|
|
234
|
251
|
-- Block waiting for request
|
|
235
|
252
|
mbreq <- atomically $ do
|
|
|
253
|
+ readTVar (pipeWriteClosed pipe) >>= \case
|
|
|
254
|
+ -- We will not be able to write the reply, so no point in continuiting.
|
|
|
255
|
+ True -> throwSTM PipeWriteClosedException
|
|
|
256
|
+ False -> pure ()
|
|
236
|
257
|
isClosed <- readTVar (pipeReadClosed pipe)
|
|
237
|
258
|
taken <- case mii of
|
|
238
|
259
|
Nothing -> tryReadTQueue $ requestsQueue am
|
| ... |
... |
@@ -324,7 +345,10 @@ data AsyncManager = AsyncManager |
|
324
|
345
|
, mailboxQueues :: !(TVar (IntMap (TQueue (CorrelationId, LB.ByteString))))
|
|
325
|
346
|
-- ^ Requests for a specific 'InboxId' that can be read using
|
|
326
|
347
|
-- @'handleAsyncReq' p get (Just <mailboxid>)@.
|
|
|
348
|
+ , writeQueue :: !(TQueue IddMsg)
|
|
|
349
|
+ -- ^ Messages to write on the pipe.
|
|
327
|
350
|
, asyncDrainTid :: !ThreadId
|
|
|
351
|
+ , asyncFillTid :: !ThreadId
|
|
328
|
352
|
, asyncFreshId :: !(IORef Int)
|
|
329
|
353
|
}
|
|
330
|
354
|
|
| ... |
... |
@@ -367,11 +391,16 @@ newAsyncManager pipe = do |
|
367
|
391
|
pendingReply <- newTVarIO IntMap.empty
|
|
368
|
392
|
requestsQueue <- newTQueueIO
|
|
369
|
393
|
mailboxQueues <- newTVarIO IntMap.empty
|
|
|
394
|
+ writeQueue <- newTQueueIO
|
|
370
|
395
|
|
|
371
|
396
|
-- Fork thread to drain read end.
|
|
372
|
397
|
-- (cleaned up on 'closePipe')
|
|
373
|
398
|
asyncDrainTid <- forkIOWithUnmask (drainPipe pipe)
|
|
374
|
399
|
|
|
|
400
|
+ -- Fork thread to fill write end.
|
|
|
401
|
+ -- (cleaned up on 'closePipe')
|
|
|
402
|
+ asyncFillTid <- forkIOWithUnmask (fillPipe pipe)
|
|
|
403
|
+
|
|
375
|
404
|
let am = AsyncManager{..}
|
|
376
|
405
|
pure am
|
|
377
|
406
|
|
| ... |
... |
@@ -421,6 +450,30 @@ drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPend |
|
421
|
450
|
|
|
422
|
451
|
am = pipeAsyncMngr p
|
|
423
|
452
|
|
|
|
453
|
+
|
|
|
454
|
+-- | "Fill"s write end of the Pipe with queues messages.
|
|
|
455
|
+--
|
|
|
456
|
+-- Closes the Pipe write end when stopping, notifying any requests still queued to write.
|
|
|
457
|
+fillPipe :: Pipe -> (forall x. IO x -> IO x) -> IO ()
|
|
|
458
|
+fillPipe p unmask = unmask $ catch (loop `finally` pipeCloseWrite p) failPendingWrites where
|
|
|
459
|
+ loop = do
|
|
|
460
|
+ msg <- atomically $ readTQueue (writeQueue am)
|
|
|
461
|
+ withLock p . putAll p . execPut . put $ msg
|
|
|
462
|
+ loop
|
|
|
463
|
+
|
|
|
464
|
+ am = pipeAsyncMngr p
|
|
|
465
|
+
|
|
|
466
|
+ failPendingWrites :: SomeException -> IO ()
|
|
|
467
|
+ failPendingWrites e = atomically $ do
|
|
|
468
|
+ writeTVar (pipeWriteClosed p) True
|
|
|
469
|
+
|
|
|
470
|
+ writes <- flushTQueue (writeQueue am)
|
|
|
471
|
+ let uqs = [uq | Request uq _ _ <- writes]
|
|
|
472
|
+ pendings <- readTVar (pendingReply (pipeAsyncMngr p))
|
|
|
473
|
+ let tmvars = mapMaybe (`IntMap.lookup` pendings) uqs
|
|
|
474
|
+ writeTVar (pendingReply (pipeAsyncMngr p)) $! (foldl' (flip IntMap.delete) pendings uqs)
|
|
|
475
|
+ mapM_ (`writeTMVar` Left e) tmvars
|
|
|
476
|
+
|
|
424
|
477
|
failPendingReplies :: Pipe -> SomeException -> STM ()
|
|
425
|
478
|
failPendingReplies p e = do
|
|
426
|
479
|
-- reqs are only added if pipe not closed;
|