Skip to content

Commit

Permalink
simulation: use plain queue for TCP receiver buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Saizan committed Dec 19, 2024
1 parent 78c1088 commit ba09d21
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions simulation/src/ChanTCP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import Control.Tracer as Tracer (
Tracer,
traceWith,
)
import Data.PQueue.Prio.Min (MinPQueue)
import qualified Data.PQueue.Prio.Min as PQ
import ModelTCP (
Bytes,
Expand Down Expand Up @@ -95,13 +94,13 @@ newConnectionTCP tracer tcpprops = do
return (clientChan, serverChan)

type SendBuf m a = TMVar m a
type RecvBuf m a = TVar m (MinPQueue Time a)
type RecvBuf m a = TQueue m (Time, a)

newSendBuf :: MonadSTM m => m (SendBuf m a)
newSendBuf = newEmptyTMVarIO

newRecvBuf :: MonadSTM m => m (RecvBuf m a)
newRecvBuf = newTVarIO PQ.empty
newRecvBuf = newTQueueIO

writeSendBuf :: MonadSTM m => SendBuf m a -> a -> m ()
writeSendBuf sendbuf msg = atomically (putTMVar sendbuf msg)
Expand All @@ -111,13 +110,7 @@ readRecvBuf ::
RecvBuf m a ->
m a
readRecvBuf recvbuf = do
(arrivaltime, msg) <- atomically $ do
arrivals <- readTVar recvbuf
case PQ.minViewWithKey arrivals of
Nothing -> retry
Just (res, arrivals') -> do
writeTVar recvbuf arrivals'
return res
(arrivaltime, msg) <- atomically $ readTQueue recvbuf

now <- getMonotonicTime
let delay = arrivaltime `diffTime` now
Expand Down Expand Up @@ -175,7 +168,7 @@ transport tracer tcpprops sendbuf recvbuf = do
) = assert (msgsize > 0) $ forecastTcpMsgSend tcpprops tcpstate' now' msgsize

-- schedule the arrival, and wait until it has finished sending
atomically $ modifyTVar' recvbuf (PQ.insert msgRecvTrailingEdge msg)
atomically $ writeTQueue recvbuf (msgRecvTrailingEdge, msg)
traceWith tracer (TcpSendMsg msg forecast tcpforecasts)
threadDelay (msgSendTrailingEdge `diffTime` now')
-- We keep the sendbuf full until the message has finished sending
Expand Down

0 comments on commit ba09d21

Please sign in to comment.